git-submodule.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. #!/bin/sh
  2. #
  3. # git-submodule.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] [--cached]
  8. or: $dashless [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
  9. or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
  10. or: $dashless [--quiet] init [--] [<path>...]
  11. or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
  12. or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]
  13. or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
  14. or: $dashless [--quiet] set-url [--] <path> <newurl>
  15. or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
  16. or: $dashless [--quiet] foreach [--recursive] <command>
  17. or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
  18. or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"
  19. OPTIONS_SPEC=
  20. SUBDIRECTORY_OK=Yes
  21. . git-sh-setup
  22. . git-parse-remote
  23. require_work_tree
  24. wt_prefix=$(git rev-parse --show-prefix)
  25. cd_to_toplevel
  26. # Tell the rest of git that any URLs we get don't come
  27. # directly from the user, so it can apply policy as appropriate.
  28. GIT_PROTOCOL_FROM_USER=0
  29. export GIT_PROTOCOL_FROM_USER
  30. command=
  31. branch=
  32. force=
  33. reference=
  34. cached=
  35. recursive=
  36. init=
  37. require_init=
  38. files=
  39. remote=
  40. nofetch=
  41. update=
  42. prefix=
  43. custom_name=
  44. depth=
  45. progress=
  46. dissociate=
  47. single_branch=
  48. jobs=
  49. recommend_shallow=
  50. die_if_unmatched ()
  51. {
  52. if test "$1" = "#unmatched"
  53. then
  54. exit ${2:-1}
  55. fi
  56. }
  57. isnumber()
  58. {
  59. n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
  60. }
  61. # Given a full hex object ID, is this the zero OID?
  62. is_zero_oid () {
  63. echo "$1" | sane_egrep '^0+$' >/dev/null 2>&1
  64. }
  65. # Sanitize the local git environment for use within a submodule. We
  66. # can't simply use clear_local_git_env since we want to preserve some
  67. # of the settings from GIT_CONFIG_PARAMETERS.
  68. sanitize_submodule_env()
  69. {
  70. save_config=$GIT_CONFIG_PARAMETERS
  71. clear_local_git_env
  72. GIT_CONFIG_PARAMETERS=$save_config
  73. export GIT_CONFIG_PARAMETERS
  74. }
  75. #
  76. # Add a new submodule to the working tree, .gitmodules and the index
  77. #
  78. # $@ = repo path
  79. #
  80. # optional branch is stored in global branch variable
  81. #
  82. cmd_add()
  83. {
  84. # parse $args after "submodule ... add".
  85. reference_path=
  86. while test $# -ne 0
  87. do
  88. case "$1" in
  89. -b | --branch)
  90. case "$2" in '') usage ;; esac
  91. branch=$2
  92. shift
  93. ;;
  94. -f | --force)
  95. force=$1
  96. ;;
  97. -q|--quiet)
  98. GIT_QUIET=1
  99. ;;
  100. --progress)
  101. progress=1
  102. ;;
  103. --reference)
  104. case "$2" in '') usage ;; esac
  105. reference_path=$2
  106. shift
  107. ;;
  108. --reference=*)
  109. reference_path="${1#--reference=}"
  110. ;;
  111. --dissociate)
  112. dissociate=1
  113. ;;
  114. --name)
  115. case "$2" in '') usage ;; esac
  116. custom_name=$2
  117. shift
  118. ;;
  119. --depth)
  120. case "$2" in '') usage ;; esac
  121. depth="--depth=$2"
  122. shift
  123. ;;
  124. --depth=*)
  125. depth=$1
  126. ;;
  127. --)
  128. shift
  129. break
  130. ;;
  131. -*)
  132. usage
  133. ;;
  134. *)
  135. break
  136. ;;
  137. esac
  138. shift
  139. done
  140. if ! git submodule--helper config --check-writeable >/dev/null 2>&1
  141. then
  142. die "$(eval_gettext "please make sure that the .gitmodules file is in the working tree")"
  143. fi
  144. if test -n "$reference_path"
  145. then
  146. is_absolute_path "$reference_path" ||
  147. reference_path="$wt_prefix$reference_path"
  148. reference="--reference=$reference_path"
  149. fi
  150. repo=$1
  151. sm_path=$2
  152. if test -z "$sm_path"; then
  153. sm_path=$(printf '%s\n' "$repo" |
  154. sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
  155. fi
  156. if test -z "$repo" || test -z "$sm_path"; then
  157. usage
  158. fi
  159. is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
  160. # assure repo is absolute or relative to parent
  161. case "$repo" in
  162. ./*|../*)
  163. test -z "$wt_prefix" ||
  164. die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
  165. # dereference source url relative to parent's url
  166. realrepo=$(git submodule--helper resolve-relative-url "$repo") || exit
  167. ;;
  168. *:*|/*)
  169. # absolute url
  170. realrepo=$repo
  171. ;;
  172. *)
  173. die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
  174. ;;
  175. esac
  176. # normalize path:
  177. # multiple //; leading ./; /./; /../; trailing /
  178. sm_path=$(printf '%s/\n' "$sm_path" |
  179. sed -e '
  180. s|//*|/|g
  181. s|^\(\./\)*||
  182. s|/\(\./\)*|/|g
  183. :start
  184. s|\([^/]*\)/\.\./||
  185. tstart
  186. s|/*$||
  187. ')
  188. if test -z "$force"
  189. then
  190. git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
  191. die "$(eval_gettext "'\$sm_path' already exists in the index")"
  192. else
  193. git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
  194. die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
  195. fi
  196. if test -d "$sm_path" &&
  197. test -z $(git -C "$sm_path" rev-parse --show-cdup 2>/dev/null)
  198. then
  199. git -C "$sm_path" rev-parse --verify -q HEAD >/dev/null ||
  200. die "$(eval_gettext "'\$sm_path' does not have a commit checked out")"
  201. fi
  202. if test -z "$force"
  203. then
  204. dryerr=$(git add --dry-run --ignore-missing --no-warn-embedded-repo "$sm_path" 2>&1 >/dev/null)
  205. res=$?
  206. if test $res -ne 0
  207. then
  208. echo >&2 "$dryerr"
  209. exit $res
  210. fi
  211. fi
  212. if test -n "$custom_name"
  213. then
  214. sm_name="$custom_name"
  215. else
  216. sm_name="$sm_path"
  217. fi
  218. if ! git submodule--helper check-name "$sm_name"
  219. then
  220. die "$(eval_gettext "'$sm_name' is not a valid submodule name")"
  221. fi
  222. # perhaps the path exists and is already a git repo, else clone it
  223. if test -e "$sm_path"
  224. then
  225. if test -d "$sm_path"/.git || test -f "$sm_path"/.git
  226. then
  227. eval_gettextln "Adding existing repo at '\$sm_path' to the index"
  228. else
  229. die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
  230. fi
  231. else
  232. if test -d ".git/modules/$sm_name"
  233. then
  234. if test -z "$force"
  235. then
  236. eval_gettextln >&2 "A git directory for '\$sm_name' is found locally with remote(s):"
  237. GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
  238. die "$(eval_gettextln "\
  239. If you want to reuse this local git directory instead of cloning again from
  240. \$realrepo
  241. use the '--force' option. If the local git directory is not the correct repo
  242. or you are unsure what this means choose another name with the '--name' option.")"
  243. else
  244. eval_gettextln "Reactivating local git directory for submodule '\$sm_name'."
  245. fi
  246. fi
  247. git submodule--helper clone ${GIT_QUIET:+--quiet} ${progress:+"--progress"} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${dissociate:+"--dissociate"} ${depth:+"$depth"} || exit
  248. (
  249. sanitize_submodule_env
  250. cd "$sm_path" &&
  251. # ash fails to wordsplit ${branch:+-b "$branch"...}
  252. case "$branch" in
  253. '') git checkout -f -q ;;
  254. ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
  255. esac
  256. ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
  257. fi
  258. git config submodule."$sm_name".url "$realrepo"
  259. git add --no-warn-embedded-repo $force "$sm_path" ||
  260. die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
  261. git submodule--helper config submodule."$sm_name".path "$sm_path" &&
  262. git submodule--helper config submodule."$sm_name".url "$repo" &&
  263. if test -n "$branch"
  264. then
  265. git submodule--helper config submodule."$sm_name".branch "$branch"
  266. fi &&
  267. git add --force .gitmodules ||
  268. die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
  269. # NEEDSWORK: In a multi-working-tree world, this needs to be
  270. # set in the per-worktree config.
  271. if git config --get submodule.active >/dev/null
  272. then
  273. # If the submodule being adding isn't already covered by the
  274. # current configured pathspec, set the submodule's active flag
  275. if ! git submodule--helper is-active "$sm_path"
  276. then
  277. git config submodule."$sm_name".active "true"
  278. fi
  279. else
  280. git config submodule."$sm_name".active "true"
  281. fi
  282. }
  283. #
  284. # Execute an arbitrary command sequence in each checked out
  285. # submodule
  286. #
  287. # $@ = command to execute
  288. #
  289. cmd_foreach()
  290. {
  291. # parse $args after "submodule ... foreach".
  292. while test $# -ne 0
  293. do
  294. case "$1" in
  295. -q|--quiet)
  296. GIT_QUIET=1
  297. ;;
  298. --recursive)
  299. recursive=1
  300. ;;
  301. -*)
  302. usage
  303. ;;
  304. *)
  305. break
  306. ;;
  307. esac
  308. shift
  309. done
  310. git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
  311. }
  312. #
  313. # Register submodules in .git/config
  314. #
  315. # $@ = requested paths (default to all)
  316. #
  317. cmd_init()
  318. {
  319. # parse $args after "submodule ... init".
  320. while test $# -ne 0
  321. do
  322. case "$1" in
  323. -q|--quiet)
  324. GIT_QUIET=1
  325. ;;
  326. --)
  327. shift
  328. break
  329. ;;
  330. -*)
  331. usage
  332. ;;
  333. *)
  334. break
  335. ;;
  336. esac
  337. shift
  338. done
  339. git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} -- "$@"
  340. }
  341. #
  342. # Unregister submodules from .git/config and remove their work tree
  343. #
  344. cmd_deinit()
  345. {
  346. # parse $args after "submodule ... deinit".
  347. deinit_all=
  348. while test $# -ne 0
  349. do
  350. case "$1" in
  351. -f|--force)
  352. force=$1
  353. ;;
  354. -q|--quiet)
  355. GIT_QUIET=1
  356. ;;
  357. --all)
  358. deinit_all=t
  359. ;;
  360. --)
  361. shift
  362. break
  363. ;;
  364. -*)
  365. usage
  366. ;;
  367. *)
  368. break
  369. ;;
  370. esac
  371. shift
  372. done
  373. git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} ${force:+--force} ${deinit_all:+--all} -- "$@"
  374. }
  375. is_tip_reachable () (
  376. sanitize_submodule_env &&
  377. cd "$1" &&
  378. rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
  379. test -z "$rev"
  380. )
  381. fetch_in_submodule () (
  382. sanitize_submodule_env &&
  383. cd "$1" &&
  384. case "$2" in
  385. '')
  386. git fetch ;;
  387. *)
  388. shift
  389. git fetch $(get_default_remote) "$@" ;;
  390. esac
  391. )
  392. #
  393. # Update each submodule path to correct revision, using clone and checkout as needed
  394. #
  395. # $@ = requested paths (default to all)
  396. #
  397. cmd_update()
  398. {
  399. # parse $args after "submodule ... update".
  400. while test $# -ne 0
  401. do
  402. case "$1" in
  403. -q|--quiet)
  404. GIT_QUIET=1
  405. ;;
  406. -v)
  407. unset GIT_QUIET
  408. ;;
  409. --progress)
  410. progress=1
  411. ;;
  412. -i|--init)
  413. init=1
  414. ;;
  415. --require-init)
  416. init=1
  417. require_init=1
  418. ;;
  419. --remote)
  420. remote=1
  421. ;;
  422. -N|--no-fetch)
  423. nofetch=1
  424. ;;
  425. -f|--force)
  426. force=$1
  427. ;;
  428. -r|--rebase)
  429. update="rebase"
  430. ;;
  431. --reference)
  432. case "$2" in '') usage ;; esac
  433. reference="--reference=$2"
  434. shift
  435. ;;
  436. --reference=*)
  437. reference="$1"
  438. ;;
  439. --dissociate)
  440. dissociate=1
  441. ;;
  442. -m|--merge)
  443. update="merge"
  444. ;;
  445. --recursive)
  446. recursive=1
  447. ;;
  448. --checkout)
  449. update="checkout"
  450. ;;
  451. --recommend-shallow)
  452. recommend_shallow="--recommend-shallow"
  453. ;;
  454. --no-recommend-shallow)
  455. recommend_shallow="--no-recommend-shallow"
  456. ;;
  457. --depth)
  458. case "$2" in '') usage ;; esac
  459. depth="--depth=$2"
  460. shift
  461. ;;
  462. --depth=*)
  463. depth=$1
  464. ;;
  465. -j|--jobs)
  466. case "$2" in '') usage ;; esac
  467. jobs="--jobs=$2"
  468. shift
  469. ;;
  470. --jobs=*)
  471. jobs=$1
  472. ;;
  473. --single-branch)
  474. single_branch="--single-branch"
  475. ;;
  476. --no-single-branch)
  477. single_branch="--no-single-branch"
  478. ;;
  479. --)
  480. shift
  481. break
  482. ;;
  483. -*)
  484. usage
  485. ;;
  486. *)
  487. break
  488. ;;
  489. esac
  490. shift
  491. done
  492. if test -n "$init"
  493. then
  494. cmd_init "--" "$@" || return
  495. fi
  496. {
  497. git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
  498. ${progress:+"--progress"} \
  499. ${wt_prefix:+--prefix "$wt_prefix"} \
  500. ${prefix:+--recursive-prefix "$prefix"} \
  501. ${update:+--update "$update"} \
  502. ${reference:+"$reference"} \
  503. ${dissociate:+"--dissociate"} \
  504. ${depth:+--depth "$depth"} \
  505. ${require_init:+--require-init} \
  506. $single_branch \
  507. $recommend_shallow \
  508. $jobs \
  509. -- \
  510. "$@" || echo "#unmatched" $?
  511. } | {
  512. err=
  513. while read -r quickabort sha1 just_cloned sm_path
  514. do
  515. die_if_unmatched "$quickabort" "$sha1"
  516. git submodule--helper ensure-core-worktree "$sm_path" || exit 1
  517. update_module=$(git submodule--helper update-module-mode $just_cloned "$sm_path" $update)
  518. displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
  519. if test $just_cloned -eq 1
  520. then
  521. subsha1=
  522. else
  523. subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
  524. git rev-parse --verify HEAD) ||
  525. die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
  526. fi
  527. if test -n "$remote"
  528. then
  529. branch=$(git submodule--helper remote-branch "$sm_path")
  530. if test -z "$nofetch"
  531. then
  532. # Fetch remote before determining tracking $sha1
  533. fetch_in_submodule "$sm_path" $depth ||
  534. die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
  535. fi
  536. remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
  537. sha1=$(sanitize_submodule_env; cd "$sm_path" &&
  538. git rev-parse --verify "${remote_name}/${branch}") ||
  539. die "$(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
  540. fi
  541. if test "$subsha1" != "$sha1" || test -n "$force"
  542. then
  543. subforce=$force
  544. # If we don't already have a -f flag and the submodule has never been checked out
  545. if test -z "$subsha1" && test -z "$force"
  546. then
  547. subforce="-f"
  548. fi
  549. if test -z "$nofetch"
  550. then
  551. # Run fetch only if $sha1 isn't present or it
  552. # is not reachable from a ref.
  553. is_tip_reachable "$sm_path" "$sha1" ||
  554. fetch_in_submodule "$sm_path" $depth ||
  555. say "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'; trying to directly fetch \$sha1:")"
  556. # Now we tried the usual fetch, but $sha1 may
  557. # not be reachable from any of the refs
  558. is_tip_reachable "$sm_path" "$sha1" ||
  559. fetch_in_submodule "$sm_path" $depth "$sha1" ||
  560. die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain \$sha1. Direct fetching of that commit failed.")"
  561. fi
  562. must_die_on_failure=
  563. case "$update_module" in
  564. checkout)
  565. command="git checkout $subforce -q"
  566. die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
  567. say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
  568. ;;
  569. rebase)
  570. command="git rebase ${GIT_QUIET:+--quiet}"
  571. die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
  572. say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
  573. must_die_on_failure=yes
  574. ;;
  575. merge)
  576. command="git merge ${GIT_QUIET:+--quiet}"
  577. die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
  578. say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
  579. must_die_on_failure=yes
  580. ;;
  581. !*)
  582. command="${update_module#!}"
  583. die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$displaypath'")"
  584. say_msg="$(eval_gettext "Submodule path '\$displaypath': '\$command \$sha1'")"
  585. must_die_on_failure=yes
  586. ;;
  587. *)
  588. die "$(eval_gettext "Invalid update mode '$update_module' for submodule path '$path'")"
  589. esac
  590. if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
  591. then
  592. say "$say_msg"
  593. elif test -n "$must_die_on_failure"
  594. then
  595. die_with_status 2 "$die_msg"
  596. else
  597. err="${err};$die_msg"
  598. continue
  599. fi
  600. fi
  601. if test -n "$recursive"
  602. then
  603. (
  604. prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
  605. wt_prefix=
  606. sanitize_submodule_env
  607. cd "$sm_path" &&
  608. eval cmd_update
  609. )
  610. res=$?
  611. if test $res -gt 0
  612. then
  613. die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
  614. if test $res -ne 2
  615. then
  616. err="${err};$die_msg"
  617. continue
  618. else
  619. die_with_status $res "$die_msg"
  620. fi
  621. fi
  622. fi
  623. done
  624. if test -n "$err"
  625. then
  626. OIFS=$IFS
  627. IFS=';'
  628. for e in $err
  629. do
  630. if test -n "$e"
  631. then
  632. echo >&2 "$e"
  633. fi
  634. done
  635. IFS=$OIFS
  636. exit 1
  637. fi
  638. }
  639. }
  640. #
  641. # Configures a submodule's default branch
  642. #
  643. # $@ = requested path
  644. #
  645. cmd_set_branch() {
  646. default=
  647. branch=
  648. while test $# -ne 0
  649. do
  650. case "$1" in
  651. -q|--quiet)
  652. # we don't do anything with this but we need to accept it
  653. ;;
  654. -d|--default)
  655. default=1
  656. ;;
  657. -b|--branch)
  658. case "$2" in '') usage ;; esac
  659. branch=$2
  660. shift
  661. ;;
  662. --)
  663. shift
  664. break
  665. ;;
  666. -*)
  667. usage
  668. ;;
  669. *)
  670. break
  671. ;;
  672. esac
  673. shift
  674. done
  675. git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper set-branch ${GIT_QUIET:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
  676. }
  677. #
  678. # Configures a submodule's remote url
  679. #
  680. # $@ = requested path, requested url
  681. #
  682. cmd_set_url() {
  683. while test $# -ne 0
  684. do
  685. case "$1" in
  686. -q|--quiet)
  687. GIT_QUIET=1
  688. ;;
  689. --)
  690. shift
  691. break
  692. ;;
  693. -*)
  694. usage
  695. ;;
  696. *)
  697. break
  698. ;;
  699. esac
  700. shift
  701. done
  702. git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
  703. }
  704. #
  705. # Show commit summary for submodules in index or working tree
  706. #
  707. # If '--cached' is given, show summary between index and given commit,
  708. # or between working tree and given commit
  709. #
  710. # $@ = [commit (default 'HEAD'),] requested paths (default all)
  711. #
  712. cmd_summary() {
  713. summary_limit=-1
  714. for_status=
  715. diff_cmd=diff-index
  716. # parse $args after "submodule ... summary".
  717. while test $# -ne 0
  718. do
  719. case "$1" in
  720. --cached)
  721. cached="$1"
  722. ;;
  723. --files)
  724. files="$1"
  725. ;;
  726. --for-status)
  727. for_status="$1"
  728. ;;
  729. -n|--summary-limit)
  730. summary_limit="$2"
  731. isnumber "$summary_limit" || usage
  732. shift
  733. ;;
  734. --summary-limit=*)
  735. summary_limit="${1#--summary-limit=}"
  736. isnumber "$summary_limit" || usage
  737. ;;
  738. --)
  739. shift
  740. break
  741. ;;
  742. -*)
  743. usage
  744. ;;
  745. *)
  746. break
  747. ;;
  748. esac
  749. shift
  750. done
  751. git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${prefix:+--prefix "$prefix"} ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
  752. }
  753. #
  754. # List all submodules, prefixed with:
  755. # - submodule not initialized
  756. # + different revision checked out
  757. #
  758. # If --cached was specified the revision in the index will be printed
  759. # instead of the currently checked out revision.
  760. #
  761. # $@ = requested paths (default to all)
  762. #
  763. cmd_status()
  764. {
  765. # parse $args after "submodule ... status".
  766. while test $# -ne 0
  767. do
  768. case "$1" in
  769. -q|--quiet)
  770. GIT_QUIET=1
  771. ;;
  772. --cached)
  773. cached=1
  774. ;;
  775. --recursive)
  776. recursive=1
  777. ;;
  778. --)
  779. shift
  780. break
  781. ;;
  782. -*)
  783. usage
  784. ;;
  785. *)
  786. break
  787. ;;
  788. esac
  789. shift
  790. done
  791. git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
  792. }
  793. #
  794. # Sync remote urls for submodules
  795. # This makes the value for remote.$remote.url match the value
  796. # specified in .gitmodules.
  797. #
  798. cmd_sync()
  799. {
  800. while test $# -ne 0
  801. do
  802. case "$1" in
  803. -q|--quiet)
  804. GIT_QUIET=1
  805. shift
  806. ;;
  807. --recursive)
  808. recursive=1
  809. shift
  810. ;;
  811. --)
  812. shift
  813. break
  814. ;;
  815. -*)
  816. usage
  817. ;;
  818. *)
  819. break
  820. ;;
  821. esac
  822. done
  823. git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
  824. }
  825. cmd_absorbgitdirs()
  826. {
  827. git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
  828. }
  829. # This loop parses the command line arguments to find the
  830. # subcommand name to dispatch. Parsing of the subcommand specific
  831. # options are primarily done by the subcommand implementations.
  832. # Subcommand specific options such as --branch and --cached are
  833. # parsed here as well, for backward compatibility.
  834. while test $# != 0 && test -z "$command"
  835. do
  836. case "$1" in
  837. add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
  838. command=$1
  839. ;;
  840. -q|--quiet)
  841. GIT_QUIET=1
  842. ;;
  843. -b|--branch)
  844. case "$2" in
  845. '')
  846. usage
  847. ;;
  848. esac
  849. branch="$2"; shift
  850. ;;
  851. --cached)
  852. cached="$1"
  853. ;;
  854. --)
  855. break
  856. ;;
  857. -*)
  858. usage
  859. ;;
  860. *)
  861. break
  862. ;;
  863. esac
  864. shift
  865. done
  866. # No command word defaults to "status"
  867. if test -z "$command"
  868. then
  869. if test $# = 0
  870. then
  871. command=status
  872. else
  873. usage
  874. fi
  875. fi
  876. # "-b branch" is accepted only by "add" and "set-branch"
  877. if test -n "$branch" && (test "$command" != add || test "$command" != set-branch)
  878. then
  879. usage
  880. fi
  881. # "--cached" is accepted only by "status" and "summary"
  882. if test -n "$cached" && test "$command" != status && test "$command" != summary
  883. then
  884. usage
  885. fi
  886. "cmd_$(echo $command | sed -e s/-/_/g)" "$@"