Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not automatically fast-forward branches when merging. #416

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Classes/git/PBGitRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ static NSString * PBStringFromBranchFilterType(PBGitXBranchFilterType type) {
- (BOOL) checkoutRefish:(id <PBGitRefish>)ref;
- (BOOL) checkoutFiles:(NSArray *)files fromRefish:(id <PBGitRefish>)ref;
- (BOOL) mergeWithRefish:(id <PBGitRefish>)ref;
- (BOOL) doMergeWithRefish:(id <PBGitRefish>)ref;
- (BOOL) doFastForwardWithRefish:(id <PBGitRefish>)ref;
- (BOOL) cherryPickRefish:(id <PBGitRefish>)ref;
- (BOOL) rebaseBranch:(id <PBGitRefish>)branch onRefish:(id <PBGitRefish>)upstream;
- (BOOL) createBranch:(NSString *)branchName atRefish:(id <PBGitRefish>)ref;
Expand Down
73 changes: 68 additions & 5 deletions Classes/git/PBGitRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -794,21 +794,84 @@ - (BOOL) checkoutFiles:(NSArray *)files fromRefish:(id <PBGitRefish>)ref

- (BOOL) mergeWithRefish:(id <PBGitRefish>)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<PBGitRefish>)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<PBGitRefish>)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 <PBGitRefish>)ref
Expand Down