Skip to content

Commit 8af95ca

Browse files
committed
Merge branch 'mg/maint-submodule-normalize-path' into maint
* mg/maint-submodule-normalize-path: git submodule: Fix adding of submodules at paths with ./, .. and // git submodule: Add test cases for git submodule add
2 parents 2aa93de + db75ada commit 8af95ca

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

git-submodule.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,18 @@ cmd_add()
167167
;;
168168
esac
169169

170-
# strip trailing slashes from path
171-
path=$(echo "$path" | sed -e 's|/*$||')
172-
170+
# normalize path:
171+
# multiple //; leading ./; /./; /../; trailing /
172+
path=$(printf '%s/\n' "$path" |
173+
sed -e '
174+
s|//*|/|g
175+
s|^\(\./\)*||
176+
s|/\./|/|g
177+
:start
178+
s|\([^/]*\)/\.\./||
179+
tstart
180+
s|/*$||
181+
')
173182
git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
174183
die "'$path' already exists in the index"
175184

t/t7400-submodule-basic.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,55 @@ test_expect_success 'Prepare submodule testing' '
4747
GIT_CONFIG=.gitmodules git config submodule.example.url git://example.com/init.git
4848
'
4949

50+
test_expect_success 'Prepare submodule add testing' '
51+
submodurl=$(pwd)
52+
(
53+
mkdir addtest &&
54+
cd addtest &&
55+
git init
56+
)
57+
'
58+
59+
test_expect_success 'submodule add' '
60+
(
61+
cd addtest &&
62+
git submodule add "$submodurl" submod &&
63+
git submodule init
64+
)
65+
'
66+
67+
test_expect_success 'submodule add with ./ in path' '
68+
(
69+
cd addtest &&
70+
git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
71+
git submodule init
72+
)
73+
'
74+
75+
test_expect_success 'submodule add with // in path' '
76+
(
77+
cd addtest &&
78+
git submodule add "$submodurl" slashslashsubmod///frotz// &&
79+
git submodule init
80+
)
81+
'
82+
83+
test_expect_success 'submodule add with /.. in path' '
84+
(
85+
cd addtest &&
86+
git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
87+
git submodule init
88+
)
89+
'
90+
91+
test_expect_success 'submodule add with ./, /.. and // in path' '
92+
(
93+
cd addtest &&
94+
git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
95+
git submodule init
96+
)
97+
'
98+
5099
test_expect_success 'status should fail for unmapped paths' '
51100
if git submodule status
52101
then

0 commit comments

Comments
 (0)