forked from brotherbard/gitx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PBGitHistoryList.m
374 lines (267 loc) · 8.75 KB
/
PBGitHistoryList.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
//
// PBGitHistoryList.m
// GitX
//
// Created by Nathan Kinsinger on 2/20/10.
// Copyright 2010 Nathan Kinsinger. All rights reserved.
//
#import "PBGitHistoryList.h"
#import "PBGitRepository.h"
#import "PBGitRevList.h"
#import "PBGitGrapher.h"
#import "PBGitHistoryGrapher.h"
#import "PBGitSHA.h"
@interface PBGitHistoryList ()
- (void) resetGraphing;
- (PBGitHistoryGrapher *) grapher;
- (NSInvocationOperation *) operationForCommits:(NSArray *)newCommits;
- (void) updateProjectHistoryForRev:(PBGitRevSpecifier *)rev;
- (void) updateHistoryForRev:(PBGitRevSpecifier *)rev;
@end
@implementation PBGitHistoryList
@synthesize projectRevList;
@synthesize commits;
@synthesize isUpdating;
@dynamic projectCommits;
#pragma mark -
#pragma mark Public
- (id) initWithRepository:(PBGitRepository *)repo
{
commits = [NSMutableArray array];
repository = repo;
lastBranchFilter = -1;
[repository addObserver:self forKeyPath:@"currentBranch" options:0 context:@"currentBranch"];
[repository addObserver:self forKeyPath:@"currentBranchFilter" options:0 context:@"currentBranch"];
[repository addObserver:self forKeyPath:@"hasChanged" options:0 context:@"repositoryHasChanged"];
shouldReloadProjectHistory = YES;
projectRevList = [[PBGitRevList alloc] initWithRepository:repository rev:[PBGitRevSpecifier allBranchesRevSpec] shouldGraph:NO];
return self;
}
- (void) forceUpdate
{
if ([repository.currentBranch isSimpleRef])
shouldReloadProjectHistory = YES;
[self updateHistory];
}
- (void) updateHistory
{
PBGitRevSpecifier *rev = repository.currentBranch;
if (!rev)
return;
if ([rev isSimpleRef])
[self updateProjectHistoryForRev:rev];
else
[self updateHistoryForRev:rev];
}
- (void)cleanup
{
if (currentRevList) {
[currentRevList removeObserver:self forKeyPath:@"commits"];
[currentRevList cancel];
}
[graphQueue cancelAllOperations];
[repository removeObserver:self forKeyPath:@"currentBranch"];
[repository removeObserver:self forKeyPath:@"currentBranchFilter"];
[repository removeObserver:self forKeyPath:@"hasChanged"];
}
- (NSArray *) projectCommits
{
return [projectRevList.commits copy];
}
#pragma mark -
#pragma mark History Grapher delegate methods
- (void) addCommitsFromArray:(NSArray *)array
{
if (!array || [array count] == 0)
return;
if (resetCommits) {
self.commits = [NSMutableArray array];
resetCommits = NO;
}
NSRange range = NSMakeRange([commits count], [array count]);
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:range];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"commits"];
[commits addObjectsFromArray:array];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"commits"];
}
- (void) updateCommitsFromGrapher:(NSDictionary *)commitData
{
if ([commitData objectForKey:kCurrentQueueKey] != graphQueue)
return;
[self addCommitsFromArray:[commitData objectForKey:kNewCommitsKey]];
}
- (void) finishedGraphing
{
if (!currentRevList.isParsing && ([[graphQueue operations] count] == 0)) {
self.isUpdating = NO;
}
}
#pragma mark -
#pragma mark Private
- (void) resetGraphing
{
resetCommits = YES;
self.isUpdating = YES;
[graphQueue cancelAllOperations];
graphQueue = [[NSOperationQueue alloc] init];
[graphQueue setMaxConcurrentOperationCount:1];
grapher = [self grapher];
}
- (NSInvocationOperation *) operationForCommits:(NSArray *)newCommits
{
return [[NSInvocationOperation alloc] initWithTarget:grapher selector:@selector(graphCommits:) object:newCommits];
}
- (NSSet *) baseCommitsForLocalRefs
{
NSMutableSet *baseCommitSHAs = [NSMutableSet set];
NSDictionary *refs = repository.refs;
for (PBGitSHA *sha in refs)
for (PBGitRef *ref in [refs objectForKey:sha])
if ([ref isBranch] || [ref isTag])
[baseCommitSHAs addObject:sha];
if (![[PBGitRef refFromString:[[repository headRef] simpleRef]] type])
[baseCommitSHAs addObject:[repository headSHA]];
return baseCommitSHAs;
}
- (NSSet *) baseCommitsForRemoteRefs
{
NSMutableSet *baseCommitSHAs = [NSMutableSet set];
NSDictionary *refs = repository.refs;
PBGitRef *remoteRef = [[repository.currentBranch ref] remoteRef];
for (PBGitSHA *sha in refs)
for (PBGitRef *ref in [refs objectForKey:sha])
if ([remoteRef isEqualToRef:[ref remoteRef]])
[baseCommitSHAs addObject:sha];
return baseCommitSHAs;
}
- (NSSet *) baseCommits
{
if ((repository.currentBranchFilter == kGitXSelectedBranchFilter) || (repository.currentBranchFilter == kGitXAllBranchesFilter)) {
if (lastSHA)
return [NSMutableSet setWithObject:lastSHA];
else if ([repository.currentBranch isSimpleRef]) {
PBGitRef *currentRef = [repository.currentBranch ref];
PBGitSHA *sha = [repository shaForRef:currentRef];
if (sha)
return [NSMutableSet setWithObject:sha];
}
}
else if (repository.currentBranchFilter == kGitXLocalRemoteBranchesFilter) {
if ([[repository.currentBranch ref] isRemote])
return [self baseCommitsForRemoteRefs];
else
return [self baseCommitsForLocalRefs];
}
return [NSMutableSet set];
}
- (PBGitHistoryGrapher *) grapher
{
BOOL viewAllBranches = (repository.currentBranchFilter == kGitXAllBranchesFilter);
return [[PBGitHistoryGrapher alloc] initWithBaseCommits:[self baseCommits] viewAllBranches:viewAllBranches queue:graphQueue delegate:self];
}
- (void) setCurrentRevList:(PBGitRevList *)parser
{
if (currentRevList == parser)
return;
if (currentRevList)
[currentRevList removeObserver:self forKeyPath:@"commits"];
currentRevList = parser;
[currentRevList addObserver:self forKeyPath:@"commits" options:NSKeyValueObservingOptionNew context:@"commitsUpdated"];
}
- (BOOL) isAllBranchesOnlyUpdate
{
return (lastBranchFilter == kGitXAllBranchesFilter) && (repository.currentBranchFilter == kGitXAllBranchesFilter);
}
- (BOOL) isLocalRemoteOnlyUpdate:(PBGitRevSpecifier *)rev
{
if ((lastBranchFilter == kGitXLocalRemoteBranchesFilter) && (repository.currentBranchFilter == kGitXLocalRemoteBranchesFilter)) {
if (!lastRemoteRef && ![[rev ref] isRemote])
return YES;
if ([lastRemoteRef isEqualToRef:[[rev ref] remoteRef]])
return YES;
}
return NO;
}
- (BOOL) selectedBranchNeedsNewGraph:(PBGitRevSpecifier *)rev
{
if (![rev isSimpleRef])
return YES;
if ([self isAllBranchesOnlyUpdate] || [self isLocalRemoteOnlyUpdate:rev]) {
lastRemoteRef = [[rev ref] remoteRef];
lastSHA = nil;
self.isUpdating = NO;
return NO;
}
PBGitSHA *revSHA = [repository shaForRef:[rev ref]];
if ([revSHA isEqual:lastSHA] && (lastBranchFilter == repository.currentBranchFilter))
return NO;
lastBranchFilter = repository.currentBranchFilter;
lastRemoteRef = [[rev ref] remoteRef];
lastSHA = revSHA;
return YES;
}
- (BOOL) haveRefsBeenModified
{
[repository reloadRefs];
NSMutableSet *currentRefSHAs = [NSMutableSet setWithArray:[repository.refs allKeys]];
[currentRefSHAs minusSet:lastRefSHAs];
lastRefSHAs = [NSSet setWithArray:[repository.refs allKeys]];
return [currentRefSHAs count] != 0;
}
#pragma mark updating history
- (void) updateProjectHistoryForRev:(PBGitRevSpecifier *)rev
{
[self setCurrentRevList:projectRevList];
if ([self haveRefsBeenModified])
shouldReloadProjectHistory = YES;
if (![self selectedBranchNeedsNewGraph:rev] && !shouldReloadProjectHistory)
return;
[self resetGraphing];
if (shouldReloadProjectHistory) {
shouldReloadProjectHistory = NO;
lastBranchFilter = -1;
lastRemoteRef = nil;
lastSHA = nil;
self.commits = [NSMutableArray array];
[projectRevList loadRevisons];
return;
}
[graphQueue addOperation:[self operationForCommits:projectRevList.commits]];
}
- (void) updateHistoryForRev:(PBGitRevSpecifier *)rev
{
PBGitRevList *otherRevListParser = [[PBGitRevList alloc] initWithRepository:repository rev:rev shouldGraph:YES];
[self setCurrentRevList:otherRevListParser];
[self resetGraphing];
lastBranchFilter = -1;
lastRemoteRef = nil;
lastSHA = nil;
self.commits = [NSMutableArray array];
[otherRevListParser loadRevisons];
}
#pragma mark -
#pragma mark Key Value Observing
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([@"currentBranch" isEqualToString:context]) {
[self updateHistory];
return;
}
if ([@"repositoryHasChanged" isEqualToString:context]) {
[self forceUpdate];
return;
}
if ([@"commitsUpdated" isEqualToString:context]) {
NSInteger changeKind = [(NSNumber *)[change objectForKey:NSKeyValueChangeKindKey] intValue];
if (changeKind == NSKeyValueChangeInsertion) {
NSArray *newCommits = [change objectForKey:NSKeyValueChangeNewKey];
if ([repository.currentBranch isSimpleRef])
[graphQueue addOperation:[self operationForCommits:newCommits]];
else
[self addCommitsFromArray:newCommits];
}
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
@end