Code

tests: unset COLUMNS inherited from environment
[git.git] / git-submodule.sh
index 814d0d914eaf994462e61559b0692c958f97513b..efc86ad4e0b90edbbf5a927aa87e7ad4b1a1949e 100755 (executable)
@@ -104,9 +104,9 @@ module_name()
        re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
        name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
                sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
-       test -z "$name" &&
-       die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
-       echo "$name"
+       test -z "$name" &&
+       die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
+       echo "$name"
 }
 
 #
@@ -128,13 +128,50 @@ module_clone()
                quiet=-q
        fi
 
-       if test -n "$reference"
+       gitdir=
+       gitdir_base=
+       name=$(module_name "$path" 2>/dev/null)
+       test -n "$name" || name="$path"
+       base_name=$(dirname "$name")
+
+       gitdir=$(git rev-parse --git-dir)
+       gitdir_base="$gitdir/modules/$base_name"
+       gitdir="$gitdir/modules/$name"
+
+       if test -d "$gitdir"
        then
-               git-clone $quiet "$reference" -n "$url" "$path"
+               mkdir -p "$path"
+               rm -f "$gitdir/index"
        else
-               git-clone $quiet -n "$url" "$path"
-       fi ||
-       die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
+               mkdir -p "$gitdir_base"
+               git clone $quiet -n ${reference:+"$reference"} \
+                       --separate-git-dir "$gitdir" "$url" "$path" ||
+               die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
+       fi
+
+       a=$(cd "$gitdir" && pwd)/
+       b=$(cd "$path" && pwd)/
+       # normalize Windows-style absolute paths to POSIX-style absolute paths
+       case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
+       case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
+       # Remove all common leading directories after a sanity check
+       if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
+               die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
+       fi
+       while test "${a%%/*}" = "${b%%/*}"
+       do
+               a=${a#*/}
+               b=${b#*/}
+       done
+       # Now chop off the trailing '/'s that were added in the beginning
+       a=${a%/}
+       b=${b%/}
+
+       rel=$(echo $b | sed -e 's|[^/]*|..|g')
+       echo "gitdir: $rel/$a" >"$path/.git"
+
+       rel=$(echo $a | sed -e 's|[^/]*|..|g')
+       (clear_local_git_env; cd "$path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
 }
 
 #
@@ -426,6 +463,9 @@ cmd_update()
                --recursive)
                        recursive=1
                        ;;
+               --checkout)
+                       update="checkout"
+                       ;;
                --)
                        shift
                        break
@@ -458,7 +498,19 @@ cmd_update()
                fi
                name=$(module_name "$path") || exit
                url=$(git config submodule."$name".url)
-               update_module=$(git config submodule."$name".update)
+               if ! test -z "$update"
+               then
+                       update_module=$update
+               else
+                       update_module=$(git config submodule."$name".update)
+               fi
+
+               if test "$update_module" = "none"
+               then
+                       echo "Skipping submodule '$path'"
+                       continue
+               fi
+
                if test -z "$url"
                then
                        # Only mention uninitialized submodules when its
@@ -480,11 +532,6 @@ Maybe you want to use 'update --init'?")"
                        die "$(eval_gettext "Unable to find current revision in submodule path '\$path'")"
                fi
 
-               if ! test -z "$update"
-               then
-                       update_module=$update
-               fi
-
                if test "$subsha1" != "$sha1"
                then
                        subforce=$force