git apply --directory broken for new files
We carefully verify that the input to git-apply is sane, including cross-checking that the filenames we see in "+++" headers match what was provided on the command line of "diff --git". When --directory is used, however, we ended up comparing the unadorned name to one with the prepended root, causing us to complain about a mismatch. We simply need to prepend the root directory, if any, when pulling the name out of the git header. Signed-off-by: Jeff King <peff@peff.net> Acked-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Jeff King committed
Oct 12, 2008 at 00:06 UTC
969c877506cf8cc760c7b251fef6c5b6850bfc19
2 files changed
+59
builtin-apply.c
+7
@@ -810,6 +810,13 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch
810
* the default name from the header.
811
*/
812
patch->def_name = git_header_name(line, len);
813
+ if (patch->def_name && root) {
814
+ char *s = xmalloc(root_len + strlen(patch->def_name) + 1);
815
+ strcpy(s, root);
816
+ strcpy(s + root_len, patch->def_name);
817
+ free(patch->def_name);
818
+ patch->def_name = s;
819
+ }
820
821
line += len;
822
size -= len;
t/t4128-apply-root.sh
+52
@@ -40,4 +40,56 @@ test_expect_success 'apply --directory -p (2) ' '
40
41
'
42
43
+cat > patch << EOF
44
+diff --git a/newfile b/newfile
45
+new file mode 100644
46
+index 0000000..d95f3ad
47
+--- /dev/null
48
++++ b/newfile
49
+@@ -0,0 +1 @@
50
++content
51
+EOF
52
+
53
+test_expect_success 'apply --directory (new file)' '
54
+ git reset --hard initial &&
55
+ git apply --directory=some/sub/dir/ --index patch &&
56
+ test content = $(git show :some/sub/dir/newfile) &&
57
+ test content = $(cat some/sub/dir/newfile)
58
+'
59
+
60
+cat > patch << EOF
61
+diff --git a/delfile b/delfile
62
+deleted file mode 100644
63
+index d95f3ad..0000000
64
+--- a/delfile
65
++++ /dev/null
66
+@@ -1 +0,0 @@
67
+-content
68
+EOF
69
+
70
+test_expect_success 'apply --directory (delete file)' '
71
+ git reset --hard initial &&
72
+ echo content >some/sub/dir/delfile &&
73
+ git add some/sub/dir/delfile &&
74
+ git apply --directory=some/sub/dir/ --index patch &&
75
+ ! git ls-files | grep delfile
76
+'
77
+
78
+cat > patch << 'EOF'
79
+diff --git "a/qu\157tefile" "b/qu\157tefile"
80
+new file mode 100644
81
+index 0000000..d95f3ad
82
+--- /dev/null
83
++++ "b/qu\157tefile"
84
+@@ -0,0 +1 @@
85
++content
86
+EOF
87
+
88
+test_expect_success 'apply --directory (quoted filename)' '
89
+ git reset --hard initial &&
90
+ git apply --directory=some/sub/dir/ --index patch &&
91
+ test content = $(git show :some/sub/dir/quotefile) &&
92
+ test content = $(cat some/sub/dir/quotefile)
93
+'
94
+
95
test_done