Skip to content

Commit 969c877

Browse files
peffspearce
authored andcommitted
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 <[email protected]> Acked-by: Junio C Hamano <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent ff74126 commit 969c877

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

builtin-apply.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,13 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch
810810
* the default name from the header.
811811
*/
812812
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+
}
813820

814821
line += len;
815822
size -= len;

t/t4128-apply-root.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,56 @@ test_expect_success 'apply --directory -p (2) ' '
4040
4141
'
4242

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+
4395
test_done

0 commit comments

Comments
 (0)