Skip to content

Commit 8ac66a8

Browse files
committed
apply: normalize path in --directory argument
When passing a relative path like --directory=./some/sub, the leading "./" caused apply to prepend it literally to patch filenames, resulting in an error (invalid path). There may be more cases like this where users pass some/./path to the directory which can easily be normalized to an acceptable path, so these changes try to normalize the path before using it. Signed-off-by: Joaquim Rocha <joaquim@amutable.com>
1 parent 6fcee47 commit 8ac66a8

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
@@ -5002,6 +5002,10 @@ static int apply_option_parse_directory(const struct option *opt,
50025002

50035003
strbuf_reset(&state->root);
50045004
strbuf_addstr(&state->root, arg);
5005+
5006+
if (strbuf_normalize_path(&state->root) < 0)
5007+
return error(_("unable to normalize directory: '%s'"), arg);
5008+
50055009
strbuf_complete(&state->root, '/');
50065010
return 0;
50075011
}

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)