Skip to content

Commit dbc8d0c

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 gitx#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.5 to prune, 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 2e3d439 commit dbc8d0c

4 files changed

Lines changed: 120 additions & 1 deletion

File tree

‎Classes/git/PBGitBinary.h‎

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

1111
#define MIN_GIT_VERSION "1.6.0"
12+
#define PBGitWorktreePruneVersion "2.5"
13+
#define PBGitWorktreeLockVersion "2.10"
14+
#define PBGitWorktreeStateVersion "2.31"
1215

1316
@interface PBGitBinary : NSObject
1417

1518
+ (NSString *)path;
1619
+ (NSString *)version;
1720
+ (NSArray *)searchLocations;
1821
+ (NSString *)notFoundError;
22+
23+
+ (BOOL)version:(NSString *)version isAtLeast:(NSString *)minimum;
24+
+ (NSString *)explanationForVersion:(NSString *)version belowRequired:(NSString *)minimum;
1925
@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
@@ -165,6 +165,7 @@
165165
7A1C4E2B0D3F58A9C6B41201 /* PBGitHistoryListCleanupTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1C4E2B0D3F58A9C6B41202 /* PBGitHistoryListCleanupTests.m */; };
166166
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */; };
167167
2F9D30A7C15B4E68B4720A50 /* PBGitBinarySearchTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2F9D30A7C15B4E68B4720A51 /* PBGitBinarySearchTests.m */; };
168+
E6091F19ADC34F5D96951951 /* PBGitBinaryVersionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2387DE092483427FA369F82A /* PBGitBinaryVersionTests.m */; };
168169
8A1C64D2B3E7492FA0561D40 /* OpenRecentControllerErrorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */; };
169170
5C0E7A2D9B4F4E1A8D3C6B70 /* OpenRecentControllerKeyboardTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5C0E7A2D9B4F4E1A8D3C6B71 /* OpenRecentControllerKeyboardTests.m */; };
170171
3D8A5F14C7E9420B96741C80 /* PBGitRepositoryIgnoreTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */; };
@@ -743,6 +744,7 @@
743744
7A1C4E2B0D3F58A9C6B41202 /* PBGitHistoryListCleanupTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitHistoryListCleanupTests.m; path = GitXTests/PBGitHistoryListCleanupTests.m; sourceTree = "<group>"; };
744745
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryFetchArgumentsTests.m; path = GitXTests/PBGitRepositoryFetchArgumentsTests.m; sourceTree = "<group>"; };
745746
2F9D30A7C15B4E68B4720A51 /* PBGitBinarySearchTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitBinarySearchTests.m; path = GitXTests/PBGitBinarySearchTests.m; sourceTree = "<group>"; };
747+
2387DE092483427FA369F82A /* PBGitBinaryVersionTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitBinaryVersionTests.m; path = GitXTests/PBGitBinaryVersionTests.m; sourceTree = "<group>"; };
746748
8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = OpenRecentControllerErrorTests.m; path = GitXTests/OpenRecentControllerErrorTests.m; sourceTree = "<group>"; };
747749
5C0E7A2D9B4F4E1A8D3C6B71 /* OpenRecentControllerKeyboardTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = OpenRecentControllerKeyboardTests.m; path = GitXTests/OpenRecentControllerKeyboardTests.m; sourceTree = "<group>"; };
748750
3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = PBGitRepositoryIgnoreTests.m; path = GitXTests/PBGitRepositoryIgnoreTests.m; sourceTree = "<group>"; };
@@ -902,6 +904,7 @@
902904
7A1C4E2B0D3F58A9C6B41202 /* PBGitHistoryListCleanupTests.m */,
903905
7A1C4E2B90D3F5A6C1B80012 /* PBGitRepositoryFetchArgumentsTests.m */,
904906
2F9D30A7C15B4E68B4720A51 /* PBGitBinarySearchTests.m */,
907+
2387DE092483427FA369F82A /* PBGitBinaryVersionTests.m */,
905908
8A1C64D2B3E7492FA0561D41 /* OpenRecentControllerErrorTests.m */,
906909
5C0E7A2D9B4F4E1A8D3C6B71 /* OpenRecentControllerKeyboardTests.m */,
907910
3D8A5F14C7E9420B96741C81 /* PBGitRepositoryIgnoreTests.m */,
@@ -1883,6 +1886,7 @@
18831886
7A1C4E2B0D3F58A9C6B41201 /* PBGitHistoryListCleanupTests.m in Sources */,
18841887
7A1C4E2B90D3F5A6C1B80011 /* PBGitRepositoryFetchArgumentsTests.m in Sources */,
18851888
2F9D30A7C15B4E68B4720A50 /* PBGitBinarySearchTests.m in Sources */,
1889+
E6091F19ADC34F5D96951951 /* PBGitBinaryVersionTests.m in Sources */,
18861890
8A1C64D2B3E7492FA0561D40 /* OpenRecentControllerErrorTests.m in Sources */,
18871891
5C0E7A2D9B4F4E1A8D3C6B70 /* OpenRecentControllerKeyboardTests.m in Sources */,
18881892
3D8A5F14C7E9420B96741C80 /* PBGitRepositoryIgnoreTests.m in Sources */,
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.4.6" isAtLeast:@PBGitWorktreePruneVersion]);
53+
XCTAssertTrue([PBGitBinary version:@"2.5.0" isAtLeast:@PBGitWorktreePruneVersion]);
54+
55+
XCTAssertFalse([PBGitBinary version:@"2.9.5" isAtLeast:@PBGitWorktreeLockVersion]);
56+
XCTAssertTrue([PBGitBinary version:@"2.10.0" isAtLeast:@PBGitWorktreeLockVersion]);
57+
58+
XCTAssertFalse([PBGitBinary version:@"2.30.9" isAtLeast:@PBGitWorktreeStateVersion]);
59+
XCTAssertTrue([PBGitBinary version:@"2.31.0" isAtLeast:@PBGitWorktreeStateVersion]);
60+
}
61+
62+
- (void)testAppleGitIsReadByItsVersionNumber
63+
{
64+
NSString *version = [PBGitBinary extractGitVersion:@"git version 2.50.1 (Apple Git-155)"];
65+
66+
XCTAssertEqualObjects(version, @"2.50.1");
67+
XCTAssertTrue([PBGitBinary version:version isAtLeast:@PBGitWorktreeStateVersion]);
68+
}
69+
70+
- (void)testAnUnknownVersionMeetsNoMinimum
71+
{
72+
XCTAssertFalse([PBGitBinary version:nil isAtLeast:@"1.0"], @"a git whose version could not be read cannot be trusted with anything newer");
73+
}
74+
75+
- (void)testTheExplanationNamesTheVersionNeededAndTheOneFound
76+
{
77+
NSString *explanation = [PBGitBinary explanationForVersion:@"2.9.5" belowRequired:@"2.10"];
78+
79+
XCTAssertTrue([explanation containsString:@"2.10"], @"%@", explanation);
80+
XCTAssertTrue([explanation containsString:@"2.9.5"], @"%@", explanation);
81+
}
82+
83+
- (void)testTheExplanationStillReadsWhenNoVersionWasFound
84+
{
85+
NSString *explanation = [PBGitBinary explanationForVersion:nil belowRequired:@"2.10"];
86+
87+
XCTAssertTrue([explanation containsString:@"2.10"], @"%@", explanation);
88+
XCTAssertFalse([explanation containsString:@"(null)"], @"%@", explanation);
89+
}
90+
91+
@end

0 commit comments

Comments
 (0)