Skip to content

Commit 8591c8b

Browse files
committed
Fix the git path fallback freezing launch when validation fails
Run the default git search off the main thread and keep the configured path until a replacement is found, so a failed background check no longer blocks the UI or leaves the git path nil while the search runs. - Search for a default git binary on the background queue without touching the shared state - Switch to the binary found and show the alert on the main thread - Keep the configured path when no fallback is found - Build the search locations list once, as it is now read off the main thread - Log which binary replaced the configured one Refs #744
1 parent c604676 commit 8591c8b

1 file changed

Lines changed: 72 additions & 32 deletions

File tree

‎Classes/git/PBGitBinary.m‎

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,31 @@ + (void)takeVersion:(NSString *)version
6363
});
6464
}
6565

66-
+ (BOOL)acceptBinary:(NSString *)path
66+
+ (NSString *)acceptableVersionForPath:(NSString *)path
6767
{
6868
if (!path)
69-
return NO;
69+
return nil;
7070

7171
NSString *version = [self versionForPath:path];
7272
if (!version)
73-
return NO;
73+
return nil;
7474

75-
NSComparisonResult c = [version compare:@"" MIN_GIT_VERSION options:NSNumericSearch];
76-
if (c == NSOrderedSame || c == NSOrderedDescending) {
77-
gitPath = path;
78-
[self takeVersion:version];
79-
return YES;
80-
}
75+
if ([self version:version isAtLeast:@"" MIN_GIT_VERSION])
76+
return version;
8177

8278
NSLog(@"Found a git binary at %@, but is only version %@", path, version);
83-
return NO;
79+
return nil;
80+
}
81+
82+
+ (BOOL)acceptBinary:(NSString *)path
83+
{
84+
NSString *version = [self acceptableVersionForPath:path];
85+
if (!version)
86+
return NO;
87+
88+
gitPath = path;
89+
[self takeVersion:version];
90+
return YES;
8491
}
8592

8693
+ (void)showInvalidGitPathAlert:(NSString *)path
@@ -99,50 +106,82 @@ + (void)showInvalidGitPathAlert:(NSString *)path
99106
[alert runModal];
100107
}
101108

102-
+ (void)findDefaultBinary
109+
+ (NSString *)findDefaultBinaryVersion:(NSString **)foundVersion
103110
{
111+
NSString * (^accepted)(NSString *) = ^NSString *(NSString *candidate) {
112+
NSString *version = [self acceptableVersionForPath:candidate];
113+
if (!version)
114+
return nil;
115+
116+
*foundVersion = version;
117+
return candidate;
118+
};
119+
104120
// Try to find the path of the Git binary
105-
char *path = getenv("GIT_PATH");
106-
if (path && [self acceptBinary:[NSString stringWithUTF8String:path]])
107-
return;
121+
char *envPath = getenv("GIT_PATH");
122+
if (envPath) {
123+
NSString *found = accepted([NSString stringWithUTF8String:envPath]);
124+
if (found)
125+
return found;
126+
}
108127

109128
// No explicit path.
110129

111130
// Try to find git with "which"
112131
NSString *whichPath = [PBTask outputForCommand:@"/usr/bin/which" arguments:@[ @"git" ] error:NULL];
113-
if ([self acceptBinary:whichPath])
114-
return;
132+
NSString *found = accepted(whichPath);
133+
if (found)
134+
return found;
115135

116136
// Still no path. Let's try some default locations.
117137
for (NSString *location in [PBGitBinary searchLocations]) {
118-
if ([self acceptBinary:location])
119-
return;
138+
found = accepted(location);
139+
if (found)
140+
return found;
120141
}
121142

122143
// Lastly, try `xcrun git`
123144
NSString *xcrunPath = [PBTask outputForCommand:@"/usr/bin/xcrun" arguments:@[ @"-f", @"git" ] error:NULL];
124-
if ([self acceptBinary:xcrunPath]) {
125-
return;
126-
}
145+
found = accepted(xcrunPath);
146+
if (found)
147+
return found;
127148

128149
NSLog(@"Could not find a git binary higher than version " MIN_GIT_VERSION);
150+
return nil;
151+
}
152+
153+
+ (void)findDefaultBinary
154+
{
155+
NSString *version = nil;
156+
NSString *found = [self findDefaultBinaryVersion:&version];
157+
if (!found)
158+
return;
159+
160+
gitPath = found;
161+
[self takeVersion:version];
129162
}
130163

131164
+ (void)validateConfiguredPath:(NSString *)path
132165
{
133166
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
134-
NSString *version = [self versionForPath:path];
135-
BOOL accepted = [self version:version isAtLeast:@"" MIN_GIT_VERSION];
167+
NSString *version = [self acceptableVersionForPath:path];
168+
if (version) {
169+
dispatch_async(dispatch_get_main_queue(), ^{
170+
[self takeVersion:version];
171+
});
172+
return;
173+
}
174+
175+
NSLog(@"The configured git path %@ failed validation, falling back to the default search", path);
176+
NSString *fallbackVersion = nil;
177+
NSString *fallbackPath = [self findDefaultBinaryVersion:&fallbackVersion];
136178

137179
dispatch_async(dispatch_get_main_queue(), ^{
138-
if (accepted) {
139-
[self takeVersion:version];
140-
return;
180+
if (fallbackPath) {
181+
NSLog(@"Using the git binary %@ (version %@) instead of %@", fallbackPath, fallbackVersion, path);
182+
gitPath = fallbackPath;
183+
[self takeVersion:fallbackVersion];
141184
}
142-
143-
NSLog(@"The configured git path %@ failed validation (version: %@), falling back to the default search", path, version);
144-
gitPath = nil;
145-
[self findDefaultBinary];
146185
[self showInvalidGitPathAlert:path];
147186
});
148187
});
@@ -176,7 +215,8 @@ + (NSString *)path;
176215

177216
+ (NSArray *)searchLocations
178217
{
179-
if (!locations) {
218+
static dispatch_once_t onceToken;
219+
dispatch_once(&onceToken, ^{
180220
locations = [[NSMutableArray alloc] initWithObjects:
181221
@"/opt/homebrew/bin/git",
182222
@"/opt/local/bin/git",
@@ -188,7 +228,7 @@ + (NSArray *)searchLocations
188228

189229
[locations addObject:[@"~/bin/git" stringByExpandingTildeInPath]];
190230
[locations addObject:@"/usr/bin/git"];
191-
}
231+
});
192232
return locations;
193233
}
194234

0 commit comments

Comments
 (0)