diff options
author | Keith Dahlby <dahlbyk@gmail.com> | 2012-04-21 05:06:46 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-04-24 12:50:22 +0400 |
commit | 9444c7e40a9ef1fac18f71b7805e5e216f00839a (patch) | |
tree | 845dc0f58d48ab257afd830906284dfdc7e3351f | |
parent | 0c73aa31871ec646a9d839659b0c6c83601e3147 (diff) |
Add Repository.CreateBranch() overload that accepts Commit directly
-rw-r--r-- | LibGit2Sharp.Tests/BranchFixture.cs | 17 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/CommitFixture.cs | 2 | ||||
-rw-r--r-- | LibGit2Sharp/RepositoryExtensions.cs | 13 |
3 files changed, 30 insertions, 2 deletions
diff --git a/LibGit2Sharp.Tests/BranchFixture.cs b/LibGit2Sharp.Tests/BranchFixture.cs index 5203ed73..0942aef8 100644 --- a/LibGit2Sharp.Tests/BranchFixture.cs +++ b/LibGit2Sharp.Tests/BranchFixture.cs @@ -74,6 +74,20 @@ namespace LibGit2Sharp.Tests } [Fact] + public void CanCreateBranchFromCommit() + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(); + using (var repo = new Repository(path.RepositoryPath)) + { + const string name = "unit_test"; + var commit = repo.Lookup<Commit>("HEAD"); + Branch newBranch = repo.CreateBranch(name, commit); + newBranch.ShouldNotBeNull(); + newBranch.Tip.Sha.ShouldEqual("4c062a6361ae6959e06292c1fa5e2822d9c96345"); + } + } + + [Fact] public void CreatingABranchFromATagPeelsToTheCommit() { TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(); @@ -151,8 +165,9 @@ namespace LibGit2Sharp.Tests { Assert.Throws<ArgumentNullException>(() => repo.Branches.Create(null, repo.Head.CanonicalName)); Assert.Throws<ArgumentException>(() => repo.Branches.Create(string.Empty, repo.Head.CanonicalName)); - Assert.Throws<ArgumentNullException>(() => repo.Branches.Create("bad_branch", null)); + Assert.Throws<ArgumentNullException>(() => repo.Branches.Create("bad_branch", default(string))); Assert.Throws<ArgumentException>(() => repo.Branches.Create("bad_branch", string.Empty)); + Assert.Throws<ArgumentNullException>(() => repo.CreateBranch("bad_branch", default(Commit))); } } diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index 766a1278..b603c27d 100644 --- a/LibGit2Sharp.Tests/CommitFixture.cs +++ b/LibGit2Sharp.Tests/CommitFixture.cs @@ -517,7 +517,7 @@ namespace LibGit2Sharp.Tests commit2.ParentsCount.ShouldEqual(1); commit2.Parents.First().Id.ShouldEqual(commit.Id); - Branch firstCommitBranch = repo.CreateBranch("davidfowl-rules", commit.Id.Sha); //TODO: This cries for a shortcut method :-/ + Branch firstCommitBranch = repo.CreateBranch("davidfowl-rules", commit); repo.Checkout(firstCommitBranch); File.WriteAllText(filePath, "davidfowl commits!\n"); diff --git a/LibGit2Sharp/RepositoryExtensions.cs b/LibGit2Sharp/RepositoryExtensions.cs index a9d44489..017cdba0 100644 --- a/LibGit2Sharp/RepositoryExtensions.cs +++ b/LibGit2Sharp/RepositoryExtensions.cs @@ -1,4 +1,5 @@ using System; +using LibGit2Sharp.Core; namespace LibGit2Sharp { @@ -88,6 +89,18 @@ namespace LibGit2Sharp } /// <summary> + /// Creates a branch with the specified name. This branch will point at <paramref name="target"/>. + /// </summary> + /// <param name = "repository">The <see cref = "Repository" /> being worked with.</param> + /// <param name = "branchName">The name of the branch to create.</param> + /// <param name = "target">The commit which should be pointed at by the Branch.</param> + public static Branch CreateBranch(this Repository repository, string branchName, Commit target) + { + Ensure.ArgumentNotNull(target, "target"); + return CreateBranch(repository, branchName, target.Id.Sha); + } + + /// <summary> /// Creates a branch with the specified name. This branch will point at the commit pointed at by the <see cref = "Repository.Head" />. /// </summary> /// <param name = "repository">The <see cref = "Repository" /> being worked with.</param> |