Skip to content

Commit 3296766

Browse files
committed
get_pathspec(): die when an out-of-tree path is given
An earlier commit d089eba (setup: sanitize absolute and funny paths) made get_pathspec() aware of absolute paths, but with a botched interface that forced the callers to count the resulting pathspecs in order to detect an error of giving a path that is outside the work tree. This fixes it, by dying inside the function. We had ls-tree test that relied on a misfeature in the original implementation of its pathspec handling. Leading slashes were silently removed from them. However we allow giving absolute pathnames (people want to cut and paste from elsewhere) that are inside work tree these days, so a pathspec that begin with slash _should_ be treated as a full path. The test is adjusted to match the updated rule for get_pathspec(). Earlier I mistook three tests given by Robin that they should succeed, but these are attempts to add path outside work tree, which should fail loudly. These tests also have been fixed. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b921764 commit 3296766

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

setup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
202202
const char *p = prefix_path(prefix, prefixlen, *src);
203203
if (p)
204204
*(dst++) = p;
205+
else
206+
exit(128); /* error message already given */
205207
src++;
206208
}
207209
*dst = NULL;

t/t3101-ls-tree-dirname.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ EOF
120120
# having 1.txt and path3
121121
test_expect_success \
122122
'ls-tree filter odd names' \
123-
'git ls-tree $tree 1.txt /1.txt //1.txt path3/1.txt /path3/1.txt //path3//1.txt path3 /path3/ path3// >current &&
123+
'git ls-tree $tree 1.txt ./1.txt .//1.txt path3/1.txt path3/./1.txt path3 path3// >current &&
124124
cat >expected <<\EOF &&
125125
100644 blob X 1.txt
126126
100644 blob X path3/1.txt

t/t7010-setup.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,24 @@ test_expect_success 'setup deeper work tree' '
142142
test_expect_success 'add a directory outside the work tree' '(
143143
cd tester &&
144144
d1="$(cd .. ; pwd)" &&
145-
git add "$d1"
145+
test_must_fail git add "$d1"
146146
)'
147147

148+
148149
test_expect_success 'add a file outside the work tree, nasty case 1' '(
149150
cd tester &&
150151
f="$(pwd)x" &&
151152
echo "$f" &&
152153
touch "$f" &&
153-
git add "$f"
154+
test_must_fail git add "$f"
154155
)'
155156

156157
test_expect_success 'add a file outside the work tree, nasty case 2' '(
157158
cd tester &&
158159
f="$(pwd | sed "s/.$//")x" &&
159160
echo "$f" &&
160161
touch "$f" &&
161-
git add "$f"
162+
test_must_fail git add "$f"
162163
)'
163164

164165
test_done

0 commit comments

Comments
 (0)