Skip to content

Commit 1512f40

Browse files
committed
Add function to query git for all generated files
1 parent 0b43179 commit 1512f40

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/git-utils.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from "fs";
2+
import * as os from "os";
23
import * as path from "path";
34

45
import * as core from "@actions/core";
@@ -392,3 +393,41 @@ test("getFileOidsUnderPath throws on unexpected output format", async (t) => {
392393
runGitCommandStub.restore();
393394
}
394395
});
396+
397+
test("listFiles returns array of file paths", async (t) => {
398+
sinon
399+
.stub(gitUtils, "runGitCommand")
400+
.resolves(["dir/file.txt", "README.txt"].join(os.EOL));
401+
402+
await t.notThrowsAsync(async () => {
403+
const result = await gitUtils.listFiles("/some/path");
404+
t.is(result.length, 2);
405+
t.is(result[0], "dir/file.txt");
406+
});
407+
});
408+
409+
test("getGeneratedFiles returns generated files only", async (t) => {
410+
const runGitCommandStub = sinon.stub(gitUtils, "runGitCommand");
411+
412+
runGitCommandStub
413+
.onFirstCall()
414+
.resolves(["dir/file.txt", "test.json", "README.txt"].join(os.EOL));
415+
runGitCommandStub
416+
.onSecondCall()
417+
.resolves(
418+
[
419+
"dir/file.txt: linguist-generated: unspecified",
420+
"test.json: linguist-generated: true",
421+
"README.txt: linguist-generated: false",
422+
].join(os.EOL),
423+
);
424+
425+
await t.notThrowsAsync(async () => {
426+
const result = await gitUtils.getGeneratedFiles("/some/path");
427+
428+
t.assert(runGitCommandStub.calledTwice);
429+
430+
t.is(result.length, 1);
431+
t.is(result[0], "test.json");
432+
});
433+
});

src/git-utils.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as os from "os";
2+
13
import * as core from "@actions/core";
24
import * as toolrunner from "@actions/exec/lib/toolrunner";
35
import * as io from "@actions/io";
@@ -408,3 +410,34 @@ export async function isAnalyzingDefaultBranch(): Promise<boolean> {
408410

409411
return currentRef === defaultBranch;
410412
}
413+
414+
export async function listFiles(workingDirectory: string): Promise<string[]> {
415+
const stdout = await runGitCommand(
416+
workingDirectory,
417+
["ls-files"],
418+
"Unable to list tracked files.",
419+
);
420+
return stdout.split(os.EOL);
421+
}
422+
423+
export async function getGeneratedFiles(
424+
workingDirectory: string,
425+
): Promise<string[]> {
426+
const files = await listFiles(workingDirectory);
427+
const stdout = await runGitCommand(
428+
workingDirectory,
429+
["check-attr", "linguist-generated", "--", ...files],
430+
"Unable to check attributes of files.",
431+
);
432+
433+
const generatedFiles: string[] = [];
434+
const regex = /^([^:]+): linguist-generated: true$/;
435+
for (const result of stdout.split(os.EOL)) {
436+
const match = result.match(regex);
437+
if (match) {
438+
generatedFiles.push(match[1]);
439+
}
440+
}
441+
442+
return generatedFiles;
443+
}

0 commit comments

Comments
 (0)