8000 Make Refs.Add() accept an ObjectId as its target · rlazev/libgit2sharp@a69653f · GitHub
[go: up one dir, main page]

Skip to content

Commit a69653f

Browse files
committed
Make Refs.Add() accept an ObjectId as its target
1 parent 41364ef commit a69653f

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

LibGit2Sharp.Tests/ReferenceFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public void AddWithNullForTargetThrows()
149149
{
150150
using (var repo = new Repository(BareTestRepoPath))
151151
{
152-
Assert.Throws<ArgumentNullException>(() => repo.Refs.Add("refs/heads/newref", null));
152+
Assert.Throws<ArgumentNullException>(() => repo.Refs.Add("refs/heads/newref", (string)null));
153+
Assert.Throws<ArgumentNullException>(() => repo.Refs.Add("refs/heads/newref", (ObjectId)null));
153154
}
154155
}
155156

LibGit2Sharp/ReferenceCollection.cs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,37 @@ public virtual Reference Add(string name, string canonicalRefNameOrObjectish, bo
107107
Ensure.ArgumentNotNullOrEmptyString(name, "name");
108108
Ensure.ArgumentNotNullOrEmptyString(canonicalRefNameOrObjectish, "canonicalRefNameOrObjectish");
109109

110-
Func<string, bool, ReferenceSafeHandle> referenceCreator;
111-
112110
Reference reference;
113111
RefState refState = TryResolveReference(out reference, canonicalRefNameOrObjectish);
114112

115113
var gitObject = repo.Lookup(canonicalRefNameOrObjectish, GitObjectType.Any, LookUpOptions.None);
116114

117115
if (refState == RefState.Exists || (refState == RefState.DoesNotExistButLooksValid && gitObject == null))
118116
{
119-
referenceCreator = (n, o) => CreateSymbolicReference(n, canonicalRefNameOrObjectish, o);
120-
}
121-
else
122-
{
123-
referenceCreator = (n, o) => CreateDirectReference(n, gitObject.Id, o);
117+
using (ReferenceSafeHandle handle = CreateSymbolicReference(name, canonicalRefNameOrObjectish, allowOverwrite))
118+
{
119+
return Reference.BuildFromPtr<Reference>(handle, repo);
120+
}
124121
}
125122

126-
using (ReferenceSafeHandle handle = referenceCreator(name, allowOverwrite))
123+
return Add(name, gitObject.Id, allowOverwrite);
124+
}
125+
126+
/// <summary>
127+
/// Creates a direct reference with the specified name and target
128+
/// </summary>
129+
/// <param name = "name">The name of the reference to create.</param>
130+
/// <param name = "targetId">Id of the target object.</param>
131+
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
132+
/// <returns>A new <see cref = "Reference" />.</returns>
133+
public virtual DirectReference Add(string name, ObjectId targetId, bool allowOverwrite = false)
134+
{
135+
Ensure.ArgumentNotNullOrEmptyString(name, "name");
136+
Ensure.ArgumentNotNull(targetId, "targetId");
137+
138+
using (ReferenceSafeHandle handle = CreateDirectReference(name, targetId, allowOverwrite))
127139
{
128-
return Reference.BuildFromPtr<Reference>(handle, repo);
140+
return (DirectReference)Reference.BuildFromPtr<Reference>(handle, repo);
129141
}
130142
}
131143

@@ -151,32 +163,13 @@ private ReferenceSafeHandle CreateSymbolicReference(string name, string target,
151163

152164
private ReferenceSafeHandle CreateDirectReference(string name, ObjectId targetId, bool allowOverwrite)
153165
{
154-
targetId = Unabbreviate(targetId);
155-
156166
GitOid oid = targetId.Oid;
157167

158168
ReferenceSafeHandle handle;
159169
Ensure.Success(NativeMethods.git_reference_create_oid(out handle, repo.Handle, name, ref oid, allowOverwrite));
160170
return handle;
161171
}
162172

163-
private ObjectId Unabbreviate(ObjectId targetId)
164-
{
165-
if (!(targetId is AbbreviatedObjectId))
166-
{
167-
return targetId;
168-
}
169-
170-
GitObject obj = repo.Lookup(targetId);
171-
172-
if (obj == null)
173-
{
174-
Ensure.Success((int)GitErrorCode.NotFound);
175-
}
176-
177-
return obj.Id;
178-
}
179-
180173
/// <summary>
181174
/// Delete a reference with the specified name
182175
/// </summary>

0 commit comments

Comments
 (0)
0