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