Skip to content

Commit 43f6ab3

Browse files
committed
Fix a spurious invalid git path alert at login
Validate the configured git path in the background so a transient failure of the `/usr/bin/git` shim at login no longer blocks launch or raises a false alert. - Trust an executable configured path at once and run `--version` off the main thread - On failed validation, log it, fall back to the default search and show the alert - Log launch errors and unparseable `--version` output instead of dropping them - Move the default search and the alert into their own methods Fixes #744
1 parent 07cf85c commit 43f6ab3

1 file changed

Lines changed: 67 additions & 23 deletions

File tree

‎Classes/git/PBGitBinary.m‎

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ + (NSString *)versionForPath:(NSString *)path
2222
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
2323
return nil;
2424

25-
NSString *version = [PBTask outputForCommand:path arguments:@[ @"--version" ] error:NULL];
25+
NSError *error = nil;
26+
NSString *version = [PBTask outputForCommand:path arguments:@[ @"--version" ] error:&error];
27+
if (!version)
28+
NSLog(@"Running '%@ --version' failed: %@", path, error);
29+
30+
NSString *extracted = [self extractGitVersion:version];
31+
if (version && !extracted)
32+
NSLog(@"Could not parse the output of '%@ --version': %@", path, version);
2633

27-
return [self extractGitVersion:version];
34+
return extracted;
2835
}
2936

3037
+ (NSString *)extractGitVersion:(NSString *)versionString
@@ -33,6 +40,9 @@ + (NSString *)extractGitVersion:(NSString *)versionString
3340
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"git version ([0-9.]+)"
3441
options:0
3542
error:&error];
43+
if (!versionString)
44+
return nil;
45+
3646
NSTextCheckingResult *result = [regex firstMatchInString:versionString
3747
options:0
3848
range:NSMakeRange(0, versionString.length)];
@@ -62,29 +72,24 @@ + (BOOL)acceptBinary:(NSString *)path
6272
return NO;
6373
}
6474

65-
+ (void)initialize
75+
+ (void)showInvalidGitPathAlert:(NSString *)path
6676
{
67-
// Check what we might have in user defaults
68-
// NOTE: Currently this should NOT have a registered default, or the searching bits below won't work
69-
gitPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"gitExecutable"];
70-
if (gitPath.length > 0) {
71-
if ([self acceptBinary:gitPath])
72-
return;
73-
74-
NSAlert *alert = [[NSAlert alloc] init];
75-
76-
alert.messageText = NSLocalizedString(@"Invalid git path", @"Error message for NSUserDefaults configured path to git binary that does not point to a git binary");
77-
alert.informativeText = [NSString stringWithFormat:NSLocalizedString(
78-
@"The path „%@“, which is configured as a custom git path in the "
79-
"preferences window, is not a valid git v" MIN_GIT_VERSION " or higher binary. "
80-
"Using the default search paths instead.",
81-
"Informative text for NSUserDefaults configured path to git binary that does not point to a git binary"),
82-
gitPath];
83-
[alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK")];
84-
85-
[alert runModal];
86-
}
77+
NSAlert *alert = [[NSAlert alloc] init];
78+
79+
alert.messageText = NSLocalizedString(@"Invalid git path", @"Error message for NSUserDefaults configured path to git binary that does not point to a git binary");
80+
alert.informativeText = [NSString stringWithFormat:NSLocalizedString(
81+
@"The path „%@“, which is configured as a custom git path in the "
82+
"preferences window, is not a valid git v" MIN_GIT_VERSION " or higher binary. "
83+
"Using the default search paths instead.",
84+
"Informative text for NSUserDefaults configured path to git binary that does not point to a git binary"),
85+
path];
86+
[alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK")];
87+
88+
[alert runModal];
89+
}
8790

91+
+ (void)findDefaultBinary
92+
{
8893
// Try to find the path of the Git binary
8994
char *path = getenv("GIT_PATH");
9095
if (path && [self acceptBinary:[NSString stringWithUTF8String:path]])
@@ -112,6 +117,45 @@ + (void)initialize
112117
NSLog(@"Could not find a git binary higher than version " MIN_GIT_VERSION);
113118
}
114119

120+
+ (void)validateConfiguredPath:(NSString *)path
121+
{
122+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
123+
NSString *version = [self versionForPath:path];
124+
BOOL accepted = [self version:version isAtLeast:@"" MIN_GIT_VERSION];
125+
126+
dispatch_async(dispatch_get_main_queue(), ^{
127+
if (accepted) {
128+
gitVersion = version;
129+
return;
130+
}
131+
132+
NSLog(@"The configured git path %@ failed validation (version: %@), falling back to the default search", path, version);
133+
gitPath = nil;
134+
[self findDefaultBinary];
135+
[self showInvalidGitPathAlert:path];
136+
});
137+
});
138+
}
139+
140+
+ (void)initialize
141+
{
142+
// Check what we might have in user defaults
143+
// NOTE: Currently this should NOT have a registered default, or the searching bits below won't work
144+
gitPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"gitExecutable"];
145+
if (gitPath.length > 0) {
146+
if ([[NSFileManager defaultManager] isExecutableFileAtPath:gitPath]) {
147+
NSLog(@"Using the configured git path %@, validating it in the background", gitPath);
148+
[self validateConfiguredPath:gitPath];
149+
return;
150+
}
151+
152+
NSLog(@"The configured git path %@ is not an executable file", gitPath);
153+
[self showInvalidGitPathAlert:gitPath];
154+
}
155+
156+
[self findDefaultBinary];
157+
}
158+
115159
+ (NSString *)path;
116160
{
117161
return gitPath;

0 commit comments

Comments
 (0)