@@ -31,6 +31,14 @@ @interface PBGitRevList ()
3131
3232@property (nonatomic , strong ) NSOperationQueue *operationQueue;
3333
34+ @property (nonatomic , assign ) NSUInteger loadGeneration;
35+ @property (nonatomic , strong ) NSMutableSet <NSString *> *publishedSHAs;
36+
37+ - (BOOL )isLoadGenerationCurrent : (NSUInteger )generation ;
38+ - (void )updateCommits : (NSArray <PBGitCommit *> *)revisions operation : (NSOperation *)operation generation : (NSUInteger )generation ;
39+ - (void )addCommitsFromEnumerator : (GTEnumerator *)enumerator operation : (NSOperation *)operation generation : (NSUInteger )generation ;
40+ - (void )finishLoadGeneration : (NSUInteger )generation completionBlock : (void (^)(void ))completionBlock ;
41+
3442@end
3543
3644
@@ -58,9 +66,14 @@ - (id)initWithRepository:(PBGitRepository *)repo rev:(PBGitRevSpecifier *)rev sh
5866
5967- (void )loadRevisionsWithCompletionBlock : (void (^)(void ))completionBlock
6068{
61- [self cancel ];
62-
63- self.resetCommits = YES ;
69+ [self .operationQueue cancelAllOperations ];
70+ NSUInteger generation;
71+ @synchronized (self) {
72+ generation = ++self.loadGeneration ;
73+ self.resetCommits = YES ;
74+ self.publishedSHAs = [NSMutableSet set ];
75+ }
76+ NSLog (@" [GitX] Starting revision load generation %lu " , (unsigned long )generation);
6477
6578 NSBlockOperation *parseOperation = [[NSBlockOperation alloc ] init ];
6679
@@ -75,16 +88,23 @@ - (void)loadRevisionsWithCompletionBlock:(void (^)(void))completionBlock
7588 GTEnumerator *enu = [[GTEnumerator alloc ] initWithRepository: repo error: &error];
7689
7790 [weakSelf setupEnumerator: enu forRevspec: weakSelf.currentRev];
78- [weakSelf addCommitsFromEnumerator: enu operation: weakParseOperation];
91+ [weakSelf addCommitsFromEnumerator: enu operation: weakParseOperation generation: generation];
92+ }];
93+ [parseOperation setCompletionBlock: ^{
94+ dispatch_async (dispatch_get_main_queue (), ^{
95+ [weakSelf finishLoadGeneration: generation completionBlock: completionBlock];
96+ });
7997 }];
80- [parseOperation setCompletionBlock: completionBlock];
8198
8299 [self .operationQueue addOperation: parseOperation];
83100}
84101
85102
86103- (void )cancel
87104{
105+ @synchronized (self) {
106+ self.loadGeneration ++;
107+ }
88108 [self .operationQueue cancelAllOperations ];
89109}
90110
@@ -94,24 +114,61 @@ - (BOOL)isParsing
94114}
95115
96116
97- - (void ) updateCommits : ( NSArray <PBGitCommit *> *) revisions operation : ( NSOperation *) operation
117+ - (BOOL ) isLoadGenerationCurrent : ( NSUInteger ) generation
98118{
99- if (!revisions || [revisions count ] == 0 || operation.cancelled )
119+ @synchronized (self) {
120+ return generation == self.loadGeneration ;
121+ }
122+ }
123+
124+ - (void )updateCommits : (NSArray <PBGitCommit *> *)revisions operation : (NSOperation *)operation generation : (NSUInteger )generation
125+ {
126+ if (!revisions || [revisions count ] == 0 || operation.cancelled || ![self isLoadGenerationCurrent: generation]) {
127+ if (revisions.count && !operation.cancelled )
128+ NSLog (@" [GitX] Dropped %lu stale commits from revision load generation %lu " , (unsigned long )revisions.count , (unsigned long )generation);
100129 return ;
130+ }
131+
132+ NSMutableArray <PBGitCommit *> *uniqueRevisions = [NSMutableArray arrayWithCapacity: revisions.count];
133+ @synchronized (self) {
134+ if (generation != self.loadGeneration ) return ;
135+ for (PBGitCommit *commit in revisions) {
136+ if ([self .publishedSHAs containsObject: commit.SHA ]) continue ;
137+ [self .publishedSHAs addObject: commit.SHA ];
138+ [uniqueRevisions addObject: commit];
139+ }
140+ }
141+ if (uniqueRevisions.count == 0 ) return ;
101142
102143 if (self.resetCommits ) {
103- self.commits = [NSMutableArray array ];
144+ self.commits = [uniqueRevisions mutableCopy ];
104145 self.resetCommits = NO ;
146+ NSLog (@" [GitX] Published %lu initial commits for revision load generation %lu " , (unsigned long )uniqueRevisions.count , (unsigned long )generation);
147+ return ;
105148 }
106149
107- NSRange range = NSMakeRange ([self .commits count ], [revisions count ]);
150+ NSRange range = NSMakeRange ([self .commits count ], [uniqueRevisions count ]);
108151 NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange: range];
109152
110153 [self willChange: NSKeyValueChangeInsertion valuesAtIndexes: indexes forKey: @" commits" ];
111- [self .commits addObjectsFromArray: revisions ];
154+ [self .commits addObjectsFromArray: uniqueRevisions ];
112155 [self didChange: NSKeyValueChangeInsertion valuesAtIndexes: indexes forKey: @" commits" ];
113156}
114157
158+ - (void )finishLoadGeneration : (NSUInteger )generation completionBlock : (void (^)(void ))completionBlock
159+ {
160+ if (![self isLoadGenerationCurrent: generation]) {
161+ NSLog (@" [GitX] Ignored completion for stale revision load generation %lu " , (unsigned long )generation);
162+ return ;
163+ }
164+ if (self.resetCommits ) {
165+ self.commits = [NSMutableArray array ];
166+ self.resetCommits = NO ;
167+ NSLog (@" [GitX] Revision load generation %lu completed empty" , (unsigned long )generation);
168+ }
169+ if (completionBlock) completionBlock ();
170+ }
171+
115172static BOOL hasParameter (NSMutableArray *parameters, NSString *paramName)
116173{
117174 NSUInteger index = NSNotFound ;
@@ -193,7 +250,7 @@ - (void)setupEnumerator:(GTEnumerator *)enumerator
193250 }
194251}
195252
196- - (void )addCommitsFromEnumerator : (GTEnumerator *)enumerator operation : (NSOperation *)operation
253+ - (void )addCommitsFromEnumerator : (GTEnumerator *)enumerator operation : (NSOperation *)operation generation : ( NSUInteger ) generation
197254{
198255 PBGitGrapher *g = [[PBGitGrapher alloc ] init ];
199256 __block NSDate *lastUpdate = [NSDate date ];
@@ -248,7 +305,7 @@ - (void)addCommitsFromEnumerator:(GTEnumerator *)enumerator operation:(NSOperati
248305 NSArray <PBGitCommit *> *updatedRevisions = [revisions copy ];
249306
250307 dispatch_async (dispatch_get_main_queue (), ^{
251- [self updateCommits: updatedRevisions operation: operation];
308+ [self updateCommits: updatedRevisions operation: operation generation: generation ];
252309 });
253310
254311 [revisions removeAllObjects ];
@@ -266,7 +323,7 @@ - (void)addCommitsFromEnumerator:(GTEnumerator *)enumerator operation:(NSOperati
266323 NSArray <PBGitCommit *> *updatedRevisions = [revisions copy ];
267324
268325 dispatch_async (dispatch_get_main_queue (), ^{
269- [self updateCommits: updatedRevisions operation: operation];
326+ [self updateCommits: updatedRevisions operation: operation generation: generation ];
270327 });
271328}
272329
0 commit comments