Git - improve logging for git detection (#192297)

This commit is contained in:
Ladislau Szomoru 2023-09-06 16:30:59 +02:00 committed by GitHub
parent 4247015d33
commit 3788d799d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 10 deletions

View file

@ -66,14 +66,14 @@ function parseVersion(raw: string): string {
function findSpecificGit(path: string, onValidate: (path: string) => boolean): Promise<IGit> {
return new Promise<IGit>((c, e) => {
if (!onValidate(path)) {
return e('git not found');
return e(new Error(`Path "${path}" is invalid.`));
}
const buffers: Buffer[] = [];
const child = cp.spawn(path, ['--version']);
child.stdout.on('data', (b: Buffer) => buffers.push(b));
child.on('error', cpErrorHandler(e));
child.on('close', code => code ? e(new Error('Not found')) : c({ path, version: parseVersion(Buffer.concat(buffers).toString('utf8').trim()) }));
child.on('close', code => code ? e(new Error(`Not found. Code: ${code}`)) : c({ path, version: parseVersion(Buffer.concat(buffers).toString('utf8').trim()) }));
});
}
@ -81,21 +81,21 @@ function findGitDarwin(onValidate: (path: string) => boolean): Promise<IGit> {
return new Promise<IGit>((c, e) => {
cp.exec('which git', (err, gitPathBuffer) => {
if (err) {
return e('git not found');
return e(new Error(`Executing "which git" failed: ${err.message}`));
}
const path = gitPathBuffer.toString().trim();
function getVersion(path: string) {
if (!onValidate(path)) {
return e('git not found');
return e(new Error(`Path "${path}" is invalid.`));
}
// make sure git executes
cp.exec('git --version', (err, stdout) => {
if (err) {
return e('git not found');
return e(new Error(`Executing "git --version" failed: ${err.message}`));
}
return c({ path, version: parseVersion(stdout.trim()) });
@ -112,7 +112,7 @@ function findGitDarwin(onValidate: (path: string) => boolean): Promise<IGit> {
// git is not installed, and launching /usr/bin/git
// will prompt the user to install it
return e('git not found');
return e(new Error('Executing "xcode-select -p" failed with error code 2.'));
}
getVersion(path);
@ -142,12 +142,13 @@ function findGitWin32(onValidate: (path: string) => boolean): Promise<IGit> {
.then(undefined, () => findGitWin32InPath(onValidate));
}
export async function findGit(hints: string[], onValidate: (path: string) => boolean): Promise<IGit> {
export async function findGit(hints: string[], onValidate: (path: string) => boolean, logger: LogOutputChannel): Promise<IGit> {
for (const hint of hints) {
try {
return await findSpecificGit(hint, onValidate);
} catch {
} catch (err) {
// noop
logger.info(`Unable to find git on the PATH: "${hint}". Error: ${err.message}`);
}
}
@ -157,8 +158,9 @@ export async function findGit(hints: string[], onValidate: (path: string) => boo
case 'win32': return await findGitWin32(onValidate);
default: return await findSpecificGit('git', onValidate);
}
} catch {
} catch (err) {
// noop
logger.warn(`Unable to find git. Error: ${err.message}`);
}
throw new Error('Git installation not found.');

View file

@ -58,7 +58,7 @@ async function createModel(context: ExtensionContext, logger: LogOutputChannel,
logger.info(l10n.t('Skipped found git in: "{0}"', gitPath));
}
return !skip;
});
}, logger);
let ipcServer: IPCServer | undefined = undefined;