Skip to content

fix(git-data): pass cwd to git diff subprocess for out-of-source runs #1599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/git-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export class GitData {
}
}

static changedFiles (defaultBranch: string) {
return Utils.syncSpawn(["git", "diff", "--name-only", defaultBranch]).stdout.split("\n");
static changedFiles (defaultBranch: string, cwd: string) {
return Utils.syncSpawn(["git", "diff", "--name-only", defaultBranch], cwd).stdout.split("\n");
}

private async initRemoteData (cwd: string, writeStreams: WriteStreams): Promise<void> {
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class Utils {
for (const rule of opt.rules) {
if (!Utils.evaluateRuleIf(rule.if, opt.variables)) continue;
if (!Utils.evaluateRuleExist(opt.cwd, rule.exists)) continue;
if (evaluateRuleChanges && !Utils.evaluateRuleChanges(gitData.branches.default, rule.changes)) continue;
if (evaluateRuleChanges && !Utils.evaluateRuleChanges(gitData.branches.default, rule.changes, opt.cwd)) continue;

when = rule.when ? rule.when : jobWhen;
allowFailure = rule.allow_failure ?? allowFailure;
Expand Down Expand Up @@ -337,7 +337,7 @@ export class Utils {
return false;
}

static evaluateRuleChanges (defaultBranch: string, ruleChanges: string[] | {paths: string[]} | undefined): boolean {
static evaluateRuleChanges (defaultBranch: string, ruleChanges: string[] | {paths: string[]} | undefined, cwd: string): boolean {
if (ruleChanges === undefined) return true;

// Normalize rules:changes:paths to rules:changes
Expand All @@ -346,7 +346,7 @@ export class Utils {
// NOTE: https://docs.gitlab.com/ee/ci/yaml/#ruleschanges
// Glob patterns are interpreted with Ruby's [File.fnmatch](https://docs.ruby-lang.org/en/master/File.html#method-c-fnmatch)
// with the flags File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB.
return micromatch.some(GitData.changedFiles(`origin/${defaultBranch}`), ruleChanges, {
return micromatch.some(GitData.changedFiles(`origin/${defaultBranch}`, cwd), ruleChanges, {
nonegate: true,
noextglob: true,
posix: false,
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe("evaluateRuleChanges", () => {
test.concurrent(`${t.description} \t\t [input: ${t.input} pattern: ${t.pattern} hasChanges: ${t.hasChanges}]`, () => {
const spy = import.meta.jest.spyOn(GitData, "changedFiles");
spy.mockReturnValue(t.input);
expect(Utils.evaluateRuleChanges("origin/master", t.pattern)).toBe(t.hasChanges);
expect(Utils.evaluateRuleChanges("origin/master", t.pattern, ".")).toBe(t.hasChanges);
});
});
});
Expand Down