@matheusbsbs / matheus-bagliano / commits / 56185f49d0
Fork 0

git-apply: require -p<n> when working in a subdirectory.

git-apply running inside a subdirectory, with or without --index, used to always assume that the patch is formatted in such a way to apply with -p1 from the toplevel, but it is more useful and consistent with the use of "GNU patch -p1" if it defaulted to assume that its input is meant to apply at the level it is invoked in. This changes the behaviour. It used to be that the patch generated this way would apply without any trick: edit Documentation/Makefile git diff >patch.file cd Documentation git apply ../patch.file You need to give an explicit -p2 to git-apply now. On the other hand, if you got a patch from somebody else who did not follow "patch is to apply from the top with -p1" convention, the input patch would start with: diff -u Makefile.old Makefile --- Makefile.old +++ Makefile and in such a case, you can apply it with: git apply -p0 patch.file Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 19, 2007 at 17:57 UTC 56185f49d03cae28048146e902089ea366c6cd6c
2 files changed +46 -28
builtin-apply.c
+28 -14
@@ -238,7 +238,7 @@ static int name_terminate(const char *name, int namelen, int c, int terminate)
238 return 1;
239 }
240
241 -static char * find_name(const char *line, char *def, int p_value, int terminate)
241 +static char *find_name(const char *line, char *def, int p_value, int terminate)
242 {
243 int len;
244 const char *start = line;
@@ -362,7 +362,7 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
362 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
363 {
364 if (!orig_name && !isnull)
365 - return find_name(line, NULL, 1, TERM_TAB);
365 + return find_name(line, NULL, p_value, TERM_TAB);
366
367 if (orig_name) {
368 int len;
@@ -372,7 +372,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
372 len = strlen(name);
373 if (isnull)
374 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
375 - another = find_name(line, NULL, 1, TERM_TAB);
375 + another = find_name(line, NULL, p_value, TERM_TAB);
376 if (!another || memcmp(another, name, len))
377 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
378 free(another);
@@ -427,28 +427,28 @@ static int gitdiff_newfile(const char *line, struct patch *patch)
427 static int gitdiff_copysrc(const char *line, struct patch *patch)
428 {
429 patch->is_copy = 1;
430 - patch->old_name = find_name(line, NULL, 0, 0);
430 + patch->old_name = find_name(line, NULL, p_value-1, 0);
431 return 0;
432 }
433
434 static int gitdiff_copydst(const char *line, struct patch *patch)
435 {
436 patch->is_copy = 1;
437 - patch->new_name = find_name(line, NULL, 0, 0);
437 + patch->new_name = find_name(line, NULL, p_value-1, 0);
438 return 0;
439 }
440
441 static int gitdiff_renamesrc(const char *line, struct patch *patch)
442 {
443 patch->is_rename = 1;
444 - patch->old_name = find_name(line, NULL, 0, 0);
444 + patch->old_name = find_name(line, NULL, p_value-1, 0);
445 return 0;
446 }
447
448 static int gitdiff_renamedst(const char *line, struct patch *patch)
449 {
450 patch->is_rename = 1;
451 - patch->new_name = find_name(line, NULL, 0, 0);
451 + patch->new_name = find_name(line, NULL, p_value-1, 0);
452 return 0;
453 }
454
@@ -2499,15 +2499,26 @@ static int use_patch(struct patch *p)
2499 return 0;
2500 x = x->next;
2501 }
2502 - if (0 < prefix_length) {
2503 - int pathlen = strlen(pathname);
2504 - if (pathlen <= prefix_length ||
2505 - memcmp(prefix, pathname, prefix_length))
2506 - return 0;
2507 - }
2502 return 1;
2503 }
2504
2505 +static char *prefix_one(char *name)
2506 +{
2507 + if (!name)
2508 + return name;
2509 + return xstrdup(prefix_filename(prefix, prefix_length, name));
2510 +}
2511 +
2512 +static void prefix_patches(struct patch *p)
2513 +{
2514 + if (!prefix)
2515 + return;
2516 + for ( ; p; p = p->next) {
2517 + p->new_name = prefix_one(p->new_name);
2518 + p->old_name = prefix_one(p->old_name);
2519 + }
2520 +}
2521 +
2522 static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2523 {
2524 unsigned long offset, size;
@@ -2530,11 +2541,14 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2541 break;
2542 if (apply_in_reverse)
2543 reverse_patches(patch);
2544 + if (prefix)
2545 + prefix_patches(patch);
2546 if (use_patch(patch)) {
2547 patch_stats(patch);
2548 *listp = patch;
2549 listp = &patch->next;
2537 - } else {
2550 + }
2551 + else {
2552 /* perhaps free it a bit better? */
2553 free(patch);
2554 skipped_patch++;
t/t4119-apply-config.sh
+18 -14
@@ -10,20 +10,22 @@ test_description='git-apply --whitespace=strip and configuration file.
10 . ./test-lib.sh
11
12 test_expect_success setup '
13 - echo A >file1 &&
14 - cp file1 saved &&
15 - git add file1 &&
16 - echo "B " >file1 &&
13 + mkdir sub &&
14 + echo A >sub/file1 &&
15 + cp sub/file1 saved &&
16 + git add sub/file1 &&
17 + echo "B " >sub/file1 &&
18 git diff >patch.file
19 '
20
21 test_expect_success 'apply --whitespace=strip' '
22
22 - cp saved file1 &&
23 + rm -f sub/file1 &&
24 + cp saved sub/file1 &&
25 git update-index --refresh &&
26
27 git apply --whitespace=strip patch.file &&
26 - if grep " " file1
28 + if grep " " sub/file1
29 then
30 echo "Eh?"
31 false
@@ -34,12 +36,13 @@ test_expect_success 'apply --whitespace=strip' '
36
37 test_expect_success 'apply --whitespace=strip from config' '
38
37 - cp saved file1 &&
39 + rm -f sub/file1 &&
40 + cp saved sub/file1 &&
41 git update-index --refresh &&
42
43 git config apply.whitespace strip &&
44 git apply patch.file &&
42 - if grep " " file1
45 + if grep " " sub/file1
46 then
47 echo "Eh?"
48 false
@@ -48,19 +51,19 @@ test_expect_success 'apply --whitespace=strip from config' '
51 fi
52 '
53
51 -mkdir sub
54 D=`pwd`
55
56 test_expect_success 'apply --whitespace=strip in subdir' '
57
58 cd "$D" &&
59 git config --unset-all apply.whitespace
58 - cp saved file1 &&
60 + rm -f sub/file1 &&
61 + cp saved sub/file1 &&
62 git update-index --refresh &&
63
64 cd sub &&
62 - git apply --whitespace=strip ../patch.file &&
63 - if grep " " ../file1
65 + git apply --whitespace=strip -p2 ../patch.file &&
66 + if grep " " file1
67 then
68 echo "Eh?"
69 false
@@ -73,11 +76,12 @@ test_expect_success 'apply --whitespace=strip from config in subdir' '
76
77 cd "$D" &&
78 git config apply.whitespace strip &&
76 - cp saved file1 &&
79 + rm -f sub/file1 &&
80 + cp saved sub/file1 &&
81 git update-index --refresh &&
82
83 cd sub &&
80 - git apply ../patch.file &&
84 + git apply -p2 ../patch.file &&
85 if grep " " file1
86 then
87 echo "Eh?"