-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathObjGitCommit.m
124 lines (109 loc) · 3.67 KB
/
ObjGitCommit.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
//
// ObjGitCommit.m
// ObjGit
//
#import "ObjGitObject.h"
#import "ObjGitCommit.h"
@implementation ObjGitCommit
@synthesize parentShas;
@synthesize treeSha;
@synthesize author;
@synthesize author_email;
@synthesize authored_date;
@synthesize committer;
@synthesize committer_email;
@synthesize committed_date;
@synthesize message;
@synthesize git_object;
@synthesize sha;
- (id) initFromGitObject:(ObjGitObject *)gitObject {
self = [super init];
self.sha = [gitObject sha];
self.git_object = gitObject;
[self parseContent];
return self;
}
- (id) initFromRaw:(NSData *)rawData withSha:(NSString *)shaValue
{
self = [super init];
self.git_object = [[ObjGitObject alloc] initFromRaw:rawData withSha:shaValue];
self.sha = shaValue;
[self parseContent];
//[self logObject];
return self;
}
- (void) logObject
{
NSLog(@"tree : %@", treeSha);
NSLog(@"author : %@, %@ : %@", author, author_email, authored_date);
NSLog(@"committer: %@, %@ : %@", committer, committer_email, committed_date);
NSLog(@"parents : %@", parentShas);
NSLog(@"message : %@", message);
}
- (NSArray *) authorArray
{
return [NSArray arrayWithObjects:self.author, self.author_email, self.authored_date, nil];
}
- (void) parseContent
{
// extract parent shas, tree sha, author/committer info, message
NSArray *lines = [self.git_object.contents componentsSeparatedByString:@"\n"];
NSEnumerator *enumerator;
NSMutableArray *parents;
NSMutableString *buildMessage;
NSString *line, *key, *val;
int inMessage = 0;
buildMessage = [NSMutableString new];
parents = [NSMutableArray new];
enumerator = [lines objectEnumerator];
while ((line = [enumerator nextObject]) != nil) {
// NSLog(@"line: %@", line);
if(!inMessage) {
if([line length] == 0) {
inMessage = 1;
} else {
NSArray *values = [line componentsSeparatedByString:@" "];
key = [values objectAtIndex: 0];
val = [values objectAtIndex: 1];
if([key isEqualToString: @"tree"]) {
self.treeSha = val;
} else if ([key isEqualToString: @"parent"]) {
[parents addObject: val];
} else if ([key isEqualToString: @"author"]) {
NSArray *name_email_date = [self parseAuthorString:line withType:@"author "];
self.author = [name_email_date objectAtIndex: 0];
self.author_email = [name_email_date objectAtIndex: 1];
self.authored_date = [name_email_date objectAtIndex: 2];
} else if ([key isEqualToString: @"committer"]) {
NSArray *name_email_date = [self parseAuthorString:line withType:@"committer "];
self.committer = [name_email_date objectAtIndex: 0];
self.committer_email = [name_email_date objectAtIndex: 1];
self.committed_date = [name_email_date objectAtIndex: 2];
}
}
} else {
[buildMessage appendString: line];
[buildMessage appendString: @"\n"];
}
}
self.message = buildMessage;
self.parentShas = parents;
}
- (NSArray *) parseAuthorString:(NSString *)authorString withType:(NSString *)typeString
{
NSArray *name_email_date;
name_email_date = [authorString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
NSString *nameVal = [name_email_date objectAtIndex: 0];
NSString *emailVal = [name_email_date objectAtIndex: 1];
NSString *dateVal = [name_email_date objectAtIndex: 2];
NSDate *dateDateVal;
dateDateVal = [NSDate dateWithTimeIntervalSince1970:[dateVal doubleValue]];
NSMutableString *tempValue = [[NSMutableString alloc] init];
[tempValue setString:nameVal];
[tempValue replaceOccurrencesOfString: typeString
withString: @""
options: 0
range: NSMakeRange(0, [tempValue length])];
return [NSArray arrayWithObjects:tempValue, emailVal, dateDateVal, nil];
}
@end