Skip to content

Commit 74f9dc5

Browse files
committed
Add enumerating merge head entries
1 parent 5978a65 commit 74f9dc5

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

ObjectiveGit/GTRepository+Merging.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// GTRepository+Merging.h
3+
// ObjectiveGitFramework
4+
//
5+
// Created by Piet Brauer on 02/03/16.
6+
// Copyright © 2016 GitHub, Inc. All rights reserved.
7+
//
8+
9+
#import "GTRepository.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface GTRepository (Merging)
14+
15+
/// Enumerate all available merge head entries.
16+
///
17+
/// error - The error if one ocurred. Can be NULL.
18+
/// block - A block to execute for each MERGE_HEAD entry. `mergeHeadEntry` will
19+
/// be the current merge head entry. Setting `stop` to YES will cause
20+
/// enumeration to stop after the block returns. Must not be nil.
21+
///
22+
/// Returns YES if the operation succedded, NO otherwise.
23+
- (BOOL)enumerateMergeHeadEntriesWithError:(NSError **)error usingBlock:(void (^)(GTCommit *mergeHeadEntry, BOOL *stop))block;
24+
25+
/// Convenience method for -enumerateMergeHeadEntriesWithError:usingBlock: that retuns an NSArray with all the fetch head entries.
26+
///
27+
/// error - The error if one ocurred. Can be NULL.
28+
///
29+
/// Retruns a (possibly empty) array with GTCommit objects. Will not be nil.
30+
- (NSArray <GTCommit *>*)mergeHeadEntriesWithError:(NSError **)error;
31+
32+
@end
33+
34+
NS_ASSUME_NONNULL_END

ObjectiveGit/GTRepository+Merging.m

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// GTRepository+Merging.m
3+
// ObjectiveGitFramework
4+
//
5+
// Created by Piet Brauer on 02/03/16.
6+
// Copyright © 2016 GitHub, Inc. All rights reserved.
7+
//
8+
9+
#import "GTRepository+Merging.h"
10+
#import "GTOID.h"
11+
#import "NSError+Git.h"
12+
#import "git2/errors.h"
13+
14+
typedef void (^GTRemoteFetchTransferProgressBlock)(const git_transfer_progress *stats, BOOL *stop);
15+
16+
@implementation GTRepository (Merging)
17+
18+
typedef void (^GTRepositoryEnumerateMergeHeadEntryBlock)(GTCommit *entry, BOOL *stop);
19+
20+
typedef struct {
21+
__unsafe_unretained GTRepository *repository;
22+
__unsafe_unretained GTRepositoryEnumerateMergeHeadEntryBlock enumerationBlock;
23+
} GTEnumerateMergeHeadEntriesPayload;
24+
25+
int GTMergeHeadEntriesCallback(const git_oid *oid, void *payload) {
26+
GTEnumerateMergeHeadEntriesPayload *entriesPayload = payload;
27+
28+
GTRepository *repository = entriesPayload->repository;
29+
GTRepositoryEnumerateMergeHeadEntryBlock enumerationBlock = entriesPayload->enumerationBlock;
30+
31+
GTCommit *commit = [repository lookUpObjectByOID:[GTOID oidWithGitOid:oid] objectType:GTObjectTypeCommit error:NULL];
32+
33+
BOOL stop = NO;
34+
35+
enumerationBlock(commit, &stop);
36+
37+
return (stop == YES ? GIT_EUSER : 0);
38+
}
39+
40+
- (BOOL)enumerateMergeHeadEntriesWithError:(NSError **)error usingBlock:(void (^)(GTCommit *mergeHeadEntry, BOOL *stop))block {
41+
NSParameterAssert(block != nil);
42+
43+
GTEnumerateMergeHeadEntriesPayload payload = {
44+
.repository = self,
45+
.enumerationBlock = block,
46+
};
47+
48+
int gitError = git_repository_mergehead_foreach(self.git_repository, GTMergeHeadEntriesCallback, &payload);
49+
50+
if (gitError != GIT_OK) {
51+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to get mergehead entries"];
52+
return NO;
53+
}
54+
55+
return YES;
56+
}
57+
58+
- (NSArray *)mergeHeadEntriesWithError:(NSError **)error {
59+
NSMutableArray *entries = [NSMutableArray array];
60+
61+
[self enumerateMergeHeadEntriesWithError:error usingBlock:^(GTCommit *mergeHeadEntry, BOOL *stop) {
62+
[entries addObject:mergeHeadEntry];
63+
64+
*stop = NO;
65+
}];
66+
67+
return entries;
68+
}
69+
70+
@end

