From be0a15632639d7518921a8ab3fb5ed0dd452aba8 Mon Sep 17 00:00:00 2001 From: Henry Mercer Date: Mon, 30 Mar 2026 13:37:14 +0100 Subject: [PATCH] Save a computation of the git root --- lib/analyze-action-post.js | 16 ++++++++-------- lib/analyze-action.js | 16 ++++++++-------- lib/autobuild-action.js | 16 ++++++++-------- lib/init-action-post.js | 16 ++++++++-------- lib/init-action.js | 21 +++++++++++---------- lib/resolve-environment-action.js | 16 ++++++++-------- lib/setup-codeql-action.js | 16 ++++++++-------- lib/upload-lib.js | 16 ++++++++-------- lib/upload-sarif-action.js | 16 ++++++++-------- src/config-utils.test.ts | 2 +- src/config-utils.ts | 5 +++-- src/git-utils.ts | 19 +++++++++---------- 12 files changed, 88 insertions(+), 87 deletions(-) diff --git a/lib/analyze-action-post.js b/lib/analyze-action-post.js index 39c251b0b..36d3f5160 100644 --- a/lib/analyze-action-post.js +++ b/lib/analyze-action-post.js @@ -162174,17 +162174,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs2.existsSync(path3.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, diff --git a/lib/analyze-action.js b/lib/analyze-action.js index 2a2669d27..8ff502a83 100644 --- a/lib/analyze-action.js +++ b/lib/analyze-action.js @@ -107769,17 +107769,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs3.existsSync(path4.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, diff --git a/lib/autobuild-action.js b/lib/autobuild-action.js index 9dd07d043..1980bf1f3 100644 --- a/lib/autobuild-action.js +++ b/lib/autobuild-action.js @@ -104228,17 +104228,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs2.existsSync(path3.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, diff --git a/lib/init-action-post.js b/lib/init-action-post.js index c32b8f665..49956d89f 100644 --- a/lib/init-action-post.js +++ b/lib/init-action-post.js @@ -165686,17 +165686,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs3.existsSync(path4.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, diff --git a/lib/init-action.js b/lib/init-action.js index e31c24e7a..efd78a136 100644 --- a/lib/init-action.js +++ b/lib/init-action.js @@ -105312,17 +105312,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs3.existsSync(path5.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, @@ -107048,13 +107048,14 @@ async function validateOverlayDatabaseMode(overlayDatabaseMode, useOverlayDataba ); return new Failure("incompatible-codeql" /* IncompatibleCodeQl */); } - if (await getGitRoot(sourceRoot) === void 0) { + const gitRoot = await getGitRoot(sourceRoot); + if (gitRoot === void 0) { logger.warning( `Cannot build an ${overlayDatabaseMode} database because the source root "${sourceRoot}" is not inside a git repository. Falling back to creating a normal full database instead.` ); return new Failure("no-git-root" /* NoGitRoot */); } - if (await hasSubmodules(sourceRoot)) { + if (hasSubmodules(gitRoot)) { if (gitVersion === void 0) { logger.warning( `Cannot build an ${overlayDatabaseMode} database because the repository has submodules and the Git version could not be determined. Falling back to creating a normal full database instead.` diff --git a/lib/resolve-environment-action.js b/lib/resolve-environment-action.js index 13e75c665..8d3ba7856 100644 --- a/lib/resolve-environment-action.js +++ b/lib/resolve-environment-action.js @@ -104221,17 +104221,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs2.existsSync(path3.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, diff --git a/lib/setup-codeql-action.js b/lib/setup-codeql-action.js index 1656e0746..8c2dbdbd2 100644 --- a/lib/setup-codeql-action.js +++ b/lib/setup-codeql-action.js @@ -104087,17 +104087,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs3.existsSync(path3.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, diff --git a/lib/upload-lib.js b/lib/upload-lib.js index a797db060..3a41bf1f6 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -107377,17 +107377,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs3.existsSync(path4.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, diff --git a/lib/upload-sarif-action.js b/lib/upload-sarif-action.js index b2bac641e..6c97bdc38 100644 --- a/lib/upload-sarif-action.js +++ b/lib/upload-sarif-action.js @@ -107062,17 +107062,17 @@ var getGitRoot = async function(sourceRoot) { return void 0; } }; -async function hasSubmodules(basePath) { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === void 0) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.` - ); - } +function hasSubmodules(gitRoot) { return fs3.existsSync(path3.join(gitRoot, ".gitmodules")); } var getFileOidsUnderPath = async function(basePath) { - const args = await hasSubmodules(basePath) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; + const gitRoot = await getGitRoot(basePath); + if (gitRoot === void 0) { + throw new Error( + `Cannot determine the Git root from ${basePath}.` + ); + } + const args = hasSubmodules(gitRoot) ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand( basePath, args, diff --git a/src/config-utils.test.ts b/src/config-utils.test.ts index 59476118d..4a3b761e8 100644 --- a/src/config-utils.test.ts +++ b/src/config-utils.test.ts @@ -1099,7 +1099,7 @@ const checkOverlayEnablementMacro = test.macro({ } // Mock submodule detection - sinon.stub(gitUtils, "hasSubmodules").resolves(setup.hasSubmodules); + sinon.stub(gitUtils, "hasSubmodules").returns(setup.hasSubmodules); // Mock default branch detection sinon diff --git a/src/config-utils.ts b/src/config-utils.ts index 6d705bc31..55d5afa72 100644 --- a/src/config-utils.ts +++ b/src/config-utils.ts @@ -970,7 +970,8 @@ async function validateOverlayDatabaseMode( ); return new Failure(OverlayDisabledReason.IncompatibleCodeQl); } - if ((await getGitRoot(sourceRoot)) === undefined) { + const gitRoot = await getGitRoot(sourceRoot); + if (gitRoot === undefined) { logger.warning( `Cannot build an ${overlayDatabaseMode} database because ` + `the source root "${sourceRoot}" is not inside a git repository. ` + @@ -978,7 +979,7 @@ async function validateOverlayDatabaseMode( ); return new Failure(OverlayDisabledReason.NoGitRoot); } - if (await hasSubmodules(sourceRoot)) { + if (hasSubmodules(gitRoot)) { if (gitVersion === undefined) { logger.warning( `Cannot build an ${overlayDatabaseMode} database because ` + diff --git a/src/git-utils.ts b/src/git-utils.ts index 1d3d661fb..ec0478fa8 100644 --- a/src/git-utils.ts +++ b/src/git-utils.ts @@ -248,16 +248,12 @@ export const getGitRoot = async function ( }; /** - * Returns true if the Git repository containing `basePath` has submodules - * registered (i.e. a `.gitmodules` file exists at the repository root). + * Returns true if the Git repository has submodules registered (i.e. a + * `.gitmodules` file exists at the repository root). + * + * @param gitRoot The root of the Git repository. */ -export async function hasSubmodules(basePath: string): Promise { - const gitRoot = await getGitRoot(basePath); - if (gitRoot === undefined) { - throw new Error( - `Cannot determine whether the repository has submodules because the Git root could not be found from ${basePath}.`, - ); - } +export function hasSubmodules(gitRoot: string): boolean { return fs.existsSync(path.join(gitRoot, ".gitmodules")); } @@ -283,7 +279,10 @@ export const getFileOidsUnderPath = async function ( // We only pass --recurse-submodules when the repository actually has // submodules, because the combination of --recurse-submodules and --stage is // only supported since Git 2.36.0. - const args = (await hasSubmodules(basePath)) + const gitRoot = await getGitRoot(basePath); + const mayHaveSubmodules = + gitRoot === undefined ? true : hasSubmodules(gitRoot); + const args = mayHaveSubmodules ? ["ls-files", "--recurse-submodules", "--stage"] : ["ls-files", "--stage"]; const stdout = await runGitCommand(