Skip to content

Commit 1586208

Browse files
Andreas Gruenbachergitster
authored andcommitted
builtin-apply.c: Skip filenames without enough components
find_name() wrongly returned the whole filename for filenames without enough leading pathname components (e.g., when applying a patch to a top-level file with -p2). Include the -p value used in the error message when no filenames can be found. [jc: squashed a test from Nanako Shiraishi] Signed-off-by: Andreas Gruenbacher <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 902f235 commit 1586208

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

builtin-apply.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ static char *squash_slash(char *name)
404404
{
405405
int i = 0, j = 0;
406406

407+
if (!name)
408+
return NULL;
409+
407410
while (name[i]) {
408411
if ((name[j++] = name[i++]) == '/')
409412
while (name[i] == '/')
@@ -416,7 +419,10 @@ static char *squash_slash(char *name)
416419
static char *find_name(const char *line, char *def, int p_value, int terminate)
417420
{
418421
int len;
419-
const char *start = line;
422+
const char *start = NULL;
423+
424+
if (p_value == 0)
425+
start = line;
420426

421427
if (*line == '"') {
422428
struct strbuf name = STRBUF_INIT;
@@ -1199,7 +1205,8 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
11991205
continue;
12001206
if (!patch->old_name && !patch->new_name) {
12011207
if (!patch->def_name)
1202-
die("git diff header lacks filename information (line %d)", linenr);
1208+
die("git diff header lacks filename information when removing "
1209+
"%d leading pathname components (line %d)" , p_value, linenr);
12031210
patch->old_name = patch->new_name = patch->def_name;
12041211
}
12051212
patch->is_toplevel_relative = 1;

t/t4120-apply-popt.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ test_expect_success 'apply git diff with -p2' '
2222
git apply -p2 patch.file
2323
'
2424

25+
test_expect_success 'apply with too large -p' '
26+
test_must_fail git apply --stat -p3 patch.file 2>err &&
27+
grep "removing 3 leading" err
28+
'
29+
2530
test_done

0 commit comments

Comments
 (0)