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>...]]"
23 # print stuff on stdout unless -q was specified
33 # Resolve relative url by appending to parent's url
34 resolve_relative_url ()
36 remote=$(get_default_remote)
37 remoteurl=$(git config "remote.$remote.url") ||
38 die "remote ($remote) does not have a url defined in .git/config"
40 remoteurl=${remoteurl%/}
46 remoteurl="${remoteurl%/*}"
55 echo "$remoteurl/${url%/}"
59 # Get submodule info for registered submodules
60 # $@ = path to limit submodule list
64 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
68 # Map submodule path to submodule name
74 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
75 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
76 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
77 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
79 die "No submodule mapping found in .gitmodules for path '$path'"
86 # Prior to calling, cmd_update checks that a possibly existing
87 # path is not a git repository.
88 # Likewise, cmd_add checks that path does not exist at all,
89 # since it is the location of a new submodule.
96 # If there already is a directory at the submodule path,
97 # expect it to be empty (since that is the default checkout
98 # action) and try to remove it.
99 # Note: if $path is a symlink to a directory the test will
100 # succeed but the rmdir will fail. We might want to fix this.
103 rmdir "$path" 2>/dev/null ||
104 die "Directory '$path' exist, but is neither empty nor a git repository"
108 die "A file already exist at path '$path'"
110 git-clone -n "$url" "$path" ||
111 die "Clone of '$url' into submodule path '$path' failed"
115 # Add a new submodule to the working tree, .gitmodules and the index
119 # optional branch is stored in global branch variable
123 # parse $args after "submodule ... add".
128 case "$2" in '') usage ;; esac
152 if test -z "$repo" -o -z "$path"; then
156 # assure repo is absolute or relative to parent
159 # dereference source url relative to parent's url
160 realrepo=$(resolve_relative_url "$repo") || exit
167 die "repo URL: '$repo' must be absolute or begin with ./|../"
172 # multiple //; leading ./; /./; /../; trailing /
173 path=$(printf '%s/\n' "$path" |
183 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
184 die "'$path' already exists in the index"
186 # perhaps the path exists and is already a git repo, else clone it
189 if test -d "$path"/.git -o -f "$path"/.git
191 echo "Adding existing repo at '$path' to the index"
193 die "'$path' already exists and is not a valid git repo"
198 url=$(resolve_relative_url "$repo") || exit
204 git config submodule."$path".url "$url"
207 module_clone "$path" "$realrepo" || exit
211 # ash fails to wordsplit ${branch:+-b "$branch"...}
213 '') git checkout -f -q ;;
214 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
216 ) || die "Unable to checkout submodule '$path'"
220 die "Failed to add submodule '$path'"
222 git config -f .gitmodules submodule."$path".path "$path" &&
223 git config -f .gitmodules submodule."$path".url "$repo" &&
224 git add .gitmodules ||
225 die "Failed to register submodule '$path'"
229 # Execute an arbitrary command sequence in each checked out
232 # $@ = command to execute
237 while read mode sha1 stage path
239 if test -e "$path"/.git
241 say "Entering '$path'"
242 (cd "$path" && eval "$@") ||
243 die "Stopping at '$path'; script returned non-zero status."
249 # Register submodules in .git/config
251 # $@ = requested paths (default to all)
255 # parse $args after "submodule ... init".
277 while read mode sha1 stage path
279 # Skip already registered paths
280 name=$(module_name "$path") || exit
281 url=$(git config submodule."$name".url)
282 test -z "$url" || continue
284 url=$(git config -f .gitmodules submodule."$name".url)
286 die "No url found for submodule path '$path' in .gitmodules"
288 # Possibly a url relative to parent
291 url=$(resolve_relative_url "$url") || exit
295 git config submodule."$name".url "$url" ||
296 die "Failed to register url for submodule path '$path'"
298 upd="$(git config -f .gitmodules submodule."$name".update)"
300 git config submodule."$name".update "$upd" ||
301 die "Failed to register update mode for submodule path '$path'"
303 say "Submodule '$name' ($url) registered for path '$path'"
308 # Update each submodule path to correct revision, using clone and checkout as needed
310 # $@ = requested paths (default to all)
314 # parse $args after "submodule ... update".
324 cmd_init "$@" || return
348 while read mode sha1 stage path
350 name=$(module_name "$path") || exit
351 url=$(git config submodule."$name".url)
352 update_module=$(git config submodule."$name".update)
355 # Only mention uninitialized submodules when its
356 # path have been specified
358 say "Submodule path '$path' not initialized" &&
359 say "Maybe you want to use 'update --init'?"
363 if ! test -d "$path"/.git -o -f "$path"/.git
365 module_clone "$path" "$url" || exit
368 subsha1=$(unset GIT_DIR; cd "$path" &&
369 git rev-parse --verify HEAD) ||
370 die "Unable to find current revision in submodule path '$path'"
373 if ! test -z "$update"
375 update_module=$update
378 if test "$subsha1" != "$sha1"
381 if test -z "$subsha1"
386 if test -z "$nofetch"
388 (unset GIT_DIR; cd "$path" &&
390 die "Unable to fetch in submodule path '$path'"
393 case "$update_module" in
400 command="git checkout $force -q"
406 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
407 die "Unable to $action '$sha1' in submodule path '$path'"
408 say "Submodule path '$path': $msg '$sha1'"
417 git describe "$2" 2>/dev/null ||
418 git describe --tags "$2" 2>/dev/null ||
419 git describe --contains "$2" 2>/dev/null ||
420 git describe --all --always "$2"
423 test -z "$revname" || revname=" ($revname)"
426 # Show commit summary for submodules in index or working tree
428 # If '--cached' is given, show summary between index and given commit,
429 # or between working tree and given commit
431 # $@ = [commit (default 'HEAD'),] requested paths (default all)
437 # parse $args after "submodule ... summary".
448 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
470 test $summary_limit = 0 && return
472 if rev=$(git rev-parse -q --verify "$1^0")
481 # Get modified modules cared by user
482 modules=$(git diff-index $cached --raw $head -- "$@" |
483 egrep '^:([0-7]* )?160000' |
484 while read mod_src mod_dst sha1_src sha1_dst status name
486 # Always show modules deleted or type-changed (blob<->module)
487 test $status = D -o $status = T && echo "$name" && continue
488 # Also show added or modified modules which are checked out
489 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
494 test -z "$modules" && return
496 git diff-index $cached --raw $head -- $modules |
497 egrep '^:([0-7]* )?160000' |
499 while read mod_src mod_dst sha1_src sha1_dst status name
501 if test -z "$cached" &&
502 test $sha1_dst = 0000000000000000000000000000000000000000
506 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
508 100644 | 100755 | 120000)
509 sha1_dst=$(git hash-object $name)
515 echo >&2 "unexpected mode $mod_dst"
522 test $mod_src = 160000 &&
523 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
526 test $mod_dst = 160000 &&
527 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
531 case "$missing_src,$missing_dst" in
533 errmsg=" Warn: $name doesn't contain commit $sha1_src"
536 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
539 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
544 if test $mod_src = 160000 -a $mod_dst = 160000
546 range="$sha1_src...$sha1_dst"
547 elif test $mod_src = 160000
553 GIT_DIR="$name/.git" \
554 git log --pretty=oneline --first-parent $range | wc -l
556 total_commits=" ($(($total_commits + 0)))"
560 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
561 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
564 if test $mod_dst = 160000
566 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
568 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
571 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
575 # Don't give error msg for modification whose dst is not submodule
576 # i.e. deleted or changed to blob
577 test $mod_dst = 160000 && echo "$errmsg"
579 if test $mod_src = 160000 -a $mod_dst = 160000
582 test $summary_limit -gt 0 && limit="-$summary_limit"
583 GIT_DIR="$name/.git" \
584 git log $limit --pretty='format: %m %s' \
585 --first-parent $sha1_src...$sha1_dst
586 elif test $mod_dst = 160000
588 GIT_DIR="$name/.git" \
589 git log --pretty='format: > %s' -1 $sha1_dst
591 GIT_DIR="$name/.git" \
592 git log --pretty='format: < %s' -1 $sha1_src
598 if test -n "$for_status"; then
599 echo "# Modified submodules:"
601 sed -e 's|^|# |' -e 's|^# $|#|'
607 # List all submodules, prefixed with:
608 # - submodule not initialized
609 # + different revision checked out
611 # If --cached was specified the revision in the index will be printed
612 # instead of the currently checked out revision.
614 # $@ = requested paths (default to all)
618 # parse $args after "submodule ... status".
643 while read mode sha1 stage path
645 name=$(module_name "$path") || exit
646 url=$(git config submodule."$name".url)
647 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
652 set_name_rev "$path" "$sha1"
653 if git diff-files --quiet -- "$path"
655 say " $sha1 $path$revname"
659 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
660 set_name_rev "$path" "$sha1"
662 say "+$sha1 $path$revname"
667 # Sync remote urls for submodules
668 # This makes the value for remote.$remote.url match the value
669 # specified in .gitmodules.
694 while read mode sha1 stage path
696 name=$(module_name "$path")
697 url=$(git config -f .gitmodules --get submodule."$name".url)
699 # Possibly a url relative to parent
702 url=$(resolve_relative_url "$url") || exit
706 if test -e "$path"/.git
711 remote=$(get_default_remote)
712 say "Synchronizing submodule url for '$name'"
713 git config remote."$remote".url "$url"
719 # This loop parses the command line arguments to find the
720 # subcommand name to dispatch. Parsing of the subcommand specific
721 # options are primarily done by the subcommand implementations.
722 # Subcommand specific options such as --branch and --cached are
723 # parsed here as well, for backward compatibility.
725 while test $# != 0 && test -z "$command"
728 add | foreach | init | update | status | summary | sync)
758 # No command word defaults to "status"
759 test -n "$command" || command=status
761 # "-b branch" is accepted only by "add"
762 if test -n "$branch" && test "$command" != add
767 # "--cached" is accepted only by "status" and "summary"
768 if test -n "$cached" && test "$command" != status -a "$command" != summary