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