@matheusbsbs / matheus-bagliano / commits / 79ee194e52
Fork 0

Honor -p<n> when applying git diffs

If the user is trying to apply a Git generated diff file and they have specified a -p<n> option, where <n> is not 1, the user probably has a good reason for doing this. Such as they are me, trying to apply a patch generated in git.git for the git-gui subdirectory to the git-gui.git repository, where there is no git-gui subdirectory present. Users shouldn't supply -p2 unless they mean it. But if they are supplying it, they probably have thought about how to make this patch apply to their working directory, and want to risk whatever results may come from that. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Apr 4, 2007 at 11:19 UTC 79ee194e52a140412da475e102145bad80d5d00a
2 files changed +27 -2
builtin-apply.c
+2 -2
@@ -417,7 +417,7 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
417 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
418 {
419 if (!orig_name && !isnull)
420 - return find_name(line, NULL, 1, TERM_TAB);
420 + return find_name(line, NULL, p_value, TERM_TAB);
421
422 if (orig_name) {
423 int len;
@@ -427,7 +427,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
427 len = strlen(name);
428 if (isnull)
429 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
430 - another = find_name(line, NULL, 1, TERM_TAB);
430 + another = find_name(line, NULL, p_value, TERM_TAB);
431 if (!another || memcmp(another, name, len))
432 die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
433 free(another);
t/t4120-apply-popt.sh new
+25
@@ -0,0 +1,25 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2007 Shawn O. Pearce
4 +#
5 +
6 +test_description='git-apply -p handling.'
7 +
8 +. ./test-lib.sh
9 +
10 +test_expect_success setup '
11 + mkdir sub &&
12 + echo A >sub/file1 &&
13 + cp sub/file1 file1 &&
14 + git add sub/file1 &&
15 + echo B >sub/file1 &&
16 + git diff >patch.file &&
17 + rm sub/file1 &&
18 + rmdir sub
19 +'
20 +
21 +test_expect_success 'apply git diff with -p2' '
22 + git apply -p2 patch.file
23 +'
24 +
25 +test_done