PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/cgit-0.9.0.2/git/git-submodule.sh

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