Skip to content

Commit f36739d

Browse files
committed
Overlay databases: use --overlay-file-list
This commit changes overlay database creation to use the --overlay-file-list flag. It also implements Git-based file change detection to generate the list of files to extract for the overlay database.
1 parent 7254660 commit f36739d

File tree

9 files changed

+339
-11
lines changed

9 files changed

+339
-11
lines changed

lib/codeql.js

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/git-utils.js

Lines changed: 35 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/git-utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/overlay-database-utils.js

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/overlay-database-utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/codeql.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import {
2424
import { isAnalyzingDefaultBranch } from "./git-utils";
2525
import { Language } from "./languages";
2626
import { Logger } from "./logging";
27-
import { OverlayDatabaseMode } from "./overlay-database-utils";
27+
import {
28+
OverlayDatabaseMode,
29+
writeBaseDatabaseOidsFile,
30+
writeOverlayChangesFile,
31+
} from "./overlay-database-utils";
2832
import * as setupCodeql from "./setup-codeql";
2933
import { ZstdAvailability } from "./tar";
3034
import { ToolsDownloadStatusReport } from "./tools-download";
@@ -610,7 +614,12 @@ export async function getCodeQLForCmd(
610614
: "--overwrite";
611615

612616
if (overlayDatabaseMode === OverlayDatabaseMode.Overlay) {
613-
extraArgs.push("--overlay");
617+
const overlayChangesFile = await writeOverlayChangesFile(
618+
config,
619+
sourceRoot,
620+
logger,
621+
);
622+
extraArgs.push(`--overlay-file-list=${overlayChangesFile}`);
614623
} else if (overlayDatabaseMode === OverlayDatabaseMode.OverlayBase) {
615624
extraArgs.push("--overlay-base");
616625
}
@@ -636,6 +645,10 @@ export async function getCodeQLForCmd(
636645
],
637646
{ stdin: externalRepositoryToken },
638647
);
648+
649+
if (overlayDatabaseMode === OverlayDatabaseMode.OverlayBase) {
650+
await writeBaseDatabaseOidsFile(config, sourceRoot);
651+
}
639652
},
640653
async runAutobuild(config: Config, language: Language) {
641654
applyAutobuildAzurePipelinesTimeoutFix();

src/git-utils.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { ConfigurationError, getRequiredEnvParam } from "./util";
1111

1212
async function runGitCommand(
13-
checkoutPath: string | undefined,
13+
workingDirectory: string | undefined,
1414
args: string[],
1515
customErrorMessage: string,
1616
): Promise<string> {
@@ -28,7 +28,7 @@ async function runGitCommand(
2828
stderr += data.toString();
2929
},
3030
},
31-
cwd: checkoutPath,
31+
cwd: workingDirectory,
3232
}).exec();
3333
return stdout;
3434
} catch (error) {
@@ -322,6 +322,44 @@ export const getGitRoot = async function (
322322
}
323323
};
324324

325+
/**
326+
* Returns the Git OIDs at `HEAD` of all tracked files that are the given base
327+
* path. Untracked files and files not under the given base path are ignored.
328+
*
329+
* @param basePath A path into the Git repository.
330+
* @returns a map from file paths (relative to `basePath`) to Git OIDs.
331+
* @throws {Error} if "git ls-tree" produces unexpected output.
332+
*/
333+
export const getFileOidsUnderPath = async function (
334+
basePath: string,
335+
): Promise<{ [key: string]: string }> {
336+
// Without the --full-name flag, the path is relative to the current working
337+
// directory of the git command, which is basePath.
338+
// Without the --full-tree flag, the file listing is confined to the current
339+
// working directory of the git command, which is basePath.
340+
const stdout = await runGitCommand(
341+
basePath,
342+
["ls-tree", "--format=%(objectname)_%(path)", "-r", "HEAD"],
343+
"Cannot list file OIDs in HEAD.",
344+
);
345+
346+
const fileOidMap: { [key: string]: string } = {};
347+
const regex = /^([0-9a-f]{40})_(.+)$/;
348+
for (const line of stdout.split("\n")) {
349+
if (line) {
350+
const match = line.match(regex);
351+
if (match) {
352+
const oid = match[1];
353+
const path = decodeGitFilePath(match[2]);
354+
fileOidMap[path] = oid;
355+
} else {
356+
throw new Error(`Unexpected "git ls-tree" output: ${line}`);
357+
}
358+
}
359+
}
360+
return fileOidMap;
361+
};
362+
325363
function getRefFromEnv(): string {
326364
// To workaround a limitation of Actions dynamic workflows not setting
327365
// the GITHUB_REF in some cases, we accept also the ref within the

0 commit comments

Comments
 (0)