1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Diagnostics ;
3
4
using System . Globalization ;
4
5
using System . Linq ;
6
+ using System . Security . Policy ;
5
7
using LibGit2Sharp . Core ;
6
8
using LibGit2Sharp . Core . Handles ;
7
9
using LibGit2Sharp . Handlers ;
@@ -88,32 +90,60 @@ public virtual IEnumerable<DirectReference> ListReferences(string url)
88
90
}
89
91
}
90
92
91
- static void DoFetch ( RemoteSafeHandle remoteHandle , FetchOptions options , string logMessage )
93
+ static RemoteSafeHandle Build ( RepositorySafeHandle repoHandle , Remote remote , string url )
94
+ {
95
+ Debug . Assert ( ( remote == null ) ^ ( url == null ) ) ;
96
+
97
+ RemoteSafeHandle remoteHandle ;
98
+
99
+ if ( url != null )
100
+ {
101
+ remoteHandle = Proxy . git_remote_create_anonymous ( repoHandle , url , null ) ;
102
+ }
103
+ else
104
+ {
105
+ remoteHandle = Proxy . git_remote_lookup ( repoHandle , remote . Name , true ) ;
106
+ }
107
+
108
+ return remoteHandle ;
109
+ }
110
+
111
+ static void DoFetch ( RepositorySafeHandle repoHandle , Remote remote , string url ,
112
+ FetchOptions options , string logMessage ,
113
+ IEnumerable < string > refspecs = null )
92
114
{
93
115
if ( options == null )
94
116
{
95
117
options = new FetchOptions ( ) ;
96
118
}
97
119
98
- if ( options . TagFetchMode . HasValue )
120
+ using ( RemoteSafeHandle remoteHandle = Build ( repoHandle , remote , url ) )
99
121
{
100
- Proxy . git_remote_set_autotag ( remoteHandle , options . TagFetchMode . Value ) ;
101
- }
122
+ if ( options . TagFetchMode . HasValue )
123
+ {
124
+ Proxy . git_remote_set_autotag ( repoHandle , remote . Name , options . TagFetchMode . Value ) ;
125
+ }
126
+
127
+ if ( refspecs != null )
128
+ {
129
+ Proxy . git_remote_set_fetch_refspecs ( remoteHandle , refspecs ) ;
130
+ }
102
131
103
- var callbacks = new RemoteCallbacks ( options ) ;
104
- GitRemoteCallbacks gitCallbacks = callbacks . GenerateCallbacks ( ) ;
132
+ var callbacks = new RemoteCallbacks ( options ) ;
133
+ GitRemoteCallbacks gitCallbacks = callbacks . GenerateCallbacks ( ) ;
105
134
106
- // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
107
- // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
108
- // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
109
- // where the managed layer could move the git_remote_callbacks to a different location in memory,
110
- // but libgit2 would still reference the old address.
111
- //
112
- // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
113
- // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
114
- Proxy . git_remote_set_callbacks ( remoteHandle , ref gitCallbacks ) ;
135
+ // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
136
+ // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
137
+ // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
138
+ // where the managed layer could move the git_remote_callbacks to a different location in memory,
139
+ // but libgit2 would still reference the old address.
140
+ //
141
+ // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
142
+ // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
143
+ Proxy . git_remote_set_callbacks ( remoteHandle , ref gitCallbacks ) ;
115
144
116
- Proxy . git_remote_fetch ( remoteHandle , logMessage ) ;
145
+ Proxy . git_remote_fetch ( remoteHandle , logMessage ) ;
146
+ }
117
147
}
118
148
119
149
/// <summary>
@@ -127,10 +157,7 @@ public virtual void Fetch(Remote remote, FetchOptions options = null,
127
157
{
128
158
Ensure . ArgumentNotNull ( remote , "remote" ) ;
129
159
130
- using ( RemoteSafeHandle remoteHandle = Proxy . git_remote_lookup ( repository . Handle , remote . Name , true ) )
131
- {
132
- DoFetch ( remoteHandle , options , logMessage ) ;
133
- }
160
+ DoFetch ( repository . Handle , remote , null , options , logMessage ) ;
134
161
}
135
162
136
163
/// <summary>
@@ -146,12 +173,7 @@ public virtual void Fetch(Remote remote, IEnumerable<string> refspecs, FetchOpti
146
173
Ensure . ArgumentNotNull ( remote , "remote" ) ;
147
174
Ensure . ArgumentNotNull ( refspecs , "refspecs" ) ;
148
175
149
- using ( RemoteSafeHandle remoteHandle = Proxy . git_remote_lookup ( repository . Handle , remote . Name , true ) )
150
- {
151
- Proxy . git_remote_set_fetch_refspecs ( remoteHandle , refspecs ) ;
152
-
153
- DoFetch ( remoteHandle , options , logMessage ) ;
154
- }
176
+ DoFetch ( repository . Handle , remote , null , options , logMessage , refspecs ) ;
155
177
}
156
178
157
179
/// <summary>
@@ -170,12 +192,7 @@ public virtual void Fetch(
170
192
Ensure . ArgumentNotNull ( url , "url" ) ;
171
193
Ensure . ArgumentNotNull ( refspecs , "refspecs" ) ;
172
194
173
- using ( RemoteSafeHandle remoteHandle = Proxy . git_remote_create_anonymous ( repository . Handle , url , null ) )
174
- {
175
- Proxy . git_remote_set_fetch_refspecs ( remoteHandle , refspecs ) ;
176
-
177
- DoFetch ( remoteHandle , options , logMessage ) ;
178
- }
195
+ DoFetch ( repository . Handle , null , options , logMessage , refspecs ) ;
179
196
}
180
197
181
198
/// <summary>
0 commit comments