1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
- using System . Globalization ;
5
4
using System . Linq ;
6
5
using LibGit2Sharp . Core ;
7
6
using LibGit2Sharp . Core . Handles ;
@@ -13,7 +12,7 @@ namespace LibGit2Sharp
13
12
/// </summary>
14
13
public class ReferenceCollection : IEnumerable < Reference >
15
14
{
16
- private readonly Repository repo ;
15
+ internal readonly Repository repo ;
17
16
18
17
/// <summary>
19
18
/// Needed for mocking purposes.
@@ -64,64 +63,6 @@ IEnumerator IEnumerable.GetEnumerator()
64
63
65
64
#endregion
66
65
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
-
125
66
/// <summary>
126
67
/// Creates a direct reference with the specified name and target
127
68
/// </summary>
@@ -134,7 +75,7 @@ public virtual DirectReference Add(string name, ObjectId targetId, bool allowOve
134
75
Ensure . ArgumentNotNullOrEmptyString ( name , "name" ) ;
135
76
Ensure . ArgumentNotNull ( targetId , "targetId" ) ;
136
77
137
- using ( ReferenceSafeHandle handle = CreateDirectReference ( name , targetId , allowOverwrite ) )
78
+ using ( ReferenceSafeHandle handle = Proxy . git_reference_create_oid ( repo . Handle , name , targetId , allowOverwrite ) )
138
79
{
139
80
return ( DirectReference ) Reference . BuildFromPtr < Reference > ( handle , repo ) ;
140
81
}
@@ -152,7 +93,7 @@ public virtual SymbolicReference Add(string name, Reference targetRef, bool allo
152
93
Ensure . ArgumentNotNullOrEmptyString ( name , "name" ) ;
153
94
Ensure . ArgumentNotNull ( targetRef , "targetRef" ) ;
154
95
155
- using ( ReferenceSafeHandle handle = CreateSymbolicReference ( name , targetRef . CanonicalName , allowOverwrite ) )
96
+ using ( ReferenceSafeHandle handle = Proxy . git_reference_create_symbolic ( repo . Handle , name , targetRef . CanonicalName , allowOverwrite ) )
156
97
{
157
98
return ( SymbolicReference ) Reference . BuildFromPtr < Reference > ( handle , repo ) ;
158
99
}
@@ -168,35 +109,7 @@ public virtual SymbolicReference Add(string name, Reference targetRef, bool allo
168
109
[ Obsolete ( "This method will be removed in the next release. Please use Add() instead." ) ]
169
110
public virtual Reference Create ( string name , string target , bool allowOverwrite = false )
170
111
{
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 ) ;
200
113
}
201
114
202
115
/// <summary>
@@ -220,28 +133,7 @@ public virtual void Remove(Reference reference)
220
133
[ Obsolete ( "This method will be removed in the next release. Please use Remove() instead." ) ]
221
134
public virtual void Delete ( string name )
222
135
{
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 ) ;
245
137
}
246
138
247
139
/// <summary>
@@ -274,23 +166,6 @@ internal T Resolve<T>(string name) where T : Reference
274
166
}
275
167
}
276
168
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
-
294
169
/// <summary>
295
170
/// Updates the target of a direct reference.
296
171
/// </summary>
@@ -345,55 +220,6 @@ private Reference UpdateTarget<T>(Reference reference, T target, Action<Referenc
345
220
}
346
221
}
347
222
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
-
397
223
private ReferenceSafeHandle RetrieveReferencePtr ( string referenceName , bool shouldThrowIfNotFound = true )
398
224
{
399
225
ReferenceSafeHandle reference = Proxy . git_reference_lookup ( repo . Handle , referenceName , shouldThrowIfNotFound ) ;
0 commit comments