for-each-ref: report broken references correctly
[gitweb.git] / git-submodule.sh
index 4a30087768a16d77bfcec41889f58b64fdf5f89c..36797c3c00f4890cfb6f176f298e050da7eb5a34 100755 (executable)
@@ -9,7 +9,7 @@ USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <re
    or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
    or: $dashless [--quiet] init [--] [<path>...]
    or: $dashless [--quiet] deinit [-f|--force] [--] <path>...
-   or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
+   or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--reference <repository>] [--recursive] [--] [<path>...]
    or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
    or: $dashless [--quiet] foreach [--recursive] <command>
    or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
@@ -235,12 +235,18 @@ module_name()
                sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
        test -z "$name" &&
        die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
-       echo "$name"
+       printf '%s\n' "$name"
 }
 
 #
 # Clone a submodule
 #
+# $1 = submodule path
+# $2 = submodule name
+# $3 = URL to clone
+# $4 = reference repository to reuse (empty for independent)
+# $5 = depth argument for shallow clones (empty for deep)
+#
 # Prior to calling, cmd_update checks that a possibly existing
 # path is not a git repository.
 # Likewise, cmd_add checks that path does not exist at all,
@@ -285,9 +291,6 @@ module_clone()
        # resolve any symlinks that might be present in $PWD
        a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
        b=$(cd_to_toplevel && cd "$sm_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")"
@@ -302,10 +305,10 @@ module_clone()
        b=${b%/}
 
        # Turn each leading "*/" component into "../"
-       rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
-       echo "gitdir: $rel/$a" >"$sm_path/.git"
+       rel=$(printf '%s\n' "$b" | sed -e 's|[^/][^/]*|..|g')
+       printf '%s\n' "gitdir: $rel/$a" >"$sm_path/.git"
 
-       rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
+       rel=$(printf '%s\n' "$a" | sed -e 's|[^/][^/]*|..|g')
        (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
 }
 
@@ -386,11 +389,11 @@ cmd_add()
        sm_path=$2
 
        if test -z "$sm_path"; then
-               sm_path=$(echo "$repo" |
+               sm_path=$(printf '%s\n' "$repo" |
                        sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
        fi
 
-       if test -z "$repo" -o -z "$sm_path"; then
+       if test -z "$repo" || test -z "$sm_path"; then
                usage
        fi
 
@@ -420,7 +423,7 @@ cmd_add()
                sed -e '
                        s|//*|/|g
                        s|^\(\./\)*||
-                       s|/\./|/|g
+                       s|/\(\./\)*|/|g
                        :start
                        s|\([^/]*\)/\.\./||
                        tstart
@@ -447,7 +450,7 @@ Use -f if you really want to add it." >&2
        # perhaps the path exists and is already a git repo, else clone it
        if test -e "$sm_path"
        then
-               if test -d "$sm_path"/.git -o -f "$sm_path"/.git
+               if test -d "$sm_path"/.git || test -f "$sm_path"/.git
                then
                        eval_gettextln "Adding existing repo at '\$sm_path' to the index"
                else
@@ -805,17 +808,10 @@ cmd_update()
                        update_module=$update
                else
                        update_module=$(git config submodule."$name".update)
-                       case "$update_module" in
-                       '')
-                               ;; # Unset update mode
-                       checkout | rebase | merge | none)
-                               ;; # Known update modes
-                       !*)
-                               ;; # Custom update command
-                       *)
-                               die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
-                               ;;
-                       esac
+                       if test -z "$update_module"
+                       then
+                               update_module="checkout"
+                       fi
                fi
 
                displaypath=$(relative_path "$prefix$sm_path")
@@ -836,7 +832,7 @@ Maybe you want to use 'update --init'?")"
                        continue
                fi
 
-               if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
+               if ! test -d "$sm_path"/.git && ! test -f "$sm_path"/.git
                then
                        module_clone "$sm_path" "$name" "$url" "$reference" "$depth" || exit
                        cloned_modules="$cloned_modules;$name"
@@ -861,11 +857,11 @@ Maybe you want to use 'update --init'?")"
                        die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
                fi
 
