-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathGTMerge.m
150 lines (111 loc) · 3.88 KB
/
GTMerge.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// GTMergeFile.m
// ObjectiveGitFramework
//
// Created by Etienne on 26/10/2018.
// Copyright © 2018 GitHub, Inc. All rights reserved.
//
#import "GTMerge.h"
#import "GTOID.h"
#import "GTObjectDatabase.h"
#import "GTOdbObject.h"
#import "GTRepository.h"
#import "GTIndex.h"
#import "GTIndexEntry.h"
#import "NSError+Git.h"
@interface GTMergeResult ()
@property (assign) git_merge_file_result result;
@end
@implementation GTMergeResult
- (instancetype)initWithGitMergeFileResult:(git_merge_file_result *)result {
self = [super init];
if (!self) return nil;
memcpy(&_result, result, sizeof(_result));
return self;
}
- (void)dealloc {
git_merge_file_result_free(&_result);
}
- (BOOL)isAutomergeable {
return !!_result.automergeable;
}
- (NSString *)path {
return (_result.path ? [NSString stringWithUTF8String:_result.path] : nil);
}
- (unsigned int)mode {
return _result.mode;
}
- (NSData *)data {
return [[NSData alloc] initWithBytesNoCopy:(void *)_result.ptr length:_result.len freeWhenDone:NO];
}
@end
@interface GTMergeFile ()
@property (copy) NSData *data;
@property (copy) NSString *path;
@property (assign) unsigned int mode;
@property (assign) git_merge_file_input file;
@end
@implementation GTMergeFile
+ (instancetype)fileWithString:(NSString *)string path:(NSString * _Nullable)path mode:(unsigned int)mode {
NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
NSAssert(stringData != nil, @"String couldn't be converted to UTF-8");
return [[self alloc] initWithData:stringData path:path mode:mode];
}
+ (instancetype)fileWithIndexEntry:(GTIndexEntry *)entry error:(NSError **)error {
NSParameterAssert(entry);
const git_index_entry *git_entry = entry.git_index_entry;
GTOID *ancestorOID = [[GTOID alloc] initWithGitOid:&git_entry->id];
GTRepository *repository = entry.index.repository;
GTObjectDatabase *database = [repository objectDatabaseWithError:error];
NSData *contents = [[database objectWithOID:ancestorOID error:error] data];
if (contents == nil) {
return nil;
}
return [[self alloc] initWithData:contents path:entry.path mode:git_entry->mode];
}
- (instancetype)initWithData:(NSData *)data path:(NSString *)path mode:(unsigned int)mode {
NSParameterAssert(data);
self = [super init];
if (!self) return nil;
_data = data;
_path = path;
_mode = mode;
git_merge_file_init_input(&_file, GIT_MERGE_FILE_INPUT_VERSION);
_file.ptr = self.data.bytes;
_file.size = self.data.length;
_file.path = [self.path UTF8String];
_file.mode = self.mode;
return self;
}
- (git_merge_file_input *)git_merge_file_input {
return &_file;
}
+ (BOOL)handleMergeFileOptions:(git_merge_file_options *)opts optionsDict:(NSDictionary *)dict error:(NSError **)error {
NSParameterAssert(opts);
int gitError = git_merge_file_init_options(opts, GIT_MERGE_FILE_OPTIONS_VERSION);
if (gitError != 0) {
if (error) *error = [NSError git_errorFor:gitError description:@"Invalid option initialization"];
return NO;
}
if (dict.count != 0) {
if (error) *error = [NSError git_errorFor:-1 description:@"No options handled"];
return NO;
}
return YES;
}
+ (GTMergeResult *)performMergeWithAncestor:(GTMergeFile *)ancestorFile ourFile:(GTMergeFile *)ourFile theirFile:(GTMergeFile *)theirFile options:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(ourFile);
NSParameterAssert(theirFile);
NSParameterAssert(ancestorFile);
git_merge_file_result gitResult;
git_merge_file_options opts;
BOOL success = [GTMergeFile handleMergeFileOptions:&opts optionsDict:options error:error];
if (!success) return nil;
int gitError = git_merge_file(&gitResult, ancestorFile.git_merge_file_input, ourFile.git_merge_file_input, theirFile.git_merge_file_input, &opts);
if (gitError != 0) {
if (error) *error = [NSError git_errorFor:gitError description:@"Merge file failed"];
return nil;
}
return [[GTMergeResult alloc] initWithGitMergeFileResult:&gitResult];
}
@end