8000 Turn repo.Refs string based overloads into extension methods · rlazev/libgit2sharp@3dbac13 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3dbac13

Browse files
committed
Turn repo.Refs string based overloads into extension methods
1 parent 8fab83b commit 3dbac13

File tree

3 files changed

+177
-179
lines changed

3 files changed

+177
-179
lines changed

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="ConfigurationEntry.cs" />
6969
<Compile Include="ContentChanges.cs" />
7070
<Compile Include="Core\Proxy.cs" />
71+
<Compile Include="ReferenceCollectionExtensions.cs" />
7172
<Compile Include="TagCollectionExtensions.cs" />
7273
<Compile Include="Core\Compat\Environment.cs" />
7374
<Compile Include="Core\FilePath.cs" />

LibGit2Sharp/ReferenceCollection.cs

Lines changed: 5 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.Globalization;
54
using System.Linq;
65
using LibGit2Sharp.Core;
76
using LibGit2Sharp.Core.Handles;
@@ -13,7 +12,7 @@ namespace LibGit2Sharp
1312
/// </summary>
1413
public class ReferenceCollection : IEnumerable<Reference>
1514
{
16-
private readonly Repository repo;
15+
internal readonly Repository repo;
1716

1817
/// <summary>
1918
/// Needed for mocking purposes.
@@ -64,64 +63,6 @@ IEnumerator IEnumerable.GetEnumerator()
6463

6564
#endregion
6665

67-
private enum RefState
68-
{
69-
Exists,
70-
DoesNotExistButLooksValid,
71-
DoesNotLookValid,
72-
}
73-
74-
private RefState TryResolveReference(out Reference reference, string canonicalName)
75-
{
76-
try
77-
{
78-
//TODO: Maybe would it be better to rather rely on git_reference_normalize_name()
79-
//This would be much more straightforward and less subject to fail for the wrong reason.
80-
81-
reference = repo.Refs[canonicalName];
82-
83-
if (reference != null)
84-
{
85-
return RefState.Exists;
86-
}
87-
88-
return RefState.DoesNotExistButLooksValid;
89-
}
90-
catch (LibGit2SharpException)
91-
{
92-
reference = null;
93-
return RefState.DoesNotLookValid;
94-
}
95-
}
96-
97-
/// <summary>
98-
/// Creates a direct or symbolic reference with the specified name and target
99-
/// </summary>
100-
/// <param name = "name">The name of the reference to create.</param>
101-
/// <param name = "canonicalRefNameOrObjectish">The target which can be either the canonical name of a branch reference or a revparse spec.</param>
102-
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
103-
/// <returns>A new <see cref = "Reference" />.</returns>
104-
public virtual Reference Add(string name, string canonicalRefNameOrObjectish, bool allowOverwrite = false)
105-
{
106-
Ensure.ArgumentNotNullOrEmptyString(name, "name");
107-
Ensure.ArgumentNotNullOrEmptyString(canonicalRefNameOrObjectish, "canonicalRefNameOrObjectish");
108-
109-
Reference reference;
110-
RefState refState = TryResolveReference(out reference, canonicalRefNameOrObjectish);
111-
112-
var gitObject = repo.Lookup(canonicalRefNameOrObjectish, GitObjectType.Any, LookUpOptions.None);
113-
114-
if (refState == RefState.Exists || (refState == RefState.DoesNotExistButLooksValid && gitObject == null))
115-
{
116-
using (ReferenceSafeHandle handle = CreateSymbolicReference(name, canonicalRefNameOrObjectish, allowOverwrite))
117-
{
118-
return Reference.BuildFromPtr<Reference>(handle, repo);
119-
}
120-
}
121-
122-
return Add(name, gitObject.Id, allowOverwrite);
123-
}
124-
12566
/// <summary>
12667
/// Creates a direct reference with the specified name and target
12768
/// </summary>
@@ -134,7 +75,7 @@ public virtual DirectReference Add(string name, ObjectId targetId, bool allowOve
13475
Ensure.ArgumentNotNullOrEmptyString(name, "name");
13576
Ensure.ArgumentNotNull(targetId, "targetId");
13677

137-
using (ReferenceSafeHandle handle = CreateDirectReference(name, targetId, allowOverwrite))
78+
using (ReferenceSafeHandle handle = Proxy.git_reference_create_oid(repo.Handle, name, targetId, allowOverwrite))
13879
{
13980
return (DirectReference)Reference.BuildFromPtr<Reference>(handle, repo);
14081
}
@@ -152,7 +93,7 @@ public virtual SymbolicReference Add(string name, Reference targetRef, bool allo
15293
Ensure.ArgumentNotNullOrEmptyString(name, "name");
15394
Ensure.ArgumentNotNull(targetRef, "targetRef");
15495

155-
using (ReferenceSafeHandle handle = CreateSymbolicReference(name, targetRef.CanonicalName, allowOverwrite))
96+
using (ReferenceSafeHandle handle = Proxy.git_reference_create_symbolic(repo.Handle, name, targetRef.CanonicalName, allowOverwrite))
15697
{
15798
return (SymbolicReference)Reference.BuildFromPtr<Reference>(handle, repo);
15899
}
@@ -168,35 +109,7 @@ public virtual SymbolicReference Add(string name, Reference targetRef, bool allo
168109
[Obsolete("This method will be removed in the next release. Please use Add() instead.")]
169110
public virtual Reference Create(string name, string target, bool allowOverwrite = false)
170111
{
171-
return Add(name, target, allowOverwrite);
172-
}
173-
174-
private ReferenceSafeHandle CreateSymbolicReference(string name, string target, bool allowOverwrite)
175-
{
176-
return Proxy.git_reference_create_symbolic(repo.Handle, name, target, allowOverwrite);
177-
}
178-
179-
private ReferenceSafeHandle CreateDirectReference(string name, ObjectId targetId, bool allowOverwrite)
180-
{
181-
return Proxy.git_reference_create_oid(repo.Handle, name, targetId, allowOverwrite);
182-
}
183-
184-
/// <summary>
185-
/// Delete a reference with the specified name
186-
/// </summary>
187-
/// <param name = "name">The canonical name of the reference to delete.</param>
188-
public virtual void Remove(string name)
189-
{
190-
Ensure.ArgumentNotNullOrEmptyString(name, "name");
191-
192-
Reference reference = this[name];
193-
194-
if (reference == null)
195-
{
196-
return;
197-
}
198-
199-
Remove(reference);
112+
return this.Add(name, target, allowOverwrite);
200113
}
201114

202115
/// <summary>
@@ -220,28 +133,7 @@ public virtual void Remove(Reference reference)
220133
[Obsolete("This method will be removed in the next release. Please use Remove() instead.")]
221134
public virtual void Delete(string name)
222135
{
223-
Remove(name);
224-
}
225-
226-
/// <summary>
227-
/// Rename an existing reference with a new name
228-
/// </summary>
229-
/// <param name = "currentName">The canonical name of the reference to rename.</param>
230-
/// <param name = "newName">The new canonical name.</param>
231-
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing reference, false otherwise.</param>
232-
/// <returns>A new <see cref = "Reference" />.</returns>
233-
public virtual Reference Move(string currentName, string newName, bool allowOverwrite = false)
234-
{
235-
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
236-
237-
Reference reference = this[currentName];
238-
239-
if (reference == null)
240-
{
241-
throw new LibGit2SharpException(string.Format("Reference '{0}' doesn't exist. One cannot move a non existing reference.", currentName));
242-
}
243-
244-
return Move(reference, newName, allowOverwrite);
136+
this.Remove(name);
245137
}
246138

247139
/// <summary>
@@ -274,23 +166,6 @@ internal T Resolve<T>(string name) where T : Reference
274166
}
275167
}
276168

277-
/// <summary>
278-
/// Updates the target of a direct reference.
279-
/// </summary>
280-
/// <param name = "directRef">The direct reference which target should be updated.</param>
281-
/// <param name = "objectish">The revparse spec of the target.</param>
282-
public virtual Reference UpdateTarget(Reference directRef, string objectish)
283-
{
284-
Ensure.ArgumentNotNull(directRef, "directRef");
285-
Ensure.ArgumentNotNull(objectish, "objectish");
286-
287-
GitObject target = repo.Lookup(objectish);
288-
289-
Ensure.GitObjectIsNotNull(target, objectish);
290-
291-
return UpdateTarget(directRef, target.Id);
292-
}
293-
294169
/// <summary>
295170
/// Updates the target of a direct reference.
296171
/// </summary>
@@ -345,55 +220,6 @@ private Reference UpdateTarget<T>(Reference reference, T target, Action<Referenc
345220
}
346221
}
347222

348-
/// <summary>
349-
/// Updates the target of a reference.
350-
/// </summary>
351-
/// <param name = "name">The canonical name of the reference.</param>
352-
/// <param name = "canonicalRefNameOrObjectish">The target which can be either the canonical name of a reference or a revparse spec.</param>
353-
/// <returns>A new <see cref = "Reference" />.</returns>
354-
public virtual Reference UpdateTarget(string name, string canonicalRefNameOrObjectish)
355-
{
356-
Ensure.ArgumentNotNullOrEmptyString(name, "name");
357-
Ensure.ArgumentNotNullOrEmptyString(canonicalRefNameOrObjectish, "canonicalRefNameOrObjectish");
358-
359-
if (name == "HEAD")
360-
{
361-
return this.Add("HEAD", canonicalRefNameOrObjectish, true);
362-
}
363-
364-
Reference reference = this[name];
365-
366-
var directReference = reference as DirectReference;
367-
if (directReference != null)
368-
{
369-
ObjectId id;
370-
bool isObjectIdentifier = ObjectId.TryParse(canonicalRefNameOrObjectish, out id);
371-
372-
if (!isObjectIdentifier)
373-
{
374-
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The reference specified by {0} is an Oid reference, you must provide a sha as the target.", name), "target");
375-
}
376-
377-
378-
return UpdateTarget(directReference, id);
379-
}
380-
381-
var symbolicReference = reference as SymbolicReference;
382-
if (symbolicReference != null)
383-
{
384-
Reference targetRef = this[canonicalRefNameOrObjectish];
385-
386-
if (targetRef == null)
387-
{
388-
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The reference specified by {0} is a Symbolic reference, you must provide a reference canonical name as the target.", name), "target");
389-
}
390-
391-
return UpdateTarget(symbolicReference, targetRef);
392-
}
393-
394-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Reference '{0}' has an unexpected type ('{1}').", name, reference.GetType()));
395-
}
396-
397223
private ReferenceSafeHandle RetrieveReferencePtr(string referenceName, bool shouldThrowIfNotFound = true)
398224
{
399225
ReferenceSafeHandle reference = Proxy.git_reference_lookup(repo.Handle, referenceName, shouldThrowIfNotFound);

0 commit comments

Comments
 (0)
0