diff --git a/src/git-utils.test.ts b/src/git-utils.test.ts index 6d6a32867..d2072e798 100644 --- a/src/git-utils.test.ts +++ b/src/git-utils.test.ts @@ -465,59 +465,7 @@ test("getGitVersionOrThrow throws when git command fails", async (t) => { } }); -test("getGitVersion returns version and caches it", async (t) => { - gitUtils.resetCachedGitVersion(); - const runGitCommandStub = sinon - .stub(gitUtils as any, "runGitCommand") - .resolves("git version 2.40.0\n"); - - const messages: LoggedMessage[] = []; - const logger = getRecordingLogger(messages); - - try { - // First call should fetch and cache - const version1 = await gitUtils.getGitVersion(logger); - t.is(version1, "2.40.0"); - t.is(runGitCommandStub.callCount, 1); - - // Second call should use cache - const version2 = await gitUtils.getGitVersion(logger); - t.is(version2, "2.40.0"); - t.is(runGitCommandStub.callCount, 1); // Should still be 1 - } finally { - runGitCommandStub.restore(); - gitUtils.resetCachedGitVersion(); - } -}); - -test("getGitVersion returns undefined when version cannot be determined", async (t) => { - gitUtils.resetCachedGitVersion(); - const runGitCommandStub = sinon - .stub(gitUtils as any, "runGitCommand") - .rejects(new Error("git not found")); - - const messages: LoggedMessage[] = []; - const logger = getRecordingLogger(messages); - - try { - const version = await gitUtils.getGitVersion(logger); - t.is(version, undefined); - t.true( - messages.some( - (m) => - m.type === "debug" && - typeof m.message === "string" && - m.message.includes("Could not determine Git version"), - ), - ); - } finally { - runGitCommandStub.restore(); - gitUtils.resetCachedGitVersion(); - } -}); - test("gitVersionAtLeast returns true for version meeting requirement", async (t) => { - gitUtils.resetCachedGitVersion(); const runGitCommandStub = sinon .stub(gitUtils as any, "runGitCommand") .resolves("git version 2.40.0\n"); @@ -537,12 +485,10 @@ test("gitVersionAtLeast returns true for version meeting requirement", async (t) ); } finally { runGitCommandStub.restore(); - gitUtils.resetCachedGitVersion(); } }); test("gitVersionAtLeast returns false for version not meeting requirement", async (t) => { - gitUtils.resetCachedGitVersion(); const runGitCommandStub = sinon .stub(gitUtils as any, "runGitCommand") .resolves("git version 2.30.0\n"); @@ -555,12 +501,10 @@ test("gitVersionAtLeast returns false for version not meeting requirement", asyn t.false(result); } finally { runGitCommandStub.restore(); - gitUtils.resetCachedGitVersion(); } }); test("gitVersionAtLeast returns false when version cannot be determined", async (t) => { - gitUtils.resetCachedGitVersion(); const runGitCommandStub = sinon .stub(gitUtils as any, "runGitCommand") .rejects(new Error("git not found")); @@ -581,6 +525,5 @@ test("gitVersionAtLeast returns false when version cannot be determined", async ); } finally { runGitCommandStub.restore(); - gitUtils.resetCachedGitVersion(); } }); diff --git a/src/git-utils.ts b/src/git-utils.ts index 9eb1a0596..71a19a665 100644 --- a/src/git-utils.ts +++ b/src/git-utils.ts @@ -23,16 +23,6 @@ import { */ export const GIT_MINIMUM_VERSION_FOR_OVERLAY = "2.38.0"; -/** Cached git version to avoid recomputing it multiple times. */ -let cachedGitVersion: string | undefined; - -/** - * Resets the cached git version. This is intended for use in tests only. - */ -export function resetCachedGitVersion(): void { - cachedGitVersion = undefined; -} - /** * Gets the version of Git installed on the system and throws an error if * the version cannot be determined. @@ -55,27 +45,6 @@ export async function getGitVersionOrThrow(): Promise { throw new Error(`Could not parse Git version from output: ${stdout.trim()}`); } -/** - * Gets the cached Git version, or fetches and caches it if not yet cached. - * - * @param logger A logger to use for logging errors. - * @returns The cached Git version, or undefined if the version could not be determined. - */ -export async function getGitVersion( - logger: Logger, -): Promise { - if (cachedGitVersion !== undefined) { - return cachedGitVersion; - } - try { - cachedGitVersion = await getGitVersionOrThrow(); - return cachedGitVersion; - } catch (e) { - logger.debug(`Could not determine Git version: ${getErrorMessage(e)}`); - return undefined; - } -} - /** * Logs the Git version as a telemetry diagnostic. Should be called once during * initialization after the config is available. @@ -87,19 +56,23 @@ export async function logGitVersionTelemetry( config: Config, logger: Logger, ): Promise { - const version = await getGitVersion(logger); - if (version !== undefined && config.languages.length > 0) { - addDiagnostic( - config, - // Arbitrarily choose the first language. We could also choose all languages, but that - // increases the risk of misinterpreting the data. - config.languages[0], - makeTelemetryDiagnostic( - "codeql-action/git-version-telemetry", - "Git version telemetry", - { gitVersion: version }, - ), - ); + try { + const version = await getGitVersionOrThrow(); + if (config.languages.length > 0) { + addDiagnostic( + config, + // Arbitrarily choose the first language. We could also choose all languages, but that + // increases the risk of misinterpreting the data. + config.languages[0], + makeTelemetryDiagnostic( + "codeql-action/git-version-telemetry", + "Git version telemetry", + { gitVersion: version }, + ), + ); + } + } catch (e) { + logger.debug(`Could not determine Git version: ${getErrorMessage(e)}`); } } @@ -115,12 +88,14 @@ export async function gitVersionAtLeast( requiredVersion: string, logger: Logger, ): Promise { - const version = await getGitVersion(logger); - if (version === undefined) { + try { + const version = await getGitVersionOrThrow(); + logger.debug(`Installed Git version is ${version}.`); + return semver.gte(version, requiredVersion); + } catch (e) { + logger.debug(`Could not determine Git version: ${getErrorMessage(e)}`); return false; } - logger.debug(`Installed Git version is ${version}.`); - return semver.gte(version, requiredVersion); } export const runGitCommand = async function (