Skip to content

Commit 6b715b1

Browse files
committed
Publish unique history snapshots by generation
gitx/gitx#184 gitx/gitx#131
1 parent a4274f3 commit 6b715b1

3 files changed

Lines changed: 131 additions & 19 deletions

File tree

‎Classes/git/PBGitHistoryList.m‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
#import "PBGitHistoryList.h"
1010
#import "PBGitRepository.h"
1111
#import "PBGitRevList.h"
12+
#import "PBGitCommit.h"
1213
#import "PBGitGrapher.h"
1314
#import "PBGitHistoryGrapher.h"
1415
#import "PBGitRef.h"
1516
#import "PBGitRevSpecifier.h"
1617

1718
@interface PBGitHistoryList () <PBGitHistoryGrapherDelegate>
1819

20+
@property (nonatomic) NSMutableSet<NSString *> *publishedCommitSHAs;
21+
1922
- (void)resetGraphing;
2023

2124
- (PBGitHistoryGrapher *)grapher;
@@ -110,17 +113,26 @@ - (void)addCommitsFromArray:(NSArray *)array
110113
{
111114
if (!array || [array count] == 0)
112115
return;
116+
NSMutableArray<PBGitCommit *> *uniqueCommits = [NSMutableArray arrayWithCapacity:array.count];
117+
for (PBGitCommit *commit in array) {
118+
if ([self.publishedCommitSHAs containsObject:commit.SHA]) continue;
119+
[self.publishedCommitSHAs addObject:commit.SHA];
120+
[uniqueCommits addObject:commit];
121+
}
122+
if (uniqueCommits.count == 0) return;
113123

114124
if (resetCommits) {
115-
self.commits = [NSMutableArray array];
125+
self.commits = [uniqueCommits mutableCopy];
116126
resetCommits = NO;
127+
NSLog(@"[GitX] Atomically replaced history with %lu commits", (unsigned long)uniqueCommits.count);
128+
return;
117129
}
118130

119-
NSRange range = NSMakeRange([commits count], [array count]);
131+
NSRange range = NSMakeRange([commits count], [uniqueCommits count]);
120132
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:range];
121133

122134
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"commits"];
123-
[commits addObjectsFromArray:array];
135+
[commits addObjectsFromArray:uniqueCommits];
124136
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"commits"];
125137
}
126138

