Code

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