8000 Make Refs.Add() accept a Reference as its target · rlazev/libgit2sharp@8fa70ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 8fa70ff

Browse files
committed
Make Refs.Add() accept a Reference as its target
1 parent a69653f commit 8fa70ff

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

LibGit2Sharp.Tests/ReferenceFixture.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void CanAddADirectReferenceFromRevParseSpec()
5252
}
5353

5454
[Fact]
55-
public void CanAddASymbolicReference()
55+
public void CanAddASymbolicReferenceFromTheTargetName()
5656
{
5757
const string name = "refs/heads/unit_test";
5858
const string target = "refs/heads/master";
@@ -61,15 +61,38 @@ public void CanAddASymbolicReference()
6161
using (var repo = new Repository(path.RepositoryPath))
6262
{
6363
var newRef = (SymbolicReference)repo.Refs.Add(name, target);
64-
Assert.NotNull(newRef);
65-
Assert.Equal(name, newRef.CanonicalName);
66-
Assert.Equal(target, newRef.Target.CanonicalName);
67-
Assert.Equal(newRef.Target.CanonicalName, newRef.TargetIdentifier);
68-
Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newRef.ResolveToDirectReference().Target.Sha);
69-
Assert.NotNull(repo.Refs[name]);
64+
65+
AssertSymbolicRef(newRef, repo, target, name);
66+
}
67+
}
68+
69+
[Fact]
70+
public void CanAddASymbolicReferenceFromTheTargetReference()
71+
{
72+
const string name = "refs/heads/unit_test";
73+
const string target = "refs/heads/master";
74+
75+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
76+
using (var repo = new Repository(path.RepositoryPath))
77+
{
78+
var targetRef = repo.Refs[target];
79+
80+
var newRef = repo.Refs.Add(name, targetRef);
81+
82+
AssertSymbolicRef(newRef, repo, target, name);
7083
}
7184
}
7285

86+
private static void AssertSymbolicRef(SymbolicReference newRef, Repository repo, string expectedTargetName, string expectedName)
87+
{
88+
Assert.NotNull(newRef);
89+
Assert.Equal(expectedName, newRef.CanonicalName);
90+
Assert.Equal(expectedTargetName, newRef.Target.CanonicalName);
91+
Assert.Equal(newRef.Target.CanonicalName, newRef.TargetIdentifier);
92+
Assert.Equal("4c062a6361ae6959e06292c1fa5e2822d9c96345", newRef.ResolveToDirectReference().Target.Sha);
93+
Assert.NotNull(repo.Refs[expectedName]);
94+
}
95+
7396
[Fact]
7497
public void BlindlyCreatingADirectReferenceOverAnExistingOneThrows()
7598
{

LibGit2Sharp/ReferenceCollection.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ public virtual DirectReference Add(string name, ObjectId targetId, bool allowOve
141141
}
142142
}
143143

144+
/// <summary>
145+
/// Creates a symbolic reference with the specified name and target
146+
/// </summary>
147+
/// <param name = "name">The name of the reference to create.</param>
148+
/// <param name = "targetRef">The target reference.</param>
149+
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
150+
/// <returns>A new <see cref = "Reference" />.</returns>
151+
public virtual SymbolicReference Add(string name, Reference targetRef, bool allowOverwrite = false)
152+
{
153+
Ensure.ArgumentNotNullOrEmptyString(name, "name");
154+
Ensure.ArgumentNotNull(targetRef, "targetRef");
155+
156+
using (ReferenceSafeHandle handle = CreateSymbolicReference(name, targetRef.CanonicalName, allowOverwrite))
157+
{
158+
return (SymbolicReference)Reference.BuildFromPtr<Reference>(handle, repo);
159+
}
160+
}
161+
144162
/// <summary>
145163
/// Creates a direct or symbolic reference with the specified name and target
146164
/// </summary>

0 commit comments

Comments
 (0)
0