-               if test "$subsha1" != "$sha1" -o -n "$force"
+               if test "$subsha1" != "$sha1" || test -n "$force"
                then
                        subforce=$force
                        # If we don't already have a -f flag and the submodule has never been checked out
-                       if test -z "$subsha1" -a -z "$force"
+                       if test -z "$subsha1" && test -z "$force"
                        then
                                subforce="-f"
                        fi
@@ -884,11 +880,16 @@ Maybe you want to use 'update --init'?")"
                        case ";$cloned_modules;" in
                        *";$name;"*)
                                # then there is no local change to integrate
-                               update_module= ;;
+                               update_module=checkout ;;
                        esac
 
                        must_die_on_failure=
                        case "$update_module" in
+                       checkout)
+                               command="git checkout $subforce -q"
+                               die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
+                               say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
+                               ;;
                        rebase)
                                command="git rebase"
                                die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
@@ -908,10 +909,7 @@ Maybe you want to use 'update --init'?")"
                                must_die_on_failure=yes
                                ;;
                        *)
-                               command="git checkout $subforce -q"
-                               die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
-                               say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
-                               ;;
+                               die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
                        esac
 
                        if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
@@ -1033,7 +1031,7 @@ cmd_summary() {
        then
                head=$rev
                test $# = 0 || shift
-       elif test -z "$1" -o "$1" = "HEAD"
+       elif test -z "$1" || test "$1" = "HEAD"
        then
                # before the first commit: compare with an empty tree
                head=$(git hash-object -w -t tree --stdin </dev/null)
@@ -1058,17 +1056,21 @@ cmd_summary() {
                while read mod_src mod_dst sha1_src sha1_dst status sm_path
                do
                        # Always show modules deleted or type-changed (blob<->module)
-                       test $status = D -o $status = T && echo "$sm_path" && continue
+                       if test "$status" = D || test "$status" = T
+                       then
+                               printf '%s\n' "$sm_path"
+                               continue
+                       fi
                        # Respect the ignore setting for --for-status.
                        if test -n "$for_status"
                        then
                                name=$(module_name "$sm_path")
                                ignore_config=$(get_submodule_config "$name" ignore none)
-                               test $status != A -a $ignore_config = all && continue
+                               test $status != A && test $ignore_config = all && continue
                        fi
                        # Also show added or modified modules which are checked out
                        GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
-                       echo "$sm_path"
+                       printf '%s\n' "$sm_path"
                done
        )
 
@@ -1124,7 +1126,7 @@ cmd_summary() {
                *)
                        errmsg=
                        total_commits=$(
-                       if test $mod_src = 160000 -a $mod_dst = 160000
+                       if test $mod_src = 160000 && test $mod_dst = 160000
                        then
                                range="$sha1_src...$sha1_dst"
                        elif test $mod_src = 160000
@@ -1161,7 +1163,7 @@ cmd_summary() {
                        # i.e. deleted or changed to blob
                        test $mod_dst = 160000 && echo "$errmsg"
                else
-                       if test $mod_src = 160000 -a $mod_dst = 160000
+                       if test $mod_src = 160000 && test $mod_dst = 160000
                        then
                                limit=
                                test $summary_limit -gt 0 && limit="-$summary_limit"
@@ -1232,7 +1234,11 @@ cmd_status()
                        say "U$sha1 $displaypath"
                        continue
                fi
-               if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
+               if test -z "$url" ||
+               {
+                       ! test -d "$sm_path"/.git &&
+                       ! test -f "$sm_path"/.git
+               }
                then
                        say "-$sha1 $displaypath"
                        continue;
@@ -1305,7 +1311,7 @@ cmd_sync()
                ./*|../*)
                        # rewrite foo/bar as ../.. to find path from
                        # submodule work tree to superproject work tree
-                       up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
+                       up_path="$(printf '%s\n' "$sm_path" | sed "s/[^/][^/]*/../g")" &&
                        # guarantee a trailing /
                        up_path=${up_path%/}/ &&
                        # path from submodule work tree to submodule origin repo
@@ -1401,7 +1407,7 @@ then
 fi
 
 # "--cached" is accepted only by "status" and "summary"
-if test -n "$cached" && test "$command" != status -a "$command" != summary
+if test -n "$cached" && test "$command" != status && test "$command" != summary
 then
        usage
 fi