diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-05-13 11:42:12 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-05-20 14:56:24 +0400 |
commit | 9c18c1987fab42ba50e72f496111666ff61cf60f (patch) | |
tree | b82332af5070d3768f792a6b0b41dd1bcf1487f6 | |
parent | 69207a6bb357cc2e801361bcbeffda5803cfa330 (diff) |
Delegate the to deletion of branches to libgit2 native method
-rw-r--r-- | LibGit2Sharp.Tests/BranchFixture.cs | 35 | ||||
-rw-r--r-- | LibGit2Sharp/BranchCollection.cs | 67 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 6 |
3 files changed, 79 insertions, 29 deletions
diff --git a/LibGit2Sharp.Tests/BranchFixture.cs b/LibGit2Sharp.Tests/BranchFixture.cs index 81edf209..5e29d740 100644 --- a/LibGit2Sharp.Tests/BranchFixture.cs +++ b/LibGit2Sharp.Tests/BranchFixture.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using System.Linq; using LibGit2Sharp.Tests.TestHelpers; using Xunit; @@ -430,6 +429,40 @@ namespace LibGit2Sharp.Tests } } + private void AssertDeletion(string branchName, bool isRemote, bool shouldPrevisoulyAssertExistence) + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath); + + using (var repo = new Repository(path.RepositoryPath)) + { + if (shouldPrevisoulyAssertExistence) + { + Assert.NotNull(repo.Branches[branchName]); + } + + repo.Branches.Delete(branchName, isRemote); + Branch branch = repo.Branches[branchName]; + Assert.Null(branch); + } + } + + [Theory] + [InlineData("i-do-numbers", false)] + [InlineData("origin/br2", true)] + public void CanDeleteAnExistingBranch(string branchName, bool isRemote) + { + AssertDeletion(branchName, isRemote, true); + } + + + [Theory] + [InlineData("I-donot-exist", false)] + [InlineData("me/neither", true)] + public void CanDeleteANonExistingBranch(string branchName, bool isRemote) + { + AssertDeletion(branchName, isRemote, false); + } + [Fact] public void DeletingABranchWhichIsTheCurrentHeadThrows() { diff --git a/LibGit2Sharp/BranchCollection.cs b/LibGit2Sharp/BranchCollection.cs index f3248d9f..4c674d91 100644 --- a/LibGit2Sharp/BranchCollection.cs +++ b/LibGit2Sharp/BranchCollection.cs @@ -31,12 +31,38 @@ namespace LibGit2Sharp get { Ensure.ArgumentNotNullOrEmptyString(name, "name"); - string canonicalName = NormalizeToCanonicalName(name); - var reference = repo.Refs.Resolve<Reference>(canonicalName); - return reference == null ? null : new Branch(repo, reference, canonicalName); + + if (LooksLikeABranchName(name)) + { + return BuildFromReferenceName(name); + } + + Branch branch = BuildFromReferenceName(ShortToLocalName(name)); + if (branch != null) + { + return branch; + } + + return BuildFromReferenceName(ShortToRemoteName(name)); } } + private static string ShortToLocalName(string name) + { + return string.Format(CultureInfo.InvariantCulture, "{0}{1}", "refs/heads/", name); + } + + private static string ShortToRemoteName(string name) + { + return string.Format(CultureInfo.InvariantCulture, "{0}{1}", "refs/remotes/", name); + } + + private Branch BuildFromReferenceName(string canonicalName) + { + var reference = repo.Refs.Resolve<Reference>(canonicalName); + return reference == null ? null : new Branch(repo, reference, canonicalName); + } + #region IEnumerable<Branch> Members /// <summary> @@ -92,26 +118,26 @@ namespace LibGit2Sharp Ensure.Success(NativeMethods.git_branch_create(out oid, repo.Handle, name, osw.ObjectPtr, allowOverwrite)); } - return this[name]; + return this[ShortToLocalName(name)]; } /// <summary> /// Deletes the branch with the specified name. /// </summary> /// <param name = "name">The name of the branch to delete.</param> - public void Delete(string name) + /// <param name = "isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param> + public void Delete(string name, bool isRemote = false) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); - string canonicalName = NormalizeToCanonicalName(name); + int res = NativeMethods.git_branch_delete(repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL); - if (canonicalName == repo.Head.CanonicalName) + if (res == (int)GitErrorCode.GIT_ENOTFOUND) { - throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Branch '{0}' can not be deleted as it is the current HEAD.", canonicalName)); + return; } - //TODO: To be replaced by native libgit2 git_branch_delete() when available. - repo.Refs.Delete(canonicalName); + Ensure.Success(res); } ///<summary> @@ -133,24 +159,9 @@ namespace LibGit2Sharp private static bool LooksLikeABranchName(string referenceName) { - return referenceName.StartsWith("refs/heads/", StringComparison.Ordinal) || referenceName.StartsWith("refs/remotes/", StringComparison.Ordinal); - } - - private static string NormalizeToCanonicalName(string name) - { - Ensure.ArgumentNotNullOrEmptyString(name, "name"); - - if (name == "HEAD") - { - return name; - } - - if (LooksLikeABranchName(name)) - { - return name; - } - - return string.Format(CultureInfo.InvariantCulture, "refs/heads/{0}", name); + return referenceName == "HEAD" || + referenceName.StartsWith("refs/heads/", StringComparison.Ordinal) || + referenceName.StartsWith("refs/remotes/", StringComparison.Ordinal); } } } diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index ce042bc6..1940c9b1 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -94,6 +94,12 @@ namespace LibGit2Sharp.Core [MarshalAs(UnmanagedType.Bool)] bool force); [DllImport(libgit2)] + public static extern int git_branch_delete( + RepositorySafeHandle repo, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string branch_name, + GitBranchType branch_type); + + [DllImport(libgit2)] public static extern int git_branch_move( RepositorySafeHandle repo, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string old_branch_name, |