Skip to content

Commit

Permalink
Add a method to complete merges from the current repository state
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennou committed Mar 24, 2019
1 parent f09bddd commit 78783db
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ObjectiveGit/GTRepository+Merging.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ typedef NS_OPTIONS(NSInteger, GTMergePreference) {
/// will point to an error describing what happened).
- (BOOL)mergeBranchIntoCurrentBranch:(GTBranch *)fromBranch withError:(NSError **)error;

/// Complete a pending merge
///
/// This method can be used to complete a merge that has been stopped because
/// of conflicts.
///
/// error - The error if one occurred. Can be NULL
///
/// Returns YES if the merge could be committed, NO otherwise.
- (BOOL)finalizeMerge:(NSError **)error;

/// Gets the file content with conflict markers for the given file
///
/// The parameters taked are the ones received from `enumerateConflictedFiles`.
Expand Down
63 changes: 63 additions & 0 deletions ObjectiveGit/GTRepository+Merging.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,69 @@ - (BOOL)mergeAnnotatedCommits:(NSArray <GTAnnotatedCommit *> *)annotatedCommits
return YES;
}

- (BOOL)finalizeMerge:(NSError **)error {
GTRepositoryStateType state;
BOOL success = [self calculateState:&state withError:error];
if (!success) {
return NO;
}

if (state != GTRepositoryStateMerge) {
if (error) *error = [NSError git_errorFor:GIT_EINVALID description:@"Repository is not in a merge state"];
return NO;
}

GTIndex *index = [self indexWithError:error];
if (index == nil) {
return NO;
}

if (index.hasConflicts) {
if (error) *error = [NSError git_errorFor:GIT_ECONFLICT description:@"Index has unmerged changes"];
return NO;
}

GTTree *mergedTree = [index writeTree:error];
if (mergedTree == nil) {
return NO;
}

GTBranch *localBranch = [self currentBranchWithError:error];
if (localBranch == nil) {
return NO;
}

GTCommit *localCommit = [localBranch targetCommitWithError:error];
if (!localCommit) {
return NO;
}

// Build the commits' parents
NSMutableArray *parents = [NSMutableArray array];
[parents addObject:localCommit];

NSArray *mergeHeads = [self mergeHeadEntriesWithError:error];
if (mergeHeads.count == 0) {
return NO;
}
for (GTOID *oid in mergeHeads) {
NSError *lookupError = nil;
GTCommit *commit = [self lookUpObjectByOID:oid objectType:GTObjectTypeCommit error:&lookupError];
if (commit == nil) {
if (error) {
*error = [NSError git_errorFor:GIT_ERROR
description:@"Failed to lookup one of the merge heads"
userInfo:@{ NSUnderlyingErrorKey: lookupError }
failureReason:nil];
}
return NO;
}
[parents addObject:commit];
}

return [self finalizeMergeOfBranch:localBranch mergedTree:mergedTree parents:parents error:error];
}

- (BOOL)finalizeMergeOfBranch:(GTBranch *)localBranch mergedTree:(GTTree *)mergedTree parents:(NSArray <GTCommit *> *)parents error:(NSError **)error {

// Load the message to use
Expand Down

0 comments on commit 78783db

Please sign in to comment.