Skip to content

Commit bee661a

Browse files
committed
Load tree contents using Objective-Git
1 parent f3758ac commit bee661a

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

Classes/git/PBGitTree.m

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ - (NSString*) contents
117117
return string;
118118
}
119119

120-
return [repository outputOfTaskWithArguments:@[@"show", self.refSpec] error:NULL];
120+
NSError *error;
121+
NSString *output = [repository outputOfTaskWithArguments:@[@"show", self.refSpec] error:&error];
122+
if (!output) {
123+
return error.userInfo[PBTaskTerminationOutputKey];
124+
}
125+
126+
return output;
121127
}
122128

123129
- (NSString *) blame
@@ -131,7 +137,11 @@ - (NSString *) blame
131137
if ([self fileSize] > 52428800) // ~50MB
132138
return [NSString stringWithFormat:@"%@ is too big to be displayed (%lld bytes)", [self fullPath], [self fileSize]];
133139

134-
NSString *contents = [repository outputOfTaskWithArguments:@[@"blame", @"-p", sha, @"--", self.fullPath] error:NULL];
140+
NSError *error = nil;
141+
NSString *contents = [repository outputOfTaskWithArguments:@[@"blame", @"-p", sha, @"--", self.fullPath] error:&error];
142+
if (!contents) {
143+
return error.userInfo[PBTaskTerminationOutputKey];
144+
}
135145

136146
if ([self hasBinaryHeader:contents])
137147
return [NSString stringWithFormat:@"%@ appears to be a binary file of %lld bytes", [self fullPath], [self fileSize]];
@@ -263,31 +273,56 @@ - (NSString*) tmpFileNameForContents
263273
return localFileName;
264274
}
265275

266-
- (NSArray*) children
276+
- (NSArray *) children
267277
{
268278
if (children != nil)
269279
return children;
270-
271-
NSString* ref = [self refSpec];
272280

273-
NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", ref, nil]];
274-
[handle readLine];
275-
[handle readLine];
276-
277-
NSMutableArray* c = [NSMutableArray array];
278-
279-
NSString* p = [handle readLine];
280-
while (p.length > 0) {
281-
BOOL isLeaf = ([p characterAtIndex:p.length - 1] != '/');
282-
if (!isLeaf)
283-
p = [p substringToIndex:p.length -1];
284-
285-
PBGitTree* child = [PBGitTree treeForTree:self andPath:p];
286-
child.leaf = isLeaf;
281+
GTOID *oid = [GTOID oidWithSHA:[[self refSpec] substringToIndex:GIT_OID_HEXSZ]];
282+
NSString *path = [[self refSpec] substringFromIndex:GIT_OID_HEXSZ + 1];
283+
284+
NSError *error;
285+
GTObject *object = [repository.gtRepo lookUpObjectByOID:oid error:&error];
286+
if (!object) {
287+
PBLogError(error);
288+
return nil;
289+
}
290+
291+
GTTree *tree = [object objectByPeelingToType:GTObjectTypeTree error:&error];
292+
if (!tree) {
293+
PBLogError(error);
294+
return nil;
295+
}
296+
297+
GTTree *actualTree = nil;
298+
if ([path isEqualToString:@""]) {
299+
actualTree = tree;
300+
} else {
301+
GTTreeEntry *entry = [tree entryWithPath:path error:&error];
302+
if (!entry) {
303+
PBLogError(error);
304+
return nil;
305+
}
306+
actualTree = [[entry GTObject:&error] objectByPeelingToType:GTObjectTypeTree error:&error];
307+
if (!actualTree) {
308+
// Some types of paths can't be peeled, like submodules
309+
return nil;
310+
}
311+
}
312+
313+
NSMutableArray *c = [NSMutableArray array];
314+
BOOL success = [actualTree enumerateEntriesWithOptions:GTTreeEnumerationOptionPre error:&error block:^BOOL(GTTreeEntry * _Nonnull entry, NSString * _Nonnull root, BOOL * _Nonnull stop) {
315+
316+
PBGitTree *child = [PBGitTree treeForTree:self andPath:entry.name];
317+
child.leaf = entry.type != GTObjectTypeTree;
287318
[c addObject: child];
288-
289-
p = [handle readLine];
319+
return NO;
320+
}];
321+
if (!success) {
322+
PBLogError(error);
323+
return nil;
290324
}
325+
291326
[c sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
292327
PBGitTree* tree1 = (PBGitTree*)obj1;
293328
PBGitTree* tree2 = (PBGitTree*)obj2;

0 commit comments

Comments
 (0)