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>...]"
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"
38 remoteurl=${remoteurl%/}
44 remoteurl="${remoteurl%/*}"
53 echo "$remoteurl/${url%/}"
57 # Get submodule info for registered submodules
58 # $@ = path to limit submodule list
62 git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
66 # Map submodule path to submodule 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' )
77 die "No submodule mapping found in .gitmodules for path '$path'"
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.
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.
102 rmdir "$path" 2>/dev/null ||
103 die "Directory '$path' exists, but is neither empty nor a git repository"
107 die "A file already exist at path '$path'"
109 if test -n "$reference"
111 git-clone "$reference" -n "$url" "$path"
113 git-clone -n "$url" "$path"
115 die "Clone of '$url' into submodule path '$path' failed"
119 # Add a new submodule to the working tree, .gitmodules and the index
123 # optional branch is stored in global branch variable
127 # parse $args after "submodule ... add".
132 case "$2" in '') usage ;; esac
140 case "$2" in '') usage ;; esac
141 reference="--reference=$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
174 # assure repo is absolute or relative to parent
177 # dereference source url relative to parent's url
178 realrepo=$(resolve_relative_url "$repo") || exit
185 die "repo URL: '$repo' must be absolute or begin with ./|../"
190 # multiple //; leading ./; /./; /../; trailing /
191 path=$(printf '%s/\n' "$path" |
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
207 if test -d "$path"/.git -o -f "$path"/.git
209 echo "Adding existing repo at '$path' to the index"
211 die "'$path' already exists and is not a valid git repo"
216 url=$(resolve_relative_url "$repo") || exit
222 git config submodule."$path".url "$url"
225 module_clone "$path" "$realrepo" "$reference" || exit
229 # ash fails to wordsplit ${branch:+-b "$branch"...}
231 '') git checkout -f -q ;;
232 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
234 ) || die "Unable to checkout submodule '$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
250 # $@ = command to execute
254 # parse $args after "submodule ... foreach".
277 while read mode sha1 stage path
279 if test -e "$path"/.git
281 say "Entering '$prefix$path'"
282 name=$(module_name "$path")
284 prefix="$prefix$path/"
288 if test -n "$recursive"
290 cmd_foreach "--recursive" "$@"
293 die "Stopping at '$path'; script returned non-zero status."
299 # Register submodules in .git/config
301 # $@ = requested paths (default to all)
305 # parse $args after "submodule ... init".
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)
336 die "No url found for submodule path '$path' in .gitmodules"
338 # Possibly a url relative to parent
341 url=$(resolve_relative_url "$url") || exit
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)"
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'"
358 # Update each submodule path to correct revision, using clone and checkout as needed
360 # $@ = requested paths (default to all)
364 # parse $args after "submodule ... update".
386 case "$2" in '') usage ;; esac
387 reference="--reference=$2"
417 cmd_init "--" "$@" || return
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)
428 # Only mention uninitialized submodules when its
429 # path have been specified
431 say "Submodule path '$path' not initialized" &&
432 say "Maybe you want to use 'update --init'?"
436 if ! test -d "$path"/.git -o -f "$path"/.git
438 module_clone "$path" "$url" "$reference"|| exit
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"
448 update_module=$update
451 if test "$subsha1" != "$sha1"
454 if test -z "$subsha1"
459 if test -z "$nofetch"
461 (clear_local_git_env; cd "$path" &&
463 die "Unable to fetch in submodule path '$path'"
466 case "$update_module" in
478 command="git checkout $force -q"
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"
491 (clear_local_git_env; cd "$path" && cmd_update $orig_args) ||
492 die "Failed to recurse into submodule path '$path'"
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)
522 # parse $args after "submodule ... summary".
536 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
558 test $summary_limit = 0 && return
560 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
564 elif test -z "$1" -o "$1" = "HEAD"
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
576 die "--cached cannot be used with --files"
582 # Get modified modules cared by user
583 modules=$(git $diff_cmd $cached --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 &&
595 test -z "$modules" && return
597 git $diff_cmd $cached --raw $head -- $modules |
598 sane_egrep '^:([0-7]* )?160000' |
600 while read mod_src mod_dst sha1_src sha1_dst status name
602 if test -z "$cached" &&
603 test $sha1_dst = 0000000000000000000000000000000000000000
607 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
609 100644 | 100755 | 120000)
610 sha1_dst=$(git hash-object $name)
616 echo >&2 "unexpected mode $mod_dst"
623 test $mod_src = 160000 &&
624 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
627 test $mod_dst = 160000 &&
628 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
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"
640 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
645 if test $mod_src = 160000 -a $mod_dst = 160000
647 range="$sha1_src...$sha1_dst"
648 elif test $mod_src = 160000
654 GIT_DIR="$name/.git" \
655 git rev-list --first-parent $range -- | wc -l
657 total_commits=" ($(($total_commits + 0)))"
661 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
662 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
665 if test $mod_dst = 160000
667 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
669 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
672 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
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"
680 if test $mod_src = 160000 -a $mod_dst = 160000
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
689 GIT_DIR="$name/.git" \
690 git log --pretty='format: > %s' -1 $sha1_dst
692 GIT_DIR="$name/.git" \
693 git log --pretty='format: < %s' -1 $sha1_src
699 if test -n "$for_status"; then
700 if [ -n "$files" ]; then
701 echo "# Submodules changed but not updated:"
703 echo "# Submodule changes to be committed:"
706 sed -e 's|^|# |' -e 's|^# $|#|'
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)
723 # parse $args after "submodule ... status".
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
759 say "-$sha1 $displaypath"
762 set_name_rev "$path" "$sha1"
763 if git diff-files --quiet -- "$path"
765 say " $sha1 $displaypath$revname"
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"
778 prefix="$displaypath/"
781 cmd_status $orig_args
783 die "Failed to recurse into submodule path '$path'"
788 # Sync remote urls for submodules
789 # This makes the value for remote.$remote.url match the value
790 # specified in .gitmodules.
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
823 url=$(resolve_relative_url "$url") || exit
827 if test -e "$path"/.git
832 remote=$(get_default_remote)
833 say "Synchronizing submodule url for '$name'"
834 git config remote."$remote".url "$url"
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"
849 add | foreach | init | update | status | summary | sync)
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
888 # "--cached" is accepted only by "status" and "summary"
889 if test -n "$cached" && test "$command" != status -a "$command" != summary