git-submodule.shon commit Merge branch 'oa/pull-reflog' into maint (2a76497)
   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        else
 250
 251                module_clone "$path" "$realrepo" "$reference" || exit
 252                (
 253                        clear_local_git_env
 254                        cd "$path" &&
 255                        # ash fails to wordsplit ${branch:+-b "$branch"...}
 256                        case "$branch" in
 257                        '') git checkout -f -q ;;
 258                        ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
 259                        esac
 260                ) || die "Unable to checkout submodule '$path'"
 261        fi
 262        git config submodule."$path".url "$url"
 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                if test -z "$(git config "submodule.$name.url")"
 363                then
 364                        url=$(git config -f .gitmodules submodule."$name".url)
 365                        test -z "$url" &&
 366                        die "No url found for submodule path '$path' in .gitmodules"
 367
 368                        # Possibly a url relative to parent
 369                        case "$url" in
 370                        ./*|../*)
 371                                url=$(resolve_relative_url "$url") || exit
 372                                ;;
 373                        esac
 374                        git config submodule."$name".url "$url" ||
 375                        die "Failed to register url for submodule path '$path'"
 376                fi
 377
 378                # Copy "update" setting when it is not set yet
 379                upd="$(git config -f .gitmodules submodule."$name".update)"
 380                test -z "$upd" ||
 381                test -n "$(git config submodule."$name".update)" ||
 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                if git config "submodule.$name.url" >/dev/null 2>/dev/null
 883                then
 884                        say "Synchronizing submodule url for '$name'"
 885                        git config submodule."$name".url "$url"
 886
 887                        if test -e "$path"/.git
 888                        then
 889                        (
 890                                clear_local_git_env
 891                                cd "$path"
 892                                remote=$(get_default_remote)
 893                                git config remote."$remote".url "$url"
 894                        )
 895                        fi
 896                fi
 897        done
 898}
 899
 900# This loop parses the command line arguments to find the
 901# subcommand name to dispatch.  Parsing of the subcommand specific
 902# options are primarily done by the subcommand implementations.
 903# Subcommand specific options such as --branch and --cached are
 904# parsed here as well, for backward compatibility.
 905
 906while test $# != 0 && test -z "$command"
 907do
 908        case "$1" in
 909        add | foreach | init | update | status | summary | sync)
 910                command=$1
 911                ;;
 912        -q|--quiet)
 913                GIT_QUIET=1
 914                ;;
 915        -b|--branch)
 916                case "$2" in
 917                '')
 918                        usage
 919                        ;;
 920                esac
 921                branch="$2"; shift
 922                ;;
 923        --cached)
 924                cached="$1"
 925                ;;
 926        --)
 927                break
 928                ;;
 929        -*)
 930                usage
 931                ;;
 932        *)
 933                break
 934                ;;
 935        esac
 936        shift
 937done
 938
 939# No command word defaults to "status"
 940test -n "$command" || command=status
 941
 942# "-b branch" is accepted only by "add"
 943if test -n "$branch" && test "$command" != add
 944then
 945        usage
 946fi
 947
 948# "--cached" is accepted only by "status" and "summary"
 949if test -n "$cached" && test "$command" != status -a "$command" != summary
 950then
 951        usage
 952fi
 953
 954"cmd_$command" "$@"