fetchpatch: add support for patches to files with apostrophes

We're feeding a list of files from `lsdiff` to `filterdiff` via `xargs`.
That list is newline separated, and looks something like this:

```
$ lsdiff <(curl -s 1e86a219f5.patch)
a/README.md
b/files/The Answer to the Ultimate Question of Life, The Universe, and Everything.txt
```

However, if the list contains files with apostrophes in it, xargs freaks
out:

```
$ echo "there's an apostrophe here" | xargs -I{} python -c "import sys; print(sys.argv)" {}
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option
```

The fix is simple, just explicitly specify a delimiter:

```
$ echo "there's an apostrophe here" | xargs -I{} --delimiter='\n' python -c "import sys; print(sys.argv)" {}
['-c', "there's an apostrophe here"]
```

I added 2 tests here:

- `nix-build -A pkgs.tests.fetchpatch.fileWithApostrophe` fails without the code change.
- `nix-build -A pkgs.tests.fetchpatch.fileWithSpace` passes both before
  and after this change, but I wanted to add it to prove that I didn't
  break anything.
This commit is contained in:
Jeremy Fleischman 2025-05-23 14:15:40 -07:00
parent acf27356b9
commit 1d32087125
No known key found for this signature in database
2 changed files with 19 additions and 1 deletions

View file

@ -67,7 +67,7 @@ lib.throwIfNot (excludes == [ ] || includes == [ ])
${lib.optionalString (relative != null) "-p1 -i ${lib.escapeShellArg relative}/'*'"} \
"$out" \
| sort -u | sed -e 's/[*?]/\\&/g' \
| xargs -I{} \
| xargs -I{} --delimiter='\n' \
filterdiff \
--include={} \
--strip=${toString stripLen} \

View file

@ -48,4 +48,22 @@ in
else
"sha256-SJHk8XrutqAyoIdORlhCpBCN626P+uzed7mjKz5eQYY=";
};
fileWithSpace = testers.invalidateFetcherByDrvHash fetchpatch {
url = "https://github.com/jfly/annoying-filenames/commit/1e86a219f5fc9c4137b409bc9c38036f3922724b.patch";
sha256 =
if isFetchpatch2 then
"sha256-RB6pjigoXtzHILkGFXYd3Lz2aM9DvO0NRmLdey1N6gg="
else
"sha256-aptUvVojqIIIVNuHqkl+C+dZBGFfs+1MUd0FNV+4j4E=";
};
fileWithApostrophe = testers.invalidateFetcherByDrvHash fetchpatch {
url = "https://github.com/jfly/annoying-filenames/commit/8b6d8f8d7094ce646523b3369cfdf5030289c66c.patch";
sha256 =
if isFetchpatch2 then
"sha256-CrQFmVvLEvWpo2ucVrWyLb5qk2GVOxyUbFN3hp9sV68="
else
"sha256-CrQFmVvLEvWpo2ucVrWyLb5qk2GVOxyUbFN3hp9sV68=";
};
}