Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git - improve logging for git detection #192297

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions extensions/git/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,36 @@ 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()) }));
});
}

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()) });
Expand All @@ -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);
Expand Down Expand Up @@ -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}`);
}
}

Expand All @@ -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.');
Expand Down
2 changes: 1 addition & 1 deletion extensions/git/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down