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>...]"
29 # Resolve relative url by appending to parent's url
30 resolve_relative_url ()
32 remote=$(get_default_remote)
33 remoteurl=$(git config "remote.$remote.url") ||
34 die "remote ($remote) does not have a url defined in .git/config"
36 remoteurl=${remoteurl%/}
42 remoteurl="${remoteurl%/*}"
51 echo "$remoteurl/${url%/}"
55 # Get submodule info for registered submodules
56 # $@ = path to limit submodule list
60 git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
64 # Map submodule path to submodule name
70 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
71 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
72 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
73 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
75 die "No submodule mapping found in .gitmodules for path '$path'"
82 # Prior to calling, cmd_update checks that a possibly existing
83 # path is not a git repository.
84 # Likewise, cmd_add checks that path does not exist at all,
85 # since it is the location of a new submodule.
93 # If there already is a directory at the submodule path,
94 # expect it to be empty (since that is the default checkout
95 # action) and try to remove it.
96 # Note: if $path is a symlink to a directory the test will
97 # succeed but the rmdir will fail. We might want to fix this.
100 rmdir "$path" 2>/dev/null ||
101 die "Directory '$path' exists, but is neither empty nor a git repository"
105 die "A file already exist at path '$path'"
107 if test -n "$reference"
109 git-clone "$reference" -n "$url" "$path"
111 git-clone -n "$url" "$path"
113 die "Clone of '$url' into submodule path '$path' failed"
117 # Add a new submodule to the working tree, .gitmodules and the index
121 # optional branch is stored in global branch variable
125 # parse $args after "submodule ... add".
130 case "$2" in '') usage ;; esac
138 case "$2" in '') usage ;; esac
139 reference="--reference=$2"
163 if test -z "$repo" -o -z "$path"; then
167 # assure repo is absolute or relative to parent
170 # dereference source url relative to parent's url
171 realrepo=$(resolve_relative_url "$repo") || exit
178 die "repo URL: '$repo' must be absolute or begin with ./|../"
183 # multiple //; leading ./; /./; /../; trailing /
184 path=$(printf '%s/\n' "$path" |
194 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
195 die "'$path' already exists in the index"
197 # perhaps the path exists and is already a git repo, else clone it
200 if test -d "$path"/.git -o -f "$path"/.git
202 echo "Adding existing repo at '$path' to the index"
204 die "'$path' already exists and is not a valid git repo"
209 url=$(resolve_relative_url "$repo") || exit
215 git config submodule."$path".url "$url"
218 module_clone "$path" "$realrepo" "$reference" || exit
222 # ash fails to wordsplit ${branch:+-b "$branch"...}
224 '') git checkout -f -q ;;
225 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
227 ) || die "Unable to checkout submodule '$path'"
231 die "Failed to add submodule '$path'"
233 git config -f .gitmodules submodule."$path".path "$path" &&
234 git config -f .gitmodules submodule."$path".url "$repo" &&
235 git add .gitmodules ||
236 die "Failed to register submodule '$path'"
240 # Execute an arbitrary command sequence in each checked out
243 # $@ = command to execute
247 # parse $args after "submodule ... foreach".
268 while read mode sha1 stage path
270 if test -e "$path"/.git
272 say "Entering '$prefix$path'"
273 name=$(module_name "$path")
275 prefix="$prefix$path/"
279 if test -n "$recursive"
281 cmd_foreach "--recursive" "$@"
284 die "Stopping at '$path'; script returned non-zero status."
290 # Register submodules in .git/config
292 # $@ = requested paths (default to all)
296 # parse $args after "submodule ... init".
318 while read mode sha1 stage path
320 # Skip already registered paths
321 name=$(module_name "$path") || exit
322 url=$(git config submodule."$name".url)
323 test -z "$url" || continue
325 url=$(git config -f .gitmodules submodule."$name".url)
327 die "No url found for submodule path '$path' in .gitmodules"
329 # Possibly a url relative to parent
332 url=$(resolve_relative_url "$url") || exit
336 git config submodule."$name".url "$url" ||
337 die "Failed to register url for submodule path '$path'"
339 upd="$(git config -f .gitmodules submodule."$name".update)"
341 git config submodule."$name".update "$upd" ||
342 die "Failed to register update mode for submodule path '$path'"
344 say "Submodule '$name' ($url) registered for path '$path'"
349 # Update each submodule path to correct revision, using clone and checkout as needed
351 # $@ = requested paths (default to all)
355 # parse $args after "submodule ... update".
377 case "$2" in '') usage ;; esac
378 reference="--reference=$2"
408 cmd_init "--" "$@" || return
412 while read mode sha1 stage path
414 name=$(module_name "$path") || exit
415 url=$(git config submodule."$name".url)
416 update_module=$(git config submodule."$name".update)
419 # Only mention uninitialized submodules when its
420 # path have been specified
422 say "Submodule path '$path' not initialized" &&
423 say "Maybe you want to use 'update --init'?"
427 if ! test -d "$path"/.git -o -f "$path"/.git
429 module_clone "$path" "$url" "$reference"|| exit
432 subsha1=$(unset GIT_DIR; cd "$path" &&
433 git rev-parse --verify HEAD) ||
434 die "Unable to find current revision in submodule path '$path'"
437 if ! test -z "$update"
439 update_module=$update
442 if test "$subsha1" != "$sha1"
445 if test -z "$subsha1"
450 if test -z "$nofetch"
452 (unset GIT_DIR; cd "$path" &&
454 die "Unable to fetch in submodule path '$path'"
457 case "$update_module" in
469 command="git checkout $force -q"
475 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
476 die "Unable to $action '$sha1' in submodule path '$path'"
477 say "Submodule path '$path': $msg '$sha1'"
480 if test -n "$recursive"
482 (unset GIT_DIR; cd "$path" && cmd_update $orig_args) ||
483 die "Failed to recurse into submodule path '$path'"
492 git describe "$2" 2>/dev/null ||
493 git describe --tags "$2" 2>/dev/null ||
494 git describe --contains "$2" 2>/dev/null ||
495 git describe --all --always "$2"
498 test -z "$revname" || revname=" ($revname)"
501 # Show commit summary for submodules in index or working tree
503 # If '--cached' is given, show summary between index and given commit,
504 # or between working tree and given commit
506 # $@ = [commit (default 'HEAD'),] requested paths (default all)
513 # parse $args after "submodule ... summary".
527 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
549 test $summary_limit = 0 && return
551 if rev=$(git rev-parse -q --verify "$1^0")
562 die "--cached cannot be used with --files"
568 # Get modified modules cared by user
569 modules=$(git $diff_cmd $cached --raw $head -- "$@" |
570 sane_egrep '^:([0-7]* )?160000' |
571 while read mod_src mod_dst sha1_src sha1_dst status name
573 # Always show modules deleted or type-changed (blob<->module)
574 test $status = D -o $status = T && echo "$name" && continue
575 # Also show added or modified modules which are checked out
576 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
581 test -z "$modules" && return
583 git $diff_cmd $cached --raw $head -- $modules |
584 sane_egrep '^:([0-7]* )?160000' |
586 while read mod_src mod_dst sha1_src sha1_dst status name
588 if test -z "$cached" &&
589 test $sha1_dst = 0000000000000000000000000000000000000000
593 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
595 100644 | 100755 | 120000)
596 sha1_dst=$(git hash-object $name)
602 echo >&2 "unexpected mode $mod_dst"
609 test $mod_src = 160000 &&
610 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
613 test $mod_dst = 160000 &&
614 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
618 case "$missing_src,$missing_dst" in
620 errmsg=" Warn: $name doesn't contain commit $sha1_src"
623 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
626 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
631 if test $mod_src = 160000 -a $mod_dst = 160000
633 range="$sha1_src...$sha1_dst"
634 elif test $mod_src = 160000
640 GIT_DIR="$name/.git" \
641 git log --pretty=oneline --first-parent $range | wc -l
643 total_commits=" ($(($total_commits + 0)))"
647 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
648 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
651 if test $mod_dst = 160000
653 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
655 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
658 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
662 # Don't give error msg for modification whose dst is not submodule
663 # i.e. deleted or changed to blob
664 test $mod_dst = 160000 && echo "$errmsg"
666 if test $mod_src = 160000 -a $mod_dst = 160000
669 test $summary_limit -gt 0 && limit="-$summary_limit"
670 GIT_DIR="$name/.git" \
671 git log $limit --pretty='format: %m %s' \
672 --first-parent $sha1_src...$sha1_dst
673 elif test $mod_dst = 160000
675 GIT_DIR="$name/.git" \
676 git log --pretty='format: > %s' -1 $sha1_dst
678 GIT_DIR="$name/.git" \
679 git log --pretty='format: < %s' -1 $sha1_src
685 if test -n "$for_status"; then
686 echo "# Modified submodules:"
688 sed -e 's|^|# |' -e 's|^# $|#|'
694 # List all submodules, prefixed with:
695 # - submodule not initialized
696 # + different revision checked out
698 # If --cached was specified the revision in the index will be printed
699 # instead of the currently checked out revision.
701 # $@ = requested paths (default to all)
705 # parse $args after "submodule ... status".
734 while read mode sha1 stage path
736 name=$(module_name "$path") || exit
737 url=$(git config submodule."$name".url)
738 displaypath="$prefix$path"
739 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
741 say "-$sha1 $displaypath"
744 set_name_rev "$path" "$sha1"
745 if git diff-files --quiet -- "$path"
747 say " $sha1 $displaypath$revname"
751 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
752 set_name_rev "$path" "$sha1"
754 say "+$sha1 $displaypath$revname"
757 if test -n "$recursive"
760 prefix="$displaypath/"
763 cmd_status $orig_args
765 die "Failed to recurse into submodule path '$path'"
770 # Sync remote urls for submodules
771 # This makes the value for remote.$remote.url match the value
772 # specified in .gitmodules.
797 while read mode sha1 stage path
799 name=$(module_name "$path")
800 url=$(git config -f .gitmodules --get submodule."$name".url)
802 # Possibly a url relative to parent
805 url=$(resolve_relative_url "$url") || exit
809 if test -e "$path"/.git
814 remote=$(get_default_remote)
815 say "Synchronizing submodule url for '$name'"
816 git config remote."$remote".url "$url"
822 # This loop parses the command line arguments to find the
823 # subcommand name to dispatch. Parsing of the subcommand specific
824 # options are primarily done by the subcommand implementations.
825 # Subcommand specific options such as --branch and --cached are
826 # parsed here as well, for backward compatibility.
828 while test $# != 0 && test -z "$command"
831 add | foreach | init | update | status | summary | sync)
861 # No command word defaults to "status"
862 test -n "$command" || command=status
864 # "-b branch" is accepted only by "add"
865 if test -n "$branch" && test "$command" != add
870 # "--cached" is accepted only by "status" and "summary"
871 if test -n "$cached" && test "$command" != status -a "$command" != summary