forked from laullon/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBGitTree.m
379 lines (307 loc) · 9.93 KB
/
PBGitTree.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
//
// PBGitTree.m
// GitTest
//
// Created by Pieter de Bie on 15-06-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGitTree.h"
#import "PBGitCommit.h"
#import "NSFileHandleExt.h"
#import "PBEasyPipe.h"
#import "PBEasyFS.h"
@implementation PBGitTree
@synthesize sha, path, repository, leaf, parent;
@synthesize filterPredicate;
@synthesize filteredChildren;
#pragma mark -
#pragma mark get/set
- (NSArray *) filteredChildren {
if (!filteredChildren) {
filteredChildren = [[NSMutableArray alloc] init];
[filteredChildren addObjectsFromArray:self.children];
}
return filteredChildren;
}
- (void) setFilterPredicate:(NSPredicate *) newPredicate {
if (newPredicate != filterPredicate) {
[filterPredicate release];
filterPredicate = [newPredicate retain];
if (leaf) {
return;
}
// initiate filtering
[filteredChildren removeAllObjects];
filteredChildren = [[NSMutableArray alloc] init];
if (filterPredicate == nil) {
[filteredChildren addObjectsFromArray:self.children];
}
for (id item in self.children) {
[item setFilterPredicate:filterPredicate];
if (filterPredicate) {
if ([item leaf]) {
if ([filterPredicate evaluateWithObject:item]) {
[filteredChildren addObject:item];
}
} else {
if ([[item filteredChildren] count] > 0) {
[filteredChildren addObject:item];
}
}
}
}
}
}
#pragma mark -
+ (PBGitTree*) rootForCommit:(id) commit
{
PBGitCommit* c = commit;
PBGitTree* tree = [[self alloc] init];
tree.parent = nil;
tree.leaf = NO;
tree.sha = [c realSha];
tree.repository = c.repository;
tree.path = @"";
return tree;
}
+ (PBGitTree*) treeForTree: (PBGitTree*) prev andPath: (NSString*) path;
{
PBGitTree* tree = [[self alloc] init];
tree.parent = prev;
tree.sha = prev.sha;
tree.repository = prev.repository;
tree.path = path;
return tree;
}
- init
{
filteredChildren = nil;
children = nil;
localFileName = nil;
leaf = YES;
return self;
}
- (NSString*) refSpec
{
return [NSString stringWithFormat:@"%@:%@", self.sha, self.fullPath];
}
- (BOOL) isLocallyCached
{
NSFileManager* fs = [NSFileManager defaultManager];
if (localFileName && [fs fileExistsAtPath:localFileName])
{
NSDate* mtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
if ([mtime compare:localMtime] == 0)
return YES;
}
return NO;
}
- (BOOL)hasBinaryAttributes
{
// First ask git check-attr if the file has a binary attribute custom set
NSFileHandle *handle = [repository handleInWorkDirForArguments:[NSArray arrayWithObjects:@"check-attr", @"binary", [self fullPath], nil]];
NSData *data = [handle readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
if (!string)
return NO;
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
if ([string hasSuffix:@"binary: set"])
return YES;
if ([string hasSuffix:@"binary: unset"])
return NO;
// Binary state unknown, do a check on common filename-extensions
for (NSString *extension in [NSArray arrayWithObjects:@".pdf", @".jpg", @".jpeg", @".png", @".bmp", @".gif", @".o", nil]) {
if ([[self fullPath] hasSuffix:extension])
return YES;
}
return NO;
}
- (NSString*) contents
{
if (!leaf)
return [NSString stringWithFormat:@"This is a tree with path %@", [self fullPath]];
if ([self isLocallyCached]) {
NSData *data = [NSData dataWithContentsOfFile:localFileName];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!string)
string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
return string;
}
return [repository outputForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
}
- (NSString *) blame:(NSError **)anError
{
NSString *error=nil;
NSString *res=nil;
if (!leaf)
error=[NSString stringWithFormat:@"This is a tree with path %@", [self fullPath]];
if ([self hasBinaryAttributes])
error=[NSString stringWithFormat:@"%@ appears to be a binary file of %d bytes", [self fullPath], [self fileSize]];
if ([self fileSize] > 52428800) // ~50MB
error=[NSString stringWithFormat:@"%@ is too big to be displayed (%d bytes)", [self fullPath], [self fileSize]];
if(error==nil){
res=[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"blame", @"-p", sha, @"--", [self fullPath], nil]];
}else{
if (anError != NULL) {
*anError = [NSError errorWithDomain:@"blame" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,NSLocalizedDescriptionKey,nil]];
}
}
return res;
}
- (NSString *) log:(NSString *)format error:(NSError **)anError
{
NSString *error=nil;
NSString *res=nil;
if (!leaf)
error=[NSString stringWithFormat:@"This is a tree with path %@", [self fullPath]];
if(error==nil){
res=[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"log", [NSString stringWithFormat:@"--pretty=format:%@",format], @"--", [self fullPath], nil]];
}else{
if (anError != NULL) {
*anError = [NSError errorWithDomain:@"log" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,NSLocalizedDescriptionKey,nil]];
}
}
return res;
}
- (NSString *) diff:(NSString *)format error:(NSError **)anError
{
NSString *error=nil;
NSString *res=nil;
if (!leaf)
error=[NSString stringWithFormat:@"This is a tree with path %@", [self fullPath]];
if ([self hasBinaryAttributes])
error=[NSString stringWithFormat:@"%@ appears to be a binary file of %d bytes", [self fullPath], [self fileSize]];
if ([self fileSize] > 52428800) // ~50MB
error=[NSString stringWithFormat:@"%@ is too big to be displayed (%d bytes)", [self fullPath], [self fileSize]];
if(error==nil){
NSString *des=@"";
if(format==@"p") {
des=[NSString stringWithFormat:@"%@^",sha];
}else if(format==@"h") {
des=@"HEAD";
}else{
des=@"--";
}
res=[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", sha, des,[self fullPath], nil]];
if ([res length]==0) {
DLog(@"--%@",[res length]);
if (anError != NULL) {
*anError = [NSError errorWithDomain:@"diff" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"No Diff",NSLocalizedDescriptionKey,nil]];
}
}else{
DLog(@"--%@",[res substringToIndex:80]);
}
}else{
if (anError != NULL) {
*anError = [NSError errorWithDomain:@"diff" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,NSLocalizedDescriptionKey,nil]];
}
}
return res;
}
- (NSString *)textContents:(NSError **)anError
{
NSString *error=nil;
NSString *res=nil;
if (!leaf)
error=[NSString stringWithFormat:@"This is a tree with path %@", [self fullPath]];
if ([self hasBinaryAttributes])
error=[NSString stringWithFormat:@"%@ appears to be a binary file of %d bytes", [self fullPath], [self fileSize]];
if ([self fileSize] > 52428800) // ~50MB
error=[NSString stringWithFormat:@"%@ is too big to be displayed (%d bytes)", [self fullPath], [self fileSize]];
if(error==nil){
res = [self contents];
}else{
if (anError != NULL) {
*anError = [NSError errorWithDomain:@"show" code:1 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:error,NSLocalizedDescriptionKey,nil]];
}
}
return res;
}
- (long long)fileSize
{
if (_fileSize)
return _fileSize;
NSFileHandle *handle = [repository handleForArguments:[NSArray arrayWithObjects:@"cat-file", @"-s", [self refSpec], nil]];
NSString *sizeString = [[NSString alloc] initWithData:[handle readDataToEndOfFile] encoding:NSISOLatin1StringEncoding];
if (!sizeString)
_fileSize = -1;
else
_fileSize = [sizeString longLongValue];
return _fileSize;
}
- (void) saveToFolder: (NSString *) dir
{
NSString* newName = [dir stringByAppendingPathComponent:path];
if (leaf) {
NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
NSData* data = [handle readDataToEndOfFile];
[data writeToFile:newName atomically:YES];
} else { // Directory
[[NSFileManager defaultManager] createDirectoryAtPath:newName withIntermediateDirectories:YES attributes:nil error:nil];
for (PBGitTree* child in [self children])
[child saveToFolder: newName];
}
}
- (NSString*) tmpDirWithContents
{
if (leaf)
return nil;
if (!localFileName)
localFileName = [PBEasyFS tmpDirWithPrefix: path];
for (PBGitTree* child in [self children]) {
[child saveToFolder: localFileName];
}
return localFileName;
}
- (NSString*) tmpFileNameForContents
{
if (!leaf)
return [self tmpDirWithContents];
if ([self isLocallyCached])
return localFileName;
if (!localFileName)
localFileName = [[PBEasyFS tmpDirWithPrefix: sha] stringByAppendingPathComponent:path];
NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
NSData* data = [handle readDataToEndOfFile];
[data writeToFile:localFileName atomically:YES];
NSFileManager* fs = [NSFileManager defaultManager];
localMtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
return localFileName;
}
- (NSArray*) children
{
if (children != nil)
return children;
NSString* ref = [self refSpec];
NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", ref, nil]];
[handle readLine];
[handle readLine];
NSMutableArray* c = [NSMutableArray array];
NSString* p = [handle readLine];
while (p.length > 0) {
BOOL isLeaf = ([p characterAtIndex:p.length - 1] != '/');
if (!isLeaf)
p = [p substringToIndex:p.length -1];
PBGitTree* child = [PBGitTree treeForTree:self andPath:p];
child.leaf = isLeaf;
[c addObject: child];
p = [handle readLine];
}
children = c;
return c;
}
- (NSString*) fullPath
{
if (!parent)
return @"";
if ([parent.fullPath isEqualToString:@""])
return self.path;
return [parent.fullPath stringByAppendingPathComponent: self.path];
}
- (void) finalize
{
if (localFileName)
[[NSFileManager defaultManager] removeItemAtPath:localFileName error:nil];
[super finalize];
}
@end