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