8000 Fix a branch creation issue · joncham/libgit2sharp@a1f568b · GitHub
[go: up one dir, main page]

Skip to content

Commit a1f568b

Browse files
committed
Fix a branch creation issue
When being passed another branch as a starting point the creation process now dereferences the branch instead of creating a symbolic reference.
1 parent d9a871e commit a1f568b

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 41 additions & 3 deletions
B222
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,26 @@ public void CreatingBranchWithEmptyNameThrows()
161161
{
162162
using (var repo = new Repository(Constants.TestRepoPath))
163163
{
164-
Assert.Throws<ArgumentException>(() => repo.Branches.Create(string.Empty, "not_important_sha"));
164+
Assert.Throws<ArgumentException>(() => repo.Branches.Create(string.Empty, repo.Head.ResolveToDirectReference().TargetIdentifier));
165+
}
166+
}
167+
168+
[Test]
169+
[Ignore("Not implemented yet.")]
170+
public void CreatingBranchWithUnknownNamedTargetThrows()
171+
{
172+
using (var repo = new Repository(Constants.TestRepoPath))
173+
{
174+
Assert.Throws<ArgumentException>(() => repo.Branches.Create("my_new_branch", "my_old_branch"));
175+
}
176+
}
177+
178+
[Test]
179+
public void CreatingBranchWithUnknownShaTargetThrows()
180+
{
181+
using (var repo = new Repository(Constants.TestRepoPath))
182+
{
183+
Assert.Throws<ApplicationException>(() => repo.Branches.Create("my_new_branch", Constants.UnknownSha));
165184
}
166185
}
167186

@@ -179,7 +198,7 @@ public void CreatingBranchWithNullNameThrows()
179198
{
180199
using (var repo = new Repository(Constants.TestRepoPath))
181200
{
182-
Assert.Throws<ArgumentNullException>(() => repo.Branches.Create(null, "not_important_sha"));
201+
Assert.Throws<ArgumentNullException>(() => repo.Branches.Create(null, repo.Head.ResolveToDirectReference().TargetIdentifier));
183202
}
184203
}
185204

@@ -260,6 +279,7 @@ public void CanMoveABranch()
260279
repo.Branches["br3"].ShouldNotBeNull();
261280
}
262281
}
282+
263283
[Test]
264284
public void BlindlyMovingABranchOverAnExistingOneThrows()
265285
{
@@ -294,5 +314,23 @@ public void CanMoveABranchWhileOverwritingAnExistingOne()
294314
newTest.Tip.ShouldEqual(br2.Tip);
295315
}
296316
}
317+
318+
[Test]
319+
public void CreatingABranchTriggersTheCreationOfADirectReference()
320+
{
321+
using (var path = new TemporaryCloneOfTestRepo())
322+
using (var repo = new Repository(path.RepositoryPath))
323+
{
324+
var newBranch = repo.CreateBranch("clone-of-master");
325+
newBranch.IsCurrentRepositoryHead.ShouldBeFalse();
326+
327+
var commitId = repo.Head.ResolveToDirectReference().TargetIdentifier;
328+
newBranch.Tip.Sha.ShouldEqual(commitId);
329+
330+
var reference = repo.Refs[newBranch.CanonicalName];
331+
reference.ShouldNotBeNull();
332+
Assert.IsInstanceOf(typeof (DirectReference), reference);
333+
}
334+
}
297335
}
298-
}
336+
}

LibGit2Sharp/BranchCollection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public Branch Create(string name, string target)
8383

8484
if (id == null)
8585
{
86-
target = NormalizeToCanonicalName(target);
86+
var reference = repo.Refs[NormalizeToCanonicalName(target)].ResolveToDirectReference();
87+
target = reference.TargetIdentifier;
8788
}
8889

8990
repo.Refs.Create(NormalizeToCanonicalName(name), target);

0 commit comments

Comments
 (0)
0