Skip to content

Commit 285cfe6

Browse files
authored
fix(git-data): pass cwd to git diff subprocess for out-of-source runs (#1599)
1 parent c43f4cf commit 285cfe6

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/git-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ export class GitData {
8080
}
8181
}
8282

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

8787
private async initRemoteData (cwd: string, writeStreams: WriteStreams): Promise<void> {

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export class Utils {
199199
for (const rule of opt.rules) {
200200
if (!Utils.evaluateRuleIf(rule.if, opt.variables)) continue;
201201
if (!Utils.evaluateRuleExist(opt.cwd, rule.exists)) continue;
202-
if (evaluateRuleChanges && !Utils.evaluateRuleChanges(gitData.branches.default, rule.changes)) continue;
202+
if (evaluateRuleChanges && !Utils.evaluateRuleChanges(gitData.branches.default, rule.changes, opt.cwd)) continue;
203203

204204
when = rule.when ? rule.when : jobWhen;
205205
allowFailure = rule.allow_failure ?? allowFailure;
@@ -337,7 +337,7 @@ export class Utils {
337337
return false;
338338
}
339339

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

343343
// Normalize rules:changes:paths to rules:changes
@@ -346,7 +346,7 @@ export class Utils {
346346
// NOTE: https://docs.gitlab.com/ee/ci/yaml/#ruleschanges
347347
// Glob patterns are interpreted with Ruby's [File.fnmatch](https://docs.ruby-lang.org/en/master/File.html#method-c-fnmatch)
348348
// with the flags File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB.
349-
return micromatch.some(GitData.changedFiles(`origin/${defaultBranch}`), ruleChanges, {
349+
return micromatch.some(GitData.changedFiles(`origin/${defaultBranch}`, cwd), ruleChanges, {
350350
nonegate: true,
351351
noextglob: true,
352352
posix: false,

tests/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe("evaluateRuleChanges", () => {
178178
test.concurrent(`${t.description} \t\t [input: ${t.input} pattern: ${t.pattern} hasChanges: ${t.hasChanges}]`, () => {
179179
const spy = import.meta.jest.spyOn(GitData, "changedFiles");
180180
spy.mockReturnValue(t.input);
181-
expect(Utils.evaluateRuleChanges("origin/master", t.pattern)).toBe(t.hasChanges);
181+
expect(Utils.evaluateRuleChanges("origin/master", t.pattern, ".")).toBe(t.hasChanges);
182182
});
183183
});
184184
});

0 commit comments

Comments
 (0)