Skip to content

Commit 7ac6609

Browse files
committed
Merge branch 'jl/maint-submodule-relative'
By Jens Lehmann (3) and Johannes Sixt (1) * jl/maint-submodule-relative: submodules: fix ambiguous absolute paths under Windows submodules: refactor computation of relative gitdir path submodules: always use a relative path from gitdir to work tree submodules: always use a relative path to gitdir
2 parents aa145bf + 4dce7d9 commit 7ac6609

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-29
lines changed

git-submodule.sh

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -132,46 +132,46 @@ module_clone()
132132
gitdir_base=
133133
name=$(module_name "$path" 2>/dev/null)
134134
test -n "$name" || name="$path"
135-
base_path=$(dirname "$path")
135+
base_name=$(dirname "$name")
136136

137137
gitdir=$(git rev-parse --git-dir)
138-
gitdir_base="$gitdir/modules/$base_path"
139-
gitdir="$gitdir/modules/$path"
140-
141-
case $gitdir in
142-
/*)
143-
a="$(cd_to_toplevel && pwd)/"
144-
b=$gitdir
145-
while [ "$b" ] && [ "${a%%/*}" = "${b%%/*}" ]
146-
do
147-
a=${a#*/} b=${b#*/};
148-
done
149-
150-
rel="$a$name"
151-
rel=`echo $rel | sed -e 's|[^/]*|..|g'`
152-
rel_gitdir="$rel/$b"
153-
;;
154-
*)
155-
rel=`echo $name | sed -e 's|[^/]*|..|g'`
156-
rel_gitdir="$rel/$gitdir"
157-
;;
158-
esac
138+
gitdir_base="$gitdir/modules/$base_name"
139+
gitdir="$gitdir/modules/$name"
159140

160141
if test -d "$gitdir"
161142
then
162143
mkdir -p "$path"
163-
echo "gitdir: $rel_gitdir" >"$path/.git"
164144
rm -f "$gitdir/index"
165145
else
166146
mkdir -p "$gitdir_base"
167-
if test -n "$reference"
168-
then
169-
git-clone $quiet "$reference" -n "$url" "$path" --separate-git-dir "$gitdir"
170-
else
171-
git-clone $quiet -n "$url" "$path" --separate-git-dir "$gitdir"
172-
fi ||
147+
git clone $quiet -n ${reference:+"$reference"} \
148+
--separate-git-dir "$gitdir" "$url" "$path" ||
173149
die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
174150
fi
151+
152+
a=$(cd "$gitdir" && pwd)/
153+
b=$(cd "$path" && pwd)/
154+
# normalize Windows-style absolute paths to POSIX-style absolute paths
155+
case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
156+
case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
157+
# Remove all common leading directories after a sanity check
158+
if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
159+
die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
160+
fi
161+
while test "${a%%/*}" = "${b%%/*}"
162+
do
163+
a=${a#*/}
164+
b=${b#*/}
165+
done
166+
# Now chop off the trailing '/'s that were added in the beginning
167+
a=${a%/}
168+
b=${b%/}
169+
170+
rel=$(echo $b | sed -e 's|[^/]*|..|g')
171+
echo "gitdir: $rel/$a" >"$path/.git"
172+
173+
rel=$(echo $a | sed -e 's|[^/]*|..|g')
174+
(clear_local_git_env; cd "$path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
175175
}
176176

177177
#

t/t7400-submodule-basic.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ test_expect_success 'submodule add' '
7979
cd addtest &&
8080
git submodule add -q "$submodurl" submod >actual &&
8181
test ! -s actual &&
82+
echo "gitdir: ../.git/modules/submod" >expect &&
83+
test_cmp expect submod/.git &&
84+
(
85+
cd submod &&
86+
git config core.worktree >actual &&
87+
echo "../../../submod" >expect &&
88+
test_cmp expect actual &&
89+
rm -f actual expect
90+
) &&
8291
git submodule init
8392
) &&
8493
@@ -498,4 +507,17 @@ test_expect_success 'relative path works with user@host:path' '
498507
)
499508
'
500509

510+
test_expect_success 'moving the superproject does not break submodules' '
511+
(
512+
cd addtest &&
513+
git submodule status >expect
514+
)
515+
mv addtest addtest2 &&
516+
(
517+
cd addtest2 &&
518+
git submodule status >actual &&
519+
test_cmp expect actual
520+
)
521+
'
522+
501523
test_done

t/t7406-submodule-update.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,4 +619,21 @@ test_expect_success 'submodule add properly re-creates deeper level submodules'
619619
)
620620
'
621621

622+
test_expect_success 'submodule update properly revives a moved submodule' '
623+
(cd super &&
624+
git commit -am "pre move" &&
625+
git status >expect&&
626+
H=$(cd submodule2; git rev-parse HEAD) &&
627+
git rm --cached submodule2 &&
628+
rm -rf submodule2 &&
629+
mkdir -p "moved/sub module" &&
630+
git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
631+
git config -f .gitmodules submodule.submodule2.path "moved/sub module"
632+
git commit -am "post move" &&
633+
git submodule update &&
634+
git status >actual &&
635+
test_cmp expect actual
636+
)
637+
'
638+
622639
test_done

0 commit comments

Comments
 (0)