@@ -136,6 +148,12 @@ - (void)updateCommitsFromGrapher:(NSDictionary *)commitData
136148
- (void)finishedGraphing
137149
{
138150
if (!currentRevList.parsing && ([[graphQueue operations] count] == 0)) {
151+
if (resetCommits && currentRevList.commits.count == 0) {
152+
self.commits = [NSMutableArray array];
153+
self.publishedCommitSHAs = [NSMutableSet set];
154+
resetCommits = NO;
155+
NSLog(@"[GitX] Cleared history after an empty revision load");
156+
}
139157
self.isUpdating = NO;
140158
}
141159
}
@@ -148,6 +166,7 @@ - (void)resetGraphing
148166
{
149167
resetCommits = YES;
150168
self.isUpdating = YES;
169+
self.publishedCommitSHAs = [NSMutableSet set];
151170

152171
[graphQueue cancelAllOperations];
153172
graphQueue = [[NSOperationQueue alloc] init];
@@ -241,8 +260,9 @@ - (void)setCurrentRevList:(PBGitRevList *)parser
241260
options:NSKeyValueObservingOptionNew
242261
block:^(MAKVONotification *notification) {
243262
PBGitHistoryList *observer = notification.observer;
244-
if (notification.kind == NSKeyValueChangeInsertion) {
263+
if (notification.kind == NSKeyValueChangeInsertion || notification.kind == NSKeyValueChangeSetting) {
245264
NSArray *newCommits = notification.newValue;
265+
if (newCommits.count == 0) return;
246266
if ([observer->repository.currentBranch isSimpleRef])
247267
[observer->graphQueue addOperation:[observer operationForCommits:newCommits]];
248268
else
@@ -327,7 +347,6 @@ - (void)updateProjectHistoryForRev:(PBGitRevSpecifier *)rev
327347
lastBranchFilter = -1;
328348
lastRemoteRef = nil;
329349
lastOID = nil;
330-
self.commits = [NSMutableArray array];
331350
[projectRevList loadRevisionsWithCompletionBlock:^{
332351
dispatch_async(dispatch_get_main_queue(), ^{
333352
[self finishedGraphing];
@@ -348,7 +367,6 @@ - (void)updateHistoryForRev:(PBGitRevSpecifier *)rev
348367
lastBranchFilter = -1;
349368
lastRemoteRef = nil;
350369
lastOID = nil;
351-
self.commits = [NSMutableArray array];
352370

353371
[otherRevListParser loadRevisionsWithCompletionBlock:^{
354372
dispatch_async(dispatch_get_main_queue(), ^{

‎Classes/git/PBGitRevList.m‎

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
115172
static 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

‎GitXTests/GitXCoreTests.m‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#import <ObjectiveGit/GTCommit.h>
33
#import <ObjectiveGit/GTOID.h>
44
#import <ObjectiveGit/GTRepository.h>
5+
#import "MAKVONotificationCenter.h"
56

67
#import "PBChangedFile.h"
78
#import "PBGraphCellInfo.h"
@@ -488,6 +489,42 @@ - (void)testNormalHistoryLoadPublishesEveryCommitOnce
488489
XCTAssertEqual(uniqueSHAs.count, expectedCount);
489490
}
490491

492+
- (void)testRapidHistoryRefreshKeepsAUniqueNonemptySnapshot
493+
{
494+
NSError *error = nil;
495+
for (NSUInteger index = 0; index < 12; index++) {
496+
NSString *path = [NSString stringWithFormat:@"rapid-history-%lu.txt", (unsigned long)index];
497+
XCTAssertTrue([self.fixture writeText:path toPath:path error:&error], @"%@", error);
498+
XCTAssertTrue([self.fixture commitAllWithMessage:path error:&error], @"%@", error);
499+
}
500+
NSUInteger expectedCount = [[self.fixture git:@[ @"rev-list", @"--count", @"HEAD" ] error:&error] integerValue];
501+
[self.repository reloadRefs];
502+
[self.repository readCurrentBranch];
503+
[self waitForHistoryUpdate];
504+
505+
PBGitHistoryList *history = self.repository.revisionList;
506+
XCTAssertEqual(history.commits.count, expectedCount);
507+
NSMutableArray<NSNumber *> *publishedCounts = [NSMutableArray array];
508+
id<MAKVOObservation> observation = [history addObserver:self
509+
keyPath:@"commits"
510+
options:0
511+
block:^(__unused MAKVONotification *notification) {
512+
[publishedCounts addObject:@(history.commits.count)];
513+
}];
514+
515+
[self.repository forceUpdateRevisions];
516+
XCTAssertEqual(history.commits.count, expectedCount, @"Refresh should retain the previous snapshot until replacement data is ready");
517+
[self.repository forceUpdateRevisions];
518+
[self waitForHistoryUpdate];
519+
[observation remove];
520+
521+
NSArray<PBGitCommit *> *commits = history.commits;
522+
NSSet<NSString *> *uniqueSHAs = [NSSet setWithArray:[commits valueForKey:@"SHA"]];
523+
XCTAssertFalse([publishedCounts containsObject:@0], @"A nonempty refresh should not flash an empty commit list");
524+
XCTAssertEqual(commits.count, expectedCount);
525+
XCTAssertEqual(uniqueSHAs.count, expectedCount);
526+
}
527+
491528
- (void)testUnchangedSymlinkRenameUsesRenameMetadata
492529
{
493530
NSError *error = nil;

0 commit comments

Comments
 (0)