git-submodule.shon commit Merge branch 'rr/prompt-revert-head' into maint (ad62fd0)
   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] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
  12   or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
  13   or: $dashless [--quiet] foreach [--recursive] <command>
  14   or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
  15OPTIONS_SPEC=
  16. git-sh-setup
  17. git-sh-i18n
  18. git-parse-remote
  19require_work_tree
  20
  21command=
  22branch=
  23force=
  24reference=
  25cached=
  26recursive=
  27init=
  28files=
  29remote=
  30nofetch=
  31update=
  32prefix=
  33custom_name=
  34
  35# The function takes at most 2 arguments. The first argument is the
  36# URL that navigates to the submodule origin repo. When relative, this URL
  37# is relative to the superproject origin URL repo. The second up_path
  38# argument, if specified, is the relative path that navigates
  39# from the submodule working tree to the superproject working tree.
  40#
  41# The output of the function is the origin URL of the submodule.
  42#
  43# The output will either be an absolute URL or filesystem path (if the
  44# superproject origin URL is an absolute URL or filesystem path,
  45# respectively) or a relative file system path (if the superproject
  46# origin URL is a relative file system path).
  47#
  48# When the output is a relative file system path, the path is either
  49# relative to the submodule working tree, if up_path is specified, or to
  50# the superproject working tree otherwise.
  51resolve_relative_url ()
  52{
  53        remote=$(get_default_remote)
  54        remoteurl=$(git config "remote.$remote.url") ||
  55                remoteurl=$(pwd) # the repository is its own authoritative upstream
  56        url="$1"
  57        remoteurl=${remoteurl%/}
  58        sep=/
  59        up_path="$2"
  60
  61        case "$remoteurl" in
  62        *:*|/*)
  63                is_relative=
  64                ;;
  65        ./*|../*)
  66                is_relative=t
  67                ;;
  68        *)
  69                is_relative=t
  70                remoteurl="./$remoteurl"
  71                ;;
  72        esac
  73
  74        while test -n "$url"
  75        do
  76                case "$url" in
  77                ../*)
  78                        url="${url#../}"
  79                        case "$remoteurl" in
  80                        */*)
  81                                remoteurl="${remoteurl%/*}"
  82                                ;;
  83                        *:*)
  84                                remoteurl="${remoteurl%:*}"
  85                                sep=:
  86                                ;;
  87                        *)
  88                                if test -z "$is_relative" || test "." = "$remoteurl"
  89                                then
  90                                        die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
  91                                else
  92                                        remoteurl=.
  93                                fi
  94                                ;;
  95                        esac
  96                        ;;
  97                ./*)
  98                        url="${url#./}"
  99                        ;;
 100                *)
 101                        break;;
 102                esac
 103        done
 104        remoteurl="$remoteurl$sep${url%/}"
 105        echo "${is_relative:+${up_path}}${remoteurl#./}"
 106}
 107
 108#
 109# Get submodule info for registered submodules
 110# $@ = path to limit submodule list
 111#
 112module_list()
 113{
 114        (
 115                git ls-files --error-unmatch --stage -- "$@" ||
 116                echo "unmatched pathspec exists"
 117        ) |
 118        perl -e '
 119        my %unmerged = ();
 120        my ($null_sha1) = ("0" x 40);
 121        my @out = ();
 122        my $unmatched = 0;
 123        while (<STDIN>) {
 124                if (/^unmatched pathspec/) {
 125                        $unmatched = 1;
 126                        next;
 127                }
 128                chomp;
 129                my ($mode, $sha1, $stage, $path) =
 130                        /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
 131                next unless $mode eq "160000";
 132                if ($stage ne "0") {
 133                        if (!$unmerged{$path}++) {
 134                                push @out, "$mode $null_sha1 U\t$path\n";
 135                        }
 136                        next;
 137                }
 138                push @out, "$_\n";
 139        }
 140        if ($unmatched) {
 141                print "#unmatched\n";
 142        } else {
 143                print for (@out);
 144        }
 145        '
 146}
 147
 148die_if_unmatched ()
 149{
 150        if test "$1" = "#unmatched"
 151        then
 152                exit 1
 153        fi
 154}
 155
 156#
 157# Print a submodule configuration setting
 158#
 159# $1 = submodule name
 160# $2 = option name
 161# $3 = default value
 162#
 163# Checks in the usual git-config places first (for overrides),
 164# otherwise it falls back on .gitmodules.  This allows you to
 165# distribute project-wide defaults in .gitmodules, while still
 166# customizing individual repositories if necessary.  If the option is
 167# not in .gitmodules either, print a default value.
 168#
 169get_submodule_config () {
 170        name="$1"
 171        option="$2"
 172        default="$3"
 173        value=$(git config submodule."$name"."$option")
 174        if test -z "$value"
 175        then
 176                value=$(git config -f .gitmodules submodule."$name"."$option")
 177        fi
 178        printf '%s' "${value:-$default}"
 179}
 180
 181
 182#
 183# Map submodule path to submodule name
 184#
 185# $1 = path
 186#
 187module_name()
 188{
 189        # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
 190        sm_path="$1"
 191        re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
 192        name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
 193                sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
 194        test -z "$name" &&
 195        die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
 196        echo "$name"
 197}
 198
 199#
 200# Clone a submodule
 201#
 202# Prior to calling, cmd_update checks that a possibly existing
 203# path is not a git repository.
 204# Likewise, cmd_add checks that path does not exist at all,
 205# since it is the location of a new submodule.
 206#
 207module_clone()
 208{
 209        sm_path=$1
 210        name=$2
 211        url=$3
 212        reference="$4"
 213        quiet=
 214        if test -n "$GIT_QUIET"
 215        then
 216                quiet=-q
 217        fi
 218
 219        gitdir=
 220        gitdir_base=
 221        base_name=$(dirname "$name")
 222
 223        gitdir=$(git rev-parse --git-dir)
 224        gitdir_base="$gitdir/modules/$base_name"
 225        gitdir="$gitdir/modules/$name"
 226
 227        if test -d "$gitdir"
 228        then
 229                mkdir -p "$sm_path"
 230                rm -f "$gitdir/index"
 231        else
 232                mkdir -p "$gitdir_base"
 233                (
 234                        clear_local_git_env
 235                        git clone $quiet -n ${reference:+"$reference"} \
 236                                --separate-git-dir "$gitdir" "$url" "$sm_path"
 237                ) ||
 238                die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
 239        fi
 240
 241        # We already are at the root of the work tree but cd_to_toplevel will
 242        # resolve any symlinks that might be present in $PWD
 243        a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
 244        b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
 245        # normalize Windows-style absolute paths to POSIX-style absolute paths
 246        case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
 247        case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
 248        # Remove all common leading directories after a sanity check
 249        if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
 250                die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
 251        fi
 252        while test "${a%%/*}" = "${b%%/*}"
 253        do
 254                a=${a#*/}
 255                b=${b#*/}
 256        done
 257        # Now chop off the trailing '/'s that were added in the beginning
 258        a=${a%/}
 259        b=${b%/}
 260
 261        # Turn each leading "*/" component into "../"
 262        rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
 263        echo "gitdir: $rel/$a" >"$sm_path/.git"
 264
 265        rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
 266        (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
 267}
 268
 269isnumber()
 270{
 271        n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
 272}
 273
 274#
 275# Add a new submodule to the working tree, .gitmodules and the index
 276#
 277# $@ = repo path
 278#
 279# optional branch is stored in global branch variable
 280#
 281cmd_add()
 282{
 283        # parse $args after "submodule ... add".
 284        while test $# -ne 0
 285        do
 286                case "$1" in
 287                -b | --branch)
 288                        case "$2" in '') usage ;; esac
 289                        branch=$2
 290                        shift
 291                        ;;
 292                -f | --force)
 293                        force=$1
 294                        ;;
 295                -q|--quiet)
 296                        GIT_QUIET=1
 297                        ;;
 298                --reference)
 299                        case "$2" in '') usage ;; esac
 300                        reference="--reference=$2"
 301                        shift
 302                        ;;
 303                --reference=*)
 304                        reference="$1"
 305                        ;;
 306                --name)
 307                        case "$2" in '') usage ;; esac
 308                        custom_name=$2
 309                        shift
 310                        ;;
 311                --)
 312                        shift
 313                        break
 314                        ;;
 315                -*)
 316                        usage
 317                        ;;
 318                *)
 319                        break
 320                        ;;
 321                esac
 322                shift
 323        done
 324
 325        repo=$1
 326        sm_path=$2
 327
 328        if test -z "$sm_path"; then
 329                sm_path=$(echo "$repo" |
 330                        sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 331        fi
 332
 333        if test -z "$repo" -o -z "$sm_path"; then
 334                usage
 335        fi
 336
 337        # assure repo is absolute or relative to parent
 338        case "$repo" in
 339        ./*|../*)
 340                # dereference source url relative to parent's url
 341                realrepo=$(resolve_relative_url "$repo") || exit
 342                ;;
 343        *:*|/*)
 344                # absolute url
 345                realrepo=$repo
 346                ;;
 347        *)
 348                die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
 349        ;;
 350        esac
 351
 352        # normalize path:
 353        # multiple //; leading ./; /./; /../; trailing /
 354        sm_path=$(printf '%s/\n' "$sm_path" |
 355                sed -e '
 356                        s|//*|/|g
 357                        s|^\(\./\)*||
 358                        s|/\./|/|g
 359                        :start
 360                        s|\([^/]*\)/\.\./||
 361                        tstart
 362                        s|/*$||
 363                ')
 364        git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
 365        die "$(eval_gettext "'\$sm_path' already exists in the index")"
 366
 367        if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
 368        then
 369                eval_gettextln "The following path is ignored by one of your .gitignore files:
 370\$sm_path
 371Use -f if you really want to add it." >&2
 372                exit 1
 373        fi
 374
 375        if test -n "$custom_name"
 376        then
 377                sm_name="$custom_name"
 378        else
 379                sm_name="$sm_path"
 380        fi
 381
 382        # perhaps the path exists and is already a git repo, else clone it
 383        if test -e "$sm_path"
 384        then
 385                if test -d "$sm_path"/.git -o -f "$sm_path"/.git
 386                then
 387                        eval_gettextln "Adding existing repo at '\$sm_path' to the index"
 388                else
 389                        die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
 390                fi
 391
 392        else
 393                if test -d ".git/modules/$sm_name"
 394                then
 395                        if test -z "$force"
 396                        then
 397                                echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
 398                                GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^,"  ", -e s,' (fetch)',, >&2
 399                                echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
 400                                echo >&2 "  $realrepo"
 401                                echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
 402                                die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
 403                        else
 404                                echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
 405                        fi
 406                fi
 407                module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
 408                (
 409                        clear_local_git_env
 410                        cd "$sm_path" &&
 411                        # ash fails to wordsplit ${branch:+-b "$branch"...}
 412                        case "$branch" in
 413                        '') git checkout -f -q ;;
 414                        ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
 415                        esac
 416                ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
 417        fi
 418        git config submodule."$sm_name".url "$realrepo"
 419
 420        git add $force "$sm_path" ||
 421        die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
 422
 423        git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
 424        git config -f .gitmodules submodule."$sm_name".url "$repo" &&
 425        if test -n "$branch"
 426        then
 427                git config -f .gitmodules submodule."$sm_name".branch "$branch"
 428        fi &&
 429        git add --force .gitmodules ||
 430        die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
 431}
 432
 433#
 434# Execute an arbitrary command sequence in each checked out
 435# submodule
 436#
 437# $@ = command to execute
 438#
 439cmd_foreach()
 440{
 441        # parse $args after "submodule ... foreach".
 442        while test $# -ne 0
 443        do
 444                case "$1" in
 445                -q|--quiet)
 446                        GIT_QUIET=1
 447                        ;;
 448                --recursive)
 449                        recursive=1
 450                        ;;
 451                -*)
 452                        usage
 453                        ;;
 454                *)
 455                        break
 456                        ;;
 457                esac
 458                shift
 459        done
 460
 461        toplevel=$(pwd)
 462
 463        # dup stdin so that it can be restored when running the external
 464        # command in the subshell (and a recursive call to this function)
 465        exec 3<&0
 466
 467        module_list |
 468        while read mode sha1 stage sm_path
 469        do
 470                die_if_unmatched "$mode"
 471                if test -e "$sm_path"/.git
 472                then
 473                        say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
 474                        name=$(module_name "$sm_path")
 475                        (
 476                                prefix="$prefix$sm_path/"
 477                                clear_local_git_env
 478                                # we make $path available to scripts ...
 479                                path=$sm_path
 480                                cd "$sm_path" &&
 481                                eval "$@" &&
 482                                if test -n "$recursive"
 483                                then
 484                                        cmd_foreach "--recursive" "$@"
 485                                fi
 486                        ) <&3 3<&- ||
 487                        die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
 488                fi
 489        done
 490}
 491
 492#
 493# Register submodules in .git/config
 494#
 495# $@ = requested paths (default to all)
 496#
 497cmd_init()
 498{
 499        # parse $args after "submodule ... init".
 500        while test $# -ne 0
 501        do
 502                case "$1" in
 503                -q|--quiet)
 504                        GIT_QUIET=1
 505                        ;;
 506                --)
 507                        shift
 508                        break
 509                        ;;
 510                -*)
 511                        usage
 512                        ;;
 513                *)
 514                        break
 515                        ;;
 516                esac
 517                shift
 518        done
 519
 520        module_list "$@" |
 521        while read mode sha1 stage sm_path
 522        do
 523                die_if_unmatched "$mode"
 524                name=$(module_name "$sm_path") || exit
 525
 526                # Copy url setting when it is not set yet
 527                if test -z "$(git config "submodule.$name.url")"
 528                then
 529                        url=$(git config -f .gitmodules submodule."$name".url)
 530                        test -z "$url" &&
 531                        die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
 532
 533                        # Possibly a url relative to parent
 534                        case "$url" in
 535                        ./*|../*)
 536                                url=$(resolve_relative_url "$url") || exit
 537                                ;;
 538                        esac
 539                        git config submodule."$name".url "$url" ||
 540                        die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
 541
 542                        say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
 543                fi
 544
 545                # Copy "update" setting when it is not set yet
 546                upd="$(git config -f .gitmodules submodule."$name".update)"
 547                test -z "$upd" ||
 548                test -n "$(git config submodule."$name".update)" ||
 549                git config submodule."$name".update "$upd" ||
 550                die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
 551        done
 552}
 553
 554#
 555# Update each submodule path to correct revision, using clone and checkout as needed
 556#
 557# $@ = requested paths (default to all)
 558#
 559cmd_update()
 560{
 561        # parse $args after "submodule ... update".
 562        orig_flags=
 563        while test $# -ne 0
 564        do
 565                case "$1" in
 566                -q|--quiet)
 567                        GIT_QUIET=1
 568                        ;;
 569                -i|--init)
 570                        init=1
 571                        ;;
 572                --remote)
 573                        remote=1
 574                        ;;
 575                -N|--no-fetch)
 576                        nofetch=1
 577                        ;;
 578                -f|--force)
 579                        force=$1
 580                        ;;
 581                -r|--rebase)
 582                        update="rebase"
 583                        ;;
 584                --reference)
 585                        case "$2" in '') usage ;; esac
 586                        reference="--reference=$2"
 587                        orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 588                        shift
 589                        ;;
 590                --reference=*)
 591                        reference="$1"
 592                        ;;
 593                -m|--merge)
 594                        update="merge"
 595                        ;;
 596                --recursive)
 597                        recursive=1
 598                        ;;
 599                --checkout)
 600                        update="checkout"
 601                        ;;
 602                --)
 603                        shift
 604                        break
 605                        ;;
 606                -*)
 607                        usage
 608                        ;;
 609                *)
 610                        break
 611                        ;;
 612                esac
 613                orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 614                shift
 615        done
 616
 617        if test -n "$init"
 618        then
 619                cmd_init "--" "$@" || return
 620        fi
 621
 622        cloned_modules=
 623        module_list "$@" | {
 624        err=
 625        while read mode sha1 stage sm_path
 626        do
 627                die_if_unmatched "$mode"
 628                if test "$stage" = U
 629                then
 630                        echo >&2 "Skipping unmerged submodule $prefix$sm_path"
 631                        continue
 632                fi
 633                name=$(module_name "$sm_path") || exit
 634                url=$(git config submodule."$name".url)
 635                branch=$(get_submodule_config "$name" branch master)
 636                if ! test -z "$update"
 637                then
 638                        update_module=$update
 639                else
 640                        update_module=$(git config submodule."$name".update)
 641                fi
 642
 643                if test "$update_module" = "none"
 644                then
 645                        echo "Skipping submodule '$prefix$sm_path'"
 646                        continue
 647                fi
 648
 649                if test -z "$url"
 650                then
 651                        # Only mention uninitialized submodules when its
 652                        # path have been specified
 653                        test "$#" != "0" &&
 654                        say "$(eval_gettext "Submodule path '\$prefix\$sm_path' not initialized
 655Maybe you want to use 'update --init'?")"
 656                        continue
 657                fi
 658
 659                if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
 660                then
 661                        module_clone "$sm_path" "$name" "$url" "$reference" || exit
 662                        cloned_modules="$cloned_modules;$name"
 663                        subsha1=
 664                else
 665                        subsha1=$(clear_local_git_env; cd "$sm_path" &&
 666                                git rev-parse --verify HEAD) ||
 667                        die "$(eval_gettext "Unable to find current revision in submodule path '\$prefix\$sm_path'")"
 668                fi
 669
 670                if test -n "$remote"
 671                then
 672                        if test -z "$nofetch"
 673                        then
 674                                # Fetch remote before determining tracking $sha1
 675                                (clear_local_git_env; cd "$sm_path" && git-fetch) ||
 676                                die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
 677                        fi
 678                        remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
 679                        sha1=$(clear_local_git_env; cd "$sm_path" &&
 680                                git rev-parse --verify "${remote_name}/${branch}") ||
 681                        die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
 682                fi
 683
 684                if test "$subsha1" != "$sha1" -o -n "$force"
 685                then
 686                        subforce=$force
 687                        # If we don't already have a -f flag and the submodule has never been checked out
 688                        if test -z "$subsha1" -a -z "$force"
 689                        then
 690                                subforce="-f"
 691                        fi
 692
 693                        if test -z "$nofetch"
 694                        then
 695                                # Run fetch only if $sha1 isn't present or it
 696                                # is not reachable from a ref.
 697                                (clear_local_git_env; cd "$sm_path" &&
 698                                        ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
 699                                         test -z "$rev") || git-fetch)) ||
 700                                die "$(eval_gettext "Unable to fetch in submodule path '\$prefix\$sm_path'")"
 701                        fi
 702
 703                        # Is this something we just cloned?
 704                        case ";$cloned_modules;" in
 705                        *";$name;"*)
 706                                # then there is no local change to integrate
 707                                update_module= ;;
 708                        esac
 709
 710                        must_die_on_failure=
 711                        case "$update_module" in
 712                        rebase)
 713                                command="git rebase"
 714                                die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$prefix\$sm_path'")"
 715                                say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': rebased into '\$sha1'")"
 716                                must_die_on_failure=yes
 717                                ;;
 718                        merge)
 719                                command="git merge"
 720                                die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$prefix\$sm_path'")"
 721                                say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': merged in '\$sha1'")"
 722                                must_die_on_failure=yes
 723                                ;;
 724                        *)
 725                                command="git checkout $subforce -q"
 726                                die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$prefix\$sm_path'")"
 727                                say_msg="$(eval_gettext "Submodule path '\$prefix\$sm_path': checked out '\$sha1'")"
 728                                ;;
 729                        esac
 730
 731                        if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
 732                        then
 733                                say "$say_msg"
 734                        elif test -n "$must_die_on_failure"
 735                        then
 736                                die_with_status 2 "$die_msg"
 737                        else
 738                                err="${err};$die_msg"
 739                                continue
 740                        fi
 741                fi
 742
 743                if test -n "$recursive"
 744                then
 745                        (
 746                                prefix="$prefix$sm_path/"
 747                                clear_local_git_env
 748                                cd "$sm_path" &&
 749                                eval cmd_update "$orig_flags"
 750                        )
 751                        res=$?
 752                        if test $res -gt 0
 753                        then
 754                                die_msg="$(eval_gettext "Failed to recurse into submodule path '\$prefix\$sm_path'")"
 755                                if test $res -eq 1
 756                                then
 757                                        err="${err};$die_msg"
 758                                        continue
 759                                else
 760                                        die_with_status $res "$die_msg"
 761                                fi
 762                        fi
 763                fi
 764        done
 765
 766        if test -n "$err"
 767        then
 768                OIFS=$IFS
 769                IFS=';'
 770                for e in $err
 771                do
 772                        if test -n "$e"
 773                        then
 774                                echo >&2 "$e"
 775                        fi
 776                done
 777                IFS=$OIFS
 778                exit 1
 779        fi
 780        }
 781}
 782
 783set_name_rev () {
 784        revname=$( (
 785                clear_local_git_env
 786                cd "$1" && {
 787                        git describe "$2" 2>/dev/null ||
 788                        git describe --tags "$2" 2>/dev/null ||
 789                        git describe --contains "$2" 2>/dev/null ||
 790                        git describe --all --always "$2"
 791                }
 792        ) )
 793        test -z "$revname" || revname=" ($revname)"
 794}
 795#
 796# Show commit summary for submodules in index or working tree
 797#
 798# If '--cached' is given, show summary between index and given commit,
 799# or between working tree and given commit
 800#
 801# $@ = [commit (default 'HEAD'),] requested paths (default all)
 802#
 803cmd_summary() {
 804        summary_limit=-1
 805        for_status=
 806        diff_cmd=diff-index
 807
 808        # parse $args after "submodule ... summary".
 809        while test $# -ne 0
 810        do
 811                case "$1" in
 812                --cached)
 813                        cached="$1"
 814                        ;;
 815                --files)
 816                        files="$1"
 817                        ;;
 818                --for-status)
 819                        for_status="$1"
 820                        ;;
 821                -n|--summary-limit)
 822                        summary_limit="$2"
 823                        isnumber "$summary_limit" || usage
 824                        shift
 825                        ;;
 826                --summary-limit=*)
 827                        summary_limit="${1#--summary-limit=}"
 828                        isnumber "$summary_limit" || usage
 829                        ;;
 830                --)
 831                        shift
 832                        break
 833                        ;;
 834                -*)
 835                        usage
 836                        ;;
 837                *)
 838                        break
 839                        ;;
 840                esac
 841                shift
 842        done
 843
 844        test $summary_limit = 0 && return
 845
 846        if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
 847        then
 848                head=$rev
 849                test $# = 0 || shift
 850        elif test -z "$1" -o "$1" = "HEAD"
 851        then
 852                # before the first commit: compare with an empty tree
 853                head=$(git hash-object -w -t tree --stdin </dev/null)
 854                test -z "$1" || shift
 855        else
 856                head="HEAD"
 857        fi
 858
 859        if [ -n "$files" ]
 860        then
 861                test -n "$cached" &&
 862                die "$(gettext "The --cached option cannot be used with the --files option")"
 863                diff_cmd=diff-files
 864                head=
 865        fi
 866
 867        cd_to_toplevel
 868        # Get modified modules cared by user
 869        modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
 870                sane_egrep '^:([0-7]* )?160000' |
 871                while read mod_src mod_dst sha1_src sha1_dst status name
 872                do
 873                        # Always show modules deleted or type-changed (blob<->module)
 874                        test $status = D -o $status = T && echo "$name" && continue
 875                        # Also show added or modified modules which are checked out
 876                        GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
 877                        echo "$name"
 878                done
 879        )
 880
 881        test -z "$modules" && return
 882
 883        git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
 884        sane_egrep '^:([0-7]* )?160000' |
 885        cut -c2- |
 886        while read mod_src mod_dst sha1_src sha1_dst status name
 887        do
 888                if test -z "$cached" &&
 889                        test $sha1_dst = 0000000000000000000000000000000000000000
 890                then
 891                        case "$mod_dst" in
 892                        160000)
 893                                sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
 894                                ;;
 895                        100644 | 100755 | 120000)
 896                                sha1_dst=$(git hash-object $name)
 897                                ;;
 898                        000000)
 899                                ;; # removed
 900                        *)
 901                                # unexpected type
 902                                eval_gettextln "unexpected mode \$mod_dst" >&2
 903                                continue ;;
 904                        esac
 905                fi
 906                missing_src=
 907                missing_dst=
 908
 909                test $mod_src = 160000 &&
 910                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
 911                missing_src=t
 912
 913                test $mod_dst = 160000 &&
 914                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
 915                missing_dst=t
 916
 917                total_commits=
 918                case "$missing_src,$missing_dst" in
 919                t,)
 920                        errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_src")"
 921                        ;;
 922                ,t)
 923                        errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_dst")"
 924                        ;;
 925                t,t)
 926                        errmsg="$(eval_gettext "  Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
 927                        ;;
 928                *)
 929                        errmsg=
 930                        total_commits=$(
 931                        if test $mod_src = 160000 -a $mod_dst = 160000
 932                        then
 933                                range="$sha1_src...$sha1_dst"
 934                        elif test $mod_src = 160000
 935                        then
 936                                range=$sha1_src
 937                        else
 938                                range=$sha1_dst
 939                        fi
 940                        GIT_DIR="$name/.git" \
 941                        git rev-list --first-parent $range -- | wc -l
 942                        )
 943                        total_commits=" ($(($total_commits + 0)))"
 944                        ;;
 945                esac
 946
 947                sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
 948                sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
 949                if test $status = T
 950                then
 951                        blob="$(gettext "blob")"
 952                        submodule="$(gettext "submodule")"
 953                        if test $mod_dst = 160000
 954                        then
 955                                echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
 956                        else
 957                                echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
 958                        fi
 959                else
 960                        echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
 961                fi
 962                if test -n "$errmsg"
 963                then
 964                        # Don't give error msg for modification whose dst is not submodule
 965                        # i.e. deleted or changed to blob
 966                        test $mod_dst = 160000 && echo "$errmsg"
 967                else
 968                        if test $mod_src = 160000 -a $mod_dst = 160000
 969                        then
 970                                limit=
 971                                test $summary_limit -gt 0 && limit="-$summary_limit"
 972                                GIT_DIR="$name/.git" \
 973                                git log $limit --pretty='format:  %m %s' \
 974                                --first-parent $sha1_src...$sha1_dst
 975                        elif test $mod_dst = 160000
 976                        then
 977                                GIT_DIR="$name/.git" \
 978                                git log --pretty='format:  > %s' -1 $sha1_dst
 979                        else
 980                                GIT_DIR="$name/.git" \
 981                                git log --pretty='format:  < %s' -1 $sha1_src
 982                        fi
 983                        echo
 984                fi
 985                echo
 986        done |
 987        if test -n "$for_status"; then
 988                if [ -n "$files" ]; then
 989                        gettextln "Submodules changed but not updated:" | git stripspace -c
 990                else
 991                        gettextln "Submodule changes to be committed:" | git stripspace -c
 992                fi
 993                printf "\n" | git stripspace -c
 994                git stripspace -c
 995        else
 996                cat
 997        fi
 998}
 999#
1000# List all submodules, prefixed with:
1001#  - submodule not initialized
1002#  + different revision checked out
1003#
1004# If --cached was specified the revision in the index will be printed
1005# instead of the currently checked out revision.
1006#
1007# $@ = requested paths (default to all)
1008#
1009cmd_status()
1010{
1011        # parse $args after "submodule ... status".
1012        while test $# -ne 0
1013        do
1014                case "$1" in
1015                -q|--quiet)
1016                        GIT_QUIET=1
1017                        ;;
1018                --cached)
1019                        cached=1
1020                        ;;
1021                --recursive)
1022                        recursive=1
1023                        ;;
1024                --)
1025                        shift
1026                        break
1027                        ;;
1028                -*)
1029                        usage
1030                        ;;
1031                *)
1032                        break
1033                        ;;
1034                esac
1035                shift
1036        done
1037
1038        module_list "$@" |
1039        while read mode sha1 stage sm_path
1040        do
1041                die_if_unmatched "$mode"
1042                name=$(module_name "$sm_path") || exit
1043                url=$(git config submodule."$name".url)
1044                displaypath="$prefix$sm_path"
1045                if test "$stage" = U
1046                then
1047                        say "U$sha1 $displaypath"
1048                        continue
1049                fi
1050                if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
1051                then
1052                        say "-$sha1 $displaypath"
1053                        continue;
1054                fi
1055                set_name_rev "$sm_path" "$sha1"
1056                if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1057                then
1058                        say " $sha1 $displaypath$revname"
1059                else
1060                        if test -z "$cached"
1061                        then
1062                                sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1063                                set_name_rev "$sm_path" "$sha1"
1064                        fi
1065                        say "+$sha1 $displaypath$revname"
1066                fi
1067
1068                if test -n "$recursive"
1069                then
1070                        (
1071                                prefix="$displaypath/"
1072                                clear_local_git_env
1073                                cd "$sm_path" &&
1074                                eval cmd_status
1075                        ) ||
1076                        die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1077                fi
1078        done
1079}
1080#
1081# Sync remote urls for submodules
1082# This makes the value for remote.$remote.url match the value
1083# specified in .gitmodules.
1084#
1085cmd_sync()
1086{
1087        while test $# -ne 0
1088        do
1089                case "$1" in
1090                -q|--quiet)
1091                        GIT_QUIET=1
1092                        shift
1093                        ;;
1094                --recursive)
1095                        recursive=1
1096                        shift
1097                        ;;
1098                --)
1099                        shift
1100                        break
1101                        ;;
1102                -*)
1103                        usage
1104                        ;;
1105                *)
1106                        break
1107                        ;;
1108                esac
1109        done
1110        cd_to_toplevel
1111        module_list "$@" |
1112        while read mode sha1 stage sm_path
1113        do
1114                die_if_unmatched "$mode"
1115                name=$(module_name "$sm_path")
1116                url=$(git config -f .gitmodules --get submodule."$name".url)
1117
1118                # Possibly a url relative to parent
1119                case "$url" in
1120                ./*|../*)
1121                        # rewrite foo/bar as ../.. to find path from
1122                        # submodule work tree to superproject work tree
1123                        up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1124                        # guarantee a trailing /
1125                        up_path=${up_path%/}/ &&
1126                        # path from submodule work tree to submodule origin repo
1127                        sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1128                        # path from superproject work tree to submodule origin repo
1129                        super_config_url=$(resolve_relative_url "$url") || exit
1130                        ;;
1131                *)
1132                        sub_origin_url="$url"
1133                        super_config_url="$url"
1134                        ;;
1135                esac
1136
1137                if git config "submodule.$name.url" >/dev/null 2>/dev/null
1138                then
1139                        say "$(eval_gettext "Synchronizing submodule url for '\$prefix\$sm_path'")"
1140                        git config submodule."$name".url "$super_config_url"
1141
1142                        if test -e "$sm_path"/.git
1143                        then
1144                        (
1145                                clear_local_git_env
1146                                cd "$sm_path"
1147                                remote=$(get_default_remote)
1148                                git config remote."$remote".url "$sub_origin_url"
1149
1150                                if test -n "$recursive"
1151                                then
1152                                        prefix="$prefix$sm_path/"
1153                                        eval cmd_sync
1154                                fi
1155                        )
1156                        fi
1157                fi
1158        done
1159}
1160
1161# This loop parses the command line arguments to find the
1162# subcommand name to dispatch.  Parsing of the subcommand specific
1163# options are primarily done by the subcommand implementations.
1164# Subcommand specific options such as --branch and --cached are
1165# parsed here as well, for backward compatibility.
1166
1167while test $# != 0 && test -z "$command"
1168do
1169        case "$1" in
1170        add | foreach | init | update | status | summary | sync)
1171                command=$1
1172                ;;
1173        -q|--quiet)
1174                GIT_QUIET=1
1175                ;;
1176        -b|--branch)
1177                case "$2" in
1178                '')
1179                        usage
1180                        ;;
1181                esac
1182                branch="$2"; shift
1183                ;;
1184        --cached)
1185                cached="$1"
1186                ;;
1187        --)
1188                break
1189                ;;
1190        -*)
1191                usage
1192                ;;
1193        *)
1194                break
1195                ;;
1196        esac
1197        shift
1198done
1199
1200# No command word defaults to "status"
1201if test -z "$command"
1202then
1203    if test $# = 0
1204    then
1205        command=status
1206    else
1207        usage
1208    fi
1209fi
1210
1211# "-b branch" is accepted only by "add"
1212if test -n "$branch" && test "$command" != add
1213then
1214        usage
1215fi
1216
1217# "--cached" is accepted only by "status" and "summary"
1218if test -n "$cached" && test "$command" != status -a "$command" != summary
1219then
1220        usage
1221fi
1222
1223"cmd_$command" "$@"