git-submodule.shon commit Merge branch 'bp/status-rename-config' (5da4847)
   1#!/bin/sh
   2#
   3# git-submodule.sh: add, init, update or list git submodules
   4#
   5# Copyright (c) 2007 Lars Hjemli
   6
   7dashless=$(basename "$0" | sed -e 's/-/ /')
   8USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
   9   or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
  10   or: $dashless [--quiet] init [--] [<path>...]
  11   or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
  12   or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
  13   or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
  14   or: $dashless [--quiet] foreach [--recursive] <command>
  15   or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
  16   or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"
  17OPTIONS_SPEC=
  18SUBDIRECTORY_OK=Yes
  19. git-sh-setup
  20. git-parse-remote
  21require_work_tree
  22wt_prefix=$(git rev-parse --show-prefix)
  23cd_to_toplevel
  24
  25# Tell the rest of git that any URLs we get don't come
  26# directly from the user, so it can apply policy as appropriate.
  27GIT_PROTOCOL_FROM_USER=0
  28export GIT_PROTOCOL_FROM_USER
  29
  30command=
  31branch=
  32force=
  33reference=
  34cached=
  35recursive=
  36init=
  37files=
  38remote=
  39nofetch=
  40update=
  41prefix=
  42custom_name=
  43depth=
  44progress=
  45
  46die_if_unmatched ()
  47{
  48        if test "$1" = "#unmatched"
  49        then
  50                exit ${2:-1}
  51        fi
  52}
  53
  54#
  55# Print a submodule configuration setting
  56#
  57# $1 = submodule name
  58# $2 = option name
  59# $3 = default value
  60#
  61# Checks in the usual git-config places first (for overrides),
  62# otherwise it falls back on .gitmodules.  This allows you to
  63# distribute project-wide defaults in .gitmodules, while still
  64# customizing individual repositories if necessary.  If the option is
  65# not in .gitmodules either, print a default value.
  66#
  67get_submodule_config () {
  68        name="$1"
  69        option="$2"
  70        default="$3"
  71        value=$(git config submodule."$name"."$option")
  72        if test -z "$value"
  73        then
  74                value=$(git config -f .gitmodules submodule."$name"."$option")
  75        fi
  76        printf '%s' "${value:-$default}"
  77}
  78
  79isnumber()
  80{
  81        n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
  82}
  83
  84# Sanitize the local git environment for use within a submodule. We
  85# can't simply use clear_local_git_env since we want to preserve some
  86# of the settings from GIT_CONFIG_PARAMETERS.
  87sanitize_submodule_env()
  88{
  89        save_config=$GIT_CONFIG_PARAMETERS
  90        clear_local_git_env
  91        GIT_CONFIG_PARAMETERS=$save_config
  92        export GIT_CONFIG_PARAMETERS
  93}
  94
  95#
  96# Add a new submodule to the working tree, .gitmodules and the index
  97#
  98# $@ = repo path
  99#
 100# optional branch is stored in global branch variable
 101#
 102cmd_add()
 103{
 104        # parse $args after "submodule ... add".
 105        reference_path=
 106        while test $# -ne 0
 107        do
 108                case "$1" in
 109                -b | --branch)
 110                        case "$2" in '') usage ;; esac
 111                        branch=$2
 112                        shift
 113                        ;;
 114                -f | --force)
 115                        force=$1
 116                        ;;
 117                -q|--quiet)
 118                        GIT_QUIET=1
 119                        ;;
 120                --reference)
 121                        case "$2" in '') usage ;; esac
 122                        reference_path=$2
 123                        shift
 124                        ;;
 125                --reference=*)
 126                        reference_path="${1#--reference=}"
 127                        ;;
 128                --name)
 129                        case "$2" in '') usage ;; esac
 130                        custom_name=$2
 131                        shift
 132                        ;;
 133                --depth)
 134                        case "$2" in '') usage ;; esac
 135                        depth="--depth=$2"
 136                        shift
 137                        ;;
 138                --depth=*)
 139                        depth=$1
 140                        ;;
 141                --)
 142                        shift
 143                        break
 144                        ;;
 145                -*)
 146                        usage
 147                        ;;
 148                *)
 149                        break
 150                        ;;
 151                esac
 152                shift
 153        done
 154
 155        if test -n "$reference_path"
 156        then
 157                is_absolute_path "$reference_path" ||
 158                reference_path="$wt_prefix$reference_path"
 159
 160                reference="--reference=$reference_path"
 161        fi
 162
 163        repo=$1
 164        sm_path=$2
 165
 166        if test -z "$sm_path"; then
 167                sm_path=$(printf '%s\n' "$repo" |
 168                        sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 169        fi
 170
 171        if test -z "$repo" || test -z "$sm_path"; then
 172                usage
 173        fi
 174
 175        is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
 176
 177        # assure repo is absolute or relative to parent
 178        case "$repo" in
 179        ./*|../*)
 180                test -z "$wt_prefix" ||
 181                die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
 182
 183                # dereference source url relative to parent's url
 184                realrepo=$(git submodule--helper resolve-relative-url "$repo") || exit
 185                ;;
 186        *:*|/*)
 187                # absolute url
 188                realrepo=$repo
 189                ;;
 190        *)
 191                die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
 192        ;;
 193        esac
 194
 195        # normalize path:
 196        # multiple //; leading ./; /./; /../; trailing /
 197        sm_path=$(printf '%s/\n' "$sm_path" |
 198                sed -e '
 199                        s|//*|/|g
 200                        s|^\(\./\)*||
 201                        s|/\(\./\)*|/|g
 202                        :start
 203                        s|\([^/]*\)/\.\./||
 204                        tstart
 205                        s|/*$||
 206                ')
 207        if test -z "$force"
 208        then
 209                git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
 210                die "$(eval_gettext "'\$sm_path' already exists in the index")"
 211        else
 212                git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
 213                die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
 214        fi
 215
 216        if test -z "$force" &&
 217                ! git add --dry-run --ignore-missing --no-warn-embedded-repo "$sm_path" > /dev/null 2>&1
 218        then
 219                eval_gettextln "The following path is ignored by one of your .gitignore files:
 220\$sm_path
 221Use -f if you really want to add it." >&2
 222                exit 1
 223        fi
 224
 225        if test -n "$custom_name"
 226        then
 227                sm_name="$custom_name"
 228        else
 229                sm_name="$sm_path"
 230        fi
 231
 232        if ! git submodule--helper check-name "$sm_name"
 233        then
 234                die "$(eval_gettext "'$sm_name' is not a valid submodule name")"
 235        fi
 236
 237        # perhaps the path exists and is already a git repo, else clone it
 238        if test -e "$sm_path"
 239        then
 240                if test -d "$sm_path"/.git || test -f "$sm_path"/.git
 241                then
 242                        eval_gettextln "Adding existing repo at '\$sm_path' to the index"
 243                else
 244                        die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
 245                fi
 246
 247        else
 248                if test -d ".git/modules/$sm_name"
 249                then
 250                        if test -z "$force"
 251                        then
 252                                eval_gettextln >&2 "A git directory for '\$sm_name' is found locally with remote(s):"
 253                                GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^,"  ", -e s,' (fetch)',, >&2
 254                                die "$(eval_gettextln "\
 255If you want to reuse this local git directory instead of cloning again from
 256  \$realrepo
 257use the '--force' option. If the local git directory is not the correct repo
 258or you are unsure what this means choose another name with the '--name' option.")"
 259                        else
 260                                eval_gettextln "Reactivating local git directory for submodule '\$sm_name'."
 261                        fi
 262                fi
 263                git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${depth:+"$depth"} || exit
 264                (
 265                        sanitize_submodule_env
 266                        cd "$sm_path" &&
 267                        # ash fails to wordsplit ${branch:+-b "$branch"...}
 268                        case "$branch" in
 269                        '') git checkout -f -q ;;
 270                        ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
 271                        esac
 272                ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
 273        fi
 274        git config submodule."$sm_name".url "$realrepo"
 275
 276        git add --no-warn-embedded-repo $force "$sm_path" ||
 277        die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
 278
 279        git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
 280        git config -f .gitmodules submodule."$sm_name".url "$repo" &&
 281        if test -n "$branch"
 282        then
 283                git config -f .gitmodules submodule."$sm_name".branch "$branch"
 284        fi &&
 285        git add --force .gitmodules ||
 286        die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
 287
 288        # NEEDSWORK: In a multi-working-tree world, this needs to be
 289        # set in the per-worktree config.
 290        if git config --get submodule.active >/dev/null
 291        then
 292                # If the submodule being adding isn't already covered by the
 293                # current configured pathspec, set the submodule's active flag
 294                if ! git submodule--helper is-active "$sm_path"
 295                then
 296                        git config submodule."$sm_name".active "true"
 297                fi
 298        else
 299                git config submodule."$sm_name".active "true"
 300        fi
 301}
 302
 303#
 304# Execute an arbitrary command sequence in each checked out
 305# submodule
 306#
 307# $@ = command to execute
 308#
 309cmd_foreach()
 310{
 311        # parse $args after "submodule ... foreach".
 312        while test $# -ne 0
 313        do
 314                case "$1" in
 315                -q|--quiet)
 316                        GIT_QUIET=1
 317                        ;;
 318                --recursive)
 319                        recursive=1
 320                        ;;
 321                -*)
 322                        usage
 323                        ;;
 324                *)
 325                        break
 326                        ;;
 327                esac
 328                shift
 329        done
 330
 331        toplevel=$(pwd)
 332
 333        # dup stdin so that it can be restored when running the external
 334        # command in the subshell (and a recursive call to this function)
 335        exec 3<&0
 336
 337        {
 338                git submodule--helper list --prefix "$wt_prefix" ||
 339                echo "#unmatched" $?
 340        } |
 341        while read -r mode sha1 stage sm_path
 342        do
 343                die_if_unmatched "$mode" "$sha1"
 344                if test -e "$sm_path"/.git
 345                then
 346                        displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
 347                        say "$(eval_gettext "Entering '\$displaypath'")"
 348                        name=$(git submodule--helper name "$sm_path")
 349                        (
 350                                prefix="$prefix$sm_path/"
 351                                sanitize_submodule_env
 352                                cd "$sm_path" &&
 353                                sm_path=$(git submodule--helper relative-path "$sm_path" "$wt_prefix") &&
 354                                # we make $path available to scripts ...
 355                                path=$sm_path &&
 356                                if test $# -eq 1
 357                                then
 358                                        eval "$1"
 359                                else
 360                                        "$@"
 361                                fi &&
 362                                if test -n "$recursive"
 363                                then
 364                                        cmd_foreach "--recursive" "$@"
 365                                fi
 366                        ) <&3 3<&- ||
 367                        die "$(eval_gettext "Stopping at '\$displaypath'; script returned non-zero status.")"
 368                fi
 369        done
 370}
 371
 372#
 373# Register submodules in .git/config
 374#
 375# $@ = requested paths (default to all)
 376#
 377cmd_init()
 378{
 379        # parse $args after "submodule ... init".
 380        while test $# -ne 0
 381        do
 382                case "$1" in
 383                -q|--quiet)
 384                        GIT_QUIET=1
 385                        ;;
 386                --)
 387                        shift
 388                        break
 389                        ;;
 390                -*)
 391                        usage
 392                        ;;
 393                *)
 394                        break
 395                        ;;
 396                esac
 397                shift
 398        done
 399
 400        git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet}  "$@"
 401}
 402
 403#
 404# Unregister submodules from .git/config and remove their work tree
 405#
 406cmd_deinit()
 407{
 408        # parse $args after "submodule ... deinit".
 409        deinit_all=
 410        while test $# -ne 0
 411        do
 412                case "$1" in
 413                -f|--force)
 414                        force=$1
 415                        ;;
 416                -q|--quiet)
 417                        GIT_QUIET=1
 418                        ;;
 419                --all)
 420                        deinit_all=t
 421                        ;;
 422                --)
 423                        shift
 424                        break
 425                        ;;
 426                -*)
 427                        usage
 428                        ;;
 429                *)
 430                        break
 431                        ;;
 432                esac
 433                shift
 434        done
 435
 436        git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} ${force:+--force} ${deinit_all:+--all} "$@"
 437}
 438
 439is_tip_reachable () (
 440        sanitize_submodule_env &&
 441        cd "$1" &&
 442        rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
 443        test -z "$rev"
 444)
 445
 446fetch_in_submodule () (
 447        sanitize_submodule_env &&
 448        cd "$1" &&
 449        case "$2" in
 450        '')
 451                git fetch ;;
 452        *)
 453                shift
 454                git fetch $(get_default_remote) "$@" ;;
 455        esac
 456)
 457
 458#
 459# Update each submodule path to correct revision, using clone and checkout as needed
 460#
 461# $@ = requested paths (default to all)
 462#
 463cmd_update()
 464{
 465        # parse $args after "submodule ... update".
 466        while test $# -ne 0
 467        do
 468                case "$1" in
 469                -q|--quiet)
 470                        GIT_QUIET=1
 471                        ;;
 472                --progress)
 473                        progress="--progress"
 474                        ;;
 475                -i|--init)
 476                        init=1
 477                        ;;
 478                --remote)
 479                        remote=1
 480                        ;;
 481                -N|--no-fetch)
 482                        nofetch=1
 483                        ;;
 484                -f|--force)
 485                        force=$1
 486                        ;;
 487                -r|--rebase)
 488                        update="rebase"
 489                        ;;
 490                --reference)
 491                        case "$2" in '') usage ;; esac
 492                        reference="--reference=$2"
 493                        shift
 494                        ;;
 495                --reference=*)
 496                        reference="$1"
 497                        ;;
 498                -m|--merge)
 499                        update="merge"
 500                        ;;
 501                --recursive)
 502                        recursive=1
 503                        ;;
 504                --checkout)
 505                        update="checkout"
 506                        ;;
 507                --recommend-shallow)
 508                        recommend_shallow="--recommend-shallow"
 509                        ;;
 510                --no-recommend-shallow)
 511                        recommend_shallow="--no-recommend-shallow"
 512                        ;;
 513                --depth)
 514                        case "$2" in '') usage ;; esac
 515                        depth="--depth=$2"
 516                        shift
 517                        ;;
 518                --depth=*)
 519                        depth=$1
 520                        ;;
 521                -j|--jobs)
 522                        case "$2" in '') usage ;; esac
 523                        jobs="--jobs=$2"
 524                        shift
 525                        ;;
 526                --jobs=*)
 527                        jobs=$1
 528                        ;;
 529                --)
 530                        shift
 531                        break
 532                        ;;
 533                -*)
 534                        usage
 535                        ;;
 536                *)
 537                        break
 538                        ;;
 539                esac
 540                shift
 541        done
 542
 543        if test -n "$init"
 544        then
 545                cmd_init "--" "$@" || return
 546        fi
 547
 548        {
 549        git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
 550                ${progress:+"$progress"} \
 551                ${wt_prefix:+--prefix "$wt_prefix"} \
 552                ${prefix:+--recursive-prefix "$prefix"} \
 553                ${update:+--update "$update"} \
 554                ${reference:+"$reference"} \
 555                ${depth:+--depth "$depth"} \
 556                ${recommend_shallow:+"$recommend_shallow"} \
 557                ${jobs:+$jobs} \
 558                "$@" || echo "#unmatched" $?
 559        } | {
 560        err=
 561        while read -r mode sha1 stage just_cloned sm_path
 562        do
 563                die_if_unmatched "$mode" "$sha1"
 564
 565                name=$(git submodule--helper name "$sm_path") || exit
 566                if ! test -z "$update"
 567                then
 568                        update_module=$update
 569                else
 570                        update_module=$(git config submodule."$name".update)
 571                        if test -z "$update_module"
 572                        then
 573                                update_module="checkout"
 574                        fi
 575                fi
 576
 577                displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
 578
 579                if test $just_cloned -eq 1
 580                then
 581                        subsha1=
 582                        case "$update_module" in
 583                        merge | rebase | none)
 584                                update_module=checkout ;;
 585                        esac
 586                else
 587                        subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
 588                                git rev-parse --verify HEAD) ||
 589                        die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
 590                fi
 591
 592                if test -n "$remote"
 593                then
 594                        branch=$(git submodule--helper remote-branch "$sm_path")
 595                        if test -z "$nofetch"
 596                        then
 597                                # Fetch remote before determining tracking $sha1
 598                                fetch_in_submodule "$sm_path" $depth ||
 599                                die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
 600                        fi
 601                        remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
 602                        sha1=$(sanitize_submodule_env; cd "$sm_path" &&
 603                                git rev-parse --verify "${remote_name}/${branch}") ||
 604                        die "$(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
 605                fi
 606
 607                if test "$subsha1" != "$sha1" || test -n "$force"
 608                then
 609                        subforce=$force
 610                        # If we don't already have a -f flag and the submodule has never been checked out
 611                        if test -z "$subsha1" && test -z "$force"
 612                        then
 613                                subforce="-f"
 614                        fi
 615
 616                        if test -z "$nofetch"
 617                        then
 618                                # Run fetch only if $sha1 isn't present or it
 619                                # is not reachable from a ref.
 620                                is_tip_reachable "$sm_path" "$sha1" ||
 621                                fetch_in_submodule "$sm_path" $depth ||
 622                                die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
 623
 624                                # Now we tried the usual fetch, but $sha1 may
 625                                # not be reachable from any of the refs
 626                                is_tip_reachable "$sm_path" "$sha1" ||
 627                                fetch_in_submodule "$sm_path" $depth "$sha1" ||
 628                                die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain \$sha1. Direct fetching of that commit failed.")"
 629                        fi
 630
 631                        must_die_on_failure=
 632                        case "$update_module" in
 633                        checkout)
 634                                command="git checkout $subforce -q"
 635                                die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
 636                                say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
 637                                ;;
 638                        rebase)
 639                                command="git rebase"
 640                                die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
 641                                say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
 642                                must_die_on_failure=yes
 643                                ;;
 644                        merge)
 645                                command="git merge"
 646                                die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
 647                                say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
 648                                must_die_on_failure=yes
 649                                ;;
 650                        !*)
 651                                command="${update_module#!}"
 652                                die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$displaypath'")"
 653                                say_msg="$(eval_gettext "Submodule path '\$displaypath': '\$command \$sha1'")"
 654                                must_die_on_failure=yes
 655                                ;;
 656                        *)
 657                                die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
 658                        esac
 659
 660                        if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
 661                        then
 662                                say "$say_msg"
 663                        elif test -n "$must_die_on_failure"
 664                        then
 665                                die_with_status 2 "$die_msg"
 666                        else
 667                                err="${err};$die_msg"
 668                                continue
 669                        fi
 670                fi
 671
 672                if test -n "$recursive"
 673                then
 674                        (
 675                                prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
 676                                wt_prefix=
 677                                sanitize_submodule_env
 678                                cd "$sm_path" &&
 679                                eval cmd_update
 680                        )
 681                        res=$?
 682                        if test $res -gt 0
 683                        then
 684                                die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
 685                                if test $res -ne 2
 686                                then
 687                                        err="${err};$die_msg"
 688                                        continue
 689                                else
 690                                        die_with_status $res "$die_msg"
 691                                fi
 692                        fi
 693                fi
 694        done
 695
 696        if test -n "$err"
 697        then
 698                OIFS=$IFS
 699                IFS=';'
 700                for e in $err
 701                do
 702                        if test -n "$e"
 703                        then
 704                                echo >&2 "$e"
 705                        fi
 706                done
 707                IFS=$OIFS
 708                exit 1
 709        fi
 710        }
 711}
 712
 713#
 714# Show commit summary for submodules in index or working tree
 715#
 716# If '--cached' is given, show summary between index and given commit,
 717# or between working tree and given commit
 718#
 719# $@ = [commit (default 'HEAD'),] requested paths (default all)
 720#
 721cmd_summary() {
 722        summary_limit=-1
 723        for_status=
 724        diff_cmd=diff-index
 725
 726        # parse $args after "submodule ... summary".
 727        while test $# -ne 0
 728        do
 729                case "$1" in
 730                --cached)
 731                        cached="$1"
 732                        ;;
 733                --files)
 734                        files="$1"
 735                        ;;
 736                --for-status)
 737                        for_status="$1"
 738                        ;;
 739                -n|--summary-limit)
 740                        summary_limit="$2"
 741                        isnumber "$summary_limit" || usage
 742                        shift
 743                        ;;
 744                --summary-limit=*)
 745                        summary_limit="${1#--summary-limit=}"
 746                        isnumber "$summary_limit" || usage
 747                        ;;
 748                --)
 749                        shift
 750                        break
 751                        ;;
 752                -*)
 753                        usage
 754                        ;;
 755                *)
 756                        break
 757                        ;;
 758                esac
 759                shift
 760        done
 761
 762        test $summary_limit = 0 && return
 763
 764        if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
 765        then
 766                head=$rev
 767                test $# = 0 || shift
 768        elif test -z "$1" || test "$1" = "HEAD"
 769        then
 770                # before the first commit: compare with an empty tree
 771                head=$(git hash-object -w -t tree --stdin </dev/null)
 772                test -z "$1" || shift
 773        else
 774                head="HEAD"
 775        fi
 776
 777        if [ -n "$files" ]
 778        then
 779                test -n "$cached" &&
 780                die "$(gettext "The --cached option cannot be used with the --files option")"
 781                diff_cmd=diff-files
 782                head=
 783        fi
 784
 785        cd_to_toplevel
 786        eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
 787        # Get modified modules cared by user
 788        modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
 789                sane_egrep '^:([0-7]* )?160000' |
 790                while read -r mod_src mod_dst sha1_src sha1_dst status sm_path
 791                do
 792                        # Always show modules deleted or type-changed (blob<->module)
 793                        if test "$status" = D || test "$status" = T
 794                        then
 795                                printf '%s\n' "$sm_path"
 796                                continue
 797                        fi
 798                        # Respect the ignore setting for --for-status.
 799                        if test -n "$for_status"
 800                        then
 801                                name=$(git submodule--helper name "$sm_path")
 802                                ignore_config=$(get_submodule_config "$name" ignore none)
 803                                test $status != A && test $ignore_config = all && continue
 804                        fi
 805                        # Also show added or modified modules which are checked out
 806                        GIT_DIR="$sm_path/.git" git rev-parse --git-dir >/dev/null 2>&1 &&
 807                        printf '%s\n' "$sm_path"
 808                done
 809        )
 810
 811        test -z "$modules" && return
 812
 813        git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
 814        sane_egrep '^:([0-7]* )?160000' |
 815        cut -c2- |
 816        while read -r mod_src mod_dst sha1_src sha1_dst status name
 817        do
 818                if test -z "$cached" &&
 819                        test $sha1_dst = 0000000000000000000000000000000000000000
 820                then
 821                        case "$mod_dst" in
 822                        160000)
 823                                sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
 824                                ;;
 825                        100644 | 100755 | 120000)
 826                                sha1_dst=$(git hash-object $name)
 827                                ;;
 828                        000000)
 829                                ;; # removed
 830                        *)
 831                                # unexpected type
 832                                eval_gettextln "unexpected mode \$mod_dst" >&2
 833                                continue ;;
 834                        esac
 835                fi
 836                missing_src=
 837                missing_dst=
 838
 839                test $mod_src = 160000 &&
 840                ! GIT_DIR="$name/.git" git rev-parse -q --verify $sha1_src^0 >/dev/null &&
 841                missing_src=t
 842
 843                test $mod_dst = 160000 &&
 844                ! GIT_DIR="$name/.git" git rev-parse -q --verify $sha1_dst^0 >/dev/null &&
 845                missing_dst=t
 846
 847                display_name=$(git submodule--helper relative-path "$name" "$wt_prefix")
 848
 849                total_commits=
 850                case "$missing_src,$missing_dst" in
 851                t,)
 852                        errmsg="$(eval_gettext "  Warn: \$display_name doesn't contain commit \$sha1_src")"
 853                        ;;
 854                ,t)
 855                        errmsg="$(eval_gettext "  Warn: \$display_name doesn't contain commit \$sha1_dst")"
 856                        ;;
 857                t,t)
 858                        errmsg="$(eval_gettext "  Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
 859                        ;;
 860                *)
 861                        errmsg=
 862                        total_commits=$(
 863                        if test $mod_src = 160000 && test $mod_dst = 160000
 864                        then
 865                                range="$sha1_src...$sha1_dst"
 866                        elif test $mod_src = 160000
 867                        then
 868                                range=$sha1_src
 869                        else
 870                                range=$sha1_dst
 871                        fi
 872                        GIT_DIR="$name/.git" \
 873                        git rev-list --first-parent $range -- | wc -l
 874                        )
 875                        total_commits=" ($(($total_commits + 0)))"
 876                        ;;
 877                esac
 878
 879                sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
 880                sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
 881                if test $status = T
 882                then
 883                        blob="$(gettext "blob")"
 884                        submodule="$(gettext "submodule")"
 885                        if test $mod_dst = 160000
 886                        then
 887                                echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
 888                        else
 889                                echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
 890                        fi
 891                else
 892                        echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
 893                fi
 894                if test -n "$errmsg"
 895                then
 896                        # Don't give error msg for modification whose dst is not submodule
 897                        # i.e. deleted or changed to blob
 898                        test $mod_dst = 160000 && echo "$errmsg"
 899                else
 900                        if test $mod_src = 160000 && test $mod_dst = 160000
 901                        then
 902                                limit=
 903                                test $summary_limit -gt 0 && limit="-$summary_limit"
 904                                GIT_DIR="$name/.git" \
 905                                git log $limit --pretty='format:  %m %s' \
 906                                --first-parent $sha1_src...$sha1_dst
 907                        elif test $mod_dst = 160000
 908                        then
 909                                GIT_DIR="$name/.git" \
 910                                git log --pretty='format:  > %s' -1 $sha1_dst
 911                        else
 912                                GIT_DIR="$name/.git" \
 913                                git log --pretty='format:  < %s' -1 $sha1_src
 914                        fi
 915                        echo
 916                fi
 917                echo
 918        done
 919}
 920#
 921# List all submodules, prefixed with:
 922#  - submodule not initialized
 923#  + different revision checked out
 924#
 925# If --cached was specified the revision in the index will be printed
 926# instead of the currently checked out revision.
 927#
 928# $@ = requested paths (default to all)
 929#
 930cmd_status()
 931{
 932        # parse $args after "submodule ... status".
 933        while test $# -ne 0
 934        do
 935                case "$1" in
 936                -q|--quiet)
 937                        GIT_QUIET=1
 938                        ;;
 939                --cached)
 940                        cached=1
 941                        ;;
 942                --recursive)
 943                        recursive=1
 944                        ;;
 945                --)
 946                        shift
 947                        break
 948                        ;;
 949                -*)
 950                        usage
 951                        ;;
 952                *)
 953                        break
 954                        ;;
 955                esac
 956                shift
 957        done
 958
 959        git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} "$@"
 960}
 961#
 962# Sync remote urls for submodules
 963# This makes the value for remote.$remote.url match the value
 964# specified in .gitmodules.
 965#
 966cmd_sync()
 967{
 968        while test $# -ne 0
 969        do
 970                case "$1" in
 971                -q|--quiet)
 972                        GIT_QUIET=1
 973                        shift
 974                        ;;
 975                --recursive)
 976                        recursive=1
 977                        shift
 978                        ;;
 979                --)
 980                        shift
 981                        break
 982                        ;;
 983                -*)
 984                        usage
 985                        ;;
 986                *)
 987                        break
 988                        ;;
 989                esac
 990        done
 991
 992        git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@"
 993}
 994
 995cmd_absorbgitdirs()
 996{
 997        git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
 998}
 999
1000# This loop parses the command line arguments to find the
1001# subcommand name to dispatch.  Parsing of the subcommand specific
1002# options are primarily done by the subcommand implementations.
1003# Subcommand specific options such as --branch and --cached are
1004# parsed here as well, for backward compatibility.
1005
1006while test $# != 0 && test -z "$command"
1007do
1008        case "$1" in
1009        add | foreach | init | deinit | update | status | summary | sync | absorbgitdirs)
1010                command=$1
1011                ;;
1012        -q|--quiet)
1013                GIT_QUIET=1
1014                ;;
1015        -b|--branch)
1016                case "$2" in
1017                '')
1018                        usage
1019                        ;;
1020                esac
1021                branch="$2"; shift
1022                ;;
1023        --cached)
1024                cached="$1"
1025                ;;
1026        --)
1027                break
1028                ;;
1029        -*)
1030                usage
1031                ;;
1032        *)
1033                break
1034                ;;
1035        esac
1036        shift
1037done
1038
1039# No command word defaults to "status"
1040if test -z "$command"
1041then
1042    if test $# = 0
1043    then
1044        command=status
1045    else
1046        usage
1047    fi
1048fi
1049
1050# "-b branch" is accepted only by "add"
1051if test -n "$branch" && test "$command" != add
1052then
1053        usage
1054fi
1055
1056# "--cached" is accepted only by "status" and "summary"
1057if test -n "$cached" && test "$command" != status && test "$command" != summary
1058then
1059        usage
1060fi
1061
1062"cmd_$command" "$@"