From ee45adee712b282ce9a6bbc9a9601044839a10df Mon Sep 17 00:00:00 2001 From: Shady Zayat Date: Mon, 6 Oct 2014 12:25:11 -0400 Subject: [PATCH] Do not automatically fast-forward branches when merging. --- Classes/git/PBGitRepository.h | 2 + Classes/git/PBGitRepository.m | 73 ++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/Classes/git/PBGitRepository.h b/Classes/git/PBGitRepository.h index 5664984c9..780443449 100644 --- a/Classes/git/PBGitRepository.h +++ b/Classes/git/PBGitRepository.h @@ -77,6 +77,8 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) { - (BOOL) checkoutRefish:(id )ref; - (BOOL) checkoutFiles:(NSArray *)files fromRefish:(id )ref; - (BOOL) mergeWithRefish:(id )ref; +- (BOOL) doMergeWithRefish:(id )ref; +- (BOOL) doFastForwardWithRefish:(id )ref; - (BOOL) cherryPickRefish:(id )ref; - (BOOL) rebaseBranch:(id )branch onRefish:(id )upstream; - (BOOL) createBranch:(NSString *)branchName atRefish:(id )ref; diff --git a/Classes/git/PBGitRepository.m b/Classes/git/PBGitRepository.m index 2ad0a6418..77fb3673d 100644 --- a/Classes/git/PBGitRepository.m +++ b/Classes/git/PBGitRepository.m @@ -794,21 +794,84 @@ - (BOOL) checkoutFiles:(NSArray *)files fromRefish:(id )ref - (BOOL) mergeWithRefish:(id )ref { - NSString *refName = [ref refishName]; + NSString *headName = [[[self headRef] ref] shortName]; int retValue = 1; - NSArray *arguments = [NSArray arrayWithObjects:@"merge", refName, nil]; + + NSArray *arguments = [NSArray arrayWithObjects:@"rev-list", headName, [NSString stringWithFormat:@"^%@", [ref refishName]], nil]; NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue]; - if (retValue) { - NSString *headName = [[[self headRef] ref] shortName]; + if ( ! retValue ) + { + if ( [output length] == 0 ) + { + NSAlert * alert = [[NSAlert alloc] init]; + [alert setAlertStyle:NSInformationalAlertStyle]; + [alert addButtonWithTitle:@"Explicit Merge"]; + [alert addButtonWithTitle:@"Fast-Forward"]; + [alert addButtonWithTitle:@"Cancel"]; + [alert setMessageText:[NSString stringWithFormat:@"%@ can be fast-forwarded to %@\nDo you prefer to fast-forward or create an explicit merge commit?", headName, [ref shortName]]]; + [alert beginSheetModalForWindow:self.windowController.window completionHandler:^(NSModalResponse returnCode) + { + switch (returnCode) + { + case NSAlertFirstButtonReturn: + [self doMergeWithRefish:ref]; + break; + case NSAlertSecondButtonReturn: + [self doFastForwardWithRefish:ref]; + break; + default: + break; + } + }]; + } + else + { + [self doMergeWithRefish:ref]; + } + } + return YES; +} + +- (BOOL) doMergeWithRefish:(id)ref +{ + NSString *refName = [ref refishName]; + NSString *headName = [[[self headRef] ref] shortName]; + int retValue = 1; + + NSArray *arguments = [NSArray arrayWithObjects:@"merge", @"--no-ff", refName, nil]; + NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue]; + if (retValue) + { NSString *message = [NSString stringWithFormat:@"There was an error merging %@ into %@.", refName, headName]; [self.windowController showErrorSheetTitle:@"Merge failed!" message:message arguments:arguments output:output]; return NO; } + + [self reloadRefs]; + [self readCurrentBranch]; + return retValue == 0; +} +- (BOOL) doFastForwardWithRefish:(id)ref +{ + NSString *refName = [ref refishName]; + NSString *headName = [[[self headRef] ref] shortName]; + int retValue = 1; + + NSArray *arguments = [NSArray arrayWithObjects:@"merge", @"--ff-only", refName, nil]; + NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue]; + if (retValue) + { + NSString *message = [NSString stringWithFormat:@"There was an error merging %@ into %@.", refName, headName]; + [self.windowController showErrorSheetTitle:@"Merge failed!" message:message arguments:arguments output:output]; + return NO; + } + [self reloadRefs]; [self readCurrentBranch]; - return YES; + return retValue == 0; + } - (BOOL) cherryPickRefish:(id )ref