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