Skip to content

Commit 2eeff63

Browse files
committed
Fix External git diff-files being called repeatedly for the same file (issue gitx#530)
Answer the commit view from the diff it was last given while nothing it was read from has changed, instead of starting another git for a file that has not moved. Staging one file re-renders the commit view three times: - once as the selection moves on, and - once for each of the two index refreshes that follow. Each render asks for the diff of the file now on screen, so the same `diff-files -U3 -- <path>` ran three times over an untouched file, which is what issue gitx#530 reports seeing in the log. The refresh output cannot tell whether a file's content changed, only how it is staged, so the file and the index are stat'ed instead: size and modification time to the nanosecond, alongside the path, the tree being compared against and the requested context. - Read a diff through a one entry cache keyed on all of that - Fall back to reading it every time when the index cannot be located, since then there is nothing to notice a change against - Cover both halves: a diff nothing could have changed is asked for once, and an edit, a rewritten index, a different context size or an amend are each asked for again
1 parent dec174b commit 2eeff63

3 files changed

Lines changed: 228 additions & 0 deletions

File tree

‎Classes/git/PBGitIndex.m‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#import "PBTask.h"
1414
#import "PBChangedFile.h"
1515

16+
#import <sys/stat.h>
17+
1618
NSString *PBGitIndexIndexRefreshStatus = @"PBGitIndexIndexRefreshStatus";
1719
NSString *PBGitIndexIndexRefreshFailed = @"PBGitIndexIndexRefreshFailed";
1820
NSString *PBGitIndexFinishedIndexRefresh = @"PBGitIndexFinishedIndexRefresh";
@@ -54,6 +56,8 @@ @interface PBGitIndex () {
5456
NSDictionary *_stagedChanges;
5557
NSDictionary *_unstagedChanges;
5658
NSDictionary *_untrackedChanges;
59+
NSString *_diffCacheKey;
60+
NSString *_diffCacheOutput;
5761
}
5862

5963
@property (retain) NSDictionary *amendEnvironment;
@@ -670,7 +674,53 @@ - (BOOL)applyPatch:(NSString *)hunk stage:(BOOL)stage reverse:(BOOL)reverse;
670674
}
671675

672676

677+
- (NSString *)fingerprintOfFileAtURL:(nullable NSURL *)url
678+
{
679+
struct stat info;
680+
if (!url.isFileURL || stat(url.path.fileSystemRepresentation, &info) != 0)
681+
return @"absent";
682+
683+
return [NSString stringWithFormat:@"%lld.%ld.%ld",
684+
(long long)info.st_size,
685+
(long)info.st_mtimespec.tv_sec,
686+
(long)info.st_mtimespec.tv_nsec];
687+
}
688+
689+
- (nullable NSString *)diffCacheKeyForFile:(PBChangedFile *)file staged:(BOOL)staged contextLines:(NSUInteger)context
690+
{
691+
NSURL *indexURL = self.repository.indexURL;
692+
if (!indexURL)
693+
return nil;
694+
695+
NSURL *fileURL = [self.repository.workingDirectoryURL URLByAppendingPathComponent:file.path];
696+
697+
return [NSString stringWithFormat:@"%@ %d %lu %d %@ %@ %@",
698+
file.path,
699+
staged,
700+
(unsigned long)context,
701+
(int)file.status,
702+
[self parentTree],
703+
[self fingerprintOfFileAtURL:fileURL],
704+
[self fingerprintOfFileAtURL:indexURL]];
705+
}
706+
673707
- (NSString *)diffForFile:(PBChangedFile *)file staged:(BOOL)staged contextLines:(NSUInteger)context
708+
{
709+
NSString *cacheKey = [self diffCacheKeyForFile:file staged:staged contextLines:context];
710+
if (cacheKey && [cacheKey isEqualToString:_diffCacheKey])
711+
return _diffCacheOutput;
712+
713+
NSString *output = [self readDiffForFile:file staged:staged contextLines:context];
714+
715+
if (cacheKey && output) {
716+
_diffCacheKey = cacheKey;
717+
_diffCacheOutput = output;
718+
}
719+
720+
return output;
721+
}
722+
723+
- (NSString *)readDiffForFile:(PBChangedFile *)file staged:(BOOL)staged contextLines:(NSUInteger)context
674724
{
675725
NSString *parameter = [NSString stringWithFormat:@"-U%lu", context];
676726
if (staged) {

‎GitX.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */; };
157157
63E155858B2CC783EE10F1D8 /* PBGitIndexReconcileTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */; };
158158
63E155858B2CC783EE10F1D9 /* PBGitIndexRefreshTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */; };
159+
9A3F7C21E48B450D9C2A1F60 /* PBGitIndexDiffCacheTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A3F7C21E48B450D9C2A1F61 /* PBGitIndexDiffCacheTests.m */; };
159160
7C41A9E2B5D340E1A7F26C08 /* PBGitHistorySelectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */; };
160161
1D8C4F70A93E42B6BE7C0D95 /* PBGitWindowWiringTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4F2E8A13C7D9406B85A31E27 /* PBGitWindowWiringTests.m */; };
161162
7A1C3E5D2B8F49A0C6D14E82 /* PBRepositoryFinderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E9B0D4A18C7452FB3A0D95E /* PBRepositoryFinderTests.m */; };
@@ -698,6 +699,7 @@
698699
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryFetchArgumentsTests.m; path = GitXTests/PBGitRepositoryFetchArgumentsTests.m; sourceTree = "<group>"; };
699700
689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitIndexReconcileTests.m; path = GitXTests/PBGitIndexReconcileTests.m; sourceTree = "<group>"; };
700701
689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitIndexRefreshTests.m; path = GitXTests/PBGitIndexRefreshTests.m; sourceTree = "<group>"; };
702+
9A3F7C21E48B450D9C2A1F61 /* PBGitIndexDiffCacheTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitIndexDiffCacheTests.m; path = GitXTests/PBGitIndexDiffCacheTests.m; sourceTree = "<group>"; };
701703
5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitHistorySelectionTests.m; path = GitXTests/PBGitHistorySelectionTests.m; sourceTree = "<group>"; };
702704
4F2E8A13C7D9406B85A31E27 /* PBGitWindowWiringTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitWindowWiringTests.m; path = GitXTests/PBGitWindowWiringTests.m; sourceTree = "<group>"; };
703705
6E9B0D4A18C7452FB3A0D95E /* PBRepositoryFinderTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBRepositoryFinderTests.m; path = GitXTests/PBRepositoryFinderTests.m; sourceTree = "<group>"; };
@@ -826,6 +828,7 @@
826828
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */,
827829
689D57B4A78CEF3B32EB0045 /* PBGitIndexReconcileTests.m */,
828830
689D57B4A78CEF3B32EB0046 /* PBGitIndexRefreshTests.m */,
831+
9A3F7C21E48B450D9C2A1F61 /* PBGitIndexDiffCacheTests.m */,
829832
5E9D3B41C86F42A7B10D5E33 /* PBGitHistorySelectionTests.m */,
830833
4F2E8A13C7D9406B85A31E27 /* PBGitWindowWiringTests.m */,
831834
6E9B0D4A18C7452FB3A0D95E /* PBRepositoryFinderTests.m */,
@@ -1769,6 +1772,7 @@
17691772
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */,
17701773
63E155858B2CC783EE10F1D8 /* PBGitIndexReconcileTests.m in Sources */,
17711774
63E155858B2CC783EE10F1D9 /* PBGitIndexRefreshTests.m in Sources */,
1775+
9A3F7C21E48B450D9C2A1F60 /* PBGitIndexDiffCacheTests.m in Sources */,
17721776
7C41A9E2B5D340E1A7F26C08 /* PBGitHistorySelectionTests.m in Sources */,
17731777
1D8C4F70A93E42B6BE7C0D95 /* PBGitWindowWiringTests.m in Sources */,
17741778
7A1C3E5D2B8F49A0C6D14E82 /* PBRepositoryFinderTests.m in Sources */,
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
//
2+
// PBGitIndexDiffCacheTests.m
3+
// GitXTests
4+
//
5+
6+
#import <XCTest/XCTest.h>
7+
#import "PBChangedFile.h"
8+
#import "PBGitIndex.h"
9+
#import "PBGitRepository.h"
10+
#import "PBGitRepository_PBGitBinarySupport.h"
11+
12+
// Records the diff git is asked for instead of running it, and points the two
13+
// paths the cache stats at a temporary directory the test owns.
14+
@interface PBDiffStubRepository : PBGitRepository
15+
@property (nonatomic, strong) NSURL *workingURL;
16+
@property (nonatomic, strong) NSURL *indexFileURL;
17+
@property (nonatomic, strong) NSMutableArray<NSArray<NSString *> *> *askedFor;
18+
@end
19+
20+
@implementation PBDiffStubRepository
21+
22+
- (instancetype)init
23+
{
24+
if (!(self = [super init]))
25+
return nil;
26+
27+
_askedFor = [NSMutableArray array];
28+
29+
return self;
30+
}
31+
32+
- (NSURL *)workingDirectoryURL
33+
{
34+
return self.workingURL;
35+
}
36+
37+
- (NSURL *)getIndexURL
38+
{
39+
return self.indexFileURL;
40+
}
41+
42+
// So that -parentTree names HEAD and HEAD^ rather than falling back to the
43+
// empty tree for both, which is what an amend has to be told apart by.
44+
- (BOOL)revisionExists:(NSString *)spec
45+
{
46+
return YES;
47+
}
48+
49+
- (NSString *)outputOfTaskWithArguments:(NSArray *)arguments error:(NSError **)error
50+
{
51+
[self.askedFor addObject:arguments ?: @[]];
52+
53+
return [NSString stringWithFormat:@"diff %lu", (unsigned long)self.askedFor.count];
54+
}
55+
56+
@end
57+
58+
@interface PBGitIndexDiffCacheTests : XCTestCase
59+
@property (nonatomic, strong) PBDiffStubRepository *repository;
60+
@property (nonatomic, strong) PBGitIndex *gitIndex;
61+
@property (nonatomic, strong) NSURL *directory;
62+
@end
63+
64+
@implementation PBGitIndexDiffCacheTests
65+
66+
- (void)setUp
67+
{
68+
[super setUp];
69+
70+
self.directory = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]]];
71+
[[NSFileManager defaultManager] createDirectoryAtURL:self.directory
72+
withIntermediateDirectories:YES
73+
attributes:nil
74+
error:NULL];
75+
76+
[self write:@"one\n" to:@"a.txt"];
77+
[self write:@"index contents" to:@"index"];
78+
79+
// The index holds its repository weakly, so the test case owns them both
80+
self.repository = [[PBDiffStubRepository alloc] init];
81+
self.repository.workingURL = self.directory;
82+
self.repository.indexFileURL = [self.directory URLByAppendingPathComponent:@"index"];
83+
self.gitIndex = [[PBGitIndex alloc] initWithRepository:self.repository];
84+
}
85+
86+
- (void)tearDown
87+
{
88+
[[NSFileManager defaultManager] removeItemAtURL:self.directory error:NULL];
89+
90+
[super tearDown];
91+
}
92+
93+
- (void)write:(NSString *)contents to:(NSString *)name
94+
{
95+
NSURL *url = [self.directory URLByAppendingPathComponent:name];
96+
[contents writeToURL:url atomically:NO encoding:NSUTF8StringEncoding error:NULL];
97+
}
98+
99+
- (PBChangedFile *)modifiedFile
100+
{
101+
PBChangedFile *file = [[PBChangedFile alloc] initWithPath:@"a.txt"];
102+
file.status = MODIFIED;
103+
file.hasUnstagedChanges = YES;
104+
105+
return file;
106+
}
107+
108+
- (NSString *)unstagedDiff
109+
{
110+
return [self.gitIndex diffForFile:[self modifiedFile] staged:NO contextLines:3];
111+
}
112+
113+
#pragma mark A diff nothing could have changed is not asked for twice
114+
115+
- (void)testTheSameDiffIsAskedForOnlyOnce
116+
{
117+
NSString *first = [self unstagedDiff];
118+
NSString *second = [self unstagedDiff];
119+
120+
XCTAssertEqual(self.repository.askedFor.count, 1u,
121+
@"nothing about the file or the index changed between the two");
122+
XCTAssertEqualObjects(second, first);
123+
}
124+
125+
- (void)testADifferentContextSizeIsAskedFor
126+
{
127+
[self.gitIndex diffForFile:[self modifiedFile] staged:NO contextLines:3];
128+
[self.gitIndex diffForFile:[self modifiedFile] staged:NO contextLines:6];
129+
130+
XCTAssertEqual(self.repository.askedFor.count, 2u);
131+
XCTAssertEqualObjects(self.repository.askedFor.lastObject,
132+
(@[ @"diff-files", @"-U6", @"--", @"a.txt" ]));
133+
}
134+
135+
- (void)testTheStagedDiffIsAskedForSeparately
136+
{
137+
[self.gitIndex diffForFile:[self modifiedFile] staged:NO contextLines:3];
138+
[self.gitIndex diffForFile:[self modifiedFile] staged:YES contextLines:3];
139+
140+
XCTAssertEqual(self.repository.askedFor.count, 2u);
141+
XCTAssertEqualObjects(self.repository.askedFor.lastObject.firstObject, @"diff-index");
142+
}
143+
144+
#pragma mark A diff that could have changed is asked for again
145+
146+
- (void)testEditingTheWorkingFileIsAskedForAgain
147+
{
148+
NSString *first = [self unstagedDiff];
149+
[self write:@"one\ntwo\n" to:@"a.txt"];
150+
NSString *second = [self unstagedDiff];
151+
152+
XCTAssertEqual(self.repository.askedFor.count, 2u);
153+
XCTAssertNotEqualObjects(second, first);
154+
}
155+
156+
- (void)testWritingTheIndexIsAskedForAgain
157+
{
158+
[self unstagedDiff];
159+
[self write:@"index contents, changed" to:@"index"];
160+
[self unstagedDiff];
161+
162+
XCTAssertEqual(self.repository.askedFor.count, 2u);
163+
}
164+
165+
- (void)testAmendingIsAskedForAgain
166+
{
167+
[self.gitIndex diffForFile:[self modifiedFile] staged:YES contextLines:3];
168+
self.gitIndex.amend = YES;
169+
[self.gitIndex diffForFile:[self modifiedFile] staged:YES contextLines:3];
170+
171+
XCTAssertEqual(self.repository.askedFor.count, 2u);
172+
}
173+
174+
@end

0 commit comments

Comments
 (0)