forked from libgit2/objective-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGTDiff.m
More file actions
281 lines (212 loc) · 11.1 KB
/
Copy pathGTDiff.m
File metadata and controls
281 lines (212 loc) · 11.1 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// GTDiff.m
// ObjectiveGitFramework
//
// Created by Danny Greg on 29/11/2012.
// Copyright (c) 2012 GitHub, Inc. All rights reserved.
//
#import "GTDiff.h"
#import "GTCommit.h"
#import "GTRepository.h"
#import "GTTree.h"
#import "GTIndex.h"
#import "NSArray+StringArray.h"
#import "NSError+Git.h"
#import "EXTScope.h"
#import "git2/errors.h"
NSString *const GTDiffOptionsFlagsKey = @"GTDiffOptionsFlagsKey";
NSString *const GTDiffOptionsContextLinesKey = @"GTDiffOptionsContextLinesKey";
NSString *const GTDiffOptionsInterHunkLinesKey = @"GTDiffOptionsInterHunkLinesKey";
NSString *const GTDiffOptionsOldPrefixKey = @"GTDiffOptionsOldPrefixKey";
NSString *const GTDiffOptionsNewPrefixKey = @"GTDiffOptionsNewPrefixKey";
NSString *const GTDiffOptionsMaxSizeKey = @"GTDiffOptionsMaxSizeKey";
NSString *const GTDiffOptionsPathSpecArrayKey = @"GTDiffOptionsPathSpecArrayKey";
NSString *const GTDiffFindOptionsFlagsKey = @"GTDiffFindOptionsFlagsKey";
NSString *const GTDiffFindOptionsRenameThresholdKey = @"GTDiffFindOptionsRenameThresholdKey";
NSString *const GTDiffFindOptionsRenameFromRewriteThresholdKey = @"GTDiffFindOptionsRenameFromRewriteThresholdKey";
NSString *const GTDiffFindOptionsCopyThresholdKey = @"GTDiffFindOptionsCopyThresholdKey";
NSString *const GTDiffFindOptionsBreakRewriteThresholdKey = @"GTDiffFindOptionsBreakRewriteThresholdKey";
NSString *const GTDiffFindOptionsRenameLimitKey = @"GTDiffFindOptionsRenameLimitKey";
@interface GTDiff ()
@property (nonatomic, assign, readonly) git_diff *git_diff;
@property (nonatomic, strong, readonly) GTRepository *repository;
@end
@implementation GTDiff
+ (int)handleParsedOptionsDictionary:(NSDictionary *)dictionary usingBlock:(int (^)(git_diff_options *optionsStruct))block {
NSParameterAssert(block != nil);
git_diff_options newOptions = GIT_DIFF_OPTIONS_INIT;
NSNumber *flagsNumber = dictionary[GTDiffOptionsFlagsKey];
if (flagsNumber != nil) newOptions.flags = (uint32_t)flagsNumber.unsignedIntegerValue;
NSNumber *contextLinesNumber = dictionary[GTDiffOptionsContextLinesKey];
if (contextLinesNumber != nil) newOptions.context_lines = (uint16_t)contextLinesNumber.unsignedIntegerValue;
NSNumber *interHunkLinesNumber = dictionary[GTDiffOptionsInterHunkLinesKey];
if (interHunkLinesNumber != nil) newOptions.interhunk_lines = (uint16_t)interHunkLinesNumber.unsignedIntegerValue;
NSString *oldPrefix = dictionary[GTDiffOptionsOldPrefixKey];
if (oldPrefix != nil) newOptions.old_prefix = oldPrefix.UTF8String;
NSString *newPrefix = dictionary[GTDiffOptionsNewPrefixKey];
if (newPrefix != nil) newOptions.new_prefix = newPrefix.UTF8String;
NSNumber *maxSizeNumber = dictionary[GTDiffOptionsMaxSizeKey];
if (maxSizeNumber != nil) newOptions.max_size = (uint16_t)maxSizeNumber.unsignedIntegerValue;
NSArray *pathSpec = dictionary[GTDiffOptionsPathSpecArrayKey];
git_strarray strArray = pathSpec.git_strarray;
if (pathSpec != nil) newOptions.pathspec = strArray;
@onExit {
git_strarray_dispose((git_strarray *)&strArray);
};
git_diff_options *optionsPtr = &newOptions;
if (dictionary.count < 1) optionsPtr = nil;
return block(optionsPtr);
}
+ (instancetype)diffOldTree:(GTTree *)oldTree withNewTree:(GTTree *)newTree inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(repository != nil);
__block git_diff *diff;
int status = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
return git_diff_tree_to_tree(&diff, repository.git_repository, oldTree.git_tree, newTree.git_tree, optionsStruct);
}];
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to create diff between %@ and %@", oldTree.SHA, newTree.SHA];
return nil;
}
return [[self alloc] initWithGitDiff:diff repository:repository];
}
+ (instancetype)diffOldTree:(GTTree *)oldTree withNewIndex:(GTIndex *)newIndex inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(repository != nil);
__block git_diff *diff;
int status = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
return git_diff_tree_to_index(&diff, repository.git_repository, oldTree.git_tree, newIndex.git_index, optionsStruct);
}];
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to create diff between %@ and %@", oldTree.SHA, newIndex];
return nil;
}
return [[self alloc] initWithGitDiff:diff repository:repository];
}
+ (instancetype)diffOldIndex:(GTIndex *)oldIndex withNewIndex:(GTIndex *)newIndex inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error
{
NSParameterAssert(repository != nil);
__block git_diff *diff;
int status = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
return git_diff_index_to_index(&diff, repository.git_repository, oldIndex.git_index, newIndex.git_index, optionsStruct);
}];
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to create diff between %@ and %@", oldIndex, newIndex];
return nil;
}
return [[self alloc] initWithGitDiff:diff repository:repository];
}
+ (instancetype)diffIndexFromTree:(GTTree *)tree inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(repository != nil);
NSParameterAssert(tree == nil || [tree.repository isEqual:repository]);
__block git_diff *diff;
int returnValue = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
return git_diff_tree_to_index(&diff, repository.git_repository, tree.git_tree, NULL, optionsStruct);
}];
if (returnValue != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:returnValue description:@"Failed to create diff between index and %@", tree.SHA];
return nil;
}
return [[self alloc] initWithGitDiff:diff repository:repository];
}
+ (instancetype)diffIndexToWorkingDirectoryInRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(repository != nil);
__block git_diff *diff;
int returnValue = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
return git_diff_index_to_workdir(&diff, repository.git_repository, NULL, optionsStruct);
}];
if (returnValue != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:returnValue description:@"Failed to create diff between working directory and index"];
return nil;
}
return [[self alloc] initWithGitDiff:diff repository:repository];
}
+ (instancetype)diffWorkingDirectoryFromTree:(GTTree *)tree inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(repository != nil);
NSParameterAssert(tree == nil || [tree.repository isEqual:repository]);
__block git_diff *diff;
int returnValue = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
return git_diff_tree_to_workdir(&diff, repository.git_repository, tree.git_tree, optionsStruct);
}];
if (returnValue != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:returnValue description:@"Failed to create diff between working directory and %@", tree.SHA];
return nil;
}
return [[self alloc] initWithGitDiff:diff repository:repository];
}
+ (instancetype)diffWorkingDirectoryToHEADInRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(repository != nil);
GTCommit *HEADCommit = [[repository headReferenceWithError:NULL] resolvedTarget];
GTDiff *HEADIndexDiff = [self diffIndexFromTree:HEADCommit.tree inRepository:repository options:options error:error];
if (HEADIndexDiff == nil) return nil;
GTDiff *WDDiff = [self diffIndexToWorkingDirectoryInRepository:repository options:options error:error];
if (WDDiff == nil) return nil;
git_diff_merge(HEADIndexDiff.git_diff, WDDiff.git_diff);
return HEADIndexDiff;
}
- (instancetype)init {
NSAssert(NO, @"Call to an unavailable initializer.");
return nil;
}
- (instancetype)initWithGitDiff:(git_diff *)diff repository:(GTRepository *)repository {
NSParameterAssert(diff != NULL);
NSParameterAssert(repository != nil);
self = [super init];
if (self == nil) return nil;
_git_diff = diff;
_repository = repository;
return self;
}
- (void)dealloc {
if (_git_diff != NULL) {
git_diff_free(_git_diff);
_git_diff = NULL;
}
}
- (NSString *)debugDescription {
return [NSString stringWithFormat:@"%@ deltaCount: %ld", super.debugDescription, (unsigned long)self.deltaCount];
}
- (void)enumerateDeltasUsingBlock:(void (^)(GTDiffDelta *delta, BOOL *stop))block {
NSParameterAssert(block != nil);
for (NSUInteger idx = 0; idx < self.deltaCount; idx ++) {
GTDiffDelta *delta = [[GTDiffDelta alloc] initWithDiff:self deltaIndex:idx];
if (delta == nil) continue;
BOOL stop = NO;
block(delta, &stop);
if (stop) break;
}
}
- (NSUInteger)deltaCount {
return git_diff_num_deltas(self.git_diff);
}
- (NSUInteger)numberOfDeltasWithType:(GTDeltaType)deltaType {
return git_diff_num_deltas_of_type(self.git_diff, (git_delta_t)deltaType);
}
- (BOOL)findOptionsStructWithDictionary:(NSDictionary *)dictionary optionsStruct:(git_diff_find_options *)newOptions {
if (dictionary == nil || dictionary.count < 1) return NO;
NSNumber *flagsNumber = dictionary[GTDiffFindOptionsFlagsKey];
if (flagsNumber != nil) newOptions->flags = (uint32_t)flagsNumber.unsignedIntegerValue;
NSNumber *renameThresholdNumber = dictionary[GTDiffFindOptionsRenameThresholdKey];
if (renameThresholdNumber != nil) newOptions->rename_threshold = renameThresholdNumber.unsignedShortValue;
NSNumber *renameFromRewriteThresholdNumber = dictionary[GTDiffFindOptionsRenameFromRewriteThresholdKey];
if (renameFromRewriteThresholdNumber != nil) newOptions->rename_from_rewrite_threshold = renameFromRewriteThresholdNumber.unsignedShortValue;
NSNumber *copyThresholdNumber = dictionary[GTDiffFindOptionsCopyThresholdKey];
if (copyThresholdNumber != nil) newOptions->copy_threshold = copyThresholdNumber.unsignedShortValue;
NSNumber *breakRewriteThresholdNumber = dictionary[GTDiffFindOptionsBreakRewriteThresholdKey];
if (renameThresholdNumber != nil) newOptions->break_rewrite_threshold = breakRewriteThresholdNumber.unsignedShortValue;
NSNumber *renameLimitNumber = dictionary[GTDiffFindOptionsRenameLimitKey];
if (renameLimitNumber != nil) newOptions->rename_limit = renameLimitNumber.unsignedShortValue;
return YES;
}
- (void)findSimilarWithOptions:(NSDictionary *)options {
git_diff_find_options findOptions = GIT_DIFF_FIND_OPTIONS_INIT;
BOOL findOptionsCreated = [self findOptionsStructWithDictionary:options optionsStruct:&findOptions];
git_diff_find_similar(self.git_diff, (findOptionsCreated ? &findOptions : NULL));
}
- (BOOL)mergeDiffWithDiff:(GTDiff *)diff error:(NSError **)error {
int gitError = git_diff_merge(self.git_diff, diff.git_diff);
if (gitError != GIT_OK) {
if (error) *error = [NSError git_errorFor:gitError description:@"Merging diffs failed"];
return NO;
}
return YES;
}
@end