@matheusbsbs / matheus-bagliano / commits / 9987d7c58a
Fork 0

git-apply: notice "diff --git" patch again

Earlier one that tried to be too consistent with GNU patch by not stripping the leading path when we _know_ we are in a subdirectory and the patch is relative to the toplevel was a mistake. This fixes it. - No change to behaviour when it is run from the toplevel of the repository. - When run from a subdirectory to apply a git-generated patch, it uses the right -p<n> value automatically, with or without --index nor --cached option. - When run from a subdirectory to apply a randomly generated patch, it wants the right -p<n> value to be given by the user. The second one is a pure improvement to correct inconsistency between --index and non --index case, compared with 1.5.0. The third point could be further improved to guess what the right value for -p<n> should be by looking at the patch, but should be a topic of a separate patch. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 21, 2007 at 14:31 UTC 9987d7c58a847ab1605ae3216ff1ca95b19f0ad1
2 files changed +18 -9
builtin-apply.c
+16 -7
@@ -144,6 +144,7 @@ struct patch {
144 unsigned long deflate_origlen;
145 int lines_added, lines_deleted;
146 int score;
147 + unsigned int is_toplevel_relative:1;
148 unsigned int inaccurate_eof:1;
149 unsigned int is_binary:1;
150 unsigned int is_copy:1;
@@ -362,7 +363,7 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
363 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
364 {
365 if (!orig_name && !isnull)
365 - return find_name(line, NULL, p_value, TERM_TAB);
366 + return find_name(line, NULL, 1, TERM_TAB);
367
368 if (orig_name) {
369 int len;
@@ -372,7 +373,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
373 len = strlen(name);
374 if (isnull)
375 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
375 - another = find_name(line, NULL, p_value, TERM_TAB);
376 + another = find_name(line, NULL, 1, TERM_TAB);
377 if (!another || memcmp(another, name, len))
378 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
379 free(another);
@@ -427,28 +428,28 @@ static int gitdiff_newfile(const char *line, struct patch *patch)
428 static int gitdiff_copysrc(const char *line, struct patch *patch)
429 {
430 patch->is_copy = 1;
430 - patch->old_name = find_name(line, NULL, p_value-1, 0);
431 + patch->old_name = find_name(line, NULL, 0, 0);
432 return 0;
433 }
434
435 static int gitdiff_copydst(const char *line, struct patch *patch)
436 {
437 patch->is_copy = 1;
437 - patch->new_name = find_name(line, NULL, p_value-1, 0);
438 + patch->new_name = find_name(line, NULL, 0, 0);
439 return 0;
440 }
441
442 static int gitdiff_renamesrc(const char *line, struct patch *patch)
443 {
444 patch->is_rename = 1;
444 - patch->old_name = find_name(line, NULL, p_value-1, 0);
445 + patch->old_name = find_name(line, NULL, 0, 0);
446 return 0;
447 }
448
449 static int gitdiff_renamedst(const char *line, struct patch *patch)
450 {
451 patch->is_rename = 1;
451 - patch->new_name = find_name(line, NULL, p_value-1, 0);
452 + patch->new_name = find_name(line, NULL, 0, 0);
453 return 0;
454 }
455
@@ -787,6 +788,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
788 {
789 unsigned long offset, len;
790
791 + patch->is_toplevel_relative = 0;
792 patch->is_rename = patch->is_copy = 0;
793 patch->is_new = patch->is_delete = -1;
794 patch->old_mode = patch->new_mode = 0;
@@ -831,6 +833,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
833 die("git diff header lacks filename information (line %d)", linenr);
834 patch->old_name = patch->new_name = patch->def_name;
835 }
836 + patch->is_toplevel_relative = 1;
837 *hdrsize = git_hdr_len;
838 return offset;
839 }
@@ -2499,6 +2502,12 @@ static int use_patch(struct patch *p)
2502 return 0;
2503 x = x->next;
2504 }
2505 + if (0 < prefix_length) {
2506 + int pathlen = strlen(pathname);
2507 + if (pathlen <= prefix_length ||
2508 + memcmp(prefix, pathname, prefix_length))
2509 + return 0;
2510 + }
2511 return 1;
2512 }
2513
@@ -2513,7 +2522,7 @@ static void prefix_one(char **name)
2522
2523 static void prefix_patches(struct patch *p)
2524 {
2516 - if (!prefix)
2525 + if (!prefix || p->is_toplevel_relative)
2526 return;
2527 for ( ; p; p = p->next) {
2528 if (p->new_name == p->old_name) {
t/t4119-apply-config.sh
+2 -2
@@ -78,7 +78,7 @@ test_expect_success 'apply --whitespace=strip in subdir' '
78 git update-index --refresh &&
79
80 cd sub &&
81 - git apply --whitespace=strip -p2 ../patch.file &&
81 + git apply --whitespace=strip ../patch.file &&
82 if grep " " file1
83 then
84 echo "Eh?"
@@ -101,7 +101,7 @@ test_expect_success 'apply --whitespace=strip from config in subdir' '
101 git update-index --refresh &&
102
103 cd sub &&
104 - git apply -p2 ../patch.file &&
104 + git apply ../patch.file &&
105 if grep " " file1
106 then
107 echo "Eh?"