forked from laullon/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBGitRevList.mm
254 lines (194 loc) · 7.11 KB
/
PBGitRevList.mm
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
//
// PBGitRevList.m
// GitX
//
// Created by Pieter de Bie on 17-06-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGitRevList.h"
#import "PBGitRepository.h"
#import "PBGitCommit.h"
#import "PBGitGrapher.h"
#import "PBGitRevSpecifier.h"
#include <ext/stdio_filebuf.h>
#include <iostream>
#include <string>
#include <map>
using namespace std;
@interface PBGitRevList ()
@property (assign) BOOL isParsing;
@end
#define kRevListThreadKey @"thread"
#define kRevListRevisionsKey @"revisions"
@implementation PBGitRevList
@synthesize commits;
@synthesize isParsing;
- (id) initWithRepository:(PBGitRepository *)repo rev:(PBGitRevSpecifier *)rev shouldGraph:(BOOL)graph
{
repository = repo;
isGraphing = graph;
currentRev = [rev copy];
return self;
}
- (void) loadRevisons:(BOOL)all
{
[parseThread cancel];
reloadAll = all;
parseThread = [[NSThread alloc] initWithTarget:self selector:@selector(walkRevisionListWithSpecifier:) object:currentRev];
self.isParsing = YES;
resetCommits = YES;
[parseThread start];
}
- (void)cancel
{
[parseThread cancel];
}
- (void) finishedParsing
{
self.isParsing = NO;
}
- (void) updateCommits:(NSDictionary *)update
{
if ([update objectForKey:kRevListThreadKey] != parseThread)
return;
NSArray *revisions = [update objectForKey:kRevListRevisionsKey];
if (!revisions || [revisions count] == 0)
return;
if (resetCommits) {
self.commits = [NSMutableArray array];
resetCommits = NO;
}
NSRange range = NSMakeRange([commits count], [revisions count]);
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:range];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"commits"];
[commits addObjectsFromArray:revisions];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"commits"];
}
- (void) walkRevisionListWithSpecifier:(PBGitRevSpecifier*)rev
{
NSDate *start = [NSDate date];
NSDate *lastUpdate = [NSDate date];
NSMutableArray *revisions = [NSMutableArray array];
PBGitGrapher *g = [[PBGitGrapher alloc] initWithRepository:repository];
std::map<string, NSStringEncoding> encodingMap;
NSThread *currentThread = [NSThread currentThread];
NSString *formatString = @"--pretty=format:%H\01%e\01%aN\01%cN\01%P\01%at\01%s";
BOOL showSign = [rev hasLeftRight];
if (showSign)
formatString = [@"%m\01" stringByAppendingString:formatString];
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"log", @"-z", @"--topo-order", @"--children", formatString, nil];
if(!reloadAll){
[arguments insertObject:@"-n" atIndex:1];
[arguments insertObject:@"500" atIndex:2];
}
if (!rev)
[arguments addObject:@"HEAD"];
else
[arguments addObjectsFromArray:[rev parameters]];
NSString *directory = rev.workingDirectory ? rev.workingDirectory.path : repository.fileURL.path;
NSTask *task = [PBEasyPipe taskForCommand:[PBGitBinary path] withArgs:arguments inDir:directory];
[task launch];
NSFileHandle *handle = [task.standardOutput fileHandleForReading];
int fd = [handle fileDescriptor];
__gnu_cxx::stdio_filebuf<char> buf(fd, std::ios::in);
std::istream stream(&buf);
int num = 0;
while (true) {
if ([currentThread isCancelled])
break;
char c;
char sign;
if (showSign)
{
stream >> sign;
stream >> c; // Remove separator
if (sign != '>' && sign != '<' && sign != '^' && sign != '-')
DLog(@"Error loading commits: sign not correct");
}
string sha;
if (!getline(stream, sha, '\1'))
break;
// From now on, 1.2 seconds
string encoding_str;
getline(stream, encoding_str, '\1');
NSStringEncoding encoding = NSUTF8StringEncoding;
if (encoding_str.length())
{
if (encodingMap.find(encoding_str) != encodingMap.end()) {
encoding = encodingMap[encoding_str];
} else {
encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)[NSString stringWithUTF8String:encoding_str.c_str()]));
encodingMap[encoding_str] = encoding;
}
}
NSString *oid=[NSString stringWithCString:sha.c_str() encoding:encoding];
if([oid length]!=40)
break;
PBGitCommit *newCommit = [PBGitCommit commitWithRepository:repository andSha:oid];
if (showSign)
[newCommit setSign: sign];
string author;
getline(stream, author, '\1');
string committer;
getline(stream, committer, '\1');
string parentString;
getline(stream, parentString, '\1');
if (parentString.size() != 0)
{
if (((parentString.size() + 1) % 41) != 0) {
DLog(@"invalid parents: %zu", parentString.size());
continue;
}
int nParents = (parentString.size() + 1) / 41;
NSMutableArray *parents = [NSMutableArray arrayWithCapacity:nParents];
int parentIndex;
for (parentIndex = 0; parentIndex < nParents; ++parentIndex) {
NSString *pOid=[NSString stringWithCString:parentString.substr(parentIndex * 41, 40).c_str() encoding:encoding];
[parents addObject:pOid];
}
[newCommit setParents:parents];
}
int time;
stream >> time;
string subject;
getline(stream, subject, '\0');
[newCommit setSubject:[NSString stringWithCString:subject.c_str() encoding:encoding]];
[newCommit setAuthor:[NSString stringWithCString:author.c_str() encoding:encoding]];
[newCommit setCommitter:[NSString stringWithCString:committer.c_str() encoding:encoding]];
[newCommit setTimestamp:time];
[revisions addObject: newCommit];
if (isGraphing)
[g decorateCommit:newCommit];
if (++num % 100 == 0) {
if ([[NSDate date] timeIntervalSinceDate:lastUpdate] > 0.1) {
NSDictionary *update = [NSDictionary dictionaryWithObjectsAndKeys:currentThread, kRevListThreadKey, revisions, kRevListRevisionsKey, nil];
[self performSelectorOnMainThread:@selector(updateCommits:) withObject:update waitUntilDone:NO];
revisions = [NSMutableArray array];
lastUpdate = [NSDate date];
}
}
}
if (![currentThread isCancelled]) {
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:start];
DLog(@"Loaded %i commits in %f seconds (%f/sec)", num, duration, num/duration);
// Make sure the commits are stored before exiting.
NSDictionary *update = [NSDictionary dictionaryWithObjectsAndKeys:currentThread, kRevListThreadKey, revisions, kRevListRevisionsKey, nil];
[self performSelectorOnMainThread:@selector(updateCommits:) withObject:update waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(finishedParsing) withObject:nil waitUntilDone:NO];
}
else {
DLog(@"[%@ %@] thread has been canceled", [self class], NSStringFromSelector(_cmd));
}
[task terminate];
[task waitUntilExit];
//Nasty hack to work around ARC and bindings getting angry at each other
//Without this some of the view controllers can get released while not on the main thread, which crashes in bindings
__block PBGitRepository *tempRepository = repository;
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if ([tempRepository isKindOfClass:[PBGitRepository class]]) {
}
});
}
@end