git-submodule.shon commit submodule: update and add must honor --quiet flag (7e60407)
   1#!/bin/sh
   2#
   3# git-submodules.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] [--reference <repository>] [--] <repository> [<path>]
   9   or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
  10   or: $dashless [--quiet] init [--] [<path>...]
  11   or: $dashless [--quiet] update [--init] [-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 [--] [<path>...]"
  15OPTIONS_SPEC=
  16. git-sh-setup
  17. git-parse-remote
  18require_work_tree
  19
  20command=
  21branch=
  22force=
  23reference=
  24cached=
  25recursive=
  26init=
  27files=
  28nofetch=
  29update=
  30prefix=
  31
  32# Resolve relative url by appending to parent's url
  33resolve_relative_url ()
  34{
  35        remote=$(get_default_remote)
  36        remoteurl=$(git config "remote.$remote.url") ||
  37                die "remote ($remote) does not have a url defined in .git/config"
  38        url="$1"
  39        remoteurl=${remoteurl%/}
  40        sep=/
  41        while test -n "$url"
  42        do
  43                case "$url" in
  44                ../*)
  45                        url="${url#../}"
  46                        case "$remoteurl" in
  47                        */*)
  48                                remoteurl="${remoteurl%/*}"
  49                                ;;
  50                        *:*)
  51                                remoteurl="${remoteurl%:*}"
  52                                sep=:
  53                                ;;
  54                        *)
  55                                die "cannot strip one component off url '$remoteurl'"
  56                                ;;
  57                        esac
  58                        ;;
  59                ./*)
  60                        url="${url#./}"
  61                        ;;
  62                *)
  63                        break;;
  64                esac
  65        done
  66        echo "$remoteurl$sep${url%/}"
  67}
  68
  69#
  70# Get submodule info for registered submodules
  71# $@ = path to limit submodule list
  72#
  73module_list()
  74{
  75        git ls-files --error-unmatch --stage -- "$@" |
  76        perl -e '
  77        my %unmerged = ();
  78        my ($null_sha1) = ("0" x 40);
  79        while (<STDIN>) {
  80                chomp;
  81                my ($mode, $sha1, $stage, $path) =
  82                        /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
  83                next unless $mode eq "160000";
  84                if ($stage ne "0") {
  85                        if (!$unmerged{$path}++) {
  86                                print "$mode $null_sha1 U\t$path\n";
  87                        }
  88                        next;
  89                }
  90                print "$_\n";
  91        }
  92        '
  93}
  94
  95#
  96# Map submodule path to submodule name
  97#
  98# $1 = path
  99#
 100module_name()
 101{
 102        # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
 103        re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
 104        name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
 105                sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
 106       test -z "$name" &&
 107       die "No submodule mapping found in .gitmodules for path '$path'"
 108       echo "$name"
 109}
 110
 111#
 112# Clone a submodule
 113#
 114# Prior to calling, cmd_update checks that a possibly existing
 115# path is not a git repository.
 116# Likewise, cmd_add checks that path does not exist at all,
 117# since it is the location of a new submodule.
 118#
 119module_clone()
 120{
 121        path=$1
 122        url=$2
 123        reference="$3"
 124        quiet=
 125        if test -n "$GIT_QUIET"
 126        then
 127                quiet=-q
 128        fi
 129
 130        if test -n "$reference"
 131        then
 132                git-clone $quiet "$reference" -n "$url" "$path"
 133        else
 134                git-clone $quiet -n "$url" "$path"
 135        fi ||
 136        die "Clone of '$url' into submodule path '$path' failed"
 137}
 138
 139#
 140# Add a new submodule to the working tree, .gitmodules and the index
 141#
 142# $@ = repo path
 143#
 144# optional branch is stored in global branch variable
 145#
 146cmd_add()
 147{
 148        # parse $args after "submodule ... add".
 149        while test $# -ne 0
 150        do
 151                case "$1" in
 152                -b | --branch)
 153                        case "$2" in '') usage ;; esac
 154                        branch=$2
 155                        shift
 156                        ;;
 157                -f | --force)
 158                        force=$1
 159                        ;;
 160                -q|--quiet)
 161                        GIT_QUIET=1
 162                        ;;
 163                --reference)
 164                        case "$2" in '') usage ;; esac
 165                        reference="--reference=$2"
 166                        shift
 167                        ;;
 168                --reference=*)
 169                        reference="$1"
 170                        shift
 171                        ;;
 172                --)
 173                        shift
 174                        break
 175                        ;;
 176                -*)
 177                        usage
 178                        ;;
 179                *)
 180                        break
 181                        ;;
 182                esac
 183                shift
 184        done
 185
 186        repo=$1
 187        path=$2
 188
 189        if test -z "$path"; then
 190                path=$(echo "$repo" |
 191                        sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 192        fi
 193
 194        if test -z "$repo" -o -z "$path"; then
 195                usage
 196        fi
 197
 198        # assure repo is absolute or relative to parent
 199        case "$repo" in
 200        ./*|../*)
 201                # dereference source url relative to parent's url
 202                realrepo=$(resolve_relative_url "$repo") || exit
 203                ;;
 204        *:*|/*)
 205                # absolute url
 206                realrepo=$repo
 207                ;;
 208        *)
 209                die "repo URL: '$repo' must be absolute or begin with ./|../"
 210        ;;
 211        esac
 212
 213        # normalize path:
 214        # multiple //; leading ./; /./; /../; trailing /
 215        path=$(printf '%s/\n' "$path" |
 216                sed -e '
 217                        s|//*|/|g
 218                        s|^\(\./\)*||
 219                        s|/\./|/|g
 220                        :start
 221                        s|\([^/]*\)/\.\./||
 222                        tstart
 223                        s|/*$||
 224                ')
 225        git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
 226        die "'$path' already exists in the index"
 227
 228        if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
 229        then
 230                echo >&2 "The following path is ignored by one of your .gitignore files:" &&
 231                echo >&2 $path &&
 232                echo >&2 "Use -f if you really want to add it."
 233                exit 1
 234        fi
 235
 236        # perhaps the path exists and is already a git repo, else clone it
 237        if test -e "$path"
 238        then
 239                if test -d "$path"/.git -o -f "$path"/.git
 240                then
 241                        echo "Adding existing repo at '$path' to the index"
 242                else
 243                        die "'$path' already exists and is not a valid git repo"
 244                fi
 245
 246                case "$repo" in
 247                ./*|../*)
 248                        url=$(resolve_relative_url "$repo") || exit
 249                    ;;
 250                *)
 251                        url="$repo"
 252                        ;;
 253                esac
 254                git config submodule."$path".url "$url"
 255        else
 256
 257                module_clone "$path" "$realrepo" "$reference" || exit
 258                (
 259                        clear_local_git_env
 260                        cd "$path" &&
 261                        # ash fails to wordsplit ${branch:+-b "$branch"...}
 262                        case "$branch" in
 263                        '') git checkout -f -q ;;
 264                        ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
 265                        esac
 266                ) || die "Unable to checkout submodule '$path'"
 267        fi
 268
 269        git add $force "$path" ||
 270        die "Failed to add submodule '$path'"
 271
 272        git config -f .gitmodules submodule."$path".path "$path" &&
 273        git config -f .gitmodules submodule."$path".url "$repo" &&
 274        git add --force .gitmodules ||
 275        die "Failed to register submodule '$path'"
 276}
 277
 278#
 279# Execute an arbitrary command sequence in each checked out
 280# submodule
 281#
 282# $@ = command to execute
 283#
 284cmd_foreach()
 285{
 286        # parse $args after "submodule ... foreach".
 287        while test $# -ne 0
 288        do
 289                case "$1" in
 290                -q|--quiet)
 291                        GIT_QUIET=1
 292                        ;;
 293                --recursive)
 294                        recursive=1
 295                        ;;
 296                -*)
 297                        usage
 298                        ;;
 299                *)
 300                        break
 301                        ;;
 302                esac
 303                shift
 304        done
 305
 306        toplevel=$(pwd)
 307
 308        module_list |
 309        while read mode sha1 stage path
 310        do
 311                if test -e "$path"/.git
 312                then
 313                        say "Entering '$prefix$path'"
 314                        name=$(module_name "$path")
 315                        (
 316                                prefix="$prefix$path/"
 317                                clear_local_git_env
 318                                cd "$path" &&
 319                                eval "$@" &&
 320                                if test -n "$recursive"
 321                                then
 322                                        cmd_foreach "--recursive" "$@"
 323                                fi
 324                        ) ||
 325                        die "Stopping at '$path'; script returned non-zero status."
 326                fi
 327        done
 328}
 329
 330#
 331# Register submodules in .git/config
 332#
 333# $@ = requested paths (default to all)
 334#
 335cmd_init()
 336{
 337        # parse $args after "submodule ... init".
 338        while test $# -ne 0
 339        do
 340                case "$1" in
 341                -q|--quiet)
 342                        GIT_QUIET=1
 343                        ;;
 344                --)
 345                        shift
 346                        break
 347                        ;;
 348                -*)
 349                        usage
 350                        ;;
 351                *)
 352                        break
 353                        ;;
 354                esac
 355                shift
 356        done
 357
 358        module_list "$@" |
 359        while read mode sha1 stage path
 360        do
 361                # Skip already registered paths
 362                name=$(module_name "$path") || exit
 363                url=$(git config submodule."$name".url)
 364                test -z "$url" || continue
 365
 366                url=$(git config -f .gitmodules submodule."$name".url)
 367                test -z "$url" &&
 368                die "No url found for submodule path '$path' in .gitmodules"
 369
 370                # Possibly a url relative to parent
 371                case "$url" in
 372                ./*|../*)
 373                        url=$(resolve_relative_url "$url") || exit
 374                        ;;
 375                esac
 376
 377                git config submodule."$name".url "$url" ||
 378                die "Failed to register url for submodule path '$path'"
 379
 380                upd="$(git config -f .gitmodules submodule."$name".update)"
 381                test -z "$upd" ||
 382                git config submodule."$name".update "$upd" ||
 383                die "Failed to register update mode for submodule path '$path'"
 384
 385                say "Submodule '$name' ($url) registered for path '$path'"
 386        done
 387}
 388
 389#
 390# Update each submodule path to correct revision, using clone and checkout as needed
 391#
 392# $@ = requested paths (default to all)
 393#
 394cmd_update()
 395{
 396        # parse $args after "submodule ... update".
 397        orig_flags=
 398        while test $# -ne 0
 399        do
 400                case "$1" in
 401                -q|--quiet)
 402                        GIT_QUIET=1
 403                        ;;
 404                -i|--init)
 405                        init=1
 406                        ;;
 407                -N|--no-fetch)
 408                        nofetch=1
 409                        ;;
 410                -f|--force)
 411                        force=$1
 412                        ;;
 413                -r|--rebase)
 414                        update="rebase"
 415                        ;;
 416                --reference)
 417                        case "$2" in '') usage ;; esac
 418                        reference="--reference=$2"
 419                        orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 420                        shift
 421                        ;;
 422                --reference=*)
 423                        reference="$1"
 424                        ;;
 425                -m|--merge)
 426                        update="merge"
 427                        ;;
 428                --recursive)
 429                        recursive=1
 430                        ;;
 431                --)
 432                        shift
 433                        break
 434                        ;;
 435                -*)
 436                        usage
 437                        ;;
 438                *)
 439                        break
 440                        ;;
 441                esac
 442                orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 443                shift
 444        done
 445
 446        if test -n "$init"
 447        then
 448                cmd_init "--" "$@" || return
 449        fi
 450
 451        cloned_modules=
 452        module_list "$@" |
 453        while read mode sha1 stage path
 454        do
 455                if test "$stage" = U
 456                then
 457                        echo >&2 "Skipping unmerged submodule $path"
 458                        continue
 459                fi
 460                name=$(module_name "$path") || exit
 461                url=$(git config submodule."$name".url)
 462                update_module=$(git config submodule."$name".update)
 463                if test -z "$url"
 464                then
 465                        # Only mention uninitialized submodules when its
 466                        # path have been specified
 467                        test "$#" != "0" &&
 468                        say "Submodule path '$path' not initialized" &&
 469                        say "Maybe you want to use 'update --init'?"
 470                        continue
 471                fi
 472
 473                if ! test -d "$path"/.git -o -f "$path"/.git
 474                then
 475                        module_clone "$path" "$url" "$reference"|| exit
 476                        cloned_modules="$cloned_modules;$name"
 477                        subsha1=
 478                else
 479                        subsha1=$(clear_local_git_env; cd "$path" &&
 480                                git rev-parse --verify HEAD) ||
 481                        die "Unable to find current revision in submodule path '$path'"
 482                fi
 483
 484                if ! test -z "$update"
 485                then
 486                        update_module=$update
 487                fi
 488
 489                if test "$subsha1" != "$sha1"
 490                then
 491                        subforce=$force
 492                        # If we don't already have a -f flag and the submodule has never been checked out
 493                        if test -z "$subsha1" -a -z "$force"
 494                        then
 495                                subforce="-f"
 496                        fi
 497
 498                        if test -z "$nofetch"
 499                        then
 500                                # Run fetch only if $sha1 isn't present or it
 501                                # is not reachable from a ref.
 502                                (clear_local_git_env; cd "$path" &&
 503                                        ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
 504                                         test -z "$rev") || git-fetch)) ||
 505                                die "Unable to fetch in submodule path '$path'"
 506                        fi
 507
 508                        # Is this something we just cloned?
 509                        case ";$cloned_modules;" in
 510                        *";$name;"*)
 511                                # then there is no local change to integrate
 512                                update_module= ;;
 513                        esac
 514
 515                        case "$update_module" in
 516                        rebase)
 517                                command="git rebase"
 518                                action="rebase"
 519                                msg="rebased onto"
 520                                ;;
 521                        merge)
 522                                command="git merge"
 523                                action="merge"
 524                                msg="merged in"
 525                                ;;
 526                        *)
 527                                command="git checkout $subforce -q"
 528                                action="checkout"
 529                                msg="checked out"
 530                                ;;
 531                        esac
 532
 533                        (clear_local_git_env; cd "$path" && $command "$sha1") ||
 534                        die "Unable to $action '$sha1' in submodule path '$path'"
 535                        say "Submodule path '$path': $msg '$sha1'"
 536                fi
 537
 538                if test -n "$recursive"
 539                then
 540                        (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") ||
 541                        die "Failed to recurse into submodule path '$path'"
 542                fi
 543        done
 544}
 545
 546set_name_rev () {
 547        revname=$( (
 548                clear_local_git_env
 549                cd "$1" && {
 550                        git describe "$2" 2>/dev/null ||
 551                        git describe --tags "$2" 2>/dev/null ||
 552                        git describe --contains "$2" 2>/dev/null ||
 553                        git describe --all --always "$2"
 554                }
 555        ) )
 556        test -z "$revname" || revname=" ($revname)"
 557}
 558#
 559# Show commit summary for submodules in index or working tree
 560#
 561# If '--cached' is given, show summary between index and given commit,
 562# or between working tree and given commit
 563#
 564# $@ = [commit (default 'HEAD'),] requested paths (default all)
 565#
 566cmd_summary() {
 567        summary_limit=-1
 568        for_status=
 569        diff_cmd=diff-index
 570
 571        # parse $args after "submodule ... summary".
 572        while test $# -ne 0
 573        do
 574                case "$1" in
 575                --cached)
 576                        cached="$1"
 577                        ;;
 578                --files)
 579                        files="$1"
 580                        ;;
 581                --for-status)
 582                        for_status="$1"
 583                        ;;
 584                -n|--summary-limit)
 585                        if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
 586                        then
 587                                :
 588                        else
 589                                usage
 590                        fi
 591                        shift
 592                        ;;
 593                --)
 594                        shift
 595                        break
 596                        ;;
 597                -*)
 598                        usage
 599                        ;;
 600                *)
 601                        break
 602                        ;;
 603                esac
 604                shift
 605        done
 606
 607        test $summary_limit = 0 && return
 608
 609        if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
 610        then
 611                head=$rev
 612                test $# = 0 || shift
 613        elif test -z "$1" -o "$1" = "HEAD"
 614        then
 615                # before the first commit: compare with an empty tree
 616                head=$(git hash-object -w -t tree --stdin </dev/null)
 617                test -z "$1" || shift
 618        else
 619                head="HEAD"
 620        fi
 621
 622        if [ -n "$files" ]
 623        then
 624                test -n "$cached" &&
 625                die "--cached cannot be used with --files"
 626                diff_cmd=diff-files
 627                head=
 628        fi
 629
 630        cd_to_toplevel
 631        # Get modified modules cared by user
 632        modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
 633                sane_egrep '^:([0-7]* )?160000' |
 634                while read mod_src mod_dst sha1_src sha1_dst status name
 635                do
 636                        # Always show modules deleted or type-changed (blob<->module)
 637                        test $status = D -o $status = T && echo "$name" && continue
 638                        # Also show added or modified modules which are checked out
 639                        GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
 640                        echo "$name"
 641                done
 642        )
 643
 644        test -z "$modules" && return
 645
 646        git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
 647        sane_egrep '^:([0-7]* )?160000' |
 648        cut -c2- |
 649        while read mod_src mod_dst sha1_src sha1_dst status name
 650        do
 651                if test -z "$cached" &&
 652                        test $sha1_dst = 0000000000000000000000000000000000000000
 653                then
 654                        case "$mod_dst" in
 655                        160000)
 656                                sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
 657                                ;;
 658                        100644 | 100755 | 120000)
 659                                sha1_dst=$(git hash-object $name)
 660                                ;;
 661                        000000)
 662                                ;; # removed
 663                        *)
 664                                # unexpected type
 665                                echo >&2 "unexpected mode $mod_dst"
 666                                continue ;;
 667                        esac
 668                fi
 669                missing_src=
 670                missing_dst=
 671
 672                test $mod_src = 160000 &&
 673                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
 674                missing_src=t
 675
 676                test $mod_dst = 160000 &&
 677                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
 678                missing_dst=t
 679
 680                total_commits=
 681                case "$missing_src,$missing_dst" in
 682                t,)
 683                        errmsg="  Warn: $name doesn't contain commit $sha1_src"
 684                        ;;
 685                ,t)
 686                        errmsg="  Warn: $name doesn't contain commit $sha1_dst"
 687                        ;;
 688                t,t)
 689                        errmsg="  Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
 690                        ;;
 691                *)
 692                        errmsg=
 693                        total_commits=$(
 694                        if test $mod_src = 160000 -a $mod_dst = 160000
 695                        then
 696                                range="$sha1_src...$sha1_dst"
 697                        elif test $mod_src = 160000
 698                        then
 699                                range=$sha1_src
 700                        else
 701                                range=$sha1_dst
 702                        fi
 703                        GIT_DIR="$name/.git" \
 704                        git rev-list --first-parent $range -- | wc -l
 705                        )
 706                        total_commits=" ($(($total_commits + 0)))"
 707                        ;;
 708                esac
 709
 710                sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
 711                sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
 712                if test $status = T
 713                then
 714                        if test $mod_dst = 160000
 715                        then
 716                                echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
 717                        else
 718                                echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
 719                        fi
 720                else
 721                        echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
 722                fi
 723                if test -n "$errmsg"
 724                then
 725                        # Don't give error msg for modification whose dst is not submodule
 726                        # i.e. deleted or changed to blob
 727                        test $mod_dst = 160000 && echo "$errmsg"
 728                else
 729                        if test $mod_src = 160000 -a $mod_dst = 160000
 730                        then
 731                                limit=
 732                                test $summary_limit -gt 0 && limit="-$summary_limit"
 733                                GIT_DIR="$name/.git" \
 734                                git log $limit --pretty='format:  %m %s' \
 735                                --first-parent $sha1_src...$sha1_dst
 736                        elif test $mod_dst = 160000
 737                        then
 738                                GIT_DIR="$name/.git" \
 739                                git log --pretty='format:  > %s' -1 $sha1_dst
 740                        else
 741                                GIT_DIR="$name/.git" \
 742                                git log --pretty='format:  < %s' -1 $sha1_src
 743                        fi
 744                        echo
 745                fi
 746                echo
 747        done |
 748        if test -n "$for_status"; then
 749                if [ -n "$files" ]; then
 750                        echo "# Submodules changed but not updated:"
 751                else
 752                        echo "# Submodule changes to be committed:"
 753                fi
 754                echo "#"
 755                sed -e 's|^|# |' -e 's|^# $|#|'
 756        else
 757                cat
 758        fi
 759}
 760#
 761# List all submodules, prefixed with:
 762#  - submodule not initialized
 763#  + different revision checked out
 764#
 765# If --cached was specified the revision in the index will be printed
 766# instead of the currently checked out revision.
 767#
 768# $@ = requested paths (default to all)
 769#
 770cmd_status()
 771{
 772        # parse $args after "submodule ... status".
 773        orig_flags=
 774        while test $# -ne 0
 775        do
 776                case "$1" in
 777                -q|--quiet)
 778                        GIT_QUIET=1
 779                        ;;
 780                --cached)
 781                        cached=1
 782                        ;;
 783                --recursive)
 784                        recursive=1
 785                        ;;
 786                --)
 787                        shift
 788                        break
 789                        ;;
 790                -*)
 791                        usage
 792                        ;;
 793                *)
 794                        break
 795                        ;;
 796                esac
 797                orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 798                shift
 799        done
 800
 801        module_list "$@" |
 802        while read mode sha1 stage path
 803        do
 804                name=$(module_name "$path") || exit
 805                url=$(git config submodule."$name".url)
 806                displaypath="$prefix$path"
 807                if test "$stage" = U
 808                then
 809                        say "U$sha1 $displaypath"
 810                        continue
 811                fi
 812                if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
 813                then
 814                        say "-$sha1 $displaypath"
 815                        continue;
 816                fi
 817                set_name_rev "$path" "$sha1"
 818                if git diff-files --ignore-submodules=dirty --quiet -- "$path"
 819                then
 820                        say " $sha1 $displaypath$revname"
 821                else
 822                        if test -z "$cached"
 823                        then
 824                                sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
 825                                set_name_rev "$path" "$sha1"
 826                        fi
 827                        say "+$sha1 $displaypath$revname"
 828                fi
 829
 830                if test -n "$recursive"
 831                then
 832                        (
 833                                prefix="$displaypath/"
 834                                clear_local_git_env
 835                                cd "$path" &&
 836                                eval cmd_status "$orig_args"
 837                        ) ||
 838                        die "Failed to recurse into submodule path '$path'"
 839                fi
 840        done
 841}
 842#
 843# Sync remote urls for submodules
 844# This makes the value for remote.$remote.url match the value
 845# specified in .gitmodules.
 846#
 847cmd_sync()
 848{
 849        while test $# -ne 0
 850        do
 851                case "$1" in
 852                -q|--quiet)
 853                        GIT_QUIET=1
 854                        shift
 855                        ;;
 856                --)
 857                        shift
 858                        break
 859                        ;;
 860                -*)
 861                        usage
 862                        ;;
 863                *)
 864                        break
 865                        ;;
 866                esac
 867        done
 868        cd_to_toplevel
 869        module_list "$@" |
 870        while read mode sha1 stage path
 871        do
 872                name=$(module_name "$path")
 873                url=$(git config -f .gitmodules --get submodule."$name".url)
 874
 875                # Possibly a url relative to parent
 876                case "$url" in
 877                ./*|../*)
 878                        url=$(resolve_relative_url "$url") || exit
 879                        ;;
 880                esac
 881
 882                say "Synchronizing submodule url for '$name'"
 883                git config submodule."$name".url "$url"
 884
 885                if test -e "$path"/.git
 886                then
 887                (
 888                        clear_local_git_env
 889                        cd "$path"
 890                        remote=$(get_default_remote)
 891                        git config remote."$remote".url "$url"
 892                )
 893                fi
 894        done
 895}
 896
 897# This loop parses the command line arguments to find the
 898# subcommand name to dispatch.  Parsing of the subcommand specific
 899# options are primarily done by the subcommand implementations.
 900# Subcommand specific options such as --branch and --cached are
 901# parsed here as well, for backward compatibility.
 902
 903while test $# != 0 && test -z "$command"
 904do
 905        case "$1" in
 906        add | foreach | init | update | status | summary | sync)
 907                command=$1
 908                ;;
 909        -q|--quiet)
 910                GIT_QUIET=1
 911                ;;
 912        -b|--branch)
 913                case "$2" in
 914                '')
 915                        usage
 916                        ;;
 917                esac
 918                branch="$2"; shift
 919                ;;
 920        --cached)
 921                cached="$1"
 922                ;;
 923        --)
 924                break
 925                ;;
 926        -*)
 927                usage
 928                ;;
 929        *)
 930                break
 931                ;;
 932        esac
 933        shift
 934done
 935
 936# No command word defaults to "status"
 937test -n "$command" || command=status
 938
 939# "-b branch" is accepted only by "add"
 940if test -n "$branch" && test "$command" != add
 941then
 942        usage
 943fi
 944
 945# "--cached" is accepted only by "status" and "summary"
 946if test -n "$cached" && test "$command" != status -a "$command" != summary
 947then
 948        usage
 949fi
 950
 951"cmd_$command" "$@"