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