From e50ddbc503716d9676cf33a867462fc9b3a10622 Mon Sep 17 00:00:00 2001 From: Nathan Kinsinger Date: Sun, 27 Dec 2009 13:29:50 -0700 Subject: [PATCH] Add Cherry Pick to contextual menu for commits. --- PBGitRepository.h | 1 + PBGitRepository.m | 21 +++++++++++++++++++++ PBRefController.h | 1 + PBRefController.m | 9 +++++++++ PBRefMenuItem.m | 4 ++++ 5 files changed, 36 insertions(+) diff --git a/PBGitRepository.h b/PBGitRepository.h index ffa9eb7..c4e25c4 100644 --- a/PBGitRepository.h +++ b/PBGitRepository.h @@ -32,6 +32,7 @@ extern NSString* PBGitRepositoryErrorDomain; - (BOOL) checkoutRefish:(id )ref; - (BOOL) checkoutFiles:(NSArray *)files fromRefish:(id )ref; - (BOOL) mergeWithRefish:(id )ref; +- (BOOL) cherryPickRefish:(id )ref; - (BOOL) createBranch:(NSString *)branchName atRefish:(id )ref; - (BOOL) createTag:(NSString *)tagName message:(NSString *)message atRefish:(id )commitSHA; - (BOOL) deleteRef:(PBGitRef *)ref; diff --git a/PBGitRepository.m b/PBGitRepository.m index 69c274c..675508b 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -494,6 +494,27 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; return YES; } +- (BOOL) cherryPickRefish:(id )ref +{ + if (!ref) + return NO; + + NSString *refName = [ref refishName]; + + int retValue = 1; + NSArray *arguments = [NSArray arrayWithObjects:@"cherry-pick", refName, nil]; + NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue]; + if (retValue) { + NSString *message = [NSString stringWithFormat:@"There was an error cherry picking the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]]; + [self.windowController showErrorSheetTitle:@"Cherry pick failed!" message:message arguments:arguments output:output]; + return NO; + } + + [self reloadRefs]; + [self readCurrentBranch]; + return YES; +} + - (BOOL) createBranch:(NSString *)branchName atRefish:(id )ref { if (!branchName || !ref) diff --git a/PBRefController.h b/PBRefController.h index 902d50d..0625759 100644 --- a/PBRefController.h +++ b/PBRefController.h @@ -25,6 +25,7 @@ - (void) checkout:(PBRefMenuItem *)sender; - (void) merge:(PBRefMenuItem *)sender; +- (void) cherryPick:(PBRefMenuItem *)sender; - (void) createBranch:(PBRefMenuItem *)sender; - (void) copySHA:(PBRefMenuItem *)sender; - (void) copyPatch:(PBRefMenuItem *)sender; diff --git a/PBRefController.m b/PBRefController.m index e888745..f98e320 100644 --- a/PBRefController.m +++ b/PBRefController.m @@ -57,6 +57,15 @@ } +#pragma mark Cherry Pick + +- (void) cherryPick:(PBRefMenuItem *)sender +{ + id refish = [sender refish]; + [historyController.repository cherryPickRefish:refish]; +} + + #pragma mark Create Branch - (void) createBranch:(PBRefMenuItem *)sender diff --git a/PBRefMenuItem.m b/PBRefMenuItem.m index ab87576..cecba01 100644 --- a/PBRefMenuItem.m +++ b/PBRefMenuItem.m @@ -102,6 +102,10 @@ NSString *mergeTitle = isOnHeadBranch ? @"Merge commit" : [NSString stringWithFormat:@"Merge commit into %@", headBranchName]; [items addObject:[PBRefMenuItem itemWithTitle:mergeTitle action:@selector(merge:) enabled:!isOnHeadBranch]]; + // cherry pick + NSString *cherryPickTitle = isOnHeadBranch ? @"Cherry pick commit" : [NSString stringWithFormat:@"Cherry pick commit to %@", headBranchName]; + [items addObject:[PBRefMenuItem itemWithTitle:cherryPickTitle action:@selector(cherryPick:) enabled:!isOnHeadBranch]]; + for (PBRefMenuItem *item in items) { [item setTarget:target]; [item setRefish:commit];