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