Skip to content

Commit a59c872

Browse files
committed
Merge branch 'fc/apply-p2-get-header-name' into maint
* fc/apply-p2-get-header-name: test: git-apply -p2 rename/chmod only Fix git-apply with -p greater than 1
2 parents 5605685 + aae1f6a commit a59c872

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

builtin/apply.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -942,28 +942,28 @@ static int gitdiff_newfile(const char *line, struct patch *patch)
942942
static int gitdiff_copysrc(const char *line, struct patch *patch)
943943
{
944944
patch->is_copy = 1;
945-
patch->old_name = find_name(line, NULL, 0, 0);
945+
patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
946946
return 0;
947947
}
948948

949949
static int gitdiff_copydst(const char *line, struct patch *patch)
950950
{
951951
patch->is_copy = 1;
952-
patch->new_name = find_name(line, NULL, 0, 0);
952+
patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
953953
return 0;
954954
}
955955

956956
static int gitdiff_renamesrc(const char *line, struct patch *patch)
957957
{
958958
patch->is_rename = 1;
959-
patch->old_name = find_name(line, NULL, 0, 0);
959+
patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
960960
return 0;
961961
}
962962

963963
static int gitdiff_renamedst(const char *line, struct patch *patch)
964964
{
965965
patch->is_rename = 1;
966-
patch->new_name = find_name(line, NULL, 0, 0);
966+
patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
967967
return 0;
968968
}
969969

@@ -1048,7 +1048,7 @@ static char *git_header_name(char *line, int llen)
10481048
{
10491049
const char *name;
10501050
const char *second = NULL;
1051-
size_t len;
1051+
size_t len, line_len;
10521052

10531053
line += strlen("diff --git ");
10541054
llen -= strlen("diff --git ");
@@ -1148,22 +1148,22 @@ static char *git_header_name(char *line, int llen)
11481148
* Accept a name only if it shows up twice, exactly the same
11491149
* form.
11501150
*/
1151+
second = strchr(name, '\n');
1152+
if (!second)
1153+
return NULL;
1154+
line_len = second - name;
11511155
for (len = 0 ; ; len++) {
11521156
switch (name[len]) {
11531157
default:
11541158
continue;
11551159
case '\n':
11561160
return NULL;
11571161
case '\t': case ' ':
1158-
second = name+len;
1159-
for (;;) {
1160-
char c = *second++;
1161-
if (c == '\n')
1162-
return NULL;
1163-
if (c == '/')
1164-
break;
1165-
}
1166-
if (second[len] == '\n' && !memcmp(name, second, len)) {
1162+
second = stop_at_slash(name + len, line_len - len);
1163+
if (!second)
1164+
return NULL;
1165+
second++;
1166+
if (second[len] == '\n' && !strncmp(name, second, len)) {
11671167
return xmemdupz(name, len);
11681168
}
11691169
}

t/t4120-apply-popt.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,30 @@ test_expect_success 'apply with too large -p and fancy filename' '
5656
grep "removing 3 leading" err
5757
'
5858

59+
test_expect_success 'apply (-p2) diff, mode change only' '
60+
cat >patch.chmod <<-\EOF &&
61+
diff --git a/sub/file1 b/sub/file1
62+
old mode 100644
63+
new mode 100755
64+
EOF
65+
chmod 644 file1 &&
66+
git apply -p2 patch.chmod &&
67+
test -x file1
68+
'
69+
70+
test_expect_success 'apply (-p2) diff, rename' '
71+
cat >patch.rename <<-\EOF &&
72+
diff --git a/sub/file1 b/sub/file2
73+
similarity index 100%
74+
rename from sub/file1
75+
rename to sub/file2
76+
EOF
77+
echo A >expected &&
78+
79+
cp file1.saved file1 &&
80+
rm -f file2 &&
81+
git apply -p2 patch.rename &&
82+
test_cmp expected file2
83+
'
84+
5985
test_done

0 commit comments

Comments
 (0)