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 <ddkilzer@kilzer.net> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Nov 10, 2008 at 15:49 UTC
18cdf802ca6e2772b5901f786f59de3843299b1c
2 files changed
+47
-2
builtin/apply.c
+1
-2
@@ -2979,8 +2979,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
2979
else if (get_sha1(patch->old_sha1_prefix, sha1))
2980
/* git diff has no index line for mode/type changes */
2981
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))
2983
die("mode change for %s, which is not "
2984
"in current HEAD", name);
2985
sha1_ptr = sha1;
t/t4150-am.sh
+46
@@ -116,6 +116,18 @@ test_expect_success setup '
116
git commit -m "added another file" &&
117
118
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
+
131
# reset time
132
unset test_tick &&
133
test_tick
@@ -246,8 +258,42 @@ test_expect_success 'am -3 falls back to 3-way merge' '
258
git diff --exit-code lorem
259
'
260
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
+
294
test_expect_success 'am -3 -q is quiet' '
295
rm -fr .git/rebase-apply &&
296
+ git checkout -f lorem2 &&
297
git reset master2 --hard &&
298
sed -n -e "3,\$p" msg >file &&
299
head -n 9 msg >>file &&