All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Haokai Ding via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "René Scharfe" <l.s.r@web.de>,
	"Jacob Keller" <jacob.e.keller@intel.com>,
	"Haokai Ding" <hk_ding@outlook.com>,
	"Haokai Ding" <hk_ding@outlook.com>
Subject: [PATCH] diff --no-index: fix -R with file/directory conflicts
Date: Fri, 18 Sep 2026 07:19:06 +0000	[thread overview]
Message-ID: <pull.2232.git.1789715946888.gitgitgadget@gmail.com> (raw)

From: Haokai Ding <hk_ding@outlook.com>

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 <hk_ding@outlook.com>
---
    diff --no-index: fix -R with file/directory conflicts

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2232%2FHaokaiDing%2Ffix%2Fno-index-reverse-file-directory-mail-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2232/HaokaiDing/fix/no-index-reverse-file-directory-mail-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2232

 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 f320424f05..a9151cc27a 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 8e0394cf5a..c3c703f4f4 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 \

base-commit: d38352cd43ab9745686d697872408bc3249a153f
-- 
gitgitgadget

             reply	other threads:[~2026-09-18  7:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-18  7:19 Haokai Ding via GitGitGadget [this message]
2026-09-20 15:23 ` [PATCH] diff --no-index: fix -R with file/directory conflicts René Scharfe
2026-09-20 23:04 ` Jacob Keller
2026-09-21 18:01   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.2232.git.1789715946888.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=hk_ding@outlook.com \
    --cc=jacob.e.keller@intel.com \
    --cc=l.s.r@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.