Skip to content
Open
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
11 changes: 9 additions & 2 deletions packages/plugin-patch/sources/patchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,15 @@ export async function diffFolders(folderA: PortablePath, folderB: PortablePath)

// we cannot rely on exit code, because --no-index implies --exit-code
// i.e. git diff will exit with 1 if there were differences
if (stderr.length > 0)
throw new Error(`Unable to diff directories. Make sure you have a recent version of 'git' available in PATH.\nThe following error was reported by 'git':\n${stderr}`);
// git also writes warnings to stderr on success (empty HOME can make it
// complain about /.config/git/attributes), so ignore those lines.
const gitErrors = stderr
.split(/\r?\n/)
.filter(line => line.length > 0 && !/^warning:/i.test(line))
.join(`\n`);

if (gitErrors.length > 0)
throw new Error(`Unable to diff directories. Make sure you have a recent version of 'git' available in PATH.\nThe following error was reported by 'git':\n${gitErrors}`);


const normalizePath = folderAN.startsWith(`/`)
Expand Down
19 changes: 19 additions & 0 deletions packages/plugin-patch/tests/diffFolders.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {execUtils} from '@yarnpkg/core';
import {npath, NodeFS} from '@yarnpkg/fslib';

import {diffFolders} from '../sources/patchUtils';
Expand All @@ -18,4 +19,22 @@ describe(`diffFolders`, () => {
expect(parsePatchFile(diff)).toMatchSnapshot();
});
}

it(`ignores git warnings on stderr`, async () => {
const spy = jest.spyOn(execUtils, `execvp`).mockResolvedValue({
code: 1,
stdout: `diff --git a/file.txt b/file.txt\n`,
stderr: `warning: unable to access '/.config/git/attributes': Permission denied\n`,
} as any);

try {
const diff = await diffFolders(
npath.toPortablePath(npath.join(fixtures, `update`, `a`)),
npath.toPortablePath(npath.join(fixtures, `update`, `b`)),
);
expect(diff).toContain(`diff --git`);
} finally {
spy.mockRestore();
}
});
});
Loading