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.
97 # If there already is a directory at the submodule path,
98 # expect it to be empty (since that is the default checkout
99 # action) and try to remove it.
100 # Note: if $path is a symlink to a directory the test will
101 # succeed but the rmdir will fail. We might want to fix this.
104 rmdir "$path" 2>/dev/null ||
105 die "Directory '$path' exist, but is neither empty nor a git repository"
109 die "A file already exist at path '$path'"
111 if test -n "$reference"
113 git-clone "$reference" -n "$url" "$path"
115 git-clone -n "$url" "$path"
117 die "Clone of '$url' into submodule path '$path' failed"
121 # Add a new submodule to the working tree, .gitmodules and the index
125 # optional branch is stored in global branch variable
129 # parse $args after "submodule ... add".
134 case "$2" in '') usage ;; esac
142 case "$2" in '') usage ;; esac
143 reference="--reference=$2"
167 if test -z "$repo" -o -z "$path"; then
171 # assure repo is absolute or relative to parent
174 # dereference source url relative to parent's url
175 realrepo=$(resolve_relative_url "$repo") || exit
182 die "repo URL: '$repo' must be absolute or begin with ./|../"
187 # multiple //; leading ./; /./; /../; trailing /
188 path=$(printf '%s/\n' "$path" |
198 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
199 die "'$path' already exists in the index"
201 # perhaps the path exists and is already a git repo, else clone it
204 if test -d "$path"/.git -o -f "$path"/.git
206 echo "Adding existing repo at '$path' to the index"
208 die "'$path' already exists and is not a valid git repo"
213 url=$(resolve_relative_url "$repo") || exit
219 git config submodule."$path".url "$url"
222 module_clone "$path" "$realrepo" "$reference" || exit
226 # ash fails to wordsplit ${branch:+-b "$branch"...}
228 '') git checkout -f -q ;;
229 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
231 ) || die "Unable to checkout submodule '$path'"
235 die "Failed to add submodule '$path'"
237 git config -f .gitmodules submodule."$path".path "$path" &&
238 git config -f .gitmodules submodule."$path".url "$repo" &&
239 git add .gitmodules ||
240 die "Failed to register submodule '$path'"
244 # Execute an arbitrary command sequence in each checked out
247 # $@ = command to execute
252 while read mode sha1 stage path
254 if test -e "$path"/.git
256 say "Entering '$path'"
257 (cd "$path" && eval "$@") ||
258 die "Stopping at '$path'; script returned non-zero status."
264 # Register submodules in .git/config
266 # $@ = requested paths (default to all)
270 # parse $args after "submodule ... init".
292 while read mode sha1 stage path
294 # Skip already registered paths
295 name=$(module_name "$path") || exit
296 url=$(git config submodule."$name".url)
297 test -z "$url" || continue
299 url=$(git config -f .gitmodules submodule."$name".url)
301 die "No url found for submodule path '$path' in .gitmodules"
303 # Possibly a url relative to parent
306 url=$(resolve_relative_url "$url") || exit
310 git config submodule."$name".url "$url" ||
311 die "Failed to register url for submodule path '$path'"
313 say "Submodule '$name' ($url) registered for path '$path'"
318 # Update each submodule path to correct revision, using clone and checkout as needed
320 # $@ = requested paths (default to all)
324 # parse $args after "submodule ... update".
341 case "$2" in '') usage ;; esac
342 reference="--reference=$2"
364 cmd_init "--" "$@" || return
368 while read mode sha1 stage path
370 name=$(module_name "$path") || exit
371 url=$(git config submodule."$name".url)
374 # Only mention uninitialized submodules when its
375 # path have been specified
377 say "Submodule path '$path' not initialized" &&
378 say "Maybe you want to use 'update --init'?"
382 if ! test -d "$path"/.git -o -f "$path"/.git
384 module_clone "$path" "$url" "$reference"|| exit
387 subsha1=$(unset GIT_DIR; cd "$path" &&
388 git rev-parse --verify HEAD) ||
389 die "Unable to find current revision in submodule path '$path'"
392 if test "$subsha1" != "$sha1"
395 if test -z "$subsha1"
400 if test -z "$nofetch"
402 (unset GIT_DIR; cd "$path" &&
404 die "Unable to fetch in submodule path '$path'"
407 (unset GIT_DIR; cd "$path" &&
408 git-checkout $force -q "$sha1") ||
409 die "Unable to checkout '$sha1' in submodule path '$path'"
411 say "Submodule path '$path': checked out '$sha1'"
420 git describe "$2" 2>/dev/null ||
421 git describe --tags "$2" 2>/dev/null ||
422 git describe --contains "$2" 2>/dev/null ||
423 git describe --all --always "$2"
426 test -z "$revname" || revname=" ($revname)"
429 # Show commit summary for submodules in index or working tree
431 # If '--cached' is given, show summary between index and given commit,
432 # or between working tree and given commit
434 # $@ = [commit (default 'HEAD'),] requested paths (default all)
440 # parse $args after "submodule ... summary".
451 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
473 test $summary_limit = 0 && return
475 if rev=$(git rev-parse -q --verify "$1^0")
484 # Get modified modules cared by user
485 modules=$(git diff-index $cached --raw $head -- "$@" |
486 egrep '^:([0-7]* )?160000' |
487 while read mod_src mod_dst sha1_src sha1_dst status name
489 # Always show modules deleted or type-changed (blob<->module)
490 test $status = D -o $status = T && echo "$name" && continue
491 # Also show added or modified modules which are checked out
492 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
497 test -z "$modules" && return
499 git diff-index $cached --raw $head -- $modules |
500 egrep '^:([0-7]* )?160000' |
502 while read mod_src mod_dst sha1_src sha1_dst status name
504 if test -z "$cached" &&
505 test $sha1_dst = 0000000000000000000000000000000000000000
509 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
511 100644 | 100755 | 120000)
512 sha1_dst=$(git hash-object $name)
518 echo >&2 "unexpected mode $mod_dst"
525 test $mod_src = 160000 &&
526 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
529 test $mod_dst = 160000 &&
530 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
534 case "$missing_src,$missing_dst" in
536 errmsg=" Warn: $name doesn't contain commit $sha1_src"
539 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
542 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
547 if test $mod_src = 160000 -a $mod_dst = 160000
549 range="$sha1_src...$sha1_dst"
550 elif test $mod_src = 160000
556 GIT_DIR="$name/.git" \
557 git log --pretty=oneline --first-parent $range | wc -l
559 total_commits=" ($(($total_commits + 0)))"
563 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
564 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
567 if test $mod_dst = 160000
569 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
571 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
574 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
578 # Don't give error msg for modification whose dst is not submodule
579 # i.e. deleted or changed to blob
580 test $mod_dst = 160000 && echo "$errmsg"
582 if test $mod_src = 160000 -a $mod_dst = 160000
585 test $summary_limit -gt 0 && limit="-$summary_limit"
586 GIT_DIR="$name/.git" \
587 git log $limit --pretty='format: %m %s' \
588 --first-parent $sha1_src...$sha1_dst
589 elif test $mod_dst = 160000
591 GIT_DIR="$name/.git" \
592 git log --pretty='format: > %s' -1 $sha1_dst
594 GIT_DIR="$name/.git" \
595 git log --pretty='format: < %s' -1 $sha1_src
601 if test -n "$for_status"; then
602 echo "# Modified submodules:"
604 sed -e 's|^|# |' -e 's|^# $|#|'
610 # List all submodules, prefixed with:
611 # - submodule not initialized
612 # + different revision checked out
614 # If --cached was specified the revision in the index will be printed
615 # instead of the currently checked out revision.
617 # $@ = requested paths (default to all)
621 # parse $args after "submodule ... status".
646 while read mode sha1 stage path
648 name=$(module_name "$path") || exit
649 url=$(git config submodule."$name".url)
650 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
655 set_name_rev "$path" "$sha1"
656 if git diff-files --quiet -- "$path"
658 say " $sha1 $path$revname"
662 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
663 set_name_rev "$path" "$sha1"
665 say "+$sha1 $path$revname"
670 # Sync remote urls for submodules
671 # This makes the value for remote.$remote.url match the value
672 # specified in .gitmodules.
697 while read mode sha1 stage path
699 name=$(module_name "$path")
700 url=$(git config -f .gitmodules --get submodule."$name".url)
702 # Possibly a url relative to parent
705 url=$(resolve_relative_url "$url") || exit
709 if test -e "$path"/.git
714 remote=$(get_default_remote)
715 say "Synchronizing submodule url for '$name'"
716 git config remote."$remote".url "$url"
722 # This loop parses the command line arguments to find the
723 # subcommand name to dispatch. Parsing of the subcommand specific
724 # options are primarily done by the subcommand implementations.
725 # Subcommand specific options such as --branch and --cached are
726 # parsed here as well, for backward compatibility.
728 while test $# != 0 && test -z "$command"
731 add | foreach | init | update | status | summary | sync)
761 # No command word defaults to "status"
762 test -n "$command" || command=status
764 # "-b branch" is accepted only by "add"
765 if test -n "$branch" && test "$command" != add
770 # "--cached" is accepted only by "status" and "summary"
771 if test -n "$cached" && test "$command" != status -a "$command" != summary