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