]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Honor -p<n> when applying git diffs
authorShawn O. Pearce <spearce@spearce.org>
Wed, 4 Apr 2007 15:19:14 +0000 (11:19 -0400)
committerJunio C Hamano <junkio@cox.net>
Thu, 5 Apr 2007 22:06:58 +0000 (15:06 -0700)
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>
builtin-apply.c
t/t4120-apply-popt.sh [new file with mode: 0755]

index 27a182bfaa826711b2e6c4c775fbd6e83b243f7e..a5d612655f82e071c8e7d961660a6f1d562f6f40 100644 (file)
@@ -417,7 +417,7 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
 {
        if (!orig_name && !isnull)
-               return find_name(line, NULL, 1, TERM_TAB);
+               return find_name(line, NULL, p_value, TERM_TAB);
 
        if (orig_name) {
                int len;
@@ -427,7 +427,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
                len = strlen(name);
                if (isnull)
                        die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
-               another = find_name(line, NULL, 1, TERM_TAB);
+               another = find_name(line, NULL, p_value, TERM_TAB);
                if (!another || memcmp(another, name, len))
                        die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
                free(another);
diff --git a/t/t4120-apply-popt.sh b/t/t4120-apply-popt.sh
new file mode 100755 (executable)
index 0000000..2f672f3
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Shawn O. Pearce
+#
+
+test_description='git-apply -p handling.'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+       mkdir sub &&
+       echo A >sub/file1 &&
+       cp sub/file1 file1 &&
+       git add sub/file1 &&
+       echo B >sub/file1 &&
+       git diff >patch.file &&
+       rm sub/file1 &&
+       rmdir sub
+'
+
+test_expect_success 'apply git diff with -p2' '
+       git apply -p2 patch.file
+'
+
+test_done