Skip to content

Commit be4dbbb

Browse files
pks-tgitster
authored andcommitted
pathspec: honor PATHSPEC_PREFIX_ORIGIN with empty prefix
Previous to commit 5d8f084 (pathspec: simpler logic to prefix original pathspec elements, 2017-01-04), we were always using the computed `match` variable to perform pathspec matching whenever `PATHSPEC_PREFIX_ORIGIN` is set. This is for example useful when passing the parsed pathspecs to other commands, as the computed `match` may contain a pathspec relative to the repository root. The commit changed this logic to only do so when we do have an actual prefix and when literal pathspecs are deactivated. But this change may actually break some commands which expect passed pathspecs to be relative to the repository root. One such case is `git add --patch`, which now fails when using relative paths from a subdirectory. For example if executing "git add -p ../foo.c" in a subdirectory, the `git-add--interactive` command will directly pass "../foo.c" to `git-ls-files`. As ls-files is executed at the repository's root, the command will notice that "../foo.c" is outside the repository and fail. Fix the issue by again using the computed `match` variable when `PATHSPEC_PREFIX_ORIGIN` is set and global literal pathspecs are deactivated. Note that in contrast to previous behavior, we will now always call `prefix_magic` regardless of whether a prefix is actually set. But this is the right thing to do: when the `match` variable has been resolved to the repository's root, it will be set to an empty string. When passing the empty string directly to other commands, it will result in a warning regarding deprecated empty pathspecs. By always adding the prefix magic, we will end up with at least the string ":(prefix:0)" and thus avoid the warning. Signed-off-by: Patrick Steinhardt <[email protected]> Acked-by: Brandon Williams <[email protected]> Reviewed-by: Duy Nguyen <[email protected]>
1 parent 153e0d7 commit be4dbbb

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pathspec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
505505
* original. Useful for passing to another command.
506506
*/
507507
if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
508-
prefixlen && !get_literal_global()) {
508+
!get_literal_global()) {
509509
struct strbuf sb = STRBUF_INIT;
510510

511511
/* Preserve the actual prefix length of each pattern */

t/t3701-add-interactive.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,28 @@ test_expect_success 'add -p handles globs' '
436436
test_cmp expect actual
437437
'
438438

439+
test_expect_success 'add -p handles relative paths' '
440+
git reset --hard &&
441+
442+
echo base >relpath.c &&
443+
git add "*.c" &&
444+
git commit -m relpath &&
445+
446+
echo change >relpath.c &&
447+
mkdir -p subdir &&
448+
git -C subdir add -p .. 2>error <<-\EOF &&
449+
y
450+
EOF
451+
452+
test_must_be_empty error &&
453+
454+
cat >expect <<-\EOF &&
455+
relpath.c
456+
EOF
457+
git diff --cached --name-only >actual &&
458+
test_cmp expect actual
459+
'
460+
439461
test_expect_success 'add -p does not expand argument lists' '
440462
git reset --hard &&
441463

0 commit comments

Comments
 (0)