ObjectiveGit/GTRepository+Pull.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ - (BOOL)pullBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:
112112
NSMutableArray <NSString *>*files = [NSMutableArray array];
113113
[index enumerateConflictedFilesWithError:error usingBlock:^(GTIndexEntry * _Nonnull ancestor, GTIndexEntry * _Nonnull ours, GTIndexEntry * _Nonnull theirs, BOOL * _Nonnull stop) {
114114
[files addObject:ours.path];
115+
116+
GTMergeFileResult *result = [index mergeIndexEntries:ancestor ours:ours theirs:theirs error:error];
117+
NSURL *oursFileURL = [self.fileURL URLByAppendingPathComponent:result.path];
118+
[result.contents writeToURL:oursFileURL atomically:YES encoding:NSUTF8StringEncoding error:error];
115119
}];
116120
if (error != NULL) {
117121
NSDictionary *userInfo = @{GTPullMergeConflictedFiles: files};

ObjectiveGit/ObjectiveGit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ FOUNDATION_EXPORT const unsigned char ObjectiveGitVersionString[];
4141
#import <ObjectiveGit/GTRepository+RemoteOperations.h>
4242
#import <ObjectiveGit/GTRepository+Reset.h>
4343
#import <ObjectiveGit/GTRepository+Pull.h>
44+
#import <ObjectiveGit/GTRepository+Merging.h>
4445
#import <ObjectiveGit/GTEnumerator.h>
4546
#import <ObjectiveGit/GTCommit.h>
4647
#import <ObjectiveGit/GTCredential.h>

ObjectiveGitFramework.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
23BB67BC1C7DF45300A37A66 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 23BB67BB1C7DF45300A37A66 /* libz.tbd */; };
6464
23BB67C11C7DF60300A37A66 /* GTRepository+PullSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = F8EFA0361B405020000FF7D0 /* GTRepository+PullSpec.m */; };
6565
23BB67C21C7DF60400A37A66 /* GTRepository+PullSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = F8EFA0361B405020000FF7D0 /* GTRepository+PullSpec.m */; };
66+
23F39FAD1C86DB1C00849F3C /* GTRepository+Merging.h in Headers */ = {isa = PBXBuildFile; fileRef = 23F39FAB1C86DB1C00849F3C /* GTRepository+Merging.h */; settings = {ATTRIBUTES = (Public, ); }; };
67+
23F39FAE1C86DB1C00849F3C /* GTRepository+Merging.m in Sources */ = {isa = PBXBuildFile; fileRef = 23F39FAC1C86DB1C00849F3C /* GTRepository+Merging.m */; };
68+
23F39FAF1C86DD0A00849F3C /* GTRepository+Merging.h in Headers */ = {isa = PBXBuildFile; fileRef = 23F39FAB1C86DB1C00849F3C /* GTRepository+Merging.h */; settings = {ATTRIBUTES = (Public, ); }; };
69+
23F39FB01C86E01800849F3C /* GTRepository+Merging.m in Sources */ = {isa = PBXBuildFile; fileRef = 23F39FAC1C86DB1C00849F3C /* GTRepository+Merging.m */; };
6670
3011D86B1668E48500CE3409 /* GTDiffFile.h in Headers */ = {isa = PBXBuildFile; fileRef = 3011D8691668E48500CE3409 /* GTDiffFile.h */; settings = {ATTRIBUTES = (Public, ); }; };
6771
3011D86D1668E48500CE3409 /* GTDiffFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 3011D86A1668E48500CE3409 /* GTDiffFile.m */; };
6872
3011D8711668E78500CE3409 /* GTDiffHunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3011D86F1668E78500CE3409 /* GTDiffHunk.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -444,6 +448,8 @@
444448
20F43DE118A2F667007D3621 /* GTRepository+Blame.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Blame.h"; sourceTree = "<group>"; };
445449
20F43DE218A2F667007D3621 /* GTRepository+Blame.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Blame.m"; sourceTree = "<group>"; };
446450
23BB67BB1C7DF45300A37A66 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
451+
23F39FAB1C86DB1C00849F3C /* GTRepository+Merging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Merging.h"; sourceTree = "<group>"; };
452+
23F39FAC1C86DB1C00849F3C /* GTRepository+Merging.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Merging.m"; sourceTree = "<group>"; };
447453
3011D8691668E48500CE3409 /* GTDiffFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTDiffFile.h; sourceTree = "<group>"; };
448454
3011D86A1668E48500CE3409 /* GTDiffFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTDiffFile.m; sourceTree = "<group>"; };
449455
3011D86F1668E78500CE3409 /* GTDiffHunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTDiffHunk.h; sourceTree = "<group>"; };
@@ -875,6 +881,8 @@
875881
4DFFB15A183AA8D600D1565E /* GTRepository+RemoteOperations.m */,
876882
88B2131A1B20E785005CF2C5 /* GTRepository+References.h */,
877883
88B2131B1B20E785005CF2C5 /* GTRepository+References.m */,
884+
23F39FAB1C86DB1C00849F3C /* GTRepository+Merging.h */,
885+
23F39FAC1C86DB1C00849F3C /* GTRepository+Merging.m */,
878886
BDD8AE6D13131B8800CB5D40 /* GTEnumerator.h */,
879887
BDD8AE6E13131B8800CB5D40 /* GTEnumerator.m */,
880888
BD6C22A71314625800992935 /* GTObject.h */,
@@ -1072,6 +1080,7 @@
10721080
79262F8B13C69B1600A4B1EA /* git2.h in Headers */,
10731081
88EB7E4D14AEBA600046FEA4 /* GTConfiguration.h in Headers */,
10741082
BDE4C064130EFE2C00851650 /* NSError+Git.h in Headers */,
1083+
23F39FAD1C86DB1C00849F3C /* GTRepository+Merging.h in Headers */,
10751084
55C8057D13875C11004DCB0F /* NSData+Git.h in Headers */,
10761085
D03B57A118BFFF07007124F4 /* GTDiffPatch.h in Headers */,
10771086
883CD6AB1600EBC600F57354 /* GTRemote.h in Headers */,
@@ -1108,6 +1117,7 @@
11081117
D01B6F2319F82F8700D411BC /* GTRepository+Reset.h in Headers */,
11091118
D01B6F2D19F82F8700D411BC /* GTEnumerator.h in Headers */,
11101119
D01B6F5519F82FA600D411BC /* GTReflog.h in Headers */,
1120+
23F39FAF1C86DD0A00849F3C /* GTRepository+Merging.h in Headers */,
11111121
D01B6F3519F82F8700D411BC /* GTBlob.h in Headers */,
11121122
D01B6F4D19F82F8700D411BC /* GTRemote.h in Headers */,
11131123
D01B6F1519F82F7B00D411BC /* NSData+Git.h in Headers */,
@@ -1427,6 +1437,7 @@
14271437
BDD8AE7013131B8800CB5D40 /* GTEnumerator.m in Sources */,
14281438
BD6C235313146E6600992935 /* GTCommit.m in Sources */,
14291439
30DCBA5E17B45213009B0EBD /* GTStatusDelta.m in Sources */,
1440+
23F39FAE1C86DB1C00849F3C /* GTRepository+Merging.m in Sources */,
14301441
30DCBA6517B45A78009B0EBD /* GTRepository+Status.m in Sources */,
14311442
BD6C235413146E6A00992935 /* GTObject.m in Sources */,
14321443
BD6C254613148DD300992935 /* GTSignature.m in Sources */,
@@ -1486,6 +1497,7 @@
14861497
D01B6F2E19F82F8700D411BC /* GTEnumerator.m in Sources */,
14871498
D01B6F4C19F82F8700D411BC /* GTConfiguration.m in Sources */,
14881499
D01B6F6619F82FA600D411BC /* GTRepository+Attributes.m in Sources */,
1500+
23F39FB01C86E01800849F3C /* GTRepository+Merging.m in Sources */,
14891501
D019778A19F8307600F523DA /* ObjectiveGit.m in Sources */,
14901502
D01B6F3219F82F8700D411BC /* GTCommit.m in Sources */,
14911503
D01B6F3819F82F8700D411BC /* GTTree.m in Sources */,

0 commit comments

Comments
 (0)