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