3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE="[--quiet] [--cached] \
8 [add [-b branch] <repo> <path>]|[status|init|update [-i|--init] [-N|--no-fetch]|summary [-n|--summary-limit <n>] [<commit>]] \
9 [--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
24 # print stuff on stdout unless -q was specified
34 # Resolve relative url by appending to parent's url
35 resolve_relative_url ()
37 remote=$(get_default_remote)
38 remoteurl=$(git config "remote.$remote.url") ||
39 die "remote ($remote) does not have a url defined in .git/config"
41 remoteurl=${remoteurl%/}
47 remoteurl="${remoteurl%/*}"
56 echo "$remoteurl/${url%/}"
60 # Get submodule info for registered submodules
61 # $@ = path to limit submodule list
65 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
69 # Map submodule path to submodule name
75 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
76 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
77 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
78 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
80 die "No submodule mapping found in .gitmodules for path '$path'"
87 # Prior to calling, cmd_update checks that a possibly existing
88 # path is not a git repository.
89 # Likewise, cmd_add checks that path does not exist at all,
90 # since it is the location of a new submodule.
98 # If there already is a directory at the submodule path,
99 # expect it to be empty (since that is the default checkout
100 # action) and try to remove it.
101 # Note: if $path is a symlink to a directory the test will
102 # succeed but the rmdir will fail. We might want to fix this.
105 rmdir "$path" 2>/dev/null ||
106 die "Directory '$path' exist, but is neither empty nor a git repository"
110 die "A file already exist at path '$path'"
112 if test -n "$reference"
114 git-clone "$reference" -n "$url" "$path"
116 git-clone -n "$url" "$path"
118 die "Clone of '$url' into submodule path '$path' failed"
122 # Add a new submodule to the working tree, .gitmodules and the index
126 # optional branch is stored in global branch variable
130 # parse $args after "submodule ... add".
135 case "$2" in '') usage ;; esac
143 case "$2" in '') usage ;; esac
144 reference="--reference=$2"
168 if test -z "$repo" -o -z "$path"; then
172 # assure repo is absolute or relative to parent
175 # dereference source url relative to parent's url
176 realrepo=$(resolve_relative_url "$repo") || exit
183 die "repo URL: '$repo' must be absolute or begin with ./|../"
188 # multiple //; leading ./; /./; /../; trailing /
189 path=$(printf '%s/\n' "$path" |
199 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
200 die "'$path' already exists in the index"
202 # perhaps the path exists and is already a git repo, else clone it
205 if test -d "$path"/.git -o -f "$path"/.git
207 echo "Adding existing repo at '$path' to the index"
209 die "'$path' already exists and is not a valid git repo"
214 url=$(resolve_relative_url "$repo") || exit
220 git config submodule."$path".url "$url"
223 module_clone "$path" "$realrepo" "$reference" || exit
227 # ash fails to wordsplit ${branch:+-b "$branch"...}
229 '') git checkout -f -q ;;
230 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
232 ) || die "Unable to checkout submodule '$path'"
236 die "Failed to add submodule '$path'"
238 git config -f .gitmodules submodule."$path".path "$path" &&
239 git config -f .gitmodules submodule."$path".url "$repo" &&
240 git add .gitmodules ||
241 die "Failed to register submodule '$path'"
245 # Execute an arbitrary command sequence in each checked out
248 # $@ = command to execute
253 while read mode sha1 stage path
255 if test -e "$path"/.git
257 say "Entering '$path'"
258 (cd "$path" && eval "$@") ||
259 die "Stopping at '$path'; script returned non-zero status."
265 # Register submodules in .git/config
267 # $@ = requested paths (default to all)
271 # parse $args after "submodule ... init".
293 while read mode sha1 stage path
295 # Skip already registered paths
296 name=$(module_name "$path") || exit
297 url=$(git config submodule."$name".url)
298 test -z "$url" || continue
300 url=$(git config -f .gitmodules submodule."$name".url)
302 die "No url found for submodule path '$path' in .gitmodules"
304 # Possibly a url relative to parent
307 url=$(resolve_relative_url "$url") || exit
311 git config submodule."$name".url "$url" ||
312 die "Failed to register url for submodule path '$path'"
314 upd="$(git config -f .gitmodules submodule."$name".update)"
316 git config submodule."$name".update "$upd" ||
317 die "Failed to register update mode for submodule path '$path'"
319 say "Submodule '$name' ($url) registered for path '$path'"
324 # Update each submodule path to correct revision, using clone and checkout as needed
326 # $@ = requested paths (default to all)
330 # parse $args after "submodule ... update".
351 case "$2" in '') usage ;; esac
352 reference="--reference=$2"
374 cmd_init "--" "$@" || return
378 while read mode sha1 stage path
380 name=$(module_name "$path") || exit
381 url=$(git config submodule."$name".url)
382 update_module=$(git config submodule."$name".update)
385 # Only mention uninitialized submodules when its
386 # path have been specified
388 say "Submodule path '$path' not initialized" &&
389 say "Maybe you want to use 'update --init'?"
393 if ! test -d "$path"/.git -o -f "$path"/.git
395 module_clone "$path" "$url" "$reference"|| exit
398 subsha1=$(unset GIT_DIR; cd "$path" &&
399 git rev-parse --verify HEAD) ||
400 die "Unable to find current revision in submodule path '$path'"
403 if ! test -z "$update"
405 update_module=$update
408 if test "$subsha1" != "$sha1"
411 if test -z "$subsha1"
416 if test -z "$nofetch"
418 (unset GIT_DIR; cd "$path" &&
420 die "Unable to fetch in submodule path '$path'"
423 case "$update_module" in
430 command="git checkout $force -q"
436 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
437 die "Unable to $action '$sha1' in submodule path '$path'"
438 say "Submodule path '$path': $msg '$sha1'"
447 git describe "$2" 2>/dev/null ||
448 git describe --tags "$2" 2>/dev/null ||
449 git describe --contains "$2" 2>/dev/null ||
450 git describe --all --always "$2"
453 test -z "$revname" || revname=" ($revname)"
456 # Show commit summary for submodules in index or working tree
458 # If '--cached' is given, show summary between index and given commit,
459 # or between working tree and given commit
461 # $@ = [commit (default 'HEAD'),] requested paths (default all)
467 # parse $args after "submodule ... summary".
478 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
500 test $summary_limit = 0 && return
502 if rev=$(git rev-parse -q --verify "$1^0")
511 # Get modified modules cared by user
512 modules=$(git diff-index $cached --raw $head -- "$@" |
513 egrep '^:([0-7]* )?160000' |
514 while read mod_src mod_dst sha1_src sha1_dst status name
516 # Always show modules deleted or type-changed (blob<->module)
517 test $status = D -o $status = T && echo "$name" && continue
518 # Also show added or modified modules which are checked out
519 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
524 test -z "$modules" && return
526 git diff-index $cached --raw $head -- $modules |
527 egrep '^:([0-7]* )?160000' |
529 while read mod_src mod_dst sha1_src sha1_dst status name
531 if test -z "$cached" &&
532 test $sha1_dst = 0000000000000000000000000000000000000000
536 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
538 100644 | 100755 | 120000)
539 sha1_dst=$(git hash-object $name)
545 echo >&2 "unexpected mode $mod_dst"
552 test $mod_src = 160000 &&
553 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
556 test $mod_dst = 160000 &&
557 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
561 case "$missing_src,$missing_dst" in
563 errmsg=" Warn: $name doesn't contain commit $sha1_src"
566 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
569 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
574 if test $mod_src = 160000 -a $mod_dst = 160000
576 range="$sha1_src...$sha1_dst"
577 elif test $mod_src = 160000
583 GIT_DIR="$name/.git" \
584 git log --pretty=oneline --first-parent $range | wc -l
586 total_commits=" ($(($total_commits + 0)))"
590 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
591 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
594 if test $mod_dst = 160000
596 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
598 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
601 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
605 # Don't give error msg for modification whose dst is not submodule
606 # i.e. deleted or changed to blob
607 test $mod_dst = 160000 && echo "$errmsg"
609 if test $mod_src = 160000 -a $mod_dst = 160000
612 test $summary_limit -gt 0 && limit="-$summary_limit"
613 GIT_DIR="$name/.git" \
614 git log $limit --pretty='format: %m %s' \
615 --first-parent $sha1_src...$sha1_dst
616 elif test $mod_dst = 160000
618 GIT_DIR="$name/.git" \
619 git log --pretty='format: > %s' -1 $sha1_dst
621 GIT_DIR="$name/.git" \
622 git log --pretty='format: < %s' -1 $sha1_src
628 if test -n "$for_status"; then
629 echo "# Modified submodules:"
631 sed -e 's|^|# |' -e 's|^# $|#|'
637 # List all submodules, prefixed with:
638 # - submodule not initialized
639 # + different revision checked out
641 # If --cached was specified the revision in the index will be printed
642 # instead of the currently checked out revision.
644 # $@ = requested paths (default to all)
648 # parse $args after "submodule ... status".
673 while read mode sha1 stage path
675 name=$(module_name "$path") || exit
676 url=$(git config submodule."$name".url)
677 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
682 set_name_rev "$path" "$sha1"
683 if git diff-files --quiet -- "$path"
685 say " $sha1 $path$revname"
689 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
690 set_name_rev "$path" "$sha1"
692 say "+$sha1 $path$revname"
697 # Sync remote urls for submodules
698 # This makes the value for remote.$remote.url match the value
699 # specified in .gitmodules.
724 while read mode sha1 stage path
726 name=$(module_name "$path")
727 url=$(git config -f .gitmodules --get submodule."$name".url)
729 # Possibly a url relative to parent
732 url=$(resolve_relative_url "$url") || exit
736 if test -e "$path"/.git
741 remote=$(get_default_remote)
742 say "Synchronizing submodule url for '$name'"
743 git config remote."$remote".url "$url"
749 # This loop parses the command line arguments to find the
750 # subcommand name to dispatch. Parsing of the subcommand specific
751 # options are primarily done by the subcommand implementations.
752 # Subcommand specific options such as --branch and --cached are
753 # parsed here as well, for backward compatibility.
755 while test $# != 0 && test -z "$command"
758 add | foreach | init | update | status | summary | sync)
788 # No command word defaults to "status"
789 test -n "$command" || command=status
791 # "-b branch" is accepted only by "add"
792 if test -n "$branch" && test "$command" != add
797 # "--cached" is accepted only by "status" and "summary"
798 if test -n "$cached" && test "$command" != status -a "$command" != summary