From 45aa625f20e258763f9a7567e2192d1880037e02 Mon Sep 17 00:00:00 2001 From: Haokai Ding Date: Fri, 18 Sep 2026 01:25:29 +0400 Subject: [PATCH] diff --no-index: fix -R with file/directory conflicts When a path is a file on one side and a directory on the other, queue_diff() queues the file separately before recursing into the directory. This early queue entry bypasses the reverse_diff handling used for ordinary files. As a result, comparing directories d and e where d/sub is a file and e/sub/file is another file reports both paths as deleted with -R. Reversing the operands reports both paths as added instead. Swap the filespecs of the early queue entry when reverse_diff is set, so that -R reverses the file change as well as the directory contents. Add regression tests for both directions. Signed-off-by: Haokai Ding --- diff-no-index.c | 2 ++ t/t4053-diff-no-index.sh | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/diff-no-index.c b/diff-no-index.c index f320424f05fe0f..a9151cc27a0448 100644 --- a/diff-no-index.c +++ b/diff-no-index.c @@ -188,6 +188,8 @@ static int queue_diff(struct diff_options *o, const struct git_hash_algo *algop, mode1 = 0; } /* emit that file */ + if (o->flags.reverse_diff) + SWAP(d1, d2); diff_queue(&diff_queued_diff, d1, d2); /* and then let the entire directory be created or deleted */ diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh index 8e0394cf5afd95..c3c703f4f4eb0e 100755 --- a/t/t4053-diff-no-index.sh +++ b/t/t4053-diff-no-index.sh @@ -120,6 +120,24 @@ test_expect_success 'turning a file into a directory' ' ) ' +test_expect_success 'reverse diff when turning a file into a directory' ' + ( + cd non/git && + printf "A\td/sub\nD\te/sub/file\n" >expect && + test_expect_code 1 git diff --no-index -R --name-status d e >actual && + test_cmp expect actual + ) +' + +test_expect_success 'reverse diff when turning a directory into a file' ' + ( + cd non/git && + printf "D\td/sub\nA\te/sub/file\n" >expect && + test_expect_code 1 git diff --no-index -R --name-status e d >actual && + test_cmp expect actual + ) +' + test_expect_success 'diff from repo subdir shows real paths (explicit)' ' echo "diff --git a/../../non/git/a b/../../non/git/b" >expect && test_expect_code 1 \