Skip to content

Commit 18cdf80

Browse files
committed
Teach "apply --index-info" to handle rename patches
With v1.5.3.2~14 (apply --index-info: fall back to current index for mode changes, 2007-09-17), git apply learned to stop worrying about the lack of diff index line when a file already present in the current index had no content change. But it still worries too much: for rename patches, it is checking that both the old and new filename are present in the current index. This makes no sense, since a file rename generally involves creating a file there was none before. So just check the old filename. Noticed while trying to use “git rebase” with diff.renames = copies. [jn: add tests] Reported-by: David D. Kilzer <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d27485 commit 18cdf80

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

builtin/apply.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,8 +2979,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
29792979
else if (get_sha1(patch->old_sha1_prefix, sha1))
29802980
/* git diff has no index line for mode/type changes */
29812981
if (!patch->lines_added && !patch->lines_deleted) {
2982-
if (get_current_sha1(patch->new_name, sha1) ||
2983-
get_current_sha1(patch->old_name, sha1))
2982+
if (get_current_sha1(patch->old_name, sha1))
29842983
die("mode change for %s, which is not "
29852984
"in current HEAD", name);
29862985
sha1_ptr = sha1;

t/t4150-am.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ test_expect_success setup '
116116
git commit -m "added another file" &&
117117
118118
git format-patch --stdout master >lorem-move.patch &&
119+
120+
git checkout -b rename &&
121+
git mv file renamed &&
122+
git commit -m "renamed a file" &&
123+
124+
git format-patch -M --stdout lorem >rename.patch &&
125+
126+
git reset --soft lorem^ &&
127+
git commit -m "renamed a file and added another" &&
128+
129+
git format-patch -M --stdout lorem^ >rename-add.patch &&
130+
119131
# reset time
120132
unset test_tick &&
121133
test_tick
@@ -246,8 +258,42 @@ test_expect_success 'am -3 falls back to 3-way merge' '
246258
git diff --exit-code lorem
247259
'
248260

261+
test_expect_success 'am can rename a file' '
262+
grep "^rename from" rename.patch &&
263+
rm -fr .git/rebase-apply &&
264+
git reset --hard &&
265+
git checkout lorem^0 &&
266+
git am rename.patch &&
267+
! test -d .git/rebase-apply &&
268+
git update-index --refresh &&
269+
git diff --exit-code rename
270+
'
271+
272+
test_expect_success 'am -3 can rename a file' '
273+
grep "^rename from" rename.patch &&
274+
rm -fr .git/rebase-apply &&
275+
git reset --hard &&
276+
git checkout lorem^0 &&
277+
git am -3 rename.patch &&
278+
! test -d .git/rebase-apply &&
279+
git update-index --refresh &&
280+
git diff --exit-code rename
281+
'
282+
283+
test_expect_success 'am -3 can rename a file after falling back to 3-way merge' '
284+
grep "^rename from" rename-add.patch &&
285+
rm -fr .git/rebase-apply &&
286+
git reset --hard &&
287+
git checkout lorem^0 &&
288+
git am -3 rename-add.patch &&
289+
! test -d .git/rebase-apply &&
290+
git update-index --refresh &&
291+
git diff --exit-code rename
292+
'
293+
249294
test_expect_success 'am -3 -q is quiet' '
250295
rm -fr .git/rebase-apply &&
296+
git checkout -f lorem2 &&
251297
git reset master2 --hard &&
252298
sed -n -e "3,\$p" msg >file &&
253299
head -n 9 msg >>file &&

0 commit comments

Comments
 (0)