Merge branch 'maint-1.6.6' into maint-1.7.0
[alt-git.git] / git-submodule.sh
blobd3c583d0113e133b8fc75b533ff3f88d2a9c7efe
1 #!/bin/sh
3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 dashless=$(basename "$0" | sed -e 's/-/ /')
8 USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> [<path>]
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach [--recursive] <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
15 OPTIONS_SPEC=
16 . git-sh-setup
17 . git-parse-remote
18 require_work_tree
20 command=
21 branch=
22 reference=
23 cached=
24 recursive=
25 init=
26 files=
27 nofetch=
28 update=
29 prefix=
31 # Resolve relative url by appending to parent's url
32 resolve_relative_url ()
34 remote=$(get_default_remote)
35 remoteurl=$(git config "remote.$remote.url") ||
36 die "remote ($remote) does not have a url defined in .git/config"
37 url="$1"
38 remoteurl=${remoteurl%/}
39 while test -n "$url"
41 case "$url" in
42 ../*)
43 url="${url#../}"
44 remoteurl="${remoteurl%/*}"
46 ./*)
47 url="${url#./}"
50 break;;
51 esac
52 done
53 echo "$remoteurl/${url%/}"
57 # Get submodule info for registered submodules
58 # $@ = path to limit submodule list
60 module_list()
62 git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
66 # Map submodule path to submodule name
68 # $1 = path
70 module_name()
72 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
73 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
74 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
75 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
76 test -z "$name" &&
77 die "No submodule mapping found in .gitmodules for path '$path'"
78 echo "$name"
82 # Clone a submodule
84 # Prior to calling, cmd_update checks that a possibly existing
85 # path is not a git repository.
86 # Likewise, cmd_add checks that path does not exist at all,
87 # since it is the location of a new submodule.
89 module_clone()
91 path=$1
92 url=$2
93 reference="$3"
95 # If there already is a directory at the submodule path,
96 # expect it to be empty (since that is the default checkout
97 # action) and try to remove it.
98 # Note: if $path is a symlink to a directory the test will
99 # succeed but the rmdir will fail. We might want to fix this.
100 if test -d "$path"
101 then
102 rmdir "$path" 2>/dev/null ||
103 die "Directory '$path' exists, but is neither empty nor a git repository"
106 test -e "$path" &&
107 die "A file already exist at path '$path'"
109 if test -n "$reference"
110 then
111 git-clone "$reference" -n "$url" "$path"
112 else
113 git-clone -n "$url" "$path"
114 fi ||
115 die "Clone of '$url' into submodule path '$path' failed"
119 # Add a new submodule to the working tree, .gitmodules and the index
121 # $@ = repo path
123 # optional branch is stored in global branch variable
125 cmd_add()
127 # parse $args after "submodule ... add".
128 while test $# -ne 0
130 case "$1" in
131 -b | --branch)
132 case "$2" in '') usage ;; esac
133 branch=$2
134 shift
136 -q|--quiet)
137 GIT_QUIET=1
139 --reference)
140 case "$2" in '') usage ;; esac
141 reference="--reference=$2"
142 shift
144 --reference=*)
145 reference="$1"
146 shift
149 shift
150 break
153 usage
156 break
158 esac
159 shift
160 done
162 repo=$1
163 path=$2
165 if test -z "$path"; then
166 path=$(echo "$repo" |
167 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
170 if test -z "$repo" -o -z "$path"; then
171 usage
174 # assure repo is absolute or relative to parent
175 case "$repo" in
176 ./*|../*)
177 # dereference source url relative to parent's url
178 realrepo=$(resolve_relative_url "$repo") || exit
180 *:*|/*)
181 # absolute url
182 realrepo=$repo
185 die "repo URL: '$repo' must be absolute or begin with ./|../"
187 esac
189 # normalize path:
190 # multiple //; leading ./; /./; /../; trailing /
191 path=$(printf '%s/\n' "$path" |
192 sed -e '
193 s|//*|/|g
194 s|^\(\./\)*||
195 s|/\./|/|g
196 :start
197 s|\([^/]*\)/\.\./||
198 tstart
199 s|/*$||
201 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
202 die "'$path' already exists in the index"
204 # perhaps the path exists and is already a git repo, else clone it
205 if test -e "$path"
206 then
207 if test -d "$path"/.git -o -f "$path"/.git
208 then
209 echo "Adding existing repo at '$path' to the index"
210 else
211 die "'$path' already exists and is not a valid git repo"
214 case "$repo" in
215 ./*|../*)
216 url=$(resolve_relative_url "$repo") || exit
219 url="$repo"
221 esac
222 git config submodule."$path".url "$url"
223 else
225 module_clone "$path" "$realrepo" "$reference" || exit
227 clear_local_git_env
228 cd "$path" &&
229 # ash fails to wordsplit ${branch:+-b "$branch"...}
230 case "$branch" in
231 '') git checkout -f -q ;;
232 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
233 esac
234 ) || die "Unable to checkout submodule '$path'"
237 git add "$path" ||
238 die "Failed to add submodule '$path'"
240 git config -f .gitmodules submodule."$path".path "$path" &&
241 git config -f .gitmodules submodule."$path".url "$repo" &&
242 git add .gitmodules ||
243 die "Failed to register submodule '$path'"
247 # Execute an arbitrary command sequence in each checked out
248 # submodule
250 # $@ = command to execute
252 cmd_foreach()
254 # parse $args after "submodule ... foreach".
255 while test $# -ne 0
257 case "$1" in
258 -q|--quiet)
259 GIT_QUIET=1
261 --recursive)
262 recursive=1
265 usage
268 break
270 esac
271 shift
272 done
274 module_list |
275 while read mode sha1 stage path
277 if test -e "$path"/.git
278 then
279 say "Entering '$prefix$path'"
280 name=$(module_name "$path")
282 prefix="$prefix$path/"
283 clear_local_git_env
284 cd "$path" &&
285 eval "$@" &&
286 if test -n "$recursive"
287 then
288 cmd_foreach "--recursive" "$@"
290 ) ||
291 die "Stopping at '$path'; script returned non-zero status."
293 done
297 # Register submodules in .git/config
299 # $@ = requested paths (default to all)
301 cmd_init()
303 # parse $args after "submodule ... init".
304 while test $# -ne 0
306 case "$1" in
307 -q|--quiet)
308 GIT_QUIET=1
311 shift
312 break
315 usage
318 break
320 esac
321 shift
322 done
324 module_list "$@" |
325 while read mode sha1 stage path
327 # Skip already registered paths
328 name=$(module_name "$path") || exit
329 url=$(git config submodule."$name".url)
330 test -z "$url" || continue
332 url=$(git config -f .gitmodules submodule."$name".url)
333 test -z "$url" &&
334 die "No url found for submodule path '$path' in .gitmodules"
336 # Possibly a url relative to parent
337 case "$url" in
338 ./*|../*)
339 url=$(resolve_relative_url "$url") || exit
341 esac
343 git config submodule."$name".url "$url" ||
344 die "Failed to register url for submodule path '$path'"
346 upd="$(git config -f .gitmodules submodule."$name".update)"
347 test -z "$upd" ||
348 git config submodule."$name".update "$upd" ||
349 die "Failed to register update mode for submodule path '$path'"
351 say "Submodule '$name' ($url) registered for path '$path'"
352 done
356 # Update each submodule path to correct revision, using clone and checkout as needed
358 # $@ = requested paths (default to all)
360 cmd_update()
362 # parse $args after "submodule ... update".
363 orig_args="$@"
364 while test $# -ne 0
366 case "$1" in
367 -q|--quiet)
368 shift
369 GIT_QUIET=1
371 -i|--init)
372 init=1
373 shift
375 -N|--no-fetch)
376 shift
377 nofetch=1
379 -r|--rebase)
380 shift
381 update="rebase"
383 --reference)
384 case "$2" in '') usage ;; esac
385 reference="--reference=$2"
386 shift 2
388 --reference=*)
389 reference="$1"
390 shift
392 -m|--merge)
393 shift
394 update="merge"
396 --recursive)
397 shift
398 recursive=1
401 shift
402 break
405 usage
408 break
410 esac
411 done
413 if test -n "$init"
414 then
415 cmd_init "--" "$@" || return
418 module_list "$@" |
419 while read mode sha1 stage path
421 name=$(module_name "$path") || exit
422 url=$(git config submodule."$name".url)
423 update_module=$(git config submodule."$name".update)
424 if test -z "$url"
425 then
426 # Only mention uninitialized submodules when its
427 # path have been specified
428 test "$#" != "0" &&
429 say "Submodule path '$path' not initialized" &&
430 say "Maybe you want to use 'update --init'?"
431 continue
434 if ! test -d "$path"/.git -o -f "$path"/.git
435 then
436 module_clone "$path" "$url" "$reference"|| exit
437 subsha1=
438 else
439 subsha1=$(clear_local_git_env; cd "$path" &&
440 git rev-parse --verify HEAD) ||
441 die "Unable to find current revision in submodule path '$path'"
444 if ! test -z "$update"
445 then
446 update_module=$update
449 if test "$subsha1" != "$sha1"
450 then
451 force=
452 if test -z "$subsha1"
453 then
454 force="-f"
457 if test -z "$nofetch"
458 then
459 (clear_local_git_env; cd "$path" &&
460 git-fetch) ||
461 die "Unable to fetch in submodule path '$path'"
464 case "$update_module" in
465 rebase)
466 command="git rebase"
467 action="rebase"
468 msg="rebased onto"
470 merge)
471 command="git merge"
472 action="merge"
473 msg="merged in"
476 command="git checkout $force -q"
477 action="checkout"
478 msg="checked out"
480 esac
482 (clear_local_git_env; cd "$path" && $command "$sha1") ||
483 die "Unable to $action '$sha1' in submodule path '$path'"
484 say "Submodule path '$path': $msg '$sha1'"
487 if test -n "$recursive"
488 then
489 (clear_local_git_env; cd "$path" && cmd_update $orig_args) ||
490 die "Failed to recurse into submodule path '$path'"
492 done
495 set_name_rev () {
496 revname=$( (
497 clear_local_git_env
498 cd "$1" && {
499 git describe "$2" 2>/dev/null ||
500 git describe --tags "$2" 2>/dev/null ||
501 git describe --contains "$2" 2>/dev/null ||
502 git describe --all --always "$2"
505 test -z "$revname" || revname=" ($revname)"
508 # Show commit summary for submodules in index or working tree
510 # If '--cached' is given, show summary between index and given commit,
511 # or between working tree and given commit
513 # $@ = [commit (default 'HEAD'),] requested paths (default all)
515 cmd_summary() {
516 summary_limit=-1
517 for_status=
518 diff_cmd=diff-index
520 # parse $args after "submodule ... summary".
521 while test $# -ne 0
523 case "$1" in
524 --cached)
525 cached="$1"
527 --files)
528 files="$1"
530 --for-status)
531 for_status="$1"
533 -n|--summary-limit)
534 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
535 then
537 else
538 usage
540 shift
543 shift
544 break
547 usage
550 break
552 esac
553 shift
554 done
556 test $summary_limit = 0 && return
558 if rev=$(git rev-parse -q --verify "$1^0")
559 then
560 head=$rev
561 shift
562 else
563 head=HEAD
566 if [ -n "$files" ]
567 then
568 test -n "$cached" &&
569 die "--cached cannot be used with --files"
570 diff_cmd=diff-files
571 head=
574 cd_to_toplevel
575 # Get modified modules cared by user
576 modules=$(git $diff_cmd $cached --raw $head -- "$@" |
577 sane_egrep '^:([0-7]* )?160000' |
578 while read mod_src mod_dst sha1_src sha1_dst status name
580 # Always show modules deleted or type-changed (blob<->module)
581 test $status = D -o $status = T && echo "$name" && continue
582 # Also show added or modified modules which are checked out
583 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
584 echo "$name"
585 done
588 test -z "$modules" && return
590 git $diff_cmd $cached --raw $head -- $modules |
591 sane_egrep '^:([0-7]* )?160000' |
592 cut -c2- |
593 while read mod_src mod_dst sha1_src sha1_dst status name
595 if test -z "$cached" &&
596 test $sha1_dst = 0000000000000000000000000000000000000000
597 then
598 case "$mod_dst" in
599 160000)
600 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
602 100644 | 100755 | 120000)
603 sha1_dst=$(git hash-object $name)
605 000000)
606 ;; # removed
608 # unexpected type
609 echo >&2 "unexpected mode $mod_dst"
610 continue ;;
611 esac
613 missing_src=
614 missing_dst=
616 test $mod_src = 160000 &&
617 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
618 missing_src=t
620 test $mod_dst = 160000 &&
621 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
622 missing_dst=t
624 total_commits=
625 case "$missing_src,$missing_dst" in
627 errmsg=" Warn: $name doesn't contain commit $sha1_src"
630 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
632 t,t)
633 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
636 errmsg=
637 total_commits=$(
638 if test $mod_src = 160000 -a $mod_dst = 160000
639 then
640 range="$sha1_src...$sha1_dst"
641 elif test $mod_src = 160000
642 then
643 range=$sha1_src
644 else
645 range=$sha1_dst
647 GIT_DIR="$name/.git" \
648 git log --pretty=oneline --first-parent $range | wc -l
650 total_commits=" ($(($total_commits + 0)))"
652 esac
654 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
655 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
656 if test $status = T
657 then
658 if test $mod_dst = 160000
659 then
660 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
661 else
662 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
664 else
665 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
667 if test -n "$errmsg"
668 then
669 # Don't give error msg for modification whose dst is not submodule
670 # i.e. deleted or changed to blob
671 test $mod_dst = 160000 && echo "$errmsg"
672 else
673 if test $mod_src = 160000 -a $mod_dst = 160000
674 then
675 limit=
676 test $summary_limit -gt 0 && limit="-$summary_limit"
677 GIT_DIR="$name/.git" \
678 git log $limit --pretty='format: %m %s' \
679 --first-parent $sha1_src...$sha1_dst
680 elif test $mod_dst = 160000
681 then
682 GIT_DIR="$name/.git" \
683 git log --pretty='format: > %s' -1 $sha1_dst
684 else
685 GIT_DIR="$name/.git" \
686 git log --pretty='format: < %s' -1 $sha1_src
688 echo
690 echo
691 done |
692 if test -n "$for_status"; then
693 if [ -n "$files" ]; then
694 echo "# Submodules changed but not updated:"
695 else
696 echo "# Submodule changes to be committed:"
698 echo "#"
699 sed -e 's|^|# |' -e 's|^# $|#|'
700 else
705 # List all submodules, prefixed with:
706 # - submodule not initialized
707 # + different revision checked out
709 # If --cached was specified the revision in the index will be printed
710 # instead of the currently checked out revision.
712 # $@ = requested paths (default to all)
714 cmd_status()
716 # parse $args after "submodule ... status".
717 orig_args="$@"
718 while test $# -ne 0
720 case "$1" in
721 -q|--quiet)
722 GIT_QUIET=1
724 --cached)
725 cached=1
727 --recursive)
728 recursive=1
731 shift
732 break
735 usage
738 break
740 esac
741 shift
742 done
744 module_list "$@" |
745 while read mode sha1 stage path
747 name=$(module_name "$path") || exit
748 url=$(git config submodule."$name".url)
749 displaypath="$prefix$path"
750 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
751 then
752 say "-$sha1 $displaypath"
753 continue;
755 set_name_rev "$path" "$sha1"
756 if git diff-files --quiet -- "$path"
757 then
758 say " $sha1 $displaypath$revname"
759 else
760 if test -z "$cached"
761 then
762 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
763 set_name_rev "$path" "$sha1"
765 say "+$sha1 $displaypath$revname"
768 if test -n "$recursive"
769 then
771 prefix="$displaypath/"
772 clear_local_git_env
773 cd "$path" &&
774 cmd_status $orig_args
775 ) ||
776 die "Failed to recurse into submodule path '$path'"
778 done
781 # Sync remote urls for submodules
782 # This makes the value for remote.$remote.url match the value
783 # specified in .gitmodules.
785 cmd_sync()
787 while test $# -ne 0
789 case "$1" in
790 -q|--quiet)
791 GIT_QUIET=1
792 shift
795 shift
796 break
799 usage
802 break
804 esac
805 done
806 cd_to_toplevel
807 module_list "$@" |
808 while read mode sha1 stage path
810 name=$(module_name "$path")
811 url=$(git config -f .gitmodules --get submodule."$name".url)
813 # Possibly a url relative to parent
814 case "$url" in
815 ./*|../*)
816 url=$(resolve_relative_url "$url") || exit
818 esac
820 if test -e "$path"/.git
821 then
823 clear_local_git_env
824 cd "$path"
825 remote=$(get_default_remote)
826 say "Synchronizing submodule url for '$name'"
827 git config remote."$remote".url "$url"
830 done
833 # This loop parses the command line arguments to find the
834 # subcommand name to dispatch. Parsing of the subcommand specific
835 # options are primarily done by the subcommand implementations.
836 # Subcommand specific options such as --branch and --cached are
837 # parsed here as well, for backward compatibility.
839 while test $# != 0 && test -z "$command"
841 case "$1" in
842 add | foreach | init | update | status | summary | sync)
843 command=$1
845 -q|--quiet)
846 GIT_QUIET=1
848 -b|--branch)
849 case "$2" in
851 usage
853 esac
854 branch="$2"; shift
856 --cached)
857 cached="$1"
860 break
863 usage
866 break
868 esac
869 shift
870 done
872 # No command word defaults to "status"
873 test -n "$command" || command=status
875 # "-b branch" is accepted only by "add"
876 if test -n "$branch" && test "$command" != add
877 then
878 usage
881 # "--cached" is accepted only by "status" and "summary"
882 if test -n "$cached" && test "$command" != status -a "$command" != summary
883 then
884 usage
887 "cmd_$command" "$@"