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