-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathGTIndex.m
448 lines (351 loc) · 13.4 KB
/
GTIndex.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//
// GTIndex.m
// ObjectiveGitFramework
//
// Created by Timothy Clem on 2/28/11.
//
// The MIT License
//
// Copyright (c) 2011 Tim Clem
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "GTIndex.h"
#import "EXTScope.h"
#import "GTConfiguration.h"
#import "GTIndexEntry.h"
#import "GTOID.h"
#import "GTRepository+Private.h"
#import "GTRepository.h"
#import "GTTree.h"
#import "GTBlob.h"
#import "NSArray+StringArray.h"
#import "NSError+Git.h"
#import "git2/errors.h"
// The block synonymous with libgit2's `git_index_matched_path_cb` callback.
typedef BOOL (^GTIndexPathspecMatchedBlock)(NSString *matchedPathspec, NSString *path, BOOL *stop);
@interface GTIndex ()
@property (nonatomic, assign, readonly) git_index *git_index;
@end
@implementation GTIndex
#pragma mark Properties
- (NSURL *)fileURL {
const char *cPath = git_index_path(self.git_index);
if (cPath == NULL) return nil;
NSString *path = [NSFileManager.defaultManager stringWithFileSystemRepresentation:cPath length:strlen(cPath)];
if (path == nil) return nil;
return [NSURL fileURLWithPath:path];
}
#pragma mark NSObject
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p> fileURL: %@, entryCount: %lu", NSStringFromClass([self class]), self, self.fileURL, (unsigned long)self.entryCount];
}
#pragma mark Lifecycle
- (void)dealloc {
if (_git_index != NULL) git_index_free(_git_index);
}
+ (instancetype)inMemoryIndexWithRepository:(GTRepository *)repository error:(NSError **)error {
git_index *index = NULL;
int status = git_index_new(&index);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to initialize in-memory index"];
return nil;
}
return [[self alloc] initWithGitIndex:index repository:repository];
}
- (instancetype)init {
NSAssert(NO, @"Call to an unavailable initializer.");
return nil;
}
+ (instancetype)indexWithFileURL:(NSURL *)fileURL repository:(GTRepository *)repository error:(NSError **)error {
NSParameterAssert(fileURL != nil);
NSParameterAssert(fileURL.isFileURL);
git_index *index = NULL;
int status = git_index_open(&index, fileURL.path.fileSystemRepresentation);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to initialize index with URL %@", fileURL];
return nil;
}
return [[self alloc] initWithGitIndex:index repository:repository];
}
- (id)initWithGitIndex:(git_index *)index repository:(GTRepository *)repository {
NSParameterAssert(index != NULL);
NSParameterAssert(repository != nil);
self = [super init];
if (self == nil) return nil;
_git_index = index;
_repository = repository;
return self;
}
#pragma mark Entries
- (GTOID *)checksum {
const git_oid *oid = git_index_checksum(self.git_index);
if (oid != NULL) {
return [GTOID oidWithGitOid:oid];
}
return nil;
}
- (NSUInteger)entryCount {
return git_index_entrycount(self.git_index);
}
- (BOOL)refresh:(NSError **)error {
int status = git_index_read(self.git_index, 1);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to refresh index."];
return NO;
}
return YES;
}
- (BOOL)clear:(NSError **)error {
int gitError = git_index_clear(self.git_index);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to clear index"];
return NO;
}
return YES;
}
- (GTIndexEntry *)entryAtIndex:(NSUInteger)index {
const git_index_entry *entry = git_index_get_byindex(self.git_index, (unsigned int)index);
if (entry == NULL) return nil;
return [[GTIndexEntry alloc] initWithGitIndexEntry:entry index:self error:NULL];
}
- (GTIndexEntry *)entryWithPath:(NSString *)path {
return [self entryWithPath:path error:NULL];
}
- (GTIndexEntry *)entryWithPath:(NSString *)path error:(NSError **)error {
size_t pos = 0;
int gitError = git_index_find(&pos, self.git_index, path.UTF8String);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"%@ not found in index", path];
return NULL;
}
return [self entryAtIndex:pos];
}
- (BOOL)addEntry:(GTIndexEntry *)entry error:(NSError **)error {
int status = git_index_add(self.git_index, entry.git_index_entry);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to add entry to index."];
return NO;
}
return YES;
}
- (BOOL)addFile:(NSString *)file error:(NSError **)error {
NSString *unicodeString = [self composedUnicodeStringWithString:file];
int status = git_index_add_bypath(self.git_index, unicodeString.UTF8String);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to add file %@ to index.", file];
return NO;
}
return YES;
}
- (BOOL)addData:(NSData *)data withPath:(NSString *)path error:(NSError **)error {
NSParameterAssert(data != nil);
NSParameterAssert(path != nil);
git_index_entry entry;
memset(&entry, 0x0, sizeof(git_index_entry));
entry.path = [path cStringUsingEncoding:NSUTF8StringEncoding];
entry.mode = GIT_FILEMODE_BLOB;
int status = git_index_add_frombuffer(self.git_index, &entry, [data bytes], [data length]);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to add data with name %@ into index.", path];
return NO;
}
return YES;
}
- (BOOL)addContentsOfTree:(GTTree *)tree error:(NSError **)error {
NSParameterAssert(tree != nil);
int status = git_index_read_tree(self.git_index, tree.git_tree);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to read tree %@ into index.", tree];
return NO;
}
return YES;
}
- (BOOL)removeFile:(NSString *)file error:(NSError **)error {
NSString *unicodeString = [self composedUnicodeStringWithString:file];
int status = git_index_remove_bypath(self.git_index, unicodeString.UTF8String);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to remove file %@ from index.", file];
return NO;
}
return YES;
}
- (BOOL)write:(NSError **)error {
int status = git_index_write(self.git_index);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to write index."];
return NO;
}
return YES;
}
- (GTTree *)writeTree:(NSError **)error {
git_oid oid;
int status = git_index_write_tree(&oid, self.git_index);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to write index."];
return NULL;
}
return [self.repository lookUpObjectByGitOid:&oid objectType:GTObjectTypeTree error:NULL];
}
- (GTTree *)writeTreeToRepository:(GTRepository *)repository error:(NSError **)error {
NSParameterAssert(repository != nil);
git_oid oid;
int status = git_index_write_tree_to(&oid, self.git_index, repository.git_repository);
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to write index to repository %@", repository];
return NULL;
}
return [repository lookUpObjectByGitOid:&oid objectType:GTObjectTypeTree error:NULL];
}
- (NSArray *)entries {
NSMutableArray *entries = [NSMutableArray arrayWithCapacity:self.entryCount];
for (NSUInteger i = 0; i < self.entryCount; i++) {
[entries addObject:[self entryAtIndex:i]];
}
return entries;
}
#pragma mark Conflicts
- (BOOL)hasConflicts {
return (BOOL)git_index_has_conflicts(self.git_index);
}
- (BOOL)enumerateConflictedFilesWithError:(NSError **)error usingBlock:(void (^)(GTIndexEntry *ancestor, GTIndexEntry *ours, GTIndexEntry *theirs, BOOL *stop))block {
NSParameterAssert(block != nil);
if (!self.hasConflicts) return YES;
git_index_conflict_iterator *iterator = NULL;
int returnCode = git_index_conflict_iterator_new(&iterator, self.git_index);
if (returnCode != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:returnCode description:NSLocalizedString(@"Could not create git index iterator.", nil)];
return NO;
}
@onExit {
if (iterator != NULL) git_index_conflict_iterator_free(iterator);
};
while (YES) {
const git_index_entry *ancestor = NULL;
const git_index_entry *ours = NULL;
const git_index_entry *theirs = NULL;
returnCode = git_index_conflict_next(&ancestor, &ours, &theirs, iterator);
if (returnCode == GIT_ITEROVER) break;
if (returnCode != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:returnCode description:NSLocalizedString(@"Could not iterate conflict.", nil)];
return NO;
}
GTIndexEntry *blockAncestor;
if (ancestor != NULL) {
blockAncestor = [[GTIndexEntry alloc] initWithGitIndexEntry:ancestor];
}
GTIndexEntry *blockOurs;
if (ours != NULL) {
blockOurs = [[GTIndexEntry alloc] initWithGitIndexEntry:ours];
}
GTIndexEntry *blockTheirs;
if (theirs != NULL) {
blockTheirs = [[GTIndexEntry alloc] initWithGitIndexEntry:theirs];
}
BOOL stop = NO;
block(blockAncestor, blockOurs, blockTheirs, &stop);
if (stop) break;
}
return YES;
}
struct GTIndexPathspecMatchedInfo {
__unsafe_unretained GTIndexPathspecMatchedBlock block;
BOOL shouldAbortImmediately;
};
- (BOOL)addPathspecs:(NSArray *)pathspecs flags:(GTIndexAddOptions)flags error:(NSError **)error passingTest:(GTIndexPathspecMatchedBlock)block {
__block git_strarray strarray = pathspecs.git_strarray;
@onExit {
if (strarray.count > 0) git_strarray_free(&strarray);
};
struct GTIndexPathspecMatchedInfo payload = {
.block = block,
.shouldAbortImmediately = NO,
};
int returnCode = git_index_add_all(self.git_index, &strarray, (unsigned int)flags, (block != nil ? GTIndexPathspecMatchFoundCallback : NULL), &payload);
if (returnCode != GIT_OK && returnCode != GIT_EUSER) {
if (error != nil) *error = [NSError git_errorFor:returnCode description:NSLocalizedString(@"Could not add to index.", nil)];
return NO;
}
return YES;
}
- (BOOL)removePathspecs:(NSArray *)pathspecs error:(NSError **)error passingTest:(GTIndexPathspecMatchedBlock)block {
__block git_strarray strarray = pathspecs.git_strarray;
@onExit {
if (strarray.count > 0) git_strarray_free(&strarray);
};
struct GTIndexPathspecMatchedInfo payload = {
.block = block,
.shouldAbortImmediately = NO,
};
int returnCode = git_index_remove_all(self.git_index, &strarray, (block != nil ? GTIndexPathspecMatchFoundCallback : NULL), &payload);
if (returnCode != GIT_OK && returnCode != GIT_EUSER) {
if (error != nil) *error = [NSError git_errorFor:returnCode description:NSLocalizedString(@"Could not remove from index.", nil)];
return NO;
}
return YES;
}
- (BOOL)updatePathspecs:(NSArray *)pathspecs error:(NSError **)error passingTest:(GTIndexPathspecMatchedBlock)block {
__block git_strarray strarray = pathspecs.git_strarray;
@onExit {
if (strarray.count > 0) git_strarray_free(&strarray);
};
struct GTIndexPathspecMatchedInfo payload = {
.block = block,
.shouldAbortImmediately = NO,
};
int returnCode = git_index_update_all(self.git_index, &strarray, (block != nil ? GTIndexPathspecMatchFoundCallback : NULL), &payload);
if (returnCode != GIT_OK && returnCode != GIT_EUSER) {
if (error != nil) *error = [NSError git_errorFor:returnCode description:NSLocalizedString(@"Could not update index.", nil)];
return NO;
}
return YES;
}
int GTIndexPathspecMatchFoundCallback(const char *path, const char *matched_pathspec, void *payload) {
struct GTIndexPathspecMatchedInfo *info = payload;
GTIndexPathspecMatchedBlock block = info->block;
if (info->shouldAbortImmediately) {
return GIT_EUSER;
}
BOOL shouldStop = NO;
NSString *matchedPathspec = (matched_pathspec != nil ? @(matched_pathspec): nil);
BOOL shouldUpdate = block(matchedPathspec, @(path), &shouldStop);
if (shouldUpdate) {
if (shouldStop) {
info->shouldAbortImmediately = YES;
}
return 0;
} else if (shouldStop) {
return GIT_EUSER;
} else {
return 1;
}
}
- (NSString *)composedUnicodeStringWithString:(NSString *)string {
GTConfiguration *repoConfig = [self.repository configurationWithError:NULL];
bool shouldPrecompose = [repoConfig boolForKey:@"core.precomposeunicode"];
return (shouldPrecompose ? [string precomposedStringWithCanonicalMapping] : [string decomposedStringWithCanonicalMapping]);
}
#pragma mark Deprecations
- (GTIndexEntry *)entryWithName:(NSString *)name {
return [self entryWithPath:name];
}
- (GTIndexEntry *)entryWithName:(NSString *)name error:(NSError **)error {
return [self entryWithPath:name error:error];
}
@end