@@ -80,6 +80,7 @@ function rel_realpath() {
8080# Find the top-level git directory (taking into account we could be in a submodule).
8181declare git_test_dir=.
8282declare top_dir
83+
8384while true ; do
8485 top_dir=$( cd " $git_test_dir " && git rev-parse --show-toplevel) || \
8586 error_exit " You need to be in the git repository to run this script."
@@ -90,8 +91,30 @@ while true; do
9091 if [ -d " $top_dir /.git" ]; then
9192 # We are done! top_dir is the root git directory.
9293 break
93- else
94- # We are in a submodule if .git is a file!
94+ elif [ -f " $top_dir /.git" ]; then
95+ # We are in a submodule or git work-tree if .git is a file!
96+ if [ -z $( git rev-parse --show-superproject-working-tree) ]; then
97+ # The --show-superproject-working-tree option is available and we
98+ # are in a work tree.
99+ gitdir=$( < " $top_dir /.git" )
100+ gitdir=${gitdir# gitdir: }
101+ topdir_basename=${gitdir##*/ }
102+ git_test_dir=${gitdir%/ worktrees/ $topdir_basename }
103+ break
104+ fi
105+ # If show-superproject-working-tree returns non-empty string, either:
106+ #
107+ # 1) --show-superproject-working-tree is not defined for this version of git
108+ #
109+ # 2) --show-superproject-working-tree is defined and we are in a submodule
110+ #
111+ # In the first case we will assume it is not a work tree because people
112+ # using that advanced technology will be using a recent version of git.
113+ #
114+ # In second case, we could use the value returned by
115+ # --show-superproject-working-tree directly but we do not here because
116+ # that would require extra work.
117+ #
95118 git_test_dir=" $git_test_dir /.."
96119 fi
97120done
0 commit comments