From b0f266de11e9dc3a140ff9407a47604bef0e257f Mon Sep 17 00:00:00 2001 From: Jonathan Tan Date: Tue, 20 Oct 2020 15:04:52 -0700 Subject: apply: when -R, also reverse list of sections A patch changing a symlink into a file is written with 2 sections (in the code, represented as "struct patch"): firstly, the deletion of the symlink, and secondly, the creation of the file. When applying that patch with -R, the sections are reversed, so we get: (1) creation of a symlink, then (2) deletion of a file. This causes an issue when the "deletion of a file" section is checked, because Git observes that the so-called file is not a file but a symlink, resulting in a "wrong type" error message. What we want is: (1) deletion of a file, then (2) creation of a symlink. In the code, this is reflected in the behavior of previous_patch() when invoked from check_preimage() when the deletion is checked. Creation then deletion means that when the deletion is checked, previous_patch() returns the creation section, triggering a mode conflict resulting in the "wrong type" error message. But deletion then creation means that when the deletion is checked, previous_patch() returns NULL, so the deletion mode is checked against lstat, which is what we want. There are also other ways a patch can contain 2 sections referencing the same file, for example, in 7a07841c0b ("git-apply: handle a patch that touches the same path more than once better", 2008-06-27). "git apply -R" fails in the same way, and this commit makes this case succeed. Therefore, when building the list of sections, build them in reverse order (by adding to the front of the list instead of the back) when -R is passed. Helped-by: Junio C Hamano Signed-off-by: Jonathan Tan Signed-off-by: Junio C Hamano --- apply.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'apply.c') diff --git a/apply.c b/apply.c index 76dba93c97..359ceb632c 100644 --- a/apply.c +++ b/apply.c @@ -4699,8 +4699,13 @@ static int apply_patch(struct apply_state *state, reverse_patches(patch); if (use_patch(state, patch)) { patch_stats(state, patch); - *listp = patch; - listp = &patch->next; + if (!list || !state->apply_in_reverse) { + *listp = patch; + listp = &patch->next; + } else { + patch->next = list; + list = patch; + } if ((patch->new_name && ends_with_path_components(patch->new_name, -- cgit v1.2.3