/
ObjGitTree.m
82 lines (67 loc) · 1.69 KB
/
ObjGitTree.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
//
// ObjGitTree.m
// ObjGit
//
#import "ObjGit.h"
#import "ObjGitObject.h"
#import "ObjGitTree.h"
@implementation ObjGitTree
@synthesize treeEntries;
@synthesize gitObject;
- (id) initFromGitObject:(ObjGitObject *)object {
self = [super init];
self.gitObject = object;
[self parseContent];
return self;
}
- (void) logObject
{
NSLog(@"entries : %@", treeEntries);
}
// 100644 testfile\0[20 char sha]
- (void) parseContent
{
char *contents = [self.gitObject rawContents];
char mode[9];
int modePtr = 0;
char name[255];
int namePtr = 0;
char sha[41];
unsigned char rawsha[20];
NSString *shaStr, *modeStr, *nameStr;
NSArray *entry;
NSMutableArray *entries;
entries = [[NSMutableArray alloc] init];
int i, j, state;
state = 1;
for(i = 0; i < [self.gitObject rawContentLen] - 1; i++) {
if(contents[i] == 0) {
state = 1;
for(j = 0; j < 20; j++)
rawsha[j] = contents[i + j + 1];
i += 20;
[ObjGit gitUnpackHex:rawsha fillSha:sha];
mode[modePtr] = 0;
name[namePtr] = 0;
shaStr = [[NSString alloc] initWithBytes:sha length:40 encoding:NSASCIIStringEncoding];
modeStr = [[NSString alloc] initWithBytes:mode length:modePtr encoding:NSASCIIStringEncoding];
nameStr = [[NSString alloc] initWithBytes:name length:namePtr encoding:NSUTF8StringEncoding];
entry = [NSArray arrayWithObjects:modeStr, nameStr, shaStr, nil];
[entries addObject:entry];
modePtr = 0;
namePtr = 0;
} else { // contents
if(contents[i] == 32) {
state = 2;
} else if(state == 1) { // mode
mode[modePtr] = contents[i];
modePtr++;
} else { // name
name[namePtr] = contents[i];
namePtr++;
}
}
}
treeEntries = entries;
}
@end