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