Skip to content

Commit 1ebfc21

Browse files
committed
Merge branch 'jr/apply-directory-normalize'
"git apply --directory=./un/../normalized/path" now normalizes the given path before using it. * jr/apply-directory-normalize: apply: normalize path in --directory argument
2 parents 8f760e7 + 0fbf380 commit 1ebfc21

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

apply.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5022,6 +5022,10 @@ static int apply_option_parse_directory(const struct option *opt,
50225022

50235023
strbuf_reset(&state->root);
50245024
strbuf_addstr(&state->root, arg);
5025+
5026+
if (strbuf_normalize_path(&state->root) < 0)
5027+
return error(_("unable to normalize directory: '%s'"), arg);
5028+
50255029
strbuf_complete(&state->root, '/');
50265030
return 0;
50275031
}

t/t4128-apply-root.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,47 @@ test_expect_success 'apply --directory -p (2) ' '
4343
4444
'
4545

46+
test_expect_success 'apply --directory (./ prefix)' '
47+
git reset --hard initial &&
48+
git apply --directory=./some/sub -p3 --index patch &&
49+
echo Bello >expect &&
50+
git show :some/sub/dir/file >actual &&
51+
test_cmp expect actual &&
52+
test_cmp expect some/sub/dir/file
53+
'
54+
55+
test_expect_success 'apply --directory (double slash)' '
56+
git reset --hard initial &&
57+
git apply --directory=some//sub -p3 --index patch &&
58+
echo Bello >expect &&
59+
git show :some/sub/dir/file >actual &&
60+
test_cmp expect actual &&
61+
test_cmp expect some/sub/dir/file
62+
'
63+
64+
test_expect_success 'apply --directory (./ in the middle)' '
65+
git reset --hard initial &&
66+
git apply --directory=some/./sub -p3 --index patch &&
67+
echo Bello >expect &&
68+
git show :some/sub/dir/file >actual &&
69+
test_cmp expect actual &&
70+
test_cmp expect some/sub/dir/file
71+
'
72+
73+
test_expect_success 'apply --directory (../ in the middle)' '
74+
git reset --hard initial &&
75+
git apply --directory=some/../some/sub -p3 --index patch &&
76+
echo Bello >expect &&
77+
git show :some/sub/dir/file >actual &&
78+
test_cmp expect actual &&
79+
test_cmp expect some/sub/dir/file
80+
'
81+
82+
test_expect_success 'apply --directory rejects leading ../' '
83+
test_must_fail git apply --directory=../foo -p3 patch 2>err &&
84+
test_grep "unable to normalize directory" err
85+
'
86+
4687
cat > patch << EOF
4788
diff --git a/newfile b/newfile
4889
new file mode 100644

0 commit comments

Comments
 (0)