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 <agruen@suse.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Andreas Gruenbacher committed
Jan 17, 2010 at 03:05 UTC
158620872737067e4a74ca7aed5302af8debb714
2 files changed
+14
-2
builtin-apply.c
+9
-2
@@ -404,6 +404,9 @@ static char *squash_slash(char *name)
404
{
405
int i = 0, j = 0;
406
407
+ if (!name)
408
+ return NULL;
409
+
410
while (name[i]) {
411
if ((name[j++] = name[i++]) == '/')
412
while (name[i] == '/')
@@ -416,7 +419,10 @@ static char *squash_slash(char *name)
419
static char *find_name(const char *line, char *def, int p_value, int terminate)
420
{
421
int len;
419
- const char *start = line;
422
+ const char *start = NULL;
423
+
424
+ if (p_value == 0)
425
+ start = line;
426
427
if (*line == '"') {
428
struct strbuf name = STRBUF_INIT;
@@ -1199,7 +1205,8 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
1205
continue;
1206
if (!patch->old_name && !patch->new_name) {
1207
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);
1210
patch->old_name = patch->new_name = patch->def_name;
1211
}
1212
patch->is_toplevel_relative = 1;
t/t4120-apply-popt.sh
+5
@@ -22,4 +22,9 @@ test_expect_success 'apply git diff with -p2' '
22
git apply -p2 patch.file
23
'
24
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
+
30
test_done