Skip to content

Commit 0df3e1f

Browse files
committed
Added -[GTCommit merge:error:] to quickly merge two commits together
(Essentially a wrapper around git_merge_commits())
1 parent e8c91f9 commit 0df3e1f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

ObjectiveGit/GTCommit.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
@class GTSignature;
3434
@class GTTree;
3535
@class GTOID;
36+
@class GTIndex;
3637

3738
NS_ASSUME_NONNULL_BEGIN
3839

@@ -54,6 +55,17 @@ NS_ASSUME_NONNULL_BEGIN
5455
/// The underlying `git_object` as a `git_commit` object.
5556
- (git_commit *)git_commit __attribute__((objc_returns_inner_pointer));
5657

58+
/// Merges the given commit into the receiver in memory and produces the result as
59+
/// an index.
60+
///
61+
/// otherTree - The commit with which the receiver should be merged with. Cannot be
62+
/// nil.
63+
/// error - The error if one occurred.
64+
///
65+
/// Returns an index which represents the result of the merge, or nil if an error
66+
/// occurred.
67+
- (nullable GTIndex *)merge:(GTCommit *)otherCommit error:(NSError **)error;
68+
5769
@end
5870

5971
NS_ASSUME_NONNULL_END

ObjectiveGit/GTCommit.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
#import "NSString+Git.h"
3636
#import "NSDate+GTTimeAdditions.h"
3737
#import "GTOID.h"
38+
#import "GTIndex.h"
3839

3940
#import "git2/commit.h"
4041
#import "git2/errors.h"
42+
#import "git2/merge.h"
4143

4244
@implementation GTCommit
4345

@@ -130,4 +132,20 @@ - (NSArray *)parents {
130132
return parents;
131133
}
132134

135+
#pragma mark Merging
136+
137+
- (GTIndex *)merge:(GTCommit *)otherCommit error:(NSError **)error {
138+
NSParameterAssert(otherCommit != nil);
139+
140+
git_index *index;
141+
142+
int result = git_merge_commits(&index, self.repository.git_repository, self.git_commit, otherCommit.git_commit, NULL);
143+
if (result != GIT_OK || index == NULL) {
144+
if (error != NULL) *error = [NSError git_errorFor:result description:@"Failed to merge commit %@ with commit %@", self.SHA, otherCommit.SHA];
145+
return nil;
146+
}
147+
148+
return [[GTIndex alloc] initWithGitIndex:index repository:self.repository];
149+
}
150+
133151
@end

0 commit comments

Comments
 (0)