Updates from the list to 1.7.2 Release Notes
[git/wpalmer.git] / git-submodule.sh
blobd9950c2b7fadef0876867e11ef26283ca1cfaf1e
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 toplevel=$(pwd)
276 module_list |
277 while read mode sha1 stage path
279 if test -e "$path"/.git
280 then
281 say "Entering '$prefix$path'"
282 name=$(module_name "$path")
284 prefix="$prefix$path/"
285 clear_local_git_env
286 cd "$path" &&
287 eval "$@" &&
288 if test -n "$recursive"
289 then
290 cmd_foreach "--recursive" "$@"
292 ) ||
293 die "Stopping at '$path'; script returned non-zero status."
295 done
299 # Register submodules in .git/config
301 # $@ = requested paths (default to all)
303 cmd_init()
305 # parse $args after "submodule ... init".
306 while test $# -ne 0
308 case "$1" in
309 -q|--quiet)
310 GIT_QUIET=1
313 shift
314 break
317 usage
320 break
322 esac
323 shift
324 done
326 module_list "$@" |
327 while read mode sha1 stage path
329 # Skip already registered paths
330 name=$(module_name "$path") || exit
331 url=$(git config submodule."$name".url)
332 test -z "$url" || continue
334 url=$(git config -f .gitmodules submodule."$name".url)
335 test -z "$url" &&
336 die "No url found for submodule path '$path' in .gitmodules"
338 # Possibly a url relative to parent
339 case "$url" in
340 ./*|../*)
341 url=$(resolve_relative_url "$url") || exit
343 esac
345 git config submodule."$name".url "$url" ||
346 die "Failed to register url for submodule path '$path'"
348 upd="$(git config -f .gitmodules submodule."$name".update)"
349 test -z "$upd" ||
350 git config submodule."$name".update "$upd" ||
351 die "Failed to register update mode for submodule path '$path'"
353 say "Submodule '$name' ($url) registered for path '$path'"
354 done
358 # Update each submodule path to correct revision, using clone and checkout as needed
360 # $@ = requested paths (default to all)
362 cmd_update()
364 # parse $args after "submodule ... update".
365 orig_args="$@"
366 while test $# -ne 0
368 case "$1" in
369 -q|--quiet)
370 shift
371 GIT_QUIET=1
373 -i|--init)
374 init=1
375 shift
377 -N|--no-fetch)
378 shift
379 nofetch=1
381 -r|--rebase)
382 shift
383 update="rebase"
385 --reference)
386 case "$2" in '') usage ;; esac
387 reference="--reference=$2"
388 shift 2
390 --reference=*)
391 reference="$1"
392 shift
394 -m|--merge)
395 shift
396 update="merge"
398 --recursive)
399 shift
400 recursive=1
403 shift
404 break
407 usage
410 break
412 esac
413 done
415 if test -n "$init"
416 then
417 cmd_init "--" "$@" || return
420 module_list "$@" |
421 while read mode sha1 stage path
423 name=$(module_name "$path") || exit
424 url=$(git config submodule."$name".url)
425 update_module=$(git config submodule."$name".update)
426 if test -z "$url"
427 then
428 # Only mention uninitialized submodules when its
429 # path have been specified
430 test "$#" != "0" &&
431 say "Submodule path '$path' not initialized" &&
432 say "Maybe you want to use 'update --init'?"
433 continue
436 if ! test -d "$path"/.git -o -f "$path"/.git
437 then
438 module_clone "$path" "$url" "$reference"|| exit
439 subsha1=
440 else
441 subsha1=$(clear_local_git_env; cd "$path" &&
442 git rev-parse --verify HEAD) ||
443 die "Unable to find current revision in submodule path '$path'"
446 if ! test -z "$update"
447 then
448 update_module=$update
451 if test "$subsha1" != "$sha1"
452 then
453 force=
454 if test -z "$subsha1"
455 then
456 force="-f"
459 if test -z "$nofetch"
460 then
461 (clear_local_git_env; cd "$path" &&
462 git-fetch) ||
463 die "Unable to fetch in submodule path '$path'"
466 case "$update_module" in
467 rebase)
468 command="git rebase"
469 action="rebase"
470 msg="rebased onto"
472 merge)
473 command="git merge"
474 action="merge"
475 msg="merged in"
478 command="git checkout $force -q"
479 action="checkout"
480 msg="checked out"
482 esac
484 (clear_local_git_env; cd "$path" && $command "$sha1") ||
485 die "Unable to $action '$sha1' in submodule path '$path'"
486 say "Submodule path '$path': $msg '$sha1'"
489 if test -n "$recursive"
490 then
491 (clear_local_git_env; cd "$path" && cmd_update $orig_args) ||
492 die "Failed to recurse into submodule path '$path'"
494 done
497 set_name_rev () {
498 revname=$( (
499 clear_local_git_env
500 cd "$1" && {
501 git describe "$2" 2>/dev/null ||
502 git describe --tags "$2" 2>/dev/null ||
503 git describe --contains "$2" 2>/dev/null ||
504 git describe --all --always "$2"
507 test -z "$revname" || revname=" ($revname)"
510 # Show commit summary for submodules in index or working tree
512 # If '--cached' is given, show summary between index and given commit,
513 # or between working tree and given commit
515 # $@ = [commit (default 'HEAD'),] requested paths (default all)
517 cmd_summary() {
518 summary_limit=-1
519 for_status=
520 diff_cmd=diff-index
522 # parse $args after "submodule ... summary".
523 while test $# -ne 0
525 case "$1" in
526 --cached)
527 cached="$1"
529 --files)
530 files="$1"
532 --for-status)
533 for_status="$1"
535 -n|--summary-limit)
536 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
537 then
539 else
540 usage
542 shift
545 shift
546 break
549 usage
552 break
554 esac
555 shift
556 done
558 test $summary_limit = 0 && return
560 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
561 then
562 head=$rev
563 test $# = 0 || shift
564 elif test -z "$1" -o "$1" = "HEAD"
565 then
566 # before the first commit: compare with an empty tree
567 head=$(git hash-object -w -t tree --stdin </dev/null)
568 test -z "$1" || shift
569 else
570 head="HEAD"
573 if [ -n "$files" ]
574 then
575 test -n "$cached" &&
576 die "--cached cannot be used with --files"
577 diff_cmd=diff-files
578 head=
581 cd_to_toplevel
582 # Get modified modules cared by user
583 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
584 sane_egrep '^:([0-7]* )?160000' |
585 while read mod_src mod_dst sha1_src sha1_dst status name
587 # Always show modules deleted or type-changed (blob<->module)
588 test $status = D -o $status = T && echo "$name" && continue
589 # Also show added or modified modules which are checked out
590 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
591 echo "$name"
592 done
595 test -z "$modules" && return
597 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
598 sane_egrep '^:([0-7]* )?160000' |
599 cut -c2- |
600 while read mod_src mod_dst sha1_src sha1_dst status name
602 if test -z "$cached" &&
603 test $sha1_dst = 0000000000000000000000000000000000000000
604 then
605 case "$mod_dst" in
606 160000)
607 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
609 100644 | 100755 | 120000)
610 sha1_dst=$(git hash-object $name)
612 000000)
613 ;; # removed
615 # unexpected type
616 echo >&2 "unexpected mode $mod_dst"
617 continue ;;
618 esac
620 missing_src=
621 missing_dst=
623 test $mod_src = 160000 &&
624 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
625 missing_src=t
627 test $mod_dst = 160000 &&
628 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
629 missing_dst=t
631 total_commits=
632 case "$missing_src,$missing_dst" in
634 errmsg=" Warn: $name doesn't contain commit $sha1_src"
637 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
639 t,t)
640 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
643 errmsg=
644 total_commits=$(
645 if test $mod_src = 160000 -a $mod_dst = 160000
646 then
647 range="$sha1_src...$sha1_dst"
648 elif test $mod_src = 160000
649 then
650 range=$sha1_src
651 else
652 range=$sha1_dst
654 GIT_DIR="$name/.git" \
655 git rev-list --first-parent $range -- | wc -l
657 total_commits=" ($(($total_commits + 0)))"
659 esac
661 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
662 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
663 if test $status = T
664 then
665 if test $mod_dst = 160000
666 then
667 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
668 else
669 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
671 else
672 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
674 if test -n "$errmsg"
675 then
676 # Don't give error msg for modification whose dst is not submodule
677 # i.e. deleted or changed to blob
678 test $mod_dst = 160000 && echo "$errmsg"
679 else
680 if test $mod_src = 160000 -a $mod_dst = 160000
681 then
682 limit=
683 test $summary_limit -gt 0 && limit="-$summary_limit"
684 GIT_DIR="$name/.git" \
685 git log $limit --pretty='format: %m %s' \
686 --first-parent $sha1_src...$sha1_dst
687 elif test $mod_dst = 160000
688 then
689 GIT_DIR="$name/.git" \
690 git log --pretty='format: > %s' -1 $sha1_dst
691 else
692 GIT_DIR="$name/.git" \
693 git log --pretty='format: < %s' -1 $sha1_src
695 echo
697 echo
698 done |
699 if test -n "$for_status"; then
700 if [ -n "$files" ]; then
701 echo "# Submodules changed but not updated:"
702 else
703 echo "# Submodule changes to be committed:"
705 echo "#"
706 sed -e 's|^|# |' -e 's|^# $|#|'
707 else
712 # List all submodules, prefixed with:
713 # - submodule not initialized
714 # + different revision checked out
716 # If --cached was specified the revision in the index will be printed
717 # instead of the currently checked out revision.
719 # $@ = requested paths (default to all)
721 cmd_status()
723 # parse $args after "submodule ... status".
724 orig_args="$@"
725 while test $# -ne 0
727 case "$1" in
728 -q|--quiet)
729 GIT_QUIET=1
731 --cached)
732 cached=1
734 --recursive)
735 recursive=1
738 shift
739 break
742 usage
745 break
747 esac
748 shift
749 done
751 module_list "$@" |
752 while read mode sha1 stage path
754 name=$(module_name "$path") || exit
755 url=$(git config submodule."$name".url)
756 displaypath="$prefix$path"
757 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
758 then
759 say "-$sha1 $displaypath"
760 continue;
762 set_name_rev "$path" "$sha1"
763 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
764 then
765 say " $sha1 $displaypath$revname"
766 else
767 if test -z "$cached"
768 then
769 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
770 set_name_rev "$path" "$sha1"
772 say "+$sha1 $displaypath$revname"
775 if test -n "$recursive"
776 then
778 prefix="$displaypath/"
779 clear_local_git_env
780 cd "$path" &&
781 cmd_status $orig_args
782 ) ||
783 die "Failed to recurse into submodule path '$path'"
785 done
788 # Sync remote urls for submodules
789 # This makes the value for remote.$remote.url match the value
790 # specified in .gitmodules.
792 cmd_sync()
794 while test $# -ne 0
796 case "$1" in
797 -q|--quiet)
798 GIT_QUIET=1
799 shift
802 shift
803 break
806 usage
809 break
811 esac
812 done
813 cd_to_toplevel
814 module_list "$@" |
815 while read mode sha1 stage path
817 name=$(module_name "$path")
818 url=$(git config -f .gitmodules --get submodule."$name".url)
820 # Possibly a url relative to parent
821 case "$url" in
822 ./*|../*)
823 url=$(resolve_relative_url "$url") || exit
825 esac
827 if test -e "$path"/.git
828 then
830 clear_local_git_env
831 cd "$path"
832 remote=$(get_default_remote)
833 say "Synchronizing submodule url for '$name'"
834 git config remote."$remote".url "$url"
837 done
840 # This loop parses the command line arguments to find the
841 # subcommand name to dispatch. Parsing of the subcommand specific
842 # options are primarily done by the subcommand implementations.
843 # Subcommand specific options such as --branch and --cached are
844 # parsed here as well, for backward compatibility.
846 while test $# != 0 && test -z "$command"
848 case "$1" in
849 add | foreach | init | update | status | summary | sync)
850 command=$1
852 -q|--quiet)
853 GIT_QUIET=1
855 -b|--branch)
856 case "$2" in
858 usage
860 esac
861 branch="$2"; shift
863 --cached)
864 cached="$1"
867 break
870 usage
873 break
875 esac
876 shift
877 done
879 # No command word defaults to "status"
880 test -n "$command" || command=status
882 # "-b branch" is accepted only by "add"
883 if test -n "$branch" && test "$command" != add
884 then
885 usage
888 # "--cached" is accepted only by "status" and "summary"
889 if test -n "$cached" && test "$command" != status -a "$command" != summary
890 then
891 usage
894 "cmd_$command" "$@"