Skip to content

Commit 01aff0a

Browse files
committed
apply: correctly reverse patch's pre- and post-image mode bits
When parsing the patch header, unless it is a patch that changes file modes, we only read the mode bits into the .old_mode member of the patch structure and leave .new_mode member as initialized, i.e., to 0. Later when we need the original mode bits, we consult .old_mode. However, reverse_patches() that is used to swap the names and modes of the preimage and postimage files is not aware of this convention, leading the .old_mode to be 0 while the mode we read from the patch is left in .new_mode. Only swap .old_mode and .new_mode when .new_mode is not 0 (i.e. we saw a patch that modifies the filemode and know what the new mode is). When .new_mode is set to 0, it means the preimage and the postimage files have the same mode (which is in the .old_mode member) and when applying such a patch in reverse, the value in .old_mode is what we expect the (reverse-) preimage file to have. Reported-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0482c32 commit 01aff0a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

apply.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,8 @@ static void reverse_patches(struct patch *p)
22202220
struct fragment *frag = p->fragments;
22212221

22222222
SWAP(p->new_name, p->old_name);
2223-
SWAP(p->new_mode, p->old_mode);
2223+
if (p->new_mode)
2224+
SWAP(p->new_mode, p->old_mode);
22242225
SWAP(p->is_new, p->is_delete);
22252226
SWAP(p->lines_added, p->lines_deleted);
22262227
SWAP(p->old_oid_prefix, p->new_oid_prefix);
@@ -3780,9 +3781,8 @@ static int check_preimage(struct apply_state *state,
37803781

37813782
if (!state->cached && !previous) {
37823783
if (!trust_executable_bit)
3783-
st_mode = (*ce && (*ce)->ce_mode) ? (*ce)->ce_mode :
3784-
(state->apply_in_reverse
3785-
? patch->new_mode : patch->old_mode);
3784+
st_mode = (*ce && (*ce)->ce_mode)
3785+
? (*ce)->ce_mode : patch->old_mode;
37863786
else
37873787
st_mode = ce_mode_from_stat(*ce, st->st_mode);
37883788
}

0 commit comments

Comments
 (0)