Skip to content

Commit cefd43b

Browse files
Federico Cuellogitster
authored andcommitted
Fix git-apply with -p greater than 1
Fix the case when the patch is a rename or mode-change only and -p is used with a value greater than one. The git_header_name function did not remove more than one path component. Signed-off-by: Federico Cuello <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8a90438 commit cefd43b

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

builtin/apply.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -919,28 +919,28 @@ static int gitdiff_newfile(const char *line, struct patch *patch)
919919
static int gitdiff_copysrc(const char *line, struct patch *patch)
920920
{
921921
patch->is_copy = 1;
922-
patch->old_name = find_name(line, NULL, 0, 0);
922+
patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
923923
return 0;
924924
}
925925

926926
static int gitdiff_copydst(const char *line, struct patch *patch)
927927
{
928928
patch->is_copy = 1;
929-
patch->new_name = find_name(line, NULL, 0, 0);
929+
patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
930930
return 0;
931931
}
932932

933933
static int gitdiff_renamesrc(const char *line, struct patch *patch)
934934
{
935935
patch->is_rename = 1;
936-
patch->old_name = find_name(line, NULL, 0, 0);
936+
patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
937937
return 0;
938938
}
939939

940940
static int gitdiff_renamedst(const char *line, struct patch *patch)
941941
{
942942
patch->is_rename = 1;
943-
patch->new_name = find_name(line, NULL, 0, 0);
943+
patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
944944
return 0;
945945
}
946946

@@ -1025,7 +1025,7 @@ static char *git_header_name(char *line, int llen)
10251025
{
10261026
const char *name;
10271027
const char *second = NULL;
1028-
size_t len;
1028+
size_t len, line_len;
10291029

10301030
line += strlen("diff --git ");
10311031
llen -= strlen("diff --git ");
@@ -1125,22 +1125,22 @@ static char *git_header_name(char *line, int llen)
11251125
* Accept a name only if it shows up twice, exactly the same
11261126
* form.
11271127
*/
1128+
second = strchr(name, '\n');
1129+
if (!second)
1130+
return NULL;
1131+
line_len = second - name;
11281132
for (len = 0 ; ; len++) {
11291133
switch (name[len]) {
11301134
default:
11311135
continue;
11321136
case '\n':
11331137
return NULL;
11341138
case '\t': case ' ':
1135-
second = name+len;
1136-
for (;;) {
1137-
char c = *second++;
1138-
if (c == '\n')
1139-
return NULL;
1140-
if (c == '/')
1141-
break;
1142-
}
1143-
if (second[len] == '\n' && !memcmp(name, second, len)) {
1139+
second = stop_at_slash(name + len, line_len - len);
1140+
if (!second)
1141+
return NULL;
1142+
second++;
1143+
if (second[len] == '\n' && !strncmp(name, second, len)) {
11441144
return xmemdupz(name, len);
11451145
}
11461146
}

0 commit comments

Comments
 (0)