Skip to content

Commit db75ada

Browse files
Michael J Grubergitster
authored andcommitted
git submodule: Fix adding of submodules at paths with ./, .. and //
Make 'git submodule add' normalize the submodule path in the same way as 'git ls-files' does, so that 'git submodule init' looks up the information in .gitmodules with the same key under which 'git submodule add' stores it. This fixes 4 known breakages. Signed-off-by: Michael J Gruber <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac8463d commit db75ada

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

git-submodule.sh

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

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

t/t7400-submodule-basic.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,31 @@ test_expect_success 'submodule add' '
6464
)
6565
'
6666

67-
test_expect_failure 'submodule add with ./ in path' '
67+
test_expect_success 'submodule add with ./ in path' '
6868
(
6969
cd addtest &&
7070
git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
7171
git submodule init
7272
)
7373
'
7474

75-
test_expect_failure 'submodule add with // in path' '
75+
test_expect_success 'submodule add with // in path' '
7676
(
7777
cd addtest &&
7878
git submodule add "$submodurl" slashslashsubmod///frotz// &&
7979
git submodule init
8080
)
8181
'
8282

83-
test_expect_failure 'submodule add with /.. in path' '
83+
test_expect_success 'submodule add with /.. in path' '
8484
(
8585
cd addtest &&
8686
git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
8787
git submodule init
8888
)
8989
'
9090

91-
test_expect_failure 'submodule add with ./, /.. and // in path' '
91+
test_expect_success 'submodule add with ./, /.. and // in path' '
9292
(
9393
cd addtest &&
9494
git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&

0 commit comments

Comments
 (0)