codekingpro/portable-devtools
114k
1#!/bin/sh2#3# git-submodule.sh: add, init, update or list git submodules4#5# Copyright (c) 2007 Lars Hjemli6 7dashless=$(basename "$0" | sed -e 's/-/ /')8USAGE="[--quiet] [--cached]9 or: $dashless [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]10 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]11 or: $dashless [--quiet] init [--] [<path>...]12 or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)13 or: $dashless [--quiet] update [--init [--filter=<filter-spec>]] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]14 or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>15 or: $dashless [--quiet] set-url [--] <path> <newurl>16 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]17 or: $dashless [--quiet] foreach [--recursive] <command>18 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]19 or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"20OPTIONS_SPEC=21SUBDIRECTORY_OK=Yes22. git-sh-setup23require_work_tree24wt_prefix=$(git rev-parse --show-prefix)25cd_to_toplevel26 27# Tell the rest of git that any URLs we get don't come28# directly from the user, so it can apply policy as appropriate.29GIT_PROTOCOL_FROM_USER=030export GIT_PROTOCOL_FROM_USER31 32command=33quiet=34branch=35force=36reference=37cached=38recursive=39init=40require_init=41files=42remote=43no_fetch=44rebase=45merge=46checkout=47name=48depth=49progress=50dissociate=51single_branch=52jobs=53recommend_shallow=54filter=55all=56default=57summary_limit=58for_status=59 60#61# Add a new submodule to the working tree, .gitmodules and the index62#63# $@ = repo path64#65# optional branch is stored in global branch variable66#67cmd_add()68{69 # parse $args after "submodule ... add".70 while test $# -ne 071 do72 case "$1" in73 -b | --branch)74 case "$2" in '') usage ;; esac75 branch="--branch=$2"76 shift77 ;;78 -b* | --branch=*)79 branch="$1"80 ;;81 -f | --force)82 force=$183 ;;84 -q|--quiet)85 quiet=$186 ;;87 --progress)88 progress=$189 ;;90 --reference)91 case "$2" in '') usage ;; esac92 reference="--reference=$2"93 shift94 ;;95 --reference=*)96 reference="$1"97 ;;98 --ref-format)99 case "$2" in '') usage ;; esac100 ref_format="--ref-format=$2"101 shift102 ;;103 --ref-format=*)104 ref_format="$1"105 ;;106 --dissociate)107 dissociate=$1108 ;;109 --name)110 case "$2" in '') usage ;; esac111 name="--name=$2"112 shift113 ;;114 --name=*)115 name="$1"116 ;;117 --depth)118 case "$2" in '') usage ;; esac119 depth="--depth=$2"120 shift121 ;;122 --depth=*)123 depth="$1"124 ;;125 --)126 shift127 break128 ;;129 -*)130 usage131 ;;132 *)133 break134 ;;135 esac136 shift137 done138 139 if test -z "$1"140 then141 usage142 fi143 144 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper add \145 $quiet \146 $force \147 $progress \148 ${branch:+"$branch"} \149 ${reference:+"$reference"} \150 ${ref_format:+"$ref_format"} \151 $dissociate \152 ${name:+"$name"} \153 ${depth:+"$depth"} \154 -- \155 "$@"156}157 158#159# Execute an arbitrary command sequence in each checked out160# submodule161#162# $@ = command to execute163#164cmd_foreach()165{166 # parse $args after "submodule ... foreach".167 while test $# -ne 0168 do169 case "$1" in170 -q|--quiet)171 quiet=$1172 ;;173 --recursive)174 recursive=$1175 ;;176 -*)177 usage178 ;;179 *)180 break181 ;;182 esac183 shift184 done185 186 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach \187 $quiet \188 $recursive \189 -- \190 "$@"191}192 193#194# Register submodules in .git/config195#196# $@ = requested paths (default to all)197#198cmd_init()199{200 # parse $args after "submodule ... init".201 while test $# -ne 0202 do203 case "$1" in204 -q|--quiet)205 quiet=$1206 ;;207 --)208 shift209 break210 ;;211 -*)212 usage213 ;;214 *)215 break216 ;;217 esac218 shift219 done220 221 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init \222 $quiet \223 -- \224 "$@"225}226 227#228# Unregister submodules from .git/config and remove their work tree229#230cmd_deinit()231{232 # parse $args after "submodule ... deinit".233 while test $# -ne 0234 do235 case "$1" in236 -f|--force)237 force=$1238 ;;239 -q|--quiet)240 quiet=$1241 ;;242 --all)243 all=$1244 ;;245 --)246 shift247 break248 ;;249 -*)250 usage251 ;;252 *)253 break254 ;;255 esac256 shift257 done258 259 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \260 $quiet \261 $force \262 $all \263 -- \264 "$@"265}266 267#268# Update each submodule path to correct revision, using clone and checkout as needed269#270# $@ = requested paths (default to all)271#272cmd_update()273{274 # parse $args after "submodule ... update".275 while test $# -ne 0276 do277 case "$1" in278 -q|--quiet)279 quiet=$1280 ;;281 -v|--verbose)282 quiet=283 ;;284 --progress)285 progress=$1286 ;;287 -i|--init)288 init=$1289 ;;290 --require-init)291 require_init=$1292 ;;293 --remote)294 remote=$1295 ;;296 -N|--no-fetch)297 no_fetch=$1298 ;;299 -f|--force)300 force=$1301 ;;302 -r|--rebase)303 rebase=$1304 ;;305 --ref-format)306 case "$2" in '') usage ;; esac307 ref_format="--ref-format=$2"308 shift309 ;;310 --ref-format=*)311 ref_format="$1"312 ;;313 --reference)314 case "$2" in '') usage ;; esac315 reference="--reference=$2"316 shift317 ;;318 --reference=*)319 reference="$1"320 ;;321 --dissociate)322 dissociate=$1323 ;;324 -m|--merge)325 merge=$1326 ;;327 --recursive)328 recursive=$1329 ;;330 --checkout)331 checkout=$1332 ;;333 --recommend-shallow|--no-recommend-shallow)334 recommend_shallow=$1335 ;;336 --depth)337 case "$2" in '') usage ;; esac338 depth="--depth=$2"339 shift340 ;;341 --depth=*)342 depth="$1"343 ;;344 -j|--jobs)345 case "$2" in '') usage ;; esac346 jobs="--jobs=$2"347 shift348 ;;349 -j*|--jobs=*)350 jobs="$1"351 ;;352 --single-branch|--no-single-branch)353 single_branch=$1354 ;;355 --filter)356 case "$2" in '') usage ;; esac357 filter="--filter=$2"358 shift359 ;;360 --filter=*)361 filter="$1"362 ;;363 --)364 shift365 break366 ;;367 -*)368 usage369 ;;370 *)371 break372 ;;373 esac374 shift375 done376 377 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper update \378 $quiet \379 $force \380 $progress \381 $remote \382 $recursive \383 $init \384 $no_fetch \385 $rebase \386 $merge \387 $checkout \388 ${ref_format:+"$ref_format"} \389 ${reference:+"$reference"} \390 $dissociate \391 ${depth:+"$depth"} \392 $require_init \393 $single_branch \394 $recommend_shallow \395 $jobs \396 $filter \397 -- \398 "$@"399}400 401#402# Configures a submodule's default branch403#404# $@ = requested path405#406cmd_set_branch() {407 # parse $args after "submodule ... set-branch".408 while test $# -ne 0409 do410 case "$1" in411 -q|--quiet)412 # we don't do anything with this but we need to accept it413 ;;414 -d|--default)415 default=$1416 ;;417 -b|--branch)418 case "$2" in '') usage ;; esac419 branch="--branch=$2"420 shift421 ;;422 -b*|--branch=*)423 branch="$1"424 ;;425 --)426 shift427 break428 ;;429 -*)430 usage431 ;;432 *)433 break434 ;;435 esac436 shift437 done438 439 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch \440 $quiet \441 ${branch:+"$branch"} \442 $default \443 -- \444 "$@"445}446 447#448# Configures a submodule's remote url449#450# $@ = requested path, requested url451#452cmd_set_url() {453 # parse $args after "submodule ... set-url".454 while test $# -ne 0455 do456 case "$1" in457 -q|--quiet)458 quiet=$1459 ;;460 --)461 shift462 break463 ;;464 -*)465 usage466 ;;467 *)468 break469 ;;470 esac471 shift472 done473 474 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url \475 $quiet \476 -- \477 "$@"478}479 480#481# Show commit summary for submodules in index or working tree482#483# If '--cached' is given, show summary between index and given commit,484# or between working tree and given commit485#486# $@ = [commit (default 'HEAD'),] requested paths (default all)487#488cmd_summary() {489 # parse $args after "submodule ... summary".490 while test $# -ne 0491 do492 case "$1" in493 --cached)494 cached=$1495 ;;496 --files)497 files=$1498 ;;499 --for-status)500 for_status=$1501 ;;502 -n|--summary-limit)503 case "$2" in '') usage ;; esac504 summary_limit="--summary-limit=$2"505 shift506 ;;507 -n*|--summary-limit=*)508 summary_limit="$1"509 ;;510 --)511 shift512 break513 ;;514 -*)515 usage516 ;;517 *)518 break519 ;;520 esac521 shift522 done523 524 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary \525 $files \526 $cached \527 $for_status \528 ${summary_limit:+"$summary_limit"} \529 -- \530 "$@"531}532#533# List all submodules, prefixed with:534# - submodule not initialized535# + different revision checked out536#537# If --cached was specified the revision in the index will be printed538# instead of the currently checked out revision.539#540# $@ = requested paths (default to all)541#542cmd_status()543{544 # parse $args after "submodule ... status".545 while test $# -ne 0546 do547 case "$1" in548 -q|--quiet)549 quiet=$1550 ;;551 --cached)552 cached=$1553 ;;554 --recursive)555 recursive=$1556 ;;557 --)558 shift559 break560 ;;561 -*)562 usage563 ;;564 *)565 break566 ;;567 esac568 shift569 done570 571 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status \572 $quiet \573 $cached \574 $recursive \575 -- \576 "$@"577}578 579#580# Sync remote urls for submodules581# This makes the value for remote.$remote.url match the value582# specified in .gitmodules.583#584cmd_sync()585{586 # parse $args after "submodule ... sync".587 while test $# -ne 0588 do589 case "$1" in590 -q|--quiet)591 quiet=$1592 shift593 ;;594 --recursive)595 recursive=$1596 shift597 ;;598 --)599 shift600 break601 ;;602 -*)603 usage604 ;;605 *)606 break607 ;;608 esac609 done610 611 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync \612 $quiet \613 $recursive \614 -- \615 "$@"616}617 618cmd_absorbgitdirs()619{620 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper absorbgitdirs "$@"621}622 623# This loop parses the command line arguments to find the624# subcommand name to dispatch. Parsing of the subcommand specific625# options are primarily done by the subcommand implementations.626# Subcommand specific options such as --branch and --cached are627# parsed here as well, for backward compatibility.628 629while test $# != 0 && test -z "$command"630do631 case "$1" in632 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)633 command=$1634 ;;635 -q|--quiet)636 quiet=$1637 ;;638 --cached)639 cached=$1640 ;;641 --)642 break643 ;;644 -*)645 usage646 ;;647 *)648 break649 ;;650 esac651 shift652done653 654# No command word defaults to "status"655if test -z "$command"656then657 if test $# = 0658 then659 command=status660 else661 usage662 fi663fi664 665# "--cached" is accepted only by "status" and "summary"666if test -n "$cached" && test "$command" != status && test "$command" != summary667then668 usage669fi670 671"cmd_$(echo $command | sed -e s/-/_/g)" "$@"672 