diff --git a/PBRefController.h b/PBRefController.h index 4dd5e7f..78f623b 100644 --- a/PBRefController.h +++ b/PBRefController.h @@ -40,9 +40,13 @@ - (void) selectCurrentBranch; - (void) updateBranchMenu; -- (void) pullImpl:(NSString *)refName; -- (void) pushImpl:(NSString *)refName; -- (void) rebaseImpl:(NSString *)refName; - +- (BOOL) pullImpl:(NSString *)refName; +- (BOOL) pushImpl:(NSString *)refName; +- (BOOL) rebaseImpl:(NSString *)refName; +- (BOOL) fetchImpl:(NSString *)refName; @end + +@interface NSString (PBRefSpecAdditions) +- (NSString *) refForSpec; +@end \ No newline at end of file diff --git a/PBRefController.m b/PBRefController.m index 149a5f2..db8b629 100644 --- a/PBRefController.m +++ b/PBRefController.m @@ -101,61 +101,73 @@ return [PBRefMenuItem defaultMenuItemsForRef:ref commit:commit target:self]; } -- (void) pushImpl:(NSString *)refName +- (BOOL) pushImpl:(NSString *)refName { int ret = 1; + BOOL success = NO; NSString *remote = [[historyController.repository config] valueForKeyPath:[NSString stringWithFormat:@"branch.%@.remote", refName]]; NSString *rval = [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"push", remote, refName, nil] retValue: &ret]; if (ret) { NSString *info = [NSString stringWithFormat:@"There was an error pushing the branch to the remote repository.\n\n%d\n%@", ret, rval]; [[historyController.repository windowController] showMessageSheet:@"Pushing branch failed" infoText:info]; - return; + return success; } [historyController.repository reloadRefs]; [commitController rearrangeObjects]; + success = YES; + return success; } -- (void) pullImpl:(NSString *)refName +- (BOOL) pullImpl:(NSString *)refName { int ret = 1; + BOOL success = NO; NSString *remote = [[historyController.repository config] valueForKeyPath:[NSString stringWithFormat:@"branch.%@.remote", refName]]; NSString *rval = [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"pull", remote, refName, nil] retValue: &ret]; if (ret) { NSString *info = [NSString stringWithFormat:@"There was an error pulling from the remote repository.\n\n%d\n%@", ret, rval]; [[historyController.repository windowController] showMessageSheet:@"Pulling from remote failed" infoText:info]; - return; + return success; } [historyController.repository reloadRefs]; [commitController rearrangeObjects]; + success = YES; + return success; } -- (void) rebaseImpl:(NSString *)refName +- (BOOL) rebaseImpl:(NSString *)refName { int ret = 1; - NSString *remote = [[historyController.repository config] valueForKeyPath:[NSString stringWithFormat:@"branch.%@.remote", refName]]; - NSString *rval = [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"pull", @"--rebase", remote, refName, nil] retValue: &ret]; + BOOL success = NO; + NSString *remote = [[[historyController repository] config] valueForKeyPath:[NSString stringWithFormat:@"branch.%@.remote", refName]]; + NSString *rval = [[historyController repository] outputInWorkdirForArguments:[NSArray arrayWithObjects:@"pull", @"--rebase", remote, refName, nil] retValue: &ret]; if (ret) { NSString *info = [NSString stringWithFormat:@"There was an error rebasing from the remote repository.\n\n%d\n%@", ret, rval]; - [[historyController.repository windowController] showMessageSheet:@"Rebasing from remote failed" infoText:info]; - return; + [[[historyController repository] windowController] showMessageSheet:@"Rebasing from remote failed" infoText:info]; + return success; } - [historyController.repository reloadRefs]; + [[historyController repository] reloadRefs]; [commitController rearrangeObjects]; + success = YES; + return success; } -- (void) fetchImpl:(NSString *)refName +- (BOOL) fetchImpl:(NSString *)refName { int ret = 1; + BOOL success = NO; NSString *remote = nil; if(refName != nil) remote = [[historyController.repository config] valueForKeyPath:[NSString stringWithFormat:@"branch.%@.remote", refName]]; NSString *rval = [historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"fetch", remote, refName, nil] retValue: &ret]; if (ret) { NSString *info = [NSString stringWithFormat:@"There was an error fetching from the remote repository.\n\n%d\n%@", ret, rval]; - [[historyController.repository windowController] showMessageSheet:@"Rebasing from remote failed" infoText:info]; - return; + [[historyController.repository windowController] showMessageSheet:@"Fetching from remote failed" infoText:info]; + return success; } [historyController.repository reloadRefs]; [commitController rearrangeObjects]; + success = YES; + return success; } # pragma mark Tableview delegate methods @@ -253,8 +265,9 @@ -(void)rebaseButton:(id)sender { [sender setEnabled:NO]; - NSString *refName =[historyController.repository.currentBranch simpleRef]; - [self rebaseImpl:refName]; + NSString *refName = [[historyController.repository.currentBranch simpleRef] refForSpec]; + if (refName) + [self rebaseImpl:refName]; [sender setEnabled:YES]; // NSLog([NSString stringWithFormat:@"Rebase hit for %@!", refName]); } @@ -262,8 +275,9 @@ -(void)pushButton:(id)sender { [sender setEnabled:NO]; - NSString *refName =[historyController.repository.currentBranch simpleRef]; - [self pushImpl:refName]; + NSString *refName = [[historyController.repository.currentBranch simpleRef] refForSpec]; + if (refName) + [self pushImpl:refName]; [sender setEnabled:YES]; // NSLog([NSString stringWithFormat:@"Push hit for %@!", refName]); } @@ -271,8 +285,9 @@ -(void)pullButton:(id)sender { [sender setEnabled:NO]; - NSString *refName =[historyController.repository.currentBranch simpleRef]; - [self pullImpl:refName]; + NSString *refName = [[historyController.repository.currentBranch simpleRef] refForSpec]; + if (refName) + [self pullImpl:refName]; [sender setEnabled:YES]; // NSLog([NSString stringWithFormat:@"Pull hit for %@!", refName]); } @@ -280,8 +295,9 @@ -(void)fetchButton:(id)sender { [sender setEnabled:NO]; - NSString *refName =[historyController.repository.currentBranch simpleRef]; - [self fetchImpl:refName]; + NSString *refName = [[historyController.repository.currentBranch simpleRef] refForSpec]; + if (refName) + [self fetchImpl:refName]; [sender setEnabled:YES]; // NSLog([NSString stringWithFormat:@"Fetch hit for %@!", refName]); } @@ -435,3 +451,17 @@ } @end + +@implementation NSString (PBRefSpecAdditions) + +/* convenience method to get the last part of a simple refspec like refs/heads/master -> master*/ +- (NSString *) refForSpec { + if ([self hasPrefix:@"refs/"]) { + NSArray * parts = [self componentsSeparatedByString:@"/"]; + return [parts lastObject]; + } + return self; +} + +@end + diff --git a/PBRefMenuItem.h b/PBRefMenuItem.h index a2eb80f..5d00f5d 100644 --- a/PBRefMenuItem.h +++ b/PBRefMenuItem.h @@ -18,7 +18,7 @@ @property (retain) PBGitCommit *commit; @property (retain) PBGitRef *ref; -+ (PBRefMenuItem *)addRemoteMethod:(bool)isRemote title:(NSString *)title action:(SEL)selector; ++ (PBRefMenuItem *)addRemoteMethod:(BOOL)isRemote title:(NSString *)title action:(SEL)selector; + (NSArray *)defaultMenuItemsForRef:(PBGitRef *)ref commit:(PBGitCommit *)commit target:(id)target; @end diff --git a/PBRefMenuItem.m b/PBRefMenuItem.m index 65abdb5..b8f1e58 100644 --- a/PBRefMenuItem.m +++ b/PBRefMenuItem.m @@ -12,10 +12,10 @@ @implementation PBRefMenuItem @synthesize ref, commit; -+ (PBRefMenuItem *)addRemoteMethod:(bool)isRemote title:(NSString *)title action:(SEL)selector ++ (PBRefMenuItem *)addRemoteMethod:(BOOL)hasRemote title:(NSString *)title action:(SEL)selector { PBRefMenuItem *item = [[PBRefMenuItem alloc] initWithTitle:title action:selector keyEquivalent:@""]; - [item setEnabled:isRemote]; + [item setEnabled:hasRemote]; return item; } @@ -29,12 +29,12 @@ type = @"branch"; NSString *remote = [[[commit repository] config] valueForKeyPath:[NSString stringWithFormat:@"branch.%@.remote", [ref shortName]]]; - bool has_remote = (remote != NULL) ? YES : NO; + BOOL hasRemote = (remote ? YES : NO); if ([type isEqualToString:@"branch"]) { - [array addObject:[self addRemoteMethod:has_remote title:@"Push branch to remote" action:@selector(pushRef:)]]; - [array addObject:[self addRemoteMethod:has_remote title:@"Pull down latest" action:@selector(pullRef:)]]; - [array addObject:[self addRemoteMethod:has_remote title:@"Rebase local changes with latest" action:@selector(rebaseRef:)]]; + [array addObject:[self addRemoteMethod:hasRemote title:@"Push branch to remote" action:@selector(pushRef:)]]; + [array addObject:[self addRemoteMethod:hasRemote title:@"Pull down latest" action:@selector(pullRef:)]]; + [array addObject:[self addRemoteMethod:hasRemote title:@"Rebase local changes with latest" action:@selector(rebaseRef:)]]; } if ([type isEqualToString:@"branch"])