From: blindmansion <blindmansion@gmail.com>
To: git@vger.kernel.org
Cc: blindmansion <blindmansion@gmail.com>
Subject: [PATCH] read-cache: disable renames in add_files_to_cache
Date: Wed, 1 Apr 2026 13:05:02 -0400 [thread overview]
Message-ID: <20260401170502.35877-1-blindmansion@gmail.com> (raw)
In-Reply-To: <CAF6hZH5TyFBm5H_fcVyaf1aw-mPsVbAmNvkUGCMoQYYCX1+HRQ@mail.gmail.com>
add_files_to_cache() refreshes the index from worktree changes and does
not need rename detection. When unmerged entries and a deleted stage-0
path are present together, rename detection can pair them and rewrite an
unmerged diff pair to point at the deleted path.
That later makes "git commit -a" and "git add -u" try to stat the
deleted path and die with "unable to stat". Disable rename detection in
this callback-driven staging path and add a regression test covering the
crash.
---
read-cache.c | 1 +
t/t2200-add-update.sh | 39 +++++++++++++++++++++++++++++++++++++++
2 files changed, 40 insertions(+)
diff --git a/read-cache.c b/read-cache.c
index 5049f9b..d938abc 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -4049,6 +4049,7 @@ int add_files_to_cache(struct repository *repo, const char *prefix,
rev.diffopt.format_callback = update_callback;
rev.diffopt.format_callback_data = &data;
rev.diffopt.flags.override_submodule_config = 1;
+ rev.diffopt.detect_rename = 0; /* staging worktree changes does not need renames */
rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
/*
diff --git a/t/t2200-add-update.sh b/t/t2200-add-update.sh
index 06e83d3..56c7e55 100755
--- a/t/t2200-add-update.sh
+++ b/t/t2200-add-update.sh
@@ -200,6 +200,45 @@ test_expect_success 'add -u resolves unmerged paths' '
test_cmp expect actual
'
+test_expect_success 'add -u avoids rename pairing on unmerged paths' '
+ test_create_repo rename-crash &&
+ (
+ cd rename-crash &&
+ test_seq 1 100 |
+ sed "s/.*/line &: shared content that is identical across both files/" >conflict.txt &&
+ cp conflict.txt bystander.txt &&
+ git add conflict.txt bystander.txt &&
+ git commit -m "initial: two files with identical content" &&
+ main_branch=$(git symbolic-ref --short HEAD) &&
+ git checkout -b feature &&
+ perl -pe '\''s/^line 50:.*/line 50: FEATURE BRANCH CHANGE/'\'' \
+ conflict.txt >conflict.txt.tmp &&
+ mv conflict.txt.tmp conflict.txt &&
+ git add conflict.txt &&
+ git commit -m "feature: modify line 50" &&
+ git checkout "$main_branch" &&
+ perl -pe '\''s/^line 50:.*/line 50: MAIN BRANCH CHANGE/'\'' \
+ conflict.txt >conflict.txt.tmp &&
+ mv conflict.txt.tmp conflict.txt &&
+ git add conflict.txt &&
+ git commit -m "main: modify line 50 differently" &&
+ test_must_fail git merge feature &&
+ rm bystander.txt &&
+ git add -u >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err &&
+ git ls-files -u >actual &&
+ test_must_be_empty actual &&
+ git ls-files bystander.txt >actual &&
+ test_must_be_empty actual &&
+ echo conflict.txt >expect &&
+ git ls-files conflict.txt >actual &&
+ test_cmp expect actual &&
+ git diff-files --name-only >actual &&
+ test_must_be_empty actual
+ )
+'
+
test_expect_success '"add -u non-existent" should fail' '
test_must_fail git add -u non-existent &&
git ls-files >actual &&
--
2.53.0
next prev parent reply other threads:[~2026-04-01 17:12 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-31 21:53 BUG: git commit -a crashes with "unable to stat" during unresolved merge Nick Golden
2026-04-01 17:05 ` blindmansion [this message]
2026-04-01 18:16 ` [PATCH] read-cache: disable renames in add_files_to_cache Junio C Hamano
2026-04-01 19:00 ` [PATCH v2] " Nick Golden
2026-04-01 19:08 ` Blind Mansion
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=20260401170502.35877-1-blindmansion@gmail.com \
--to=blindmansion@gmail.com \
--cc=git@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox