git-submodule.shon commit t5541: check error message against the real port number used (d202a51)
   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                remoteurl=$(pwd) # the repository is its own authoritative upstream
  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        else
 247
 248                module_clone "$path" "$realrepo" "$reference" || exit
 249                (
 250                        clear_local_git_env
 251                        cd "$path" &&
 252                        # ash fails to wordsplit ${branch:+-b "$branch"...}
 253                        case "$branch" in
 254                        '') git checkout -f -q ;;
 255                        ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
 256                        esac
 257                ) || die "Unable to checkout submodule '$path'"
 258        fi
 259        git config submodule."$path".url "$realrepo"
 260
 261        git add $force "$path" ||
 262        die "Failed to add submodule '$path'"
 263
 264        git config -f .gitmodules submodule."$path".path "$path" &&
 265        git config -f .gitmodules submodule."$path".url "$repo" &&
 266        git add --force .gitmodules ||
 267        die "Failed to register submodule '$path'"
 268}
 269
 270#
 271# Execute an arbitrary command sequence in each checked out
 272# submodule
 273#
 274# $@ = command to execute
 275#
 276cmd_foreach()
 277{
 278        # parse $args after "submodule ... foreach".
 279        while test $# -ne 0
 280        do
 281                case "$1" in
 282                -q|--quiet)
 283                        GIT_QUIET=1
 284                        ;;
 285                --recursive)
 286                        recursive=1
 287                        ;;
 288                -*)
 289                        usage
 290                        ;;
 291                *)
 292                        break
 293                        ;;
 294                esac
 295                shift
 296        done
 297
 298        toplevel=$(pwd)
 299
 300        # dup stdin so that it can be restored when running the external
 301        # command in the subshell (and a recursive call to this function)
 302        exec 3<&0
 303
 304        module_list |
 305        while read mode sha1 stage path
 306        do
 307                if test -e "$path"/.git
 308                then
 309                        say "Entering '$prefix$path'"
 310                        name=$(module_name "$path")
 311                        (
 312                                prefix="$prefix$path/"
 313                                clear_local_git_env
 314                                cd "$path" &&
 315                                eval "$@" &&
 316                                if test -n "$recursive"
 317                                then
 318                                        cmd_foreach "--recursive" "$@"
 319                                fi
 320                        ) <&3 3<&- ||
 321                        die "Stopping at '$path'; script returned non-zero status."
 322                fi
 323        done
 324}
 325
 326#
 327# Register submodules in .git/config
 328#
 329# $@ = requested paths (default to all)
 330#
 331cmd_init()
 332{
 333        # parse $args after "submodule ... init".
 334        while test $# -ne 0
 335        do
 336                case "$1" in
 337                -q|--quiet)
 338                        GIT_QUIET=1
 339                        ;;
 340                --)
 341                        shift
 342                        break
 343                        ;;
 344                -*)
 345                        usage
 346                        ;;
 347                *)
 348                        break
 349                        ;;
 350                esac
 351                shift
 352        done
 353
 354        module_list "$@" |
 355        while read mode sha1 stage path
 356        do
 357                # Skip already registered paths
 358                name=$(module_name "$path") || exit
 359                if test -z "$(git config "submodule.$name.url")"
 360                then
 361                        url=$(git config -f .gitmodules submodule."$name".url)
 362                        test -z "$url" &&
 363                        die "No url found for submodule path '$path' in .gitmodules"
 364
 365                        # Possibly a url relative to parent
 366                        case "$url" in
 367                        ./*|../*)
 368                                url=$(resolve_relative_url "$url") || exit
 369                                ;;
 370                        esac
 371                        git config submodule."$name".url "$url" ||
 372                        die "Failed to register url for submodule path '$path'"
 373                fi
 374
 375                # Copy "update" setting when it is not set yet
 376                upd="$(git config -f .gitmodules submodule."$name".update)"
 377                test -z "$upd" ||
 378                test -n "$(git config submodule."$name".update)" ||
 379                git config submodule."$name".update "$upd" ||
 380                die "Failed to register update mode for submodule path '$path'"
 381
 382                say "Submodule '$name' ($url) registered for path '$path'"
 383        done
 384}
 385
 386#
 387# Update each submodule path to correct revision, using clone and checkout as needed
 388#
 389# $@ = requested paths (default to all)
 390#
 391cmd_update()
 392{
 393        # parse $args after "submodule ... update".
 394        orig_flags=
 395        while test $# -ne 0
 396        do
 397                case "$1" in
 398                -q|--quiet)
 399                        GIT_QUIET=1
 400                        ;;
 401                -i|--init)
 402                        init=1
 403                        ;;
 404                -N|--no-fetch)
 405                        nofetch=1
 406                        ;;
 407                -f|--force)
 408                        force=$1
 409                        ;;
 410                -r|--rebase)
 411                        update="rebase"
 412                        ;;
 413                --reference)
 414                        case "$2" in '') usage ;; esac
 415                        reference="--reference=$2"
 416                        orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 417                        shift
 418                        ;;
 419                --reference=*)
 420                        reference="$1"
 421                        ;;
 422                -m|--merge)
 423                        update="merge"
 424                        ;;
 425                --recursive)
 426                        recursive=1
 427                        ;;
 428                --)
 429                        shift
 430                        break
 431                        ;;
 432                -*)
 433                        usage
 434                        ;;
 435                *)
 436                        break
 437                        ;;
 438                esac
 439                orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 440                shift
 441        done
 442
 443        if test -n "$init"
 444        then
 445                cmd_init "--" "$@" || return
 446        fi
 447
 448        cloned_modules=
 449        module_list "$@" |
 450        while read mode sha1 stage path
 451        do
 452                if test "$stage" = U
 453                then
 454                        echo >&2 "Skipping unmerged submodule $path"
 455                        continue
 456                fi
 457                name=$(module_name "$path") || exit
 458                url=$(git config submodule."$name".url)
 459                update_module=$(git config submodule."$name".update)
 460                if test -z "$url"
 461                then
 462                        # Only mention uninitialized submodules when its
 463                        # path have been specified
 464                        test "$#" != "0" &&
 465                        say "Submodule path '$path' not initialized" &&
 466                        say "Maybe you want to use 'update --init'?"
 467                        continue
 468                fi
 469
 470                if ! test -d "$path"/.git -o -f "$path"/.git
 471                then
 472                        module_clone "$path" "$url" "$reference"|| exit
 473                        cloned_modules="$cloned_modules;$name"
 474                        subsha1=
 475                else
 476                        subsha1=$(clear_local_git_env; cd "$path" &&
 477                                git rev-parse --verify HEAD) ||
 478                        die "Unable to find current revision in submodule path '$path'"
 479                fi
 480
 481                if ! test -z "$update"
 482                then
 483                        update_module=$update
 484                fi
 485
 486                if test "$subsha1" != "$sha1"
 487                then
 488                        subforce=$force
 489                        # If we don't already have a -f flag and the submodule has never been checked out
 490                        if test -z "$subsha1" -a -z "$force"
 491                        then
 492                                subforce="-f"
 493                        fi
 494
 495                        if test -z "$nofetch"
 496                        then
 497                                # Run fetch only if $sha1 isn't present or it
 498                                # is not reachable from a ref.
 499                                (clear_local_git_env; cd "$path" &&
 500                                        ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
 501                                         test -z "$rev") || git-fetch)) ||
 502                                die "Unable to fetch in submodule path '$path'"
 503                        fi
 504
 505                        # Is this something we just cloned?
 506                        case ";$cloned_modules;" in
 507                        *";$name;"*)
 508                                # then there is no local change to integrate
 509                                update_module= ;;
 510                        esac
 511
 512                        case "$update_module" in
 513                        rebase)
 514                                command="git rebase"
 515                                action="rebase"
 516                                msg="rebased onto"
 517                                ;;
 518                        merge)
 519                                command="git merge"
 520                                action="merge"
 521                                msg="merged in"
 522                                ;;
 523                        *)
 524                                command="git checkout $subforce -q"
 525                                action="checkout"
 526                                msg="checked out"
 527                                ;;
 528                        esac
 529
 530                        (clear_local_git_env; cd "$path" && $command "$sha1") ||
 531                        die "Unable to $action '$sha1' in submodule path '$path'"
 532                        say "Submodule path '$path': $msg '$sha1'"
 533                fi
 534
 535                if test -n "$recursive"
 536                then
 537                        (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") ||
 538                        die "Failed to recurse into submodule path '$path'"
 539                fi
 540        done
 541}
 542
 543set_name_rev () {
 544        revname=$( (
 545                clear_local_git_env
 546                cd "$1" && {
 547                        git describe "$2" 2>/dev/null ||
 548                        git describe --tags "$2" 2>/dev/null ||
 549                        git describe --contains "$2" 2>/dev/null ||
 550                        git describe --all --always "$2"
 551                }
 552        ) )
 553        test -z "$revname" || revname=" ($revname)"
 554}
 555#
 556# Show commit summary for submodules in index or working tree
 557#
 558# If '--cached' is given, show summary between index and given commit,
 559# or between working tree and given commit
 560#
 561# $@ = [commit (default 'HEAD'),] requested paths (default all)
 562#
 563cmd_summary() {
 564        summary_limit=-1
 565        for_status=
 566        diff_cmd=diff-index
 567
 568        # parse $args after "submodule ... summary".
 569        while test $# -ne 0
 570        do
 571                case "$1" in
 572                --cached)
 573                        cached="$1"
 574                        ;;
 575                --files)
 576                        files="$1"
 577                        ;;
 578                --for-status)
 579                        for_status="$1"
 580                        ;;
 581                -n|--summary-limit)
 582                        if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
 583                        then
 584                                :
 585                        else
 586                                usage
 587                        fi
 588                        shift
 589                        ;;
 590                --)
 591                        shift
 592                        break
 593                        ;;
 594                -*)
 595                        usage
 596                        ;;
 597                *)
 598                        break
 599                        ;;
 600                esac
 601                shift
 602        done
 603
 604        test $summary_limit = 0 && return
 605
 606        if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
 607        then
 608                head=$rev
 609                test $# = 0 || shift
 610        elif test -z "$1" -o "$1" = "HEAD"
 611        then
 612                # before the first commit: compare with an empty tree
 613                head=$(git hash-object -w -t tree --stdin </dev/null)
 614                test -z "$1" || shift
 615        else
 616                head="HEAD"
 617        fi
 618
 619        if [ -n "$files" ]
 620        then
 621                test -n "$cached" &&
 622                die "--cached cannot be used with --files"
 623                diff_cmd=diff-files
 624                head=
 625        fi
 626
 627        cd_to_toplevel
 628        # Get modified modules cared by user
 629        modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
 630                sane_egrep '^:([0-7]* )?160000' |
 631                while read mod_src mod_dst sha1_src sha1_dst status name
 632                do
 633                        # Always show modules deleted or type-changed (blob<->module)
 634                        test $status = D -o $status = T && echo "$name" && continue
 635                        # Also show added or modified modules which are checked out
 636                        GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
 637                        echo "$name"
 638                done
 639        )
 640
 641        test -z "$modules" && return
 642
 643        git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
 644        sane_egrep '^:([0-7]* )?160000' |
 645        cut -c2- |
 646        while read mod_src mod_dst sha1_src sha1_dst status name
 647        do
 648                if test -z "$cached" &&
 649                        test $sha1_dst = 0000000000000000000000000000000000000000
 650                then
 651                        case "$mod_dst" in
 652                        160000)
 653                                sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
 654                                ;;
 655                        100644 | 100755 | 120000)
 656                                sha1_dst=$(git hash-object $name)
 657                                ;;
 658                        000000)
 659                                ;; # removed
 660                        *)
 661                                # unexpected type
 662                                echo >&2 "unexpected mode $mod_dst"
 663                                continue ;;
 664                        esac
 665                fi
 666                missing_src=
 667                missing_dst=
 668
 669                test $mod_src = 160000 &&
 670                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
 671                missing_src=t
 672
 673                test $mod_dst = 160000 &&
 674                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
 675                missing_dst=t
 676
 677                total_commits=
 678                case "$missing_src,$missing_dst" in
 679                t,)
 680                        errmsg="  Warn: $name doesn't contain commit $sha1_src"
 681                        ;;
 682                ,t)
 683                        errmsg="  Warn: $name doesn't contain commit $sha1_dst"
 684                        ;;
 685                t,t)
 686                        errmsg="  Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
 687                        ;;
 688                *)
 689                        errmsg=
 690                        total_commits=$(
 691                        if test $mod_src = 160000 -a $mod_dst = 160000
 692                        then
 693                                range="$sha1_src...$sha1_dst"
 694                        elif test $mod_src = 160000
 695                        then
 696                                range=$sha1_src
 697                        else
 698                                range=$sha1_dst
 699                        fi
 700                        GIT_DIR="$name/.git" \
 701                        git rev-list --first-parent $range -- | wc -l
 702                        )
 703                        total_commits=" ($(($total_commits + 0)))"
 704                        ;;
 705                esac
 706
 707                sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
 708                sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
 709                if test $status = T
 710                then
 711                        if test $mod_dst = 160000
 712                        then
 713                                echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
 714                        else
 715                                echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
 716                        fi
 717                else
 718                        echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
 719                fi
 720                if test -n "$errmsg"
 721                then
 722                        # Don't give error msg for modification whose dst is not submodule
 723                        # i.e. deleted or changed to blob
 724                        test $mod_dst = 160000 && echo "$errmsg"
 725                else
 726                        if test $mod_src = 160000 -a $mod_dst = 160000
 727                        then
 728                                limit=
 729                                test $summary_limit -gt 0 && limit="-$summary_limit"
 730                                GIT_DIR="$name/.git" \
 731                                git log $limit --pretty='format:  %m %s' \
 732                                --first-parent $sha1_src...$sha1_dst
 733                        elif test $mod_dst = 160000
 734                        then
 735                                GIT_DIR="$name/.git" \
 736                                git log --pretty='format:  > %s' -1 $sha1_dst
 737                        else
 738                                GIT_DIR="$name/.git" \
 739                                git log --pretty='format:  < %s' -1 $sha1_src
 740                        fi
 741                        echo
 742                fi
 743                echo
 744        done |
 745        if test -n "$for_status"; then
 746                if [ -n "$files" ]; then
 747                        echo "# Submodules changed but not updated:"
 748                else
 749                        echo "# Submodule changes to be committed:"
 750                fi
 751                echo "#"
 752                sed -e 's|^|# |' -e 's|^# $|#|'
 753        else
 754                cat
 755        fi
 756}
 757#
 758# List all submodules, prefixed with:
 759#  - submodule not initialized
 760#  + different revision checked out
 761#
 762# If --cached was specified the revision in the index will be printed
 763# instead of the currently checked out revision.
 764#
 765# $@ = requested paths (default to all)
 766#
 767cmd_status()
 768{
 769        # parse $args after "submodule ... status".
 770        orig_flags=
 771        while test $# -ne 0
 772        do
 773                case "$1" in
 774                -q|--quiet)
 775                        GIT_QUIET=1
 776                        ;;
 777                --cached)
 778                        cached=1
 779                        ;;
 780                --recursive)
 781                        recursive=1
 782                        ;;
 783                --)
 784                        shift
 785                        break
 786                        ;;
 787                -*)
 788                        usage
 789                        ;;
 790                *)
 791                        break
 792                        ;;
 793                esac
 794                orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 795                shift
 796        done
 797
 798        module_list "$@" |
 799        while read mode sha1 stage path
 800        do
 801                name=$(module_name "$path") || exit
 802                url=$(git config submodule."$name".url)
 803                displaypath="$prefix$path"
 804                if test "$stage" = U
 805                then
 806                        say "U$sha1 $displaypath"
 807                        continue
 808                fi
 809                if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
 810                then
 811                        say "-$sha1 $displaypath"
 812                        continue;
 813                fi
 814                set_name_rev "$path" "$sha1"
 815                if git diff-files --ignore-submodules=dirty --quiet -- "$path"
 816                then
 817                        say " $sha1 $displaypath$revname"
 818                else
 819                        if test -z "$cached"
 820                        then
 821                                sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
 822                                set_name_rev "$path" "$sha1"
 823                        fi
 824                        say "+$sha1 $displaypath$revname"
 825                fi
 826
 827                if test -n "$recursive"
 828                then
 829                        (
 830                                prefix="$displaypath/"
 831                                clear_local_git_env
 832                                cd "$path" &&
 833                                eval cmd_status "$orig_args"
 834                        ) ||
 835                        die "Failed to recurse into submodule path '$path'"
 836                fi
 837        done
 838}
 839#
 840# Sync remote urls for submodules
 841# This makes the value for remote.$remote.url match the value
 842# specified in .gitmodules.
 843#
 844cmd_sync()
 845{
 846        while test $# -ne 0
 847        do
 848                case "$1" in
 849                -q|--quiet)
 850                        GIT_QUIET=1
 851                        shift
 852                        ;;
 853                --)
 854                        shift
 855                        break
 856                        ;;
 857                -*)
 858                        usage
 859                        ;;
 860                *)
 861                        break
 862                        ;;
 863                esac
 864        done
 865        cd_to_toplevel
 866        module_list "$@" |
 867        while read mode sha1 stage path
 868        do
 869                name=$(module_name "$path")
 870                url=$(git config -f .gitmodules --get submodule."$name".url)
 871
 872                # Possibly a url relative to parent
 873                case "$url" in
 874                ./*|../*)
 875                        url=$(resolve_relative_url "$url") || exit
 876                        ;;
 877                esac
 878
 879                if git config "submodule.$name.url" >/dev/null 2>/dev/null
 880                then
 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                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" "$@"