|
15 | 15 | #import "GTOID.h" |
16 | 16 | #import "GTRemote.h" |
17 | 17 | #import "GTSignature.h" |
| 18 | +#import "NSArray+StringArray.h" |
18 | 19 | #import "NSError+Git.h" |
19 | 20 |
|
20 | 21 | #import "git2/errors.h" |
@@ -159,4 +160,99 @@ - (NSArray *)fetchHeadEntriesWithError:(NSError **)error { |
159 | 160 | return entries; |
160 | 161 | } |
161 | 162 |
|
| 163 | +#pragma mark - Push (Public) |
| 164 | + |
| 165 | +- (BOOL)pushBranch:(GTBranch *)branch toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock { |
| 166 | + NSParameterAssert(branch != nil); |
| 167 | + NSParameterAssert(remote != nil); |
| 168 | + |
| 169 | + return [self pushBranches:@[ branch ] toRemote:remote withOptions:options error:error progress:progressBlock]; |
| 170 | +} |
| 171 | + |
| 172 | +- (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock { |
| 173 | + NSParameterAssert(branches != nil); |
| 174 | + NSParameterAssert(branches.count != 0); |
| 175 | + NSParameterAssert(remote != nil); |
| 176 | + |
| 177 | + NSMutableArray *refspecs = nil; |
| 178 | + // Build refspecs for the passed in branches |
| 179 | + refspecs = [NSMutableArray arrayWithCapacity:branches.count]; |
| 180 | + for (GTBranch *branch in branches) { |
| 181 | + // Default remote reference for when branch doesn't exist on remote - create with same short name |
| 182 | + NSString *remoteBranchReference = [NSString stringWithFormat:@"refs/heads/%@", branch.shortName]; |
| 183 | + |
| 184 | + BOOL success = NO; |
| 185 | + GTBranch *trackingBranch = [branch trackingBranchWithError:error success:&success]; |
| 186 | + |
| 187 | + if (success && trackingBranch) { |
| 188 | + // Use remote branch short name from trackingBranch, which could be different |
| 189 | + // (e.g. refs/heads/master:refs/heads/my_master) |
| 190 | + remoteBranchReference = [NSString stringWithFormat:@"refs/heads/%@", trackingBranch.shortName]; |
| 191 | + } |
| 192 | + |
| 193 | + [refspecs addObject:[NSString stringWithFormat:@"refs/heads/%@:%@", branch.shortName, remoteBranchReference]]; |
| 194 | + } |
| 195 | + |
| 196 | + return [self pushRefspecs:refspecs toRemote:remote withOptions:options error:error progress:progressBlock]; |
| 197 | +} |
| 198 | + |
| 199 | +#pragma mark - Push (Private) |
| 200 | + |
| 201 | +- (BOOL)pushRefspecs:(NSArray *)refspecs toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock { |
| 202 | + int gitError; |
| 203 | + GTCredentialProvider *credProvider = options[GTRepositoryRemoteOptionsCredentialProvider]; |
| 204 | + |
| 205 | + GTRemoteConnectionInfo connectionInfo = { |
| 206 | + .credProvider = { .credProvider = credProvider }, |
| 207 | + .direction = GIT_DIRECTION_PUSH, |
| 208 | + .pushProgressBlock = progressBlock, |
| 209 | + }; |
| 210 | + |
| 211 | + git_remote_callbacks remote_callbacks = GIT_REMOTE_CALLBACKS_INIT; |
| 212 | + remote_callbacks.credentials = (credProvider != nil ? GTCredentialAcquireCallback : NULL), |
| 213 | + remote_callbacks.transfer_progress = GTRemoteFetchTransferProgressCallback, |
| 214 | + remote_callbacks.payload = &connectionInfo, |
| 215 | + |
| 216 | + gitError = git_remote_set_callbacks(remote.git_remote, &remote_callbacks); |
| 217 | + if (gitError != GIT_OK) { |
| 218 | + if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to set callbacks on remote"]; |
| 219 | + return NO; |
| 220 | + } |
| 221 | + |
| 222 | + gitError = git_remote_connect(remote.git_remote, GIT_DIRECTION_PUSH); |
| 223 | + if (gitError != GIT_OK) { |
| 224 | + if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to connect remote"]; |
| 225 | + return NO; |
| 226 | + } |
| 227 | + @onExit { |
| 228 | + git_remote_disconnect(remote.git_remote); |
| 229 | + // Clear out callbacks by overwriting with an effectively empty git_remote_callbacks struct |
| 230 | + git_remote_set_callbacks(remote.git_remote, &((git_remote_callbacks)GIT_REMOTE_CALLBACKS_INIT)); |
| 231 | + }; |
| 232 | + |
| 233 | + git_push_options push_options = GIT_PUSH_OPTIONS_INIT; |
| 234 | + |
| 235 | + gitError = git_push_init_options(&push_options, GIT_PUSH_OPTIONS_VERSION); |
| 236 | + if (gitError != GIT_OK) { |
| 237 | + if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to init push options"]; |
| 238 | + return NO; |
| 239 | + } |
| 240 | + |
| 241 | + const git_strarray git_refspecs = refspecs.git_strarray; |
| 242 | + |
| 243 | + gitError = git_remote_upload(remote.git_remote, &git_refspecs, &push_options); |
| 244 | + if (gitError != GIT_OK) { |
| 245 | + if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Push upload to remote failed"]; |
| 246 | + return NO; |
| 247 | + } |
| 248 | + |
| 249 | + gitError = git_remote_update_tips(remote.git_remote, self.userSignatureForNow.git_signature, NULL); |
| 250 | + if (gitError != GIT_OK) { |
| 251 | + if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Update tips failed"]; |
| 252 | + return NO; |
| 253 | + } |
| 254 | + |
| 255 | + return YES; |
| 256 | +} |
| 257 | + |
162 | 258 | @end |
0 commit comments