Skip to content

Commit

Permalink
Swap diff tool for git
Browse files Browse the repository at this point in the history
go-gitdiff which aspect CLI uses cannot support patches generated by
BSD diff which is the default on MacOS (see tagged issue).

I am investigating using ape but it requires Bazel 7.4.1 which may be
a breaking change.

Alternatively, this changes diff -> git with a few extra "hacks".
Namely, we need to replace the labels to be the same file and resolve
the symlinks otherwise `git diff` thinks the file was deleted.

fixes aspect-build#487
  • Loading branch information
fzakaria committed Mar 5, 2025
1 parent 15f3584 commit 0326eac
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions lint/private/patcher.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ function debug(...kwargs) {
}
}

async function realPath(p) {
try {
const target = await fs.promises.readlink(p);
return path.resolve(path.dirname(p), target);
} catch (e) {
debug(e);
// if readlink fails, returns p
return p;
}
}

// assumes there are no recursive symlinks
async function sync(src, dst, subdir, filesToDiff) {
const files = (await fs.promises.readdir(path.join(src, subdir))).map((f) =>
Expand Down Expand Up @@ -87,7 +98,12 @@ async function main(args, sandbox) {
for (const f of config.files_to_diff) {
const origF = path.join(process.cwd(), sourcePrefix, f);
const newF = path.join(sandbox, sourcePrefix, f);
debug(`diffing ${origF} to ${newF}`);
// NB: git diff does't work with symlinks

const resolvedOrigF = await realPath(origF);
const resolvedNewF = await realPath(newF);

debug(`diffing ${resolvedOrigF} to ${resolvedNewF}`);
// TODO: don't rely on the system diff, it may not be installed i.e. on a minimal CI machine image.
// Likely replacement:
// https://github.com/google/diff-match-patch/wiki/Language:-JavaScript
Expand All @@ -96,14 +112,24 @@ async function main(args, sandbox) {
// be packaged up.
// NB: use a/ and b/ prefixes, intended so the result is applied with 'patch -p1'
const results = childProcess.spawnSync(
"diff",
[`--label=a/${f}`, `--label=b/${f}`, "--unified", origF, newF],
"git",
["diff", "--no-index", resolvedOrigF, resolvedNewF ],
{
encoding: "utf8",
}
);
debug(results.stdout);
diffOut.write(results.stdout);

let stdout = results.stdout;
// NB. Strip the lines like:
// diff --git asrc/Bar.java bsrc/Bar.java
// index db05cb3..e387b3a 100644
const stdoutLines = stdout.split("\n");
stdout = stdoutLines.slice(2).join("\n");
stdout = stdout.replace(resolvedOrigF, `/${f}`);
stdout = stdout.replace(resolvedNewF, `/${f}`);

diffOut.write(stdout);
if (results.error) {
console.error(results.error);
}
Expand Down

0 comments on commit 0326eac

Please sign in to comment.