]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
Merge branch 'jk/submodule-subdirectory-ok'
[thirdparty/git.git] / git-submodule.sh
CommitLineData
70c7ac22
LH
1#!/bin/sh
2#
4c8a9db6 3# git-submodule.sh: add, init, update or list git submodules
70c7ac22
LH
4#
5# Copyright (c) 2007 Lars Hjemli
6
1d5bec8b 7dashless=$(basename "$0" | sed -e 's/-/ /')
681b036f 8USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
64b19ffe 9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1d5bec8b 10 or: $dashless [--quiet] init [--] [<path>...]
cf419828 11 or: $dashless [--quiet] deinit [-f|--force] [--] <path>...
06b1abb5 12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
adc54235 13 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
15fc56a8 14 or: $dashless [--quiet] foreach [--recursive] <command>
82f49f29 15 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
8f321a39 16OPTIONS_SPEC=
091a6eb0 17SUBDIRECTORY_OK=Yes
70c7ac22 18. git-sh-setup
d0ad8251 19. git-sh-i18n
98fcf840 20. git-parse-remote
70c7ac22 21require_work_tree
091a6eb0
JK
22wt_prefix=$(git rev-parse --show-prefix)
23cd_to_toplevel
70c7ac22 24
5c08dbbd 25command=
ecda0723 26branch=
d27b876b 27force=
d92a3959 28reference=
70c7ac22 29cached=
48bb3033
GP
30recursive=
31init=
1c244f6e 32files=
06b1abb5 33remote=
31ca3ac3 34nofetch=
32948425 35update=
15fc56a8 36prefix=
73b0898d 37custom_name=
70c7ac22 38
967b2c66
JS
39# The function takes at most 2 arguments. The first argument is the
40# URL that navigates to the submodule origin repo. When relative, this URL
41# is relative to the superproject origin URL repo. The second up_path
42# argument, if specified, is the relative path that navigates
43# from the submodule working tree to the superproject working tree.
44#
45# The output of the function is the origin URL of the submodule.
46#
47# The output will either be an absolute URL or filesystem path (if the
48# superproject origin URL is an absolute URL or filesystem path,
49# respectively) or a relative file system path (if the superproject
50# origin URL is a relative file system path).
51#
52# When the output is a relative file system path, the path is either
53# relative to the submodule working tree, if up_path is specified, or to
54# the superproject working tree otherwise.
f31a522a
ML
55resolve_relative_url ()
56{
98fcf840 57 remote=$(get_default_remote)
8e7e6f39 58 remoteurl=$(git config "remote.$remote.url") ||
4d689320 59 remoteurl=$(pwd) # the repository is its own authoritative upstream
f31a522a 60 url="$1"
99b120af 61 remoteurl=${remoteurl%/}
ea640cc6 62 sep=/
967b2c66
JS
63 up_path="$2"
64
65 case "$remoteurl" in
66 *:*|/*)
67 is_relative=
68 ;;
758615e2
JS
69 ./*|../*)
70 is_relative=t
71 ;;
967b2c66
JS
72 *)
73 is_relative=t
758615e2 74 remoteurl="./$remoteurl"
967b2c66
JS
75 ;;
76 esac
77
f31a522a
ML
78 while test -n "$url"
79 do
80 case "$url" in
81 ../*)
82 url="${url#../}"
ea640cc6
TR
83 case "$remoteurl" in
84 */*)
85 remoteurl="${remoteurl%/*}"
86 ;;
87 *:*)
88 remoteurl="${remoteurl%:*}"
89 sep=:
90 ;;
91 *)
758615e2
JS
92 if test -z "$is_relative" || test "." = "$remoteurl"
93 then
94 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
95 else
96 remoteurl=.
97 fi
ea640cc6
TR
98 ;;
99 esac
f31a522a
ML
100 ;;
101 ./*)
102 url="${url#./}"
103 ;;
104 *)
105 break;;
106 esac
107 done
758615e2
JS
108 remoteurl="$remoteurl$sep${url%/}"
109 echo "${is_relative:+${up_path}}${remoteurl#./}"
f31a522a
ML
110}
111
091a6eb0
JK
112# Resolve a path to be relative to another path. This is intended for
113# converting submodule paths when git-submodule is run in a subdirectory
114# and only handles paths where the directory separator is '/'.
115#
116# The output is the first argument as a path relative to the second argument,
117# which defaults to $wt_prefix if it is omitted.
118relative_path ()
119{
120 local target curdir result
121 target=$1
122 curdir=${2-$wt_prefix}
123 curdir=${curdir%/}
124 result=
125
126 while test -n "$curdir"
127 do
128 case "$target" in
129 "$curdir/"*)
130 target=${target#"$curdir"/}
131 break
132 ;;
133 esac
134
135 result="${result}../"
136 if test "$curdir" = "${curdir%/*}"
137 then
138 curdir=
139 else
140 curdir="${curdir%/*}"
141 fi
142 done
143
144 echo "$result$target"
145}
146
a7b3269c
DA
147#
148# Get submodule info for registered submodules
149# $@ = path to limit submodule list
150#
151module_list()
152{
091a6eb0 153 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
be9d0a3a 154 (
74671241 155 git ls-files -z --error-unmatch --stage -- "$@" ||
be9d0a3a
HV
156 echo "unmatched pathspec exists"
157 ) |
313ee0d6
NMC
158 perl -e '
159 my %unmerged = ();
160 my ($null_sha1) = ("0" x 40);
be9d0a3a
HV
161 my @out = ();
162 my $unmatched = 0;
74671241 163 $/ = "\0";
313ee0d6 164 while (<STDIN>) {
be9d0a3a
HV
165 if (/^unmatched pathspec/) {
166 $unmatched = 1;
167 next;
168 }
313ee0d6
NMC
169 chomp;
170 my ($mode, $sha1, $stage, $path) =
171 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
172 next unless $mode eq "160000";
173 if ($stage ne "0") {
174 if (!$unmerged{$path}++) {
be9d0a3a 175 push @out, "$mode $null_sha1 U\t$path\n";
313ee0d6
NMC
176 }
177 next;
178 }
be9d0a3a
HV
179 push @out, "$_\n";
180 }
181 if ($unmatched) {
182 print "#unmatched\n";
183 } else {
184 print for (@out);
313ee0d6
NMC
185 }
186 '
a7b3269c
DA
187}
188
be9d0a3a
HV
189die_if_unmatched ()
190{
191 if test "$1" = "#unmatched"
192 then
193 exit 1
194 fi
195}
196
88ce00c3
TK
197#
198# Print a submodule configuration setting
199#
200# $1 = submodule name
201# $2 = option name
202# $3 = default value
203#
204# Checks in the usual git-config places first (for overrides),
205# otherwise it falls back on .gitmodules. This allows you to
206# distribute project-wide defaults in .gitmodules, while still
207# customizing individual repositories if necessary. If the option is
208# not in .gitmodules either, print a default value.
209#
210get_submodule_config () {
211 name="$1"
212 option="$2"
213 default="$3"
214 value=$(git config submodule."$name"."$option")
215 if test -z "$value"
216 then
217 value=$(git config -f .gitmodules submodule."$name"."$option")
218 fi
219 printf '%s' "${value:-$default}"
220}
221
222
941987a5
LH
223#
224# Map submodule path to submodule name
225#
226# $1 = path
227#
228module_name()
229{
537601ac 230 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
64394e3a 231 sm_path="$1"
fe22e542 232 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
a5099bb4 233 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
537601ac 234 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
1e42258a 235 test -z "$name" &&
64394e3a 236 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
1e42258a 237 echo "$name"
941987a5 238}
33aa6fff
LH
239
240#
241# Clone a submodule
242#
23a485e3 243# Prior to calling, cmd_update checks that a possibly existing
ecda0723 244# path is not a git repository.
23a485e3 245# Likewise, cmd_add checks that path does not exist at all,
ecda0723
SV
246# since it is the location of a new submodule.
247#
33aa6fff
LH
248module_clone()
249{
64394e3a 250 sm_path=$1
73b0898d
JL
251 name=$2
252 url=$3
253 reference="$4"
7e60407f
JL
254 quiet=
255 if test -n "$GIT_QUIET"
256 then
257 quiet=-q
258 fi
33aa6fff 259
501770e1
FG
260 gitdir=
261 gitdir_base=
69c30517 262 base_name=$(dirname "$name")
33aa6fff 263
501770e1 264 gitdir=$(git rev-parse --git-dir)
69c30517
JL
265 gitdir_base="$gitdir/modules/$base_name"
266 gitdir="$gitdir/modules/$name"
501770e1
FG
267
268 if test -d "$gitdir"
d92a3959 269 then
64394e3a 270 mkdir -p "$sm_path"
501770e1 271 rm -f "$gitdir/index"
d92a3959 272 else
501770e1 273 mkdir -p "$gitdir_base"
be8779f7
DG
274 (
275 clear_local_git_env
276 git clone $quiet -n ${reference:+"$reference"} \
277 --separate-git-dir "$gitdir" "$url" "$sm_path"
278 ) ||
64394e3a 279 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
501770e1 280 fi
ea115a0d 281
6eafa6d0
JL
282 # We already are at the root of the work tree but cd_to_toplevel will
283 # resolve any symlinks that might be present in $PWD
284 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
15b3c82c 285 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
4dce7d9b
JS
286 # normalize Windows-style absolute paths to POSIX-style absolute paths
287 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
288 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
d75219b4
JL
289 # Remove all common leading directories after a sanity check
290 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
291 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
292 fi
293 while test "${a%%/*}" = "${b%%/*}"
294 do
295 a=${a#*/}
296 b=${b#*/}
297 done
298 # Now chop off the trailing '/'s that were added in the beginning
299 a=${a%/}
300 b=${b%/}
301
c5bc42b9
BW
302 # Turn each leading "*/" component into "../"
303 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
64394e3a 304 echo "gitdir: $rel/$a" >"$sm_path/.git"
69c30517 305
c5bc42b9 306 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
64394e3a 307 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
33aa6fff
LH
308}
309
862ae6cd
RS
310isnumber()
311{
312 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
313}
314
ecda0723
SV
315#
316# Add a new submodule to the working tree, .gitmodules and the index
317#
ec05df35 318# $@ = repo path
ecda0723
SV
319#
320# optional branch is stored in global branch variable
321#
23a485e3 322cmd_add()
ecda0723 323{
5c08dbbd 324 # parse $args after "submodule ... add".
091a6eb0 325 reference_path=
5c08dbbd
JH
326 while test $# -ne 0
327 do
328 case "$1" in
329 -b | --branch)
330 case "$2" in '') usage ;; esac
331 branch=$2
332 shift
333 ;;
d27b876b
JL
334 -f | --force)
335 force=$1
336 ;;
5c08dbbd 337 -q|--quiet)
2e6a30ef 338 GIT_QUIET=1
5c08dbbd 339 ;;
d92a3959
MT
340 --reference)
341 case "$2" in '') usage ;; esac
091a6eb0 342 reference_path=$2
d92a3959
MT
343 shift
344 ;;
345 --reference=*)
091a6eb0 346 reference_path="${1#--reference=}"
d92a3959 347 ;;
73b0898d
JL
348 --name)
349 case "$2" in '') usage ;; esac
350 custom_name=$2
351 shift
352 ;;
5c08dbbd
JH
353 --)
354 shift
355 break
356 ;;
357 -*)
358 usage
359 ;;
360 *)
361 break
362 ;;
363 esac
364 shift
365 done
366
091a6eb0
JK
367 if test -n "$reference_path"
368 then
369 is_absolute_path "$reference_path" ||
370 reference_path="$wt_prefix$reference_path"
371
372 reference="--reference=$reference_path"
373 fi
374
ecda0723 375 repo=$1
64394e3a 376 sm_path=$2
ecda0723 377
64394e3a
RJ
378 if test -z "$sm_path"; then
379 sm_path=$(echo "$repo" |
1414e578
JL
380 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
381 fi
382
64394e3a 383 if test -z "$repo" -o -z "$sm_path"; then
ecda0723
SV
384 usage
385 fi
386
091a6eb0
JK
387 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
388
ec05df35
ML
389 # assure repo is absolute or relative to parent
390 case "$repo" in
391 ./*|../*)
091a6eb0
JK
392 test -z "$wt_prefix" ||
393 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
394
ec05df35
ML
395 # dereference source url relative to parent's url
396 realrepo=$(resolve_relative_url "$repo") || exit
397 ;;
398 *:*|/*)
399 # absolute url
400 realrepo=$repo
401 ;;
402 *)
497ee872 403 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
ec05df35
ML
404 ;;
405 esac
406
db75ada5
MG
407 # normalize path:
408 # multiple //; leading ./; /./; /../; trailing /
64394e3a 409 sm_path=$(printf '%s/\n' "$sm_path" |
db75ada5
MG
410 sed -e '
411 s|//*|/|g
412 s|^\(\./\)*||
413 s|/\./|/|g
414 :start
415 s|\([^/]*\)/\.\./||
416 tstart
417 s|/*$||
418 ')
64394e3a
RJ
419 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
420 die "$(eval_gettext "'\$sm_path' already exists in the index")"
ecda0723 421
64394e3a 422 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
d27b876b 423 then
6ff875c5 424 eval_gettextln "The following path is ignored by one of your .gitignore files:
64394e3a 425\$sm_path
6ff875c5 426Use -f if you really want to add it." >&2
d27b876b
JL
427 exit 1
428 fi
429
73b0898d
JL
430 if test -n "$custom_name"
431 then
432 sm_name="$custom_name"
433 else
434 sm_name="$sm_path"
435 fi
436
d4264ca3 437 # perhaps the path exists and is already a git repo, else clone it
64394e3a 438 if test -e "$sm_path"
d4264ca3 439 then
64394e3a 440 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
d4264ca3 441 then
64394e3a 442 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
d4264ca3 443 else
64394e3a 444 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
d4264ca3 445 fi
c2f93917 446
d4264ca3 447 else
4b7c286e
JL
448 if test -d ".git/modules/$sm_name"
449 then
450 if test -z "$force"
451 then
452 echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
453 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
454 echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
455 echo >&2 " $realrepo"
456 echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
457 die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
458 else
459 echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
460 fi
461 fi
73b0898d 462 module_clone "$sm_path" "$sm_name" "$realrepo" "$reference" || exit
ea10b60c 463 (
74ae1419 464 clear_local_git_env
64394e3a 465 cd "$sm_path" &&
ea10b60c
BJ
466 # ash fails to wordsplit ${branch:+-b "$branch"...}
467 case "$branch" in
468 '') git checkout -f -q ;;
502dc5b6 469 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
ea10b60c 470 esac
64394e3a 471 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
d4264ca3 472 fi
73b0898d 473 git config submodule."$sm_name".url "$realrepo"
d4264ca3 474
64394e3a
RJ
475 git add $force "$sm_path" ||
476 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
ecda0723 477
73b0898d
JL
478 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
479 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
b9289227
TK
480 if test -n "$branch"
481 then
482 git config -f .gitmodules submodule."$sm_name".branch "$branch"
483 fi &&
31991b02 484 git add --force .gitmodules ||
64394e3a 485 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
ecda0723
SV
486}
487
19a31f9c
ML
488#
489# Execute an arbitrary command sequence in each checked out
490# submodule
491#
492# $@ = command to execute
493#
494cmd_foreach()
495{
1d5bec8b
JH
496 # parse $args after "submodule ... foreach".
497 while test $# -ne 0
498 do
499 case "$1" in
500 -q|--quiet)
501 GIT_QUIET=1
502 ;;
15fc56a8
JH
503 --recursive)
504 recursive=1
505 ;;
1d5bec8b
JH
506 -*)
507 usage
508 ;;
509 *)
510 break
511 ;;
512 esac
513 shift
514 done
515
f030c96d
ÆAB
516 toplevel=$(pwd)
517
4dca1aa6
BC
518 # dup stdin so that it can be restored when running the external
519 # command in the subshell (and a recursive call to this function)
520 exec 3<&0
521
a7b3269c 522 module_list |
64394e3a 523 while read mode sha1 stage sm_path
19a31f9c 524 do
be9d0a3a 525 die_if_unmatched "$mode"
64394e3a 526 if test -e "$sm_path"/.git
19a31f9c 527 then
091a6eb0
JK
528 displaypath=$(relative_path "$sm_path")
529 say "$(eval_gettext "Entering '\$prefix\$displaypath'")"
64394e3a 530 name=$(module_name "$sm_path")
15fc56a8 531 (
64394e3a 532 prefix="$prefix$sm_path/"
74ae1419 533 clear_local_git_env
64394e3a 534 cd "$sm_path" &&
091a6eb0
JK
535 sm_path=$(relative_path "$sm_path") &&
536 # we make $path available to scripts ...
537 path=$sm_path &&
15fc56a8
JH
538 eval "$@" &&
539 if test -n "$recursive"
540 then
541 cmd_foreach "--recursive" "$@"
542 fi
4dca1aa6 543 ) <&3 3<&- ||
091a6eb0 544 die "$(eval_gettext "Stopping at '\$prefix\$displaypath'; script returned non-zero status.")"
19a31f9c
ML
545 fi
546 done
547}
548
70c7ac22 549#
211b7f19 550# Register submodules in .git/config
70c7ac22
LH
551#
552# $@ = requested paths (default to all)
553#
23a485e3 554cmd_init()
70c7ac22 555{
5c08dbbd
JH
556 # parse $args after "submodule ... init".
557 while test $# -ne 0
558 do
559 case "$1" in
560 -q|--quiet)
2e6a30ef 561 GIT_QUIET=1
5c08dbbd
JH
562 ;;
563 --)
564 shift
565 break
566 ;;
567 -*)
568 usage
569 ;;
570 *)
571 break
572 ;;
573 esac
574 shift
575 done
576
a7b3269c 577 module_list "$@" |
64394e3a 578 while read mode sha1 stage sm_path
70c7ac22 579 do
be9d0a3a 580 die_if_unmatched "$mode"
64394e3a 581 name=$(module_name "$sm_path") || exit
c1c259e2 582
091a6eb0
JK
583 displaypath=$(relative_path "$sm_path")
584
c1c259e2 585 # Copy url setting when it is not set yet
2cd9de3e
JL
586 if test -z "$(git config "submodule.$name.url")"
587 then
588 url=$(git config -f .gitmodules submodule."$name".url)
589 test -z "$url" &&
091a6eb0 590 die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")"
2cd9de3e
JL
591
592 # Possibly a url relative to parent
593 case "$url" in
594 ./*|../*)
595 url=$(resolve_relative_url "$url") || exit
596 ;;
597 esac
598 git config submodule."$name".url "$url" ||
091a6eb0 599 die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")"
c1c259e2 600
091a6eb0 601 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")"
2cd9de3e 602 fi
70c7ac22 603
2cd9de3e 604 # Copy "update" setting when it is not set yet
32948425
JH
605 upd="$(git config -f .gitmodules submodule."$name".update)"
606 test -z "$upd" ||
2cd9de3e 607 test -n "$(git config submodule."$name".update)" ||
32948425 608 git config submodule."$name".update "$upd" ||
091a6eb0 609 die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
70c7ac22
LH
610 done
611}
612
cf419828
JL
613#
614# Unregister submodules from .git/config and remove their work tree
615#
616# $@ = requested paths (use '.' to deinit all submodules)
617#
618cmd_deinit()
619{
620 # parse $args after "submodule ... deinit".
621 while test $# -ne 0
622 do
623 case "$1" in
624 -f|--force)
625 force=$1
626 ;;
627 -q|--quiet)
628 GIT_QUIET=1
629 ;;
630 --)
631 shift
632 break
633 ;;
634 -*)
635 usage
636 ;;
637 *)
638 break
639 ;;
640 esac
641 shift
642 done
643
644 if test $# = 0
645 then
646 die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
647 fi
648
649 module_list "$@" |
650 while read mode sha1 stage sm_path
651 do
652 die_if_unmatched "$mode"
653 name=$(module_name "$sm_path") || exit
654
091a6eb0
JK
655 displaypath=$(relative_path "$sm_path")
656
cf419828
JL
657 # Remove the submodule work tree (unless the user already did it)
658 if test -d "$sm_path"
659 then
660 # Protect submodules containing a .git directory
661 if test -d "$sm_path/.git"
662 then
091a6eb0 663 echo >&2 "$(eval_gettext "Submodule work tree '\$displaypath' contains a .git directory")"
cf419828
JL
664 die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
665 fi
666
667 if test -z "$force"
668 then
7b294bf4 669 git rm -qn "$sm_path" ||
091a6eb0 670 die "$(eval_gettext "Submodule work tree '\$displaypath' contains local modifications; use '-f' to discard them")"
cf419828 671 fi
7b294bf4 672 rm -rf "$sm_path" &&
091a6eb0
JK
673 say "$(eval_gettext "Cleared directory '\$displaypath'")" ||
674 say "$(eval_gettext "Could not remove submodule work tree '\$displaypath'")"
cf419828
JL
675 fi
676
091a6eb0 677 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$displaypath'")"
cf419828
JL
678
679 # Remove the .git/config entries (unless the user already did it)
680 if test -n "$(git config --get-regexp submodule."$name\.")"
681 then
682 # Remove the whole section so we have a clean state when
683 # the user later decides to init this submodule again
684 url=$(git config submodule."$name".url)
685 git config --remove-section submodule."$name" 2>/dev/null &&
091a6eb0 686 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$displaypath'")"
cf419828
JL
687 fi
688 done
689}
690
70c7ac22 691#
211b7f19 692# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
693#
694# $@ = requested paths (default to all)
695#
23a485e3 696cmd_update()
70c7ac22 697{
5c08dbbd 698 # parse $args after "submodule ... update".
98dbe63d 699 orig_flags=
5c08dbbd
JH
700 while test $# -ne 0
701 do
702 case "$1" in
703 -q|--quiet)
2e6a30ef 704 GIT_QUIET=1
5c08dbbd 705 ;;
be4d2c83 706 -i|--init)
d92a3959 707 init=1
be4d2c83 708 ;;
06b1abb5
TK
709 --remote)
710 remote=1
711 ;;
31ca3ac3 712 -N|--no-fetch)
31ca3ac3
FF
713 nofetch=1
714 ;;
9db31bdf
NMC
715 -f|--force)
716 force=$1
717 ;;
ca2cedba 718 -r|--rebase)
32948425 719 update="rebase"
ca2cedba 720 ;;
d92a3959
MT
721 --reference)
722 case "$2" in '') usage ;; esac
723 reference="--reference=$2"
98dbe63d
KB
724 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
725 shift
d92a3959
MT
726 ;;
727 --reference=*)
728 reference="$1"
d92a3959 729 ;;
42b49178 730 -m|--merge)
42b49178
JH
731 update="merge"
732 ;;
b13fd5c1 733 --recursive)
b13fd5c1
JH
734 recursive=1
735 ;;
efc5fb6a
JH
736 --checkout)
737 update="checkout"
738 ;;
5c08dbbd
JH
739 --)
740 shift
741 break
742 ;;
743 -*)
744 usage
745 ;;
746 *)
747 break
748 ;;
749 esac
98dbe63d
KB
750 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
751 shift
5c08dbbd
JH
752 done
753
d92a3959
MT
754 if test -n "$init"
755 then
756 cmd_init "--" "$@" || return
757 fi
758
1b4735d9 759 cloned_modules=
15ffb7cd
FG
760 module_list "$@" | {
761 err=
64394e3a 762 while read mode sha1 stage sm_path
70c7ac22 763 do
be9d0a3a 764 die_if_unmatched "$mode"
313ee0d6
NMC
765 if test "$stage" = U
766 then
75bf5e60 767 echo >&2 "Skipping unmerged submodule $prefix$sm_path"
313ee0d6
NMC
768 continue
769 fi
64394e3a 770 name=$(module_name "$sm_path") || exit
5be60078 771 url=$(git config submodule."$name".url)
06b1abb5 772 branch=$(get_submodule_config "$name" branch master)
efc5fb6a
JH
773 if ! test -z "$update"
774 then
775 update_module=$update
776 else
777 update_module=$(git config submodule."$name".update)
778 fi
779
091a6eb0
JK
780 displaypath=$(relative_path "$prefix$sm_path")
781
efc5fb6a
JH
782 if test "$update_module" = "none"
783 then
091a6eb0 784 echo "Skipping submodule '$displaypath'"
efc5fb6a
JH
785 continue
786 fi
787
211b7f19 788 if test -z "$url"
70c7ac22
LH
789 then
790 # Only mention uninitialized submodules when its
791 # path have been specified
792 test "$#" != "0" &&
091a6eb0 793 say "$(eval_gettext "Submodule path '\$displaypath' not initialized
1c2ef66f 794Maybe you want to use 'update --init'?")"
211b7f19
LH
795 continue
796 fi
797
64394e3a 798 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
211b7f19 799 then
73b0898d 800 module_clone "$sm_path" "$name" "$url" "$reference" || exit
1b4735d9 801 cloned_modules="$cloned_modules;$name"
bf2d8246
LH
802 subsha1=
803 else
64394e3a 804 subsha1=$(clear_local_git_env; cd "$sm_path" &&
5be60078 805 git rev-parse --verify HEAD) ||
091a6eb0 806 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
70c7ac22 807 fi
211b7f19 808
06b1abb5
TK
809 if test -n "$remote"
810 then
811 if test -z "$nofetch"
812 then
813 # Fetch remote before determining tracking $sha1
814 (clear_local_git_env; cd "$sm_path" && git-fetch) ||
815 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
816 fi
817 remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
818 sha1=$(clear_local_git_env; cd "$sm_path" &&
819 git rev-parse --verify "${remote_name}/${branch}") ||
820 die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
821 fi
822
01d47215 823 if test "$subsha1" != "$sha1" -o -n "$force"
70c7ac22 824 then
9db31bdf
NMC
825 subforce=$force
826 # If we don't already have a -f flag and the submodule has never been checked out
827 if test -z "$subsha1" -a -z "$force"
b9b378a0 828 then
9db31bdf 829 subforce="-f"
b9b378a0 830 fi
31ca3ac3
FF
831
832 if test -z "$nofetch"
833 then
e5f522d6
JL
834 # Run fetch only if $sha1 isn't present or it
835 # is not reachable from a ref.
64394e3a 836 (clear_local_git_env; cd "$sm_path" &&
f5799e05 837 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
e5f522d6 838 test -z "$rev") || git-fetch)) ||
091a6eb0 839 die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
31ca3ac3
FF
840 fi
841
1b4735d9
SO
842 # Is this something we just cloned?
843 case ";$cloned_modules;" in
844 *";$name;"*)
845 # then there is no local change to integrate
846 update_module= ;;
847 esac
848
877449c1 849 must_die_on_failure=
32948425
JH
850 case "$update_module" in
851 rebase)
852 command="git rebase"
091a6eb0
JK
853 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
854 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
877449c1 855 must_die_on_failure=yes
32948425 856 ;;
42b49178
JH
857 merge)
858 command="git merge"
091a6eb0
JK
859 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
860 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
877449c1 861 must_die_on_failure=yes
42b49178 862 ;;
32948425 863 *)
9db31bdf 864 command="git checkout $subforce -q"
091a6eb0
JK
865 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
866 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
32948425
JH
867 ;;
868 esac
70c7ac22 869
64394e3a 870 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
15ffb7cd 871 then
ff968f03 872 say "$say_msg"
877449c1
JH
873 elif test -n "$must_die_on_failure"
874 then
ff968f03 875 die_with_status 2 "$die_msg"
15ffb7cd 876 else
ff968f03 877 err="${err};$die_msg"
877449c1 878 continue
15ffb7cd 879 fi
70c7ac22 880 fi
b13fd5c1
JH
881
882 if test -n "$recursive"
883 then
75bf5e60
WE
884 (
885 prefix="$prefix$sm_path/"
886 clear_local_git_env
887 cd "$sm_path" &&
888 eval cmd_update "$orig_flags"
889 )
15ffb7cd
FG
890 res=$?
891 if test $res -gt 0
892 then
091a6eb0 893 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
15ffb7cd
FG
894 if test $res -eq 1
895 then
ff968f03 896 err="${err};$die_msg"
15ffb7cd
FG
897 continue
898 else
ff968f03 899 die_with_status $res "$die_msg"
15ffb7cd
FG
900 fi
901 fi
b13fd5c1 902 fi
70c7ac22 903 done
15ffb7cd
FG
904
905 if test -n "$err"
906 then
907 OIFS=$IFS
908 IFS=';'
909 for e in $err
910 do
911 if test -n "$e"
912 then
913 echo >&2 "$e"
914 fi
915 done
916 IFS=$OIFS
917 exit 1
918 fi
919 }
70c7ac22
LH
920}
921
bffe71f4
EM
922set_name_rev () {
923 revname=$( (
74ae1419 924 clear_local_git_env
bffe71f4 925 cd "$1" && {
5be60078
JH
926 git describe "$2" 2>/dev/null ||
927 git describe --tags "$2" 2>/dev/null ||
f669ac0b
ML
928 git describe --contains "$2" 2>/dev/null ||
929 git describe --all --always "$2"
bffe71f4
EM
930 }
931 ) )
932 test -z "$revname" || revname=" ($revname)"
933}
28f9af5d
PY
934#
935# Show commit summary for submodules in index or working tree
936#
937# If '--cached' is given, show summary between index and given commit,
938# or between working tree and given commit
939#
940# $@ = [commit (default 'HEAD'),] requested paths (default all)
941#
942cmd_summary() {
f2dc06a3 943 summary_limit=-1
d0f64dd4 944 for_status=
1c244f6e 945 diff_cmd=diff-index
f2dc06a3 946
28f9af5d
PY
947 # parse $args after "submodule ... summary".
948 while test $# -ne 0
949 do
950 case "$1" in
951 --cached)
952 cached="$1"
953 ;;
1c244f6e
JL
954 --files)
955 files="$1"
956 ;;
d0f64dd4
PY
957 --for-status)
958 for_status="$1"
959 ;;
f2dc06a3 960 -n|--summary-limit)
862ae6cd
RS
961 summary_limit="$2"
962 isnumber "$summary_limit" || usage
f2dc06a3
PY
963 shift
964 ;;
862ae6cd
RS
965 --summary-limit=*)
966 summary_limit="${1#--summary-limit=}"
967 isnumber "$summary_limit" || usage
968 ;;
28f9af5d
PY
969 --)
970 shift
971 break
972 ;;
973 -*)
974 usage
975 ;;
976 *)
977 break
978 ;;
979 esac
980 shift
981 done
bffe71f4 982
f2dc06a3
PY
983 test $summary_limit = 0 && return
984
3deea89c 985 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
28f9af5d
PY
986 then
987 head=$rev
caa9c3ca 988 test $# = 0 || shift
3deea89c
JH
989 elif test -z "$1" -o "$1" = "HEAD"
990 then
14e940d7
JH
991 # before the first commit: compare with an empty tree
992 head=$(git hash-object -w -t tree --stdin </dev/null)
2ea6c2c9 993 test -z "$1" || shift
28f9af5d 994 else
3deea89c 995 head="HEAD"
28f9af5d
PY
996 fi
997
1c244f6e
JL
998 if [ -n "$files" ]
999 then
1000 test -n "$cached" &&
465d6a00 1001 die "$(gettext "The --cached option cannot be used with the --files option")"
1c244f6e
JL
1002 diff_cmd=diff-files
1003 head=
1004 fi
1005
28f9af5d 1006 cd_to_toplevel
091a6eb0 1007 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
28f9af5d 1008 # Get modified modules cared by user
18076502 1009 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
e1622bfc 1010 sane_egrep '^:([0-7]* )?160000' |
28f9af5d
PY
1011 while read mod_src mod_dst sha1_src sha1_dst status name
1012 do
1013 # Always show modules deleted or type-changed (blob<->module)
1014 test $status = D -o $status = T && echo "$name" && continue
1015 # Also show added or modified modules which are checked out
1016 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
1017 echo "$name"
1018 done
1019 )
1cb639e6 1020
d0f64dd4
PY
1021 test -z "$modules" && return
1022
18076502 1023 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
e1622bfc 1024 sane_egrep '^:([0-7]* )?160000' |
1cb639e6
PY
1025 cut -c2- |
1026 while read mod_src mod_dst sha1_src sha1_dst status name
1027 do
1028 if test -z "$cached" &&
1029 test $sha1_dst = 0000000000000000000000000000000000000000
1030 then
1031 case "$mod_dst" in
1032 160000)
1033 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
1034 ;;
1035 100644 | 100755 | 120000)
1036 sha1_dst=$(git hash-object $name)
1037 ;;
1038 000000)
1039 ;; # removed
1040 *)
1041 # unexpected type
6ff875c5 1042 eval_gettextln "unexpected mode \$mod_dst" >&2
1cb639e6
PY
1043 continue ;;
1044 esac
1045 fi
1046 missing_src=
1047 missing_dst=
1048
1049 test $mod_src = 160000 &&
30353a40 1050 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e6
PY
1051 missing_src=t
1052
1053 test $mod_dst = 160000 &&
30353a40 1054 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e6 1055 missing_dst=t
bffe71f4 1056
091a6eb0
JK
1057 display_name=$(relative_path "$name")
1058
1cb639e6
PY
1059 total_commits=
1060 case "$missing_src,$missing_dst" in
1061 t,)
091a6eb0 1062 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
1cb639e6
PY
1063 ;;
1064 ,t)
091a6eb0 1065 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
1cb639e6
PY
1066 ;;
1067 t,t)
091a6eb0 1068 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
1cb639e6
PY
1069 ;;
1070 *)
1071 errmsg=
1072 total_commits=$(
1073 if test $mod_src = 160000 -a $mod_dst = 160000
1074 then
1075 range="$sha1_src...$sha1_dst"
1076 elif test $mod_src = 160000
1077 then
1078 range=$sha1_src
1079 else
1080 range=$sha1_dst
1081 fi
1082 GIT_DIR="$name/.git" \
b0e621ad 1083 git rev-list --first-parent $range -- | wc -l
1cb639e6 1084 )
eed35595 1085 total_commits=" ($(($total_commits + 0)))"
1cb639e6
PY
1086 ;;
1087 esac
1088
1089 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1090 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1091 if test $status = T
1092 then
b3e73449
ÆAB
1093 blob="$(gettext "blob")"
1094 submodule="$(gettext "submodule")"
1cb639e6
PY
1095 if test $mod_dst = 160000
1096 then
091a6eb0 1097 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1cb639e6 1098 else
091a6eb0 1099 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1cb639e6
PY
1100 fi
1101 else
091a6eb0 1102 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1cb639e6
PY
1103 fi
1104 if test -n "$errmsg"
1105 then
1106 # Don't give error msg for modification whose dst is not submodule
1107 # i.e. deleted or changed to blob
1108 test $mod_dst = 160000 && echo "$errmsg"
1109 else
1110 if test $mod_src = 160000 -a $mod_dst = 160000
1111 then
f2dc06a3
PY
1112 limit=
1113 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e6 1114 GIT_DIR="$name/.git" \
f2dc06a3 1115 git log $limit --pretty='format: %m %s' \
1cb639e6
PY
1116 --first-parent $sha1_src...$sha1_dst
1117 elif test $mod_dst = 160000
1118 then
1119 GIT_DIR="$name/.git" \
1120 git log --pretty='format: > %s' -1 $sha1_dst
1121 else
1122 GIT_DIR="$name/.git" \
1123 git log --pretty='format: < %s' -1 $sha1_src
1124 fi
1125 echo
1126 fi
1127 echo
d0f64dd4
PY
1128 done |
1129 if test -n "$for_status"; then
f17a5d34 1130 if [ -n "$files" ]; then
eff80a9f 1131 gettextln "Submodules changed but not updated:" | git stripspace -c
f17a5d34 1132 else
eff80a9f 1133 gettextln "Submodule changes to be committed:" | git stripspace -c
f17a5d34 1134 fi
eff80a9f
JH
1135 printf "\n" | git stripspace -c
1136 git stripspace -c
d0f64dd4
PY
1137 else
1138 cat
1139 fi
28f9af5d 1140}
70c7ac22 1141#
941987a5 1142# List all submodules, prefixed with:
70c7ac22
LH
1143# - submodule not initialized
1144# + different revision checked out
1145#
1146# If --cached was specified the revision in the index will be printed
1147# instead of the currently checked out revision.
1148#
1149# $@ = requested paths (default to all)
1150#
23a485e3 1151cmd_status()
70c7ac22 1152{
5c08dbbd
JH
1153 # parse $args after "submodule ... status".
1154 while test $# -ne 0
1155 do
1156 case "$1" in
1157 -q|--quiet)
2e6a30ef 1158 GIT_QUIET=1
5c08dbbd
JH
1159 ;;
1160 --cached)
1161 cached=1
1162 ;;
64b19ffe
JH
1163 --recursive)
1164 recursive=1
1165 ;;
5c08dbbd
JH
1166 --)
1167 shift
1168 break
1169 ;;
1170 -*)
1171 usage
1172 ;;
1173 *)
1174 break
1175 ;;
1176 esac
1177 shift
1178 done
1179
a7b3269c 1180 module_list "$@" |
64394e3a 1181 while read mode sha1 stage sm_path
70c7ac22 1182 do
be9d0a3a 1183 die_if_unmatched "$mode"
64394e3a 1184 name=$(module_name "$sm_path") || exit
5be60078 1185 url=$(git config submodule."$name".url)
091a6eb0 1186 displaypath=$(relative_path "$prefix$sm_path")
313ee0d6
NMC
1187 if test "$stage" = U
1188 then
1189 say "U$sha1 $displaypath"
1190 continue
1191 fi
64394e3a 1192 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
70c7ac22 1193 then
64b19ffe 1194 say "-$sha1 $displaypath"
70c7ac22
LH
1195 continue;
1196 fi
64394e3a 1197 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
70c7ac22 1198 then
b545cd15 1199 set_name_rev "$sm_path" "$sha1"
64b19ffe 1200 say " $sha1 $displaypath$revname"
70c7ac22
LH
1201 else
1202 if test -z "$cached"
1203 then
64394e3a 1204 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
70c7ac22 1205 fi
b545cd15 1206 set_name_rev "$sm_path" "$sha1"
64b19ffe
JH
1207 say "+$sha1 $displaypath$revname"
1208 fi
1209
1210 if test -n "$recursive"
1211 then
1212 (
1213 prefix="$displaypath/"
74ae1419 1214 clear_local_git_env
64394e3a 1215 cd "$sm_path" &&
e15bec0e 1216 eval cmd_status
64b19ffe 1217 ) ||
64394e3a 1218 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
70c7ac22
LH
1219 fi
1220 done
1221}
2327f61e
DA
1222#
1223# Sync remote urls for submodules
1224# This makes the value for remote.$remote.url match the value
1225# specified in .gitmodules.
1226#
1227cmd_sync()
1228{
1229 while test $# -ne 0
1230 do
1231 case "$1" in
1232 -q|--quiet)
2e6a30ef 1233 GIT_QUIET=1
2327f61e
DA
1234 shift
1235 ;;
82f49f29
PH
1236 --recursive)
1237 recursive=1
1238 shift
1239 ;;
2327f61e
DA
1240 --)
1241 shift
1242 break
1243 ;;
1244 -*)
1245 usage
1246 ;;
1247 *)
1248 break
1249 ;;
1250 esac
1251 done
1252 cd_to_toplevel
1253 module_list "$@" |
64394e3a 1254 while read mode sha1 stage sm_path
2327f61e 1255 do
be9d0a3a 1256 die_if_unmatched "$mode"
64394e3a 1257 name=$(module_name "$sm_path")
2327f61e 1258 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f8
JH
1259
1260 # Possibly a url relative to parent
1261 case "$url" in
1262 ./*|../*)
967b2c66
JS
1263 # rewrite foo/bar as ../.. to find path from
1264 # submodule work tree to superproject work tree
1265 up_path="$(echo "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1266 # guarantee a trailing /
1267 up_path=${up_path%/}/ &&
1268 # path from submodule work tree to submodule origin repo
1269 sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1270 # path from superproject work tree to submodule origin repo
1271 super_config_url=$(resolve_relative_url "$url") || exit
1272 ;;
1273 *)
1274 sub_origin_url="$url"
1275 super_config_url="$url"
baede9f8
JH
1276 ;;
1277 esac
1278
ccee6086 1279 if git config "submodule.$name.url" >/dev/null 2>/dev/null
2327f61e 1280 then
091a6eb0
JK
1281 displaypath=$(relative_path "$prefix$sm_path")
1282 say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
967b2c66 1283 git config submodule."$name".url "$super_config_url"
ccee6086 1284
64394e3a 1285 if test -e "$sm_path"/.git
ccee6086
JH
1286 then
1287 (
1288 clear_local_git_env
64394e3a 1289 cd "$sm_path"
ccee6086 1290 remote=$(get_default_remote)
967b2c66 1291 git config remote."$remote".url "$sub_origin_url"
82f49f29
PH
1292
1293 if test -n "$recursive"
1294 then
1295 prefix="$prefix$sm_path/"
1296 eval cmd_sync
1297 fi
ccee6086
JH
1298 )
1299 fi
2327f61e
DA
1300 fi
1301 done
1302}
70c7ac22 1303
5c08dbbd
JH
1304# This loop parses the command line arguments to find the
1305# subcommand name to dispatch. Parsing of the subcommand specific
1306# options are primarily done by the subcommand implementations.
1307# Subcommand specific options such as --branch and --cached are
1308# parsed here as well, for backward compatibility.
1309
1310while test $# != 0 && test -z "$command"
70c7ac22
LH
1311do
1312 case "$1" in
cf419828 1313 add | foreach | init | deinit | update | status | summary | sync)
5c08dbbd 1314 command=$1
70c7ac22
LH
1315 ;;
1316 -q|--quiet)
2e6a30ef 1317 GIT_QUIET=1
70c7ac22 1318 ;;
ecda0723
SV
1319 -b|--branch)
1320 case "$2" in
1321 '')
1322 usage
1323 ;;
1324 esac
1325 branch="$2"; shift
1326 ;;
70c7ac22 1327 --cached)
28f9af5d 1328 cached="$1"
70c7ac22
LH
1329 ;;
1330 --)
1331 break
1332 ;;
1333 -*)
1334 usage
1335 ;;
1336 *)
1337 break
1338 ;;
1339 esac
1340 shift
1341done
1342
5c08dbbd 1343# No command word defaults to "status"
af9c9f97
RR
1344if test -z "$command"
1345then
1346 if test $# = 0
1347 then
1348 command=status
1349 else
1350 usage
1351 fi
1352fi
5c08dbbd
JH
1353
1354# "-b branch" is accepted only by "add"
1355if test -n "$branch" && test "$command" != add
1356then
ecda0723 1357 usage
5c08dbbd
JH
1358fi
1359
28f9af5d
PY
1360# "--cached" is accepted only by "status" and "summary"
1361if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 1362then
70c7ac22 1363 usage
5c08dbbd
JH
1364fi
1365
1366"cmd_$command" "$@"