Skip to content

Commit

Permalink
Teach "apply --index-info" to handle rename patches
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Jul 23, 2010
1 parent 5d27485 commit 18cdf80
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
3 changes: 1 addition & 2 deletions builtin/apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2979,8 +2979,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
else if (get_sha1(patch->old_sha1_prefix, sha1))
/* git diff has no index line for mode/type changes */
if (!patch->lines_added && !patch->lines_deleted) {
if (get_current_sha1(patch->new_name, sha1) ||
get_current_sha1(patch->old_name, sha1))
if (get_current_sha1(patch->old_name, sha1))
die("mode change for %s, which is not "
"in current HEAD", name);
sha1_ptr = sha1;
Expand Down
46 changes: 46 additions & 0 deletions t/t4150-am.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ test_expect_success setup '
git commit -m "added another file" &&
git format-patch --stdout master >lorem-move.patch &&
git checkout -b rename &&
git mv file renamed &&
git commit -m "renamed a file" &&
git format-patch -M --stdout lorem >rename.patch &&
git reset --soft lorem^ &&
git commit -m "renamed a file and added another" &&
git format-patch -M --stdout lorem^ >rename-add.patch &&
# reset time
unset test_tick &&
test_tick
Expand Down Expand Up @@ -246,8 +258,42 @@ test_expect_success 'am -3 falls back to 3-way merge' '
git diff --exit-code lorem
'

test_expect_success 'am can rename a file' '
grep "^rename from" rename.patch &&
rm -fr .git/rebase-apply &&
git reset --hard &&
git checkout lorem^0 &&
git am rename.patch &&
! test -d .git/rebase-apply &&
git update-index --refresh &&
git diff --exit-code rename
'

test_expect_success 'am -3 can rename a file' '
grep "^rename from" rename.patch &&
rm -fr .git/rebase-apply &&
git reset --hard &&
git checkout lorem^0 &&
git am -3 rename.patch &&
! test -d .git/rebase-apply &&
git update-index --refresh &&
git diff --exit-code rename
'

test_expect_success 'am -3 can rename a file after falling back to 3-way merge' '
grep "^rename from" rename-add.patch &&
rm -fr .git/rebase-apply &&
git reset --hard &&
git checkout lorem^0 &&
git am -3 rename-add.patch &&
! test -d .git/rebase-apply &&
git update-index --refresh &&
git diff --exit-code rename
'

test_expect_success 'am -3 -q is quiet' '
rm -fr .git/rebase-apply &&
git checkout -f lorem2 &&
git reset master2 --hard &&
sed -n -e "3,\$p" msg >file &&
head -n 9 msg >>file &&
Expand Down

0 comments on commit 18cdf80

Please sign in to comment.