Skip to content

Commit dd570d2

Browse files
committed
[Worktree] Check git's version before offering what it cannot do
GitX accepts any git from 1.6.0, but the worktree commands arrived between 2.5 and 2.31, so each action has to know whether the git in use can perform it. The version table and the plan are on #675. - Keep the version read when a git binary is accepted, so a git path never comes without one and asking for it starts no second process - Compare a version against a minimum part by part, as numbers - Name the versions the worktree actions need: 2.10 to lock, and 2.31 for git to report lock and prune state - Word the tooltip for a git too old, naming the version needed and the one found
1 parent b10f42f commit dd570d2

4 files changed

Lines changed: 116 additions & 1 deletion

File tree

‎Classes/git/PBGitBinary.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
#import <Cocoa/Cocoa.h>
1010

1111
#define MIN_GIT_VERSION "1.6.0"
12+
#define PBGitWorktreeLockVersion "2.10"
13+
#define PBGitWorktreeStateVersion "2.31"
1214

1315
@interface PBGitBinary : NSObject
1416

1517
+ (NSString *)path;
1618
+ (NSString *)version;
1719
+ (NSArray *)searchLocations;
1820
+ (NSString *)notFoundError;
21+
22+
+ (BOOL)version:(NSString *)version isAtLeast:(NSString *)minimum;
23+
+ (NSString *)explanationForVersion:(NSString *)version belowRequired:(NSString *)minimum;
1924
@end

‎Classes/git/PBGitBinary.m‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@implementation PBGitBinary
1313

1414
static NSString *gitPath = nil;
15+
static NSString *gitVersion = nil;
1516

1617
+ (NSString *)versionForPath:(NSString *)path
1718
{
@@ -53,6 +54,7 @@ + (BOOL)acceptBinary:(NSString *)path
5354
NSComparisonResult c = [version compare:@"" MIN_GIT_VERSION options:NSNumericSearch];
5455
if (c == NSOrderedSame || c == NSOrderedDescending) {
5556
gitPath = path;
57+
gitVersion = version;
5658
return YES;
5759
}
5860

@@ -150,7 +152,23 @@ + (NSString *)notFoundError
150152

151153
+ (NSString *)version
152154
{
153-
return [self versionForPath:gitPath];
155+
return gitVersion;
156+
}
157+
158+
+ (BOOL)version:(NSString *)version isAtLeast:(NSString *)minimum
159+
{
160+
if (!version)
161+
return NO;
162+
163+
return [version compare:minimum options:NSNumericSearch] != NSOrderedAscending;
164+
}
165+
166+
+ (NSString *)explanationForVersion:(NSString *)version belowRequired:(NSString *)minimum
167+
{
168+
if (!version)
169+
return [NSString stringWithFormat:NSLocalizedString(@"Requires git %@ or later", @"Tooltip for an action the git in use cannot perform, when its version is unknown"), minimum];
170+
171+
return [NSString stringWithFormat:NSLocalizedString(@"Requires git %@ or later (found %@)", @"Tooltip for an action the git in use is too old to perform"), minimum, version];
154172
}
155173

156174
@end

‎GitX.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
7A1C4E2B0D3F58A9C6B41201 /* PBGitHistoryListCleanupTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1C4E2B0D3F58A9C6B41202 /* PBGitHistoryListCleanupTests.m */; };
165165
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */; };
166166
2F9D30A7C15B4E68B4720A50 /* PBGitBinarySearchTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2F9D30A7C15B4E68B4720A51 /* PBGitBinarySearchTests.m */; };
167+
E6091F19ADC34F5D96951951 /* PBGitBinaryVersionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2387DE092483427FA369F82A /* PBGitBinaryVersionTests.m */; };
167168
8A1C64D2B3E7492FA0561D40 /* OpenRecentControllerErrorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */; };
168169
3D8A5F14C7E9420B96741C80 /* PBGitRepositoryIgnoreTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */; };
169170
6B2E70F3A19C4D5E8A730C90 /* GLFileViewLayoutTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B2E70F3A19C4D5E8A730C91 /* GLFileViewLayoutTests.m */; };
@@ -741,6 +742,7 @@
741742
7A1C4E2B0D3F58A9C6B41202 /* PBGitHistoryListCleanupTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitHistoryListCleanupTests.m; path = GitXTests/PBGitHistoryListCleanupTests.m; sourceTree = "<group>"; };
742743
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryFetchArgumentsTests.m; path = GitXTests/PBGitRepositoryFetchArgumentsTests.m; sourceTree = "<group>"; };
743744
2F9D30A7C15B4E68B4720A51 /* PBGitBinarySearchTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitBinarySearchTests.m; path = GitXTests/PBGitBinarySearchTests.m; sourceTree = "<group>"; };
745+
2387DE092483427FA369F82A /* PBGitBinaryVersionTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitBinaryVersionTests.m; path = GitXTests/PBGitBinaryVersionTests.m; sourceTree = "<group>"; };
744746
8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = OpenRecentControllerErrorTests.m; path = GitXTests/OpenRecentControllerErrorTests.m; sourceTree = "<group>"; };
745747
3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryIgnoreTests.m; path = GitXTests/PBGitRepositoryIgnoreTests.m; sourceTree = "<group>"; };
746748
6B2E70F3A19C4D5E8A730C91 /* GLFileViewLayoutTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = GLFileViewLayoutTests.m; path = GitXTests/GLFileViewLayoutTests.m; sourceTree = "<group>"; };
@@ -898,6 +900,7 @@
898900
7A1C4E2B0D3F58A9C6B41202 /* PBGitHistoryListCleanupTests.m */,
899901
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */,
900902
2F9D30A7C15B4E68B4720A51 /* PBGitBinarySearchTests.m */,
903+
2387DE092483427FA369F82A /* PBGitBinaryVersionTests.m */,
901904
8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */,
902905
3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */,
903906
6B2E70F3A19C4D5E8A730C91 /* GLFileViewLayoutTests.m */,
@@ -1878,6 +1881,7 @@
18781881
7A1C4E2B0D3F58A9C6B41201 /* PBGitHistoryListCleanupTests.m in Sources */,
18791882
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */,
18801883
2F9D30A7C15B4E68B4720A50 /* PBGitBinarySearchTests.m in Sources */,
1884+
E6091F19ADC34F5D96951951 /* PBGitBinaryVersionTests.m in Sources */,
18811885
8A1C64D2B3E7492FA0561D40 /* OpenRecentControllerErrorTests.m in Sources */,
18821886
3D8A5F14C7E9420B96741C80 /* PBGitRepositoryIgnoreTests.m in Sources */,
18831887
6B2E70F3A19C4D5E8A730C90 /* GLFileViewLayoutTests.m in Sources */,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// PBGitBinaryVersionTests.m
3+
// GitXTests
4+
//
5+
6+
#import <XCTest/XCTest.h>
7+
#import "PBGitBinary.h"
8+
9+
@interface PBGitBinary (VersionTesting)
10+
+ (NSString *)extractGitVersion:(NSString *)versionString;
11+
@end
12+
13+
@interface PBGitBinaryVersionTests : XCTestCase
14+
@end
15+
16+
@implementation PBGitBinaryVersionTests
17+
18+
- (void)testTheGitInUseReportsItsVersion
19+
{
20+
NSString *path = [PBGitBinary path];
21+
XCTAssertNotNil(path, @"no git was found, so nothing below means anything");
22+
23+
NSTask *task = [[NSTask alloc] init];
24+
task.executableURL = [NSURL fileURLWithPath:path];
25+
task.arguments = @[ @"--version" ];
26+
NSPipe *pipe = [NSPipe pipe];
27+
task.standardOutput = pipe;
28+
XCTAssertTrue([task launchAndReturnError:NULL]);
29+
NSData *output = [pipe.fileHandleForReading readDataToEndOfFile];
30+
[task waitUntilExit];
31+
32+
NSString *expected = [PBGitBinary extractGitVersion:[[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding]];
33+
34+
XCTAssertNotNil([PBGitBinary version], @"every check against a minimum fails when the version is unknown");
35+
XCTAssertEqualObjects([PBGitBinary version], expected, @"the version has to be that of the git actually in use");
36+
}
37+
38+
- (void)testAVersionMeetsTheMinimumItEquals
39+
{
40+
XCTAssertTrue([PBGitBinary version:@"2.10" isAtLeast:@"2.10"]);
41+
XCTAssertTrue([PBGitBinary version:@"2.10.0" isAtLeast:@"2.10"]);
42+
}
43+
44+
- (void)testEachPartIsComparedAsANumberNotAsText
45+
{
46+
XCTAssertTrue([PBGitBinary version:@"2.10.0" isAtLeast:@"2.9"], @"10 is more than 9");
47+
XCTAssertFalse([PBGitBinary version:@"2.9.5" isAtLeast:@"2.10"], @"9 is less than 10");
48+
}
49+
50+
- (void)testTheWorktreeBoundariesFallWhereGitDrewThem
51+
{
52+
XCTAssertFalse([PBGitBinary version:@"2.9.5" isAtLeast:@PBGitWorktreeLockVersion]);
53+
XCTAssertTrue([PBGitBinary version:@"2.10.0" isAtLeast:@PBGitWorktreeLockVersion]);
54+
55+
XCTAssertFalse([PBGitBinary version:@"2.30.9" isAtLeast:@PBGitWorktreeStateVersion]);
56+
XCTAssertTrue([PBGitBinary version:@"2.31.0" isAtLeast:@PBGitWorktreeStateVersion]);
57+
}
58+
59+
- (void)testAppleGitIsReadByItsVersionNumber
60+
{
61+
NSString *version = [PBGitBinary extractGitVersion:@"git version 2.50.1 (Apple Git-155)"];
62+
63+
XCTAssertEqualObjects(version, @"2.50.1");
64+
XCTAssertTrue([PBGitBinary version:version isAtLeast:@PBGitWorktreeStateVersion]);
65+
}
66+
67+
- (void)testAnUnknownVersionMeetsNoMinimum
68+
{
69+
XCTAssertFalse([PBGitBinary version:nil isAtLeast:@"1.0"], @"a git whose version could not be read cannot be trusted with anything newer");
70+
}
71+
72+
- (void)testTheExplanationNamesTheVersionNeededAndTheOneFound
73+
{
74+
NSString *explanation = [PBGitBinary explanationForVersion:@"2.9.5" belowRequired:@"2.10"];
75+
76+
XCTAssertTrue([explanation containsString:@"2.10"], @"%@", explanation);
77+
XCTAssertTrue([explanation containsString:@"2.9.5"], @"%@", explanation);
78+
}
79+
80+
- (void)testTheExplanationStillReadsWhenNoVersionWasFound
81+
{
82+
NSString *explanation = [PBGitBinary explanationForVersion:nil belowRequired:@"2.10"];
83+
84+
XCTAssertTrue([explanation containsString:@"2.10"], @"%@", explanation);
85+
XCTAssertFalse([explanation containsString:@"(null)"], @"%@", explanation);
86+
}
87+
88+
@end

0 commit comments

Comments
 (0)