Skip to content

Commit 931a930

Browse files
authored
Merge branch 'master-test' into docker-registry
2 parents 55765a4 + dd464ef commit 931a930

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
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/job.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ export class Job {
15551555
if (include["local"]) {
15561556
const expandedInclude = Utils.expandText(include["local"], this._variables);
15571557
validateIncludeLocal(expandedInclude);
1558-
const files = resolveIncludeLocal(expandedInclude, cwd);
1558+
const files = await resolveIncludeLocal(expandedInclude, cwd);
15591559
if (files.length == 0) {
15601560
throw new AssertionError({message: `Local include file \`${include["local"]}\` specified in \`.${this.name}\` cannot be found!`});
15611561
}

src/parser-includes.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import assert, {AssertionError} from "assert";
77
import chalk from "chalk";
88
import {Parser} from "./parser.js";
99
import axios from "axios";
10-
import globby from "globby";
1110
import path from "path";
1211
import semver from "semver";
1312
import {RE2JS} from "re2js";
@@ -93,7 +92,7 @@ export class ParserIncludes {
9392
}
9493
if (value["local"]) {
9594
validateIncludeLocal(value["local"]);
96-
const files = resolveIncludeLocal(value["local"], cwd);
95+
const files = await resolveIncludeLocal(value["local"], cwd);
9796
if (files.length == 0) {
9897
throw new AssertionError({message: `Local include file cannot be found ${value["local"]}`});
9998
}
@@ -327,11 +326,11 @@ export class ParserIncludes {
327326

328327
static readonly memoLocalRepoFiles = (() => {
329328
const cache = new Map<string, string[]>();
330-
return (path: string) => {
329+
return async (path: string) => {
331330
let result = cache.get(path);
332331
if (typeof result !== "undefined") return result;
333332

334-
result = globby.sync(path, {dot: true, gitignore: true});
333+
result = (await Utils.getTrackedFiles(path)).map(p => `${path}/${p}`);
335334
cache.set(path, result);
336335
return result;
337336
};
@@ -361,8 +360,8 @@ export function resolveSemanticVersionRange (range: string, gitTags: string[]) {
361360
return found;
362361
}
363362

364-
export function resolveIncludeLocal (pattern: string, cwd: string) {
365-
const repoFiles = ParserIncludes.memoLocalRepoFiles(cwd);
363+
export async function resolveIncludeLocal (pattern: string, cwd: string) {
364+
const repoFiles = await ParserIncludes.memoLocalRepoFiles(cwd);
366365

367366
if (!pattern.startsWith("/")) pattern = `/${pattern}`; // Ensure pattern starts with `/`
368367
pattern = `${cwd}${pattern}`;

src/utils.ts

Lines changed: 10 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,
@@ -510,5 +510,12 @@ export class Utils {
510510

511511
static async stopDockerRegistry (containerExecutable: string): Promise<void> {
512512
await Utils.spawn([containerExecutable, "rm", "-f", this.gclRegistryPrefix]);
513+
514+
static async getTrackedFiles (cwd: string): Promise<string[]> {
515+
const lsFilesRes = await Utils.bash("git ls-files --deduplicate", cwd);
516+
if (lsFilesRes.exitCode != 0) {
517+
throw new Error(`Failed to list tracked files in ${cwd}: ${lsFilesRes.stderr}`);
518+
}
519+
return lsFilesRes.stdout.split("\n");
513520
}
514521
}

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)