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