-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathGTRepository+RemoteOperations.m
320 lines (242 loc) · 11.9 KB
/
GTRepository+RemoteOperations.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// GTRepository+RemoteOperations.m
// ObjectiveGitFramework
//
// Created by Etienne on 18/11/13.
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
//
#import "GTRepository+RemoteOperations.h"
#import "EXTScope.h"
#import "GTCredential.h"
#import "GTCredential+Private.h"
#import "GTFetchHeadEntry.h"
#import "GTOID.h"
#import "GTRemote.h"
#import "GTSignature.h"
#import "NSArray+StringArray.h"
#import "NSError+Git.h"
#import "GTRepository+References.h"
#import "GTNote.h"
#import "git2/errors.h"
#import "git2/remote.h"
#import "git2/notes.h"
#import "git2/buffer.h"
NSString *const GTRepositoryRemoteOptionsCredentialProvider = @"GTRepositoryRemoteOptionsCredentialProvider";
NSString *const GTRepositoryRemoteOptionsFetchPrune = @"GTRepositoryRemoteOptionsFetchPrune";
NSString *const GTRepositoryRemoteOptionsDownloadTags = @"GTRepositoryRemoteOptionsDownloadTags";
NSString *const GTRepositoryRemoteOptionsPushNotes = @"GTRepositoryRemoteOptionsPushNotes";
typedef void (^GTRemoteFetchTransferProgressBlock)(const git_transfer_progress *stats, BOOL *stop);
typedef void (^GTRemotePushTransferProgressBlock)(unsigned int current, unsigned int total, size_t bytes, BOOL *stop);
@implementation GTRepository (RemoteOperations)
#pragma mark -
#pragma mark Common Remote code
typedef struct {
GTCredentialAcquireCallbackInfo credProvider;
__unsafe_unretained GTRemoteFetchTransferProgressBlock fetchProgressBlock;
__unsafe_unretained GTRemotePushTransferProgressBlock pushProgressBlock;
git_direction direction;
} GTRemoteConnectionInfo;
int GTRemoteFetchTransferProgressCallback(const git_transfer_progress *stats, void *payload) {
GTRemoteConnectionInfo *info = payload;
BOOL stop = NO;
if (info->fetchProgressBlock != nil) {
info->fetchProgressBlock(stats, &stop);
}
return (stop == YES ? GIT_EUSER : 0);
}
int GTRemotePushTransferProgressCallback(unsigned int current, unsigned int total, size_t bytes, void *payload) {
GTRemoteConnectionInfo *pushPayload = payload;
BOOL stop = NO;
if (pushPayload->pushProgressBlock) {
pushPayload->pushProgressBlock(current, total, bytes, &stop);
}
return (stop == YES ? GIT_EUSER : 0);
}
#pragma mark - Fetch
- (BOOL)fetchRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemoteFetchTransferProgressBlock)progressBlock {
GTCredentialProvider *credProvider = options[GTRepositoryRemoteOptionsCredentialProvider];
GTRemoteConnectionInfo connectionInfo = {
.credProvider = {credProvider},
.direction = GIT_DIRECTION_FETCH,
.fetchProgressBlock = progressBlock,
};
git_remote_callbacks remote_callbacks = {
.version = GIT_REMOTE_CALLBACKS_VERSION,
.credentials = (credProvider != nil ? GTCredentialAcquireCallback : NULL),
.transfer_progress = GTRemoteFetchTransferProgressCallback,
.payload = &connectionInfo,
};
git_fetch_options fetchOptions = GIT_FETCH_OPTIONS_INIT;
fetchOptions.callbacks = remote_callbacks;
fetchOptions.prune = [options[GTRepositoryRemoteOptionsFetchPrune] unsignedIntValue];
fetchOptions.download_tags = [options[GTRepositoryRemoteOptionsDownloadTags] unsignedIntValue];
__block git_strarray refspecs;
int gitError = git_remote_get_fetch_refspecs(&refspecs, remote.git_remote);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to get fetch refspecs for remote"];
return NO;
}
@onExit {
git_strarray_free(&refspecs);
};
NSString *reflog_message = [NSString stringWithFormat:@"fetching remote %@", remote.name];
gitError = git_remote_fetch(remote.git_remote, &refspecs, &fetchOptions, reflog_message.UTF8String);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to fetch from remote"];
return NO;
}
return YES;
}
#pragma mark - Fetch Head Enumeration
typedef void (^GTRemoteEnumerateFetchHeadEntryBlock)(GTFetchHeadEntry *entry, BOOL *stop);
typedef struct {
__unsafe_unretained GTRepository *repository;
__unsafe_unretained GTRemoteEnumerateFetchHeadEntryBlock enumerationBlock;
} GTEnumerateHeadEntriesPayload;
int GTFetchHeadEntriesCallback(const char *ref_name, const char *remote_url, const git_oid *oid, unsigned int is_merge, void *payload) {
GTEnumerateHeadEntriesPayload *entriesPayload = payload;
GTRepository *repository = entriesPayload->repository;
GTRemoteEnumerateFetchHeadEntryBlock enumerationBlock = entriesPayload->enumerationBlock;
NSString *refName = @(ref_name);
NSCAssert(refName, @"refName is nil");
NSString *remoteURL = @(remote_url);
NSCAssert(remote_url, @"remoteURL is nil");
GTReference *reference = [repository lookUpReferenceWithName:refName error:NULL];
GTFetchHeadEntry *entry = [[GTFetchHeadEntry alloc] initWithReference:reference remoteURLString:remoteURL targetOID:[GTOID oidWithGitOid:oid] isMerge:(BOOL)is_merge];
BOOL stop = NO;
enumerationBlock(entry, &stop);
return (stop == YES ? GIT_EUSER : 0);
}
- (BOOL)enumerateFetchHeadEntriesWithError:(NSError **)error usingBlock:(void (^)(GTFetchHeadEntry *fetchHeadEntry, BOOL *stop))block {
NSParameterAssert(block != nil);
GTEnumerateHeadEntriesPayload payload = {
.repository = self,
.enumerationBlock = block,
};
int gitError = git_repository_fetchhead_foreach(self.git_repository, GTFetchHeadEntriesCallback, &payload);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to get fetchhead entries"];
return NO;
}
return YES;
}
- (NSArray *)fetchHeadEntriesWithError:(NSError **)error {
NSMutableArray *entries = [NSMutableArray array];
[self enumerateFetchHeadEntriesWithError:error usingBlock:^(GTFetchHeadEntry *fetchHeadEntry, BOOL *stop) {
[entries addObject:fetchHeadEntry];
*stop = NO;
}];
return entries;
}
#pragma mark - Push (Public)
- (BOOL)pushBranch:(GTBranch *)branch toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
NSParameterAssert(branch != nil);
NSParameterAssert(remote != nil);
return [self pushBranches:@[ branch ] toRemote:remote withOptions:options error:error progress:progressBlock];
}
- (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
NSParameterAssert(branches != nil);
NSParameterAssert(branches.count != 0);
NSParameterAssert(remote != nil);
NSMutableArray *refspecs = nil;
// Build refspecs for the passed in branches
refspecs = [NSMutableArray arrayWithCapacity:branches.count];
for (GTBranch *branch in branches) {
// Default remote reference for when branch doesn't exist on remote - create with same short name
NSString *remoteBranchReference = [NSString stringWithFormat:@"refs/heads/%@", branch.shortName];
BOOL success = NO;
GTBranch *trackingBranch = [branch trackingBranchWithError:error success:&success];
if (success && trackingBranch != nil) {
// Use remote branch short name from trackingBranch, which could be different
// (e.g. refs/heads/master:refs/heads/my_master)
remoteBranchReference = [NSString stringWithFormat:@"refs/heads/%@", trackingBranch.shortName];
}
[refspecs addObject:[NSString stringWithFormat:@"refs/heads/%@:%@", branch.shortName, remoteBranchReference]];
}
// Also push the notes reference(s), if needed.
id pushNotesOption = options[GTRepositoryRemoteOptionsPushNotes];
if (pushNotesOption != nil) {
if ([pushNotesOption isKindOfClass:[NSNumber class]]) { // Push notes is a bool, only push the default reference name if it's YES
if ([(NSNumber *)pushNotesOption boolValue]) {
NSString *notesReferenceName = [GTNote defaultReferenceNameForRepository:self error:nil];
// Check whether the reference name exists for the repo, or our push will fail
if (notesReferenceName != nil && [self lookUpReferenceWithName:notesReferenceName error:nil] != nil) {
[refspecs addObject:[NSString stringWithFormat:@"%@:%@", notesReferenceName, notesReferenceName]];
}
}
} else if ([pushNotesOption isKindOfClass:[NSArray class]]) {
for (NSString *notesReferenceName in (NSArray *)pushNotesOption) {
if ([notesReferenceName isKindOfClass:[NSString class]]) { // Just a sanity check, we only accept NSStrings in the array
// Check whether the reference name exists for the repo, or our push will fail
if (notesReferenceName != nil && [self lookUpReferenceWithName:notesReferenceName error:nil] != nil) {
[refspecs addObject:[NSString stringWithFormat:@"%@:%@", notesReferenceName, notesReferenceName]];
}
}
}
}
}
return [self pushRefspecs:refspecs toRemote:remote withOptions:options error:error progress:progressBlock];
}
- (BOOL)pushNotes:(NSString *)noteRef toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
NSParameterAssert(remote != nil);
if (noteRef == nil) {
noteRef = [GTNote defaultReferenceNameForRepository:self error:error];
if (noteRef == nil) return NO;
}
GTReference *notesReference = [self lookUpReferenceWithName:noteRef error:error];
if (notesReference == nil) return NO;
return [self pushRefspecs:@[[NSString stringWithFormat:@"%@:%@", noteRef, noteRef]] toRemote:remote withOptions:options error:error progress:progressBlock];
}
#pragma mark - Deletion (Public)
- (BOOL)deleteBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(branch != nil);
NSParameterAssert(remote != nil);
NSArray *refspecs = @[ [NSString stringWithFormat:@":refs/heads/%@", branch.shortName] ];
return [self pushRefspecs:refspecs toRemote:remote withOptions:options error:error progress:nil];
}
#pragma mark - Push (Private)
- (BOOL)pushRefspecs:(NSArray *)refspecs toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
int gitError;
GTCredentialProvider *credProvider = options[GTRepositoryRemoteOptionsCredentialProvider];
GTRemoteConnectionInfo connectionInfo = {
.credProvider = { .credProvider = credProvider },
.direction = GIT_DIRECTION_PUSH,
.pushProgressBlock = progressBlock,
};
git_remote_callbacks remote_callbacks = GIT_REMOTE_CALLBACKS_INIT;
remote_callbacks.credentials = (credProvider != nil ? GTCredentialAcquireCallback : NULL);
remote_callbacks.push_transfer_progress = GTRemotePushTransferProgressCallback;
remote_callbacks.payload = &connectionInfo;
gitError = git_remote_connect(remote.git_remote, GIT_DIRECTION_PUSH, &remote_callbacks, NULL, NULL);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to connect remote"];
return NO;
}
@onExit {
git_remote_disconnect(remote.git_remote);
};
git_push_options push_options = GIT_PUSH_OPTIONS_INIT;
gitError = git_push_init_options(&push_options, GIT_PUSH_OPTIONS_VERSION);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to init push options"];
return NO;
}
push_options.callbacks = remote_callbacks;
const git_strarray git_refspecs = refspecs.git_strarray;
gitError = git_remote_upload(remote.git_remote, &git_refspecs, &push_options);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Push upload to remote failed"];
return NO;
}
int update_fetchhead = 1;
// Ignored for push
git_remote_autotag_option_t download_tags = GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED;
NSString *reflog_message = [NSString stringWithFormat:@"pushing remote %@", remote.name];
gitError = git_remote_update_tips(remote.git_remote, &remote_callbacks, update_fetchhead, download_tags, reflog_message.UTF8String);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Update tips failed"];
return NO;
}
return YES;
}
@end