@matheusbsbs / matheus-bagliano / commits / ec7fc0b1a4
Fork 0

builtin-apply.c: pay attention to -p<n> when determining the name

The patch structure has def_name component that is used to validate the sanity of a "diff --git" patch by checking pathnames that appear on the patch header lines for consistency. The git_header_name() function is used to compute this out of "diff --git a/... b/..." line, but the code always stripped one level of prefix (i.e. "a/" and "b/"), without paying attention to -p<n> option. Code in find_name() function that parses other lines in the patch header (e.g. "--- a/..." and "+++ b/..." lines) however did strip the correct number of leading paths prefixes, and the sanity check between these computed values failed. Teach git_header_name() to honor -p<n> option like find_name() function does. Found and reported by Steven J. Murdoch who also wrote tests. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 25, 2009 at 02:56 UTC ec7fc0b1a46c5a352532ea3f29c5663752fd8ac6
2 files changed +20 -2
builtin-apply.c
+3 -2
@@ -823,12 +823,13 @@ static int gitdiff_unrecognized(const char *line, struct patch *patch)
823
824 static const char *stop_at_slash(const char *line, int llen)
825 {
826 + int nslash = p_value;
827 int i;
828
829 for (i = 0; i < llen; i++) {
830 int ch = line[i];
830 - if (ch == '/')
831 - return line + i;
831 + if (ch == '/' && --nslash <= 0)
832 + return &line[i];
833 }
834 return NULL;
835 }
t/t4128-apply-root.sh
+17
@@ -57,6 +57,23 @@ test_expect_success 'apply --directory (new file)' '
57 test content = $(cat some/sub/dir/newfile)
58 '
59
60 +cat > patch << EOF
61 +diff --git a/c/newfile2 b/c/newfile2
62 +new file mode 100644
63 +index 0000000..d95f3ad
64 +--- /dev/null
65 ++++ b/c/newfile2
66 +@@ -0,0 +1 @@
67 ++content
68 +EOF
69 +
70 +test_expect_success 'apply --directory -p (new file)' '
71 + git reset --hard initial &&
72 + git apply -p2 --directory=some/sub/dir/ --index patch &&
73 + test content = $(git show :some/sub/dir/newfile2) &&
74 + test content = $(cat some/sub/dir/newfile2)
75 +'
76 +
77 cat > patch << EOF
78 diff --git a/delfile b/delfile
79 deleted file mode 100644