Skip to content

Commit 7febb4f

Browse files
authored
Merge pull request #585 from Uncommon/updatepush
Add updatePushURLString:error: to GTRemote
2 parents c1473db + abface5 commit 7febb4f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

ObjectiveGit/GTRemote.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ typedef enum {
119119
/// if updating or saving the remote failed.
120120
- (BOOL)updateURLString:(NSString *)URLString error:(NSError **)error;
121121

122+
/// Updates the push URL string for this remote.
123+
///
124+
/// URLString - The URLString to update to. May not be nil.
125+
/// error - If not NULL, this will be set to any error that occurs when
126+
/// updating the URLString or saving the remote.
127+
///
128+
/// Returns YES if the push URLString was successfully updated, NO and an error
129+
/// if updating or saving the remote failed.
130+
- (BOOL)updatePushURLString:(NSString *)URLString error:(NSError **)error;
131+
122132
/// Adds a fetch refspec to this remote.
123133
///
124134
/// fetchRefspec - The fetch refspec string to add. May not be nil.

ObjectiveGit/GTRemote.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,21 @@ - (BOOL)updateURLString:(NSString *)URLString error:(NSError **)error {
204204
return YES;
205205
}
206206

207+
- (BOOL)updatePushURLString:(NSString *)URLString error:(NSError **)error {
208+
NSParameterAssert(URLString != nil);
209+
210+
if ([self.pushURLString isEqualToString:URLString]) return YES;
211+
212+
int gitError = git_remote_set_pushurl(self.repository.git_repository, self.name.UTF8String, URLString.UTF8String);
213+
if (gitError != GIT_OK) {
214+
if (error != NULL) {
215+
*error = [NSError git_errorFor:gitError description:@"Failed to update remote push URL string."];
216+
}
217+
return NO;
218+
}
219+
return YES;
220+
}
221+
207222
- (BOOL)addFetchRefspec:(NSString *)fetchRefspec error:(NSError **)error {
208223
NSParameterAssert(fetchRefspec != nil);
209224

ObjectiveGitTests/GTRemoteSpec.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@
6161
expect(remote.URLString).to(equal(newURLString));
6262
});
6363

64+
it(@"push URL string", ^{
65+
expect(remote.pushURLString).to(beNil());
66+
67+
NSString *newURLString = @"https://github.com/github/Test_App.git";
68+
69+
__block NSError *error = nil;
70+
expect(@([remote updatePushURLString:newURLString error:&error])).to(beTruthy());
71+
expect(error).to(beNil());
72+
73+
// Reload remote from disk to pick up the change
74+
remote = configuration.remotes[0];
75+
76+
expect(remote.pushURLString).to(equal(newURLString));
77+
});
78+
6479
it(@"fetch refspecs", ^{
6580
expect(remote.fetchRefspecs).to(equal(@[ fetchRefspec ]));
6681

0 commit comments

Comments
 (0)