Skip to content

Commit e052dbd

Browse files
committed
Remove caching mechanism
1 parent 32795b3 commit e052dbd

File tree

2 files changed

+23
-105
lines changed

2 files changed

+23
-105
lines changed

src/git-utils.test.ts

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -465,59 +465,7 @@ test("getGitVersionOrThrow throws when git command fails", async (t) => {
465465
}
466466
});
467467

468-
test("getGitVersion returns version and caches it", async (t) => {
469-
gitUtils.resetCachedGitVersion();
470-
const runGitCommandStub = sinon
471-
.stub(gitUtils as any, "runGitCommand")
472-
.resolves("git version 2.40.0\n");
473-
474-
const messages: LoggedMessage[] = [];
475-
const logger = getRecordingLogger(messages);
476-
477-
try {
478-
// First call should fetch and cache
479-
const version1 = await gitUtils.getGitVersion(logger);
480-
t.is(version1, "2.40.0");
481-
t.is(runGitCommandStub.callCount, 1);
482-
483-
// Second call should use cache
484-
const version2 = await gitUtils.getGitVersion(logger);
485-
t.is(version2, "2.40.0");
486-
t.is(runGitCommandStub.callCount, 1); // Should still be 1
487-
} finally {
488-
runGitCommandStub.restore();
489-
gitUtils.resetCachedGitVersion();
490-
}
491-
});
492-
493-
test("getGitVersion returns undefined when version cannot be determined", async (t) => {
494-
gitUtils.resetCachedGitVersion();
495-
const runGitCommandStub = sinon
496-
.stub(gitUtils as any, "runGitCommand")
497-
.rejects(new Error("git not found"));
498-
499-
const messages: LoggedMessage[] = [];
500-
const logger = getRecordingLogger(messages);
501-
502-
try {
503-
const version = await gitUtils.getGitVersion(logger);
504-
t.is(version, undefined);
505-
t.true(
506-
messages.some(
507-
(m) =>
508-
m.type === "debug" &&
509-
typeof m.message === "string" &&
510-
m.message.includes("Could not determine Git version"),
511-
),
512-
);
513-
} finally {
514-
runGitCommandStub.restore();
515-
gitUtils.resetCachedGitVersion();
516-
}
517-
});
518-
519468
test("gitVersionAtLeast returns true for version meeting requirement", async (t) => {
520-
gitUtils.resetCachedGitVersion();
521469
const runGitCommandStub = sinon
522470
.stub(gitUtils as any, "runGitCommand")
523471
.resolves("git version 2.40.0\n");
@@ -537,12 +485,10 @@ test("gitVersionAtLeast returns true for version meeting requirement", async (t)
537485
);
538486
} finally {
539487
runGitCommandStub.restore();
540-
gitUtils.resetCachedGitVersion();
541488
}
542489
});
543490

544491
test("gitVersionAtLeast returns false for version not meeting requirement", async (t) => {
545-
gitUtils.resetCachedGitVersion();
546492
const runGitCommandStub = sinon
547493
.stub(gitUtils as any, "runGitCommand")
548494
.resolves("git version 2.30.0\n");
@@ -555,12 +501,10 @@ test("gitVersionAtLeast returns false for version not meeting requirement", asyn
555501
t.false(result);
556502
} finally {
557503
runGitCommandStub.restore();
558-
gitUtils.resetCachedGitVersion();
559504
}
560505
});
561506

562507
test("gitVersionAtLeast returns false when version cannot be determined", async (t) => {
563-
gitUtils.resetCachedGitVersion();
564508
const runGitCommandStub = sinon
565509
.stub(gitUtils as any, "runGitCommand")
566510
.rejects(new Error("git not found"));
@@ -581,6 +525,5 @@ test("gitVersionAtLeast returns false when version cannot be determined", async
581525
);
582526
} finally {
583527
runGitCommandStub.restore();
584-
gitUtils.resetCachedGitVersion();
585528
}
586529
});

src/git-utils.ts

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ import {
2323
*/
2424
export const GIT_MINIMUM_VERSION_FOR_OVERLAY = "2.38.0";
2525

26-
/** Cached git version to avoid recomputing it multiple times. */
27-
let cachedGitVersion: string | undefined;
28-
29-
/**
30-
* Resets the cached git version. This is intended for use in tests only.
31-
*/
32-
export function resetCachedGitVersion(): void {
33-
cachedGitVersion = undefined;
34-
}
35-
3626
/**
3727
* Gets the version of Git installed on the system and throws an error if
3828
* the version cannot be determined.
@@ -55,27 +45,6 @@ export async function getGitVersionOrThrow(): Promise<string> {
5545
throw new Error(`Could not parse Git version from output: ${stdout.trim()}`);
5646
}
5747

58-
/**
59-
* Gets the cached Git version, or fetches and caches it if not yet cached.
60-
*
61-
* @param logger A logger to use for logging errors.
62-
* @returns The cached Git version, or undefined if the version could not be determined.
63-
*/
64-
export async function getGitVersion(
65-
logger: Logger,
66-
): Promise<string | undefined> {
67-
if (cachedGitVersion !== undefined) {
68-
return cachedGitVersion;
69-
}
70-
try {
71-
cachedGitVersion = await getGitVersionOrThrow();
72-
return cachedGitVersion;
73-
} catch (e) {
74-
logger.debug(`Could not determine Git version: ${getErrorMessage(e)}`);
75-
return undefined;
76-
}
77-
}
78-
7948
/**
8049
* Logs the Git version as a telemetry diagnostic. Should be called once during
8150
* initialization after the config is available.
@@ -87,19 +56,23 @@ export async function logGitVersionTelemetry(
8756
config: Config,
8857
logger: Logger,
8958
): Promise<void> {
90-
const version = await getGitVersion(logger);
91-
if (version !== undefined && config.languages.length > 0) {
92-
addDiagnostic(
93-
config,
94-
// Arbitrarily choose the first language. We could also choose all languages, but that
95-
// increases the risk of misinterpreting the data.
96-
config.languages[0],
97-
makeTelemetryDiagnostic(
98-
"codeql-action/git-version-telemetry",
99-
"Git version telemetry",
100-
{ gitVersion: version },
101-
),
102-
);
59+
try {
60+
const version = await getGitVersionOrThrow();
61+
if (config.languages.length > 0) {
62+
addDiagnostic(
63+
config,
64+
// Arbitrarily choose the first language. We could also choose all languages, but that
65+
// increases the risk of misinterpreting the data.
66+
config.languages[0],
67+
makeTelemetryDiagnostic(
68+
"codeql-action/git-version-telemetry",
69+
"Git version telemetry",
70+
{ gitVersion: version },
71+
),
72+
);
73+
}
74+
} catch (e) {
75+
logger.debug(`Could not determine Git version: ${getErrorMessage(e)}`);
10376
}
10477
}
10578

@@ -115,12 +88,14 @@ export async function gitVersionAtLeast(
11588
requiredVersion: string,
11689
logger: Logger,
11790
): Promise<boolean> {
118-
const version = await getGitVersion(logger);
119-
if (version === undefined) {
91+
try {
92+
const version = await getGitVersionOrThrow();
93+
logger.debug(`Installed Git version is ${version}.`);
94+
return semver.gte(version, requiredVersion);
95+
} catch (e) {
96+
logger.debug(`Could not determine Git version: ${getErrorMessage(e)}`);
12097
return false;
12198
}
122-
logger.debug(`Installed Git version is ${version}.`);
123-
return semver.gte(version, requiredVersion);
12499
}
125100

126101
export const runGitCommand = async function (

0 commit comments

Comments
 (0)