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