@@ -107,25 +107,37 @@ public virtual Reference Add(string name, string canonicalRefNameOrObjectish, bo
107
107
Ensure . ArgumentNotNullOrEmptyString ( name , "name" ) ;
108
108
Ensure . ArgumentNotNullOrEmptyString ( canonicalRefNameOrObjectish , "canonicalRefNameOrObjectish" ) ;
109
109
110
- Func < string , bool , ReferenceSafeHandle > referenceCreator ;
111
-
112
110
Reference reference ;
113
111
RefState refState = TryResolveReference ( out reference , canonicalRefNameOrObjectish ) ;
114
112
115
113
var gitObject = repo . Lookup ( canonicalRefNameOrObjectish , GitObjectType . Any , LookUpOptions . None ) ;
116
114
117
115
if ( refState == RefState . Exists || ( refState == RefState . DoesNotExistButLooksValid && gitObject == null ) )
118
116
{
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
+ }
124
121
}
125
122
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 ) )
127
139
{
128
- return Reference . BuildFromPtr < Reference > ( handle , repo ) ;
140
+ return ( DirectReference ) Reference . BuildFromPtr < Reference > ( handle , repo ) ;
129
141
}
130
142
}
131
143
@@ -151,32 +163,13 @@ private ReferenceSafeHandle CreateSymbolicReference(string name, string target,
151
163
152
164
private ReferenceSafeHandle CreateDirectReference ( string name , ObjectId targetId , bool allowOverwrite )
153
165
{
154
- targetId = Unabbreviate ( targetId ) ;
155
-
156
166
GitOid oid = targetId . Oid ;
157
167
158
168
ReferenceSafeHandle handle ;
159
169
Ensure . Success ( NativeMethods . git_reference_create_oid ( out handle , repo . Handle , name , ref oid , allowOverwrite ) ) ;
160
170
return handle ;
161
171
}
162
172
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
-
180
173
/// <summary>
181
174
/// Delete a reference with the specified name
182
175
/// </summary>
0 commit comments