Skip to content

Commit

Permalink
builtin-apply.c: pay attention to -p<n> when determining the name
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Nov 27, 2009
1 parent 4f36627 commit ec7fc0b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,12 +823,13 @@ static int gitdiff_unrecognized(const char *line, struct patch *patch)

static const char *stop_at_slash(const char *line, int llen)
{
int nslash = p_value;
int i;

for (i = 0; i < llen; i++) {
int ch = line[i];
if (ch == '/')
return line + i;
if (ch == '/' && --nslash <= 0)
return &line[i];
}
return NULL;
}
Expand Down
17 changes: 17 additions & 0 deletions t/t4128-apply-root.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ test_expect_success 'apply --directory (new file)' '
test content = $(cat some/sub/dir/newfile)
'

cat > patch << EOF
diff --git a/c/newfile2 b/c/newfile2
new file mode 100644
index 0000000..d95f3ad
--- /dev/null
+++ b/c/newfile2
@@ -0,0 +1 @@
+content
EOF

test_expect_success 'apply --directory -p (new file)' '
git reset --hard initial &&
git apply -p2 --directory=some/sub/dir/ --index patch &&
test content = $(git show :some/sub/dir/newfile2) &&
test content = $(cat some/sub/dir/newfile2)
'

cat > patch << EOF
diff --git a/delfile b/delfile
deleted file mode 100644
Expand Down

0 comments on commit ec7fc0b

Please sign in to comment.