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] [--rebase|--merge]|summary [-n|--summary-limit <n>] [<commit>]] \
9 [--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
22 # Resolve relative url by appending to parent's url
23 resolve_relative_url ()
25 remote=$(get_default_remote)
26 remoteurl=$(git config "remote.$remote.url") ||
27 die "remote ($remote) does not have a url defined in .git/config"
29 remoteurl=${remoteurl%/}
35 remoteurl="${remoteurl%/*}"
44 echo "$remoteurl/${url%/}"
48 # Get submodule info for registered submodules
49 # $@ = path to limit submodule list
53 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
57 # Map submodule path to submodule name
63 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
64 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
65 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
66 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
68 die "No submodule mapping found in .gitmodules for path '$path'"
75 # Prior to calling, cmd_update checks that a possibly existing
76 # path is not a git repository.
77 # Likewise, cmd_add checks that path does not exist at all,
78 # since it is the location of a new submodule.
86 # If there already is a directory at the submodule path,
87 # expect it to be empty (since that is the default checkout
88 # action) and try to remove it.
89 # Note: if $path is a symlink to a directory the test will
90 # succeed but the rmdir will fail. We might want to fix this.
93 rmdir "$path" 2>/dev/null ||
94 die "Directory '$path' exist, but is neither empty nor a git repository"
98 die "A file already exist at path '$path'"
100 if test -n "$reference"
102 git-clone "$reference" -n "$url" "$path"
104 git-clone -n "$url" "$path"
106 die "Clone of '$url' into submodule path '$path' failed"
110 # Add a new submodule to the working tree, .gitmodules and the index
114 # optional branch is stored in global branch variable
118 # parse $args after "submodule ... add".
123 case "$2" in '') usage ;; esac
131 case "$2" in '') usage ;; esac
132 reference="--reference=$2"
156 if test -z "$repo" -o -z "$path"; then
160 # assure repo is absolute or relative to parent
163 # dereference source url relative to parent's url
164 realrepo=$(resolve_relative_url "$repo") || exit
171 die "repo URL: '$repo' must be absolute or begin with ./|../"
176 # multiple //; leading ./; /./; /../; trailing /
177 path=$(printf '%s/\n' "$path" |
187 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
188 die "'$path' already exists in the index"
190 # perhaps the path exists and is already a git repo, else clone it
193 if test -d "$path"/.git -o -f "$path"/.git
195 echo "Adding existing repo at '$path' to the index"
197 die "'$path' already exists and is not a valid git repo"
202 url=$(resolve_relative_url "$repo") || exit
208 git config submodule."$path".url "$url"
211 module_clone "$path" "$realrepo" "$reference" || exit
215 # ash fails to wordsplit ${branch:+-b "$branch"...}
217 '') git checkout -f -q ;;
218 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
220 ) || die "Unable to checkout submodule '$path'"
224 die "Failed to add submodule '$path'"
226 git config -f .gitmodules submodule."$path".path "$path" &&
227 git config -f .gitmodules submodule."$path".url "$repo" &&
228 git add .gitmodules ||
229 die "Failed to register submodule '$path'"
233 # Execute an arbitrary command sequence in each checked out
236 # $@ = command to execute
241 while read mode sha1 stage path
243 if test -e "$path"/.git
245 say "Entering '$path'"
246 (cd "$path" && eval "$@") ||
247 die "Stopping at '$path'; script returned non-zero status."
253 # Register submodules in .git/config
255 # $@ = requested paths (default to all)
259 # parse $args after "submodule ... init".
281 while read mode sha1 stage path
283 # Skip already registered paths
284 name=$(module_name "$path") || exit
285 url=$(git config submodule."$name".url)
286 test -z "$url" || continue
288 url=$(git config -f .gitmodules submodule."$name".url)
290 die "No url found for submodule path '$path' in .gitmodules"
292 # Possibly a url relative to parent
295 url=$(resolve_relative_url "$url") || exit
299 git config submodule."$name".url "$url" ||
300 die "Failed to register url for submodule path '$path'"
302 upd="$(git config -f .gitmodules submodule."$name".update)"
304 git config submodule."$name".update "$upd" ||
305 die "Failed to register update mode for submodule path '$path'"
307 say "Submodule '$name' ($url) registered for path '$path'"
312 # Update each submodule path to correct revision, using clone and checkout as needed
314 # $@ = requested paths (default to all)
318 # parse $args after "submodule ... update".
339 case "$2" in '') usage ;; esac
340 reference="--reference=$2"
366 cmd_init "--" "$@" || return
370 while read mode sha1 stage path
372 name=$(module_name "$path") || exit
373 url=$(git config submodule."$name".url)
374 update_module=$(git config submodule."$name".update)
377 # Only mention uninitialized submodules when its
378 # path have been specified
380 say "Submodule path '$path' not initialized" &&
381 say "Maybe you want to use 'update --init'?"
385 if ! test -d "$path"/.git -o -f "$path"/.git
387 module_clone "$path" "$url" "$reference"|| exit
390 subsha1=$(unset GIT_DIR; cd "$path" &&
391 git rev-parse --verify HEAD) ||
392 die "Unable to find current revision in submodule path '$path'"
395 if ! test -z "$update"
397 update_module=$update
400 if test "$subsha1" != "$sha1"
403 if test -z "$subsha1"
408 if test -z "$nofetch"
410 (unset GIT_DIR; cd "$path" &&
412 die "Unable to fetch in submodule path '$path'"
415 case "$update_module" in
427 command="git checkout $force -q"
433 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
434 die "Unable to $action '$sha1' in submodule path '$path'"
435 say "Submodule path '$path': $msg '$sha1'"
444 git describe "$2" 2>/dev/null ||
445 git describe --tags "$2" 2>/dev/null ||
446 git describe --contains "$2" 2>/dev/null ||
447 git describe --all --always "$2"
450 test -z "$revname" || revname=" ($revname)"
453 # Show commit summary for submodules in index or working tree
455 # If '--cached' is given, show summary between index and given commit,
456 # or between working tree and given commit
458 # $@ = [commit (default 'HEAD'),] requested paths (default all)
464 # parse $args after "submodule ... summary".
475 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
497 test $summary_limit = 0 && return
499 if rev=$(git rev-parse -q --verify "$1^0")
508 # Get modified modules cared by user
509 modules=$(git diff-index $cached --raw $head -- "$@" |
510 egrep '^:([0-7]* )?160000' |
511 while read mod_src mod_dst sha1_src sha1_dst status name
513 # Always show modules deleted or type-changed (blob<->module)
514 test $status = D -o $status = T && echo "$name" && continue
515 # Also show added or modified modules which are checked out
516 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
521 test -z "$modules" && return
523 git diff-index $cached --raw $head -- $modules |
524 egrep '^:([0-7]* )?160000' |
526 while read mod_src mod_dst sha1_src sha1_dst status name
528 if test -z "$cached" &&
529 test $sha1_dst = 0000000000000000000000000000000000000000
533 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
535 100644 | 100755 | 120000)
536 sha1_dst=$(git hash-object $name)
542 echo >&2 "unexpected mode $mod_dst"
549 test $mod_src = 160000 &&
550 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
553 test $mod_dst = 160000 &&
554 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
558 case "$missing_src,$missing_dst" in
560 errmsg=" Warn: $name doesn't contain commit $sha1_src"
563 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
566 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
571 if test $mod_src = 160000 -a $mod_dst = 160000
573 range="$sha1_src...$sha1_dst"
574 elif test $mod_src = 160000
580 GIT_DIR="$name/.git" \
581 git log --pretty=oneline --first-parent $range | wc -l
583 total_commits=" ($(($total_commits + 0)))"
587 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
588 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
591 if test $mod_dst = 160000
593 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
595 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
598 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
602 # Don't give error msg for modification whose dst is not submodule
603 # i.e. deleted or changed to blob
604 test $mod_dst = 160000 && echo "$errmsg"
606 if test $mod_src = 160000 -a $mod_dst = 160000
609 test $summary_limit -gt 0 && limit="-$summary_limit"
610 GIT_DIR="$name/.git" \
611 git log $limit --pretty='format: %m %s' \
612 --first-parent $sha1_src...$sha1_dst
613 elif test $mod_dst = 160000
615 GIT_DIR="$name/.git" \
616 git log --pretty='format: > %s' -1 $sha1_dst
618 GIT_DIR="$name/.git" \
619 git log --pretty='format: < %s' -1 $sha1_src
625 if test -n "$for_status"; then
626 echo "# Modified submodules:"
628 sed -e 's|^|# |' -e 's|^# $|#|'
634 # List all submodules, prefixed with:
635 # - submodule not initialized
636 # + different revision checked out
638 # If --cached was specified the revision in the index will be printed
639 # instead of the currently checked out revision.
641 # $@ = requested paths (default to all)
645 # parse $args after "submodule ... status".
670 while read mode sha1 stage path
672 name=$(module_name "$path") || exit
673 url=$(git config submodule."$name".url)
674 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
679 set_name_rev "$path" "$sha1"
680 if git diff-files --quiet -- "$path"
682 say " $sha1 $path$revname"
686 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
687 set_name_rev "$path" "$sha1"
689 say "+$sha1 $path$revname"
694 # Sync remote urls for submodules
695 # This makes the value for remote.$remote.url match the value
696 # specified in .gitmodules.
721 while read mode sha1 stage path
723 name=$(module_name "$path")
724 url=$(git config -f .gitmodules --get submodule."$name".url)
726 # Possibly a url relative to parent
729 url=$(resolve_relative_url "$url") || exit
733 if test -e "$path"/.git
738 remote=$(get_default_remote)
739 say "Synchronizing submodule url for '$name'"
740 git config remote."$remote".url "$url"
746 # This loop parses the command line arguments to find the
747 # subcommand name to dispatch. Parsing of the subcommand specific
748 # options are primarily done by the subcommand implementations.
749 # Subcommand specific options such as --branch and --cached are
750 # parsed here as well, for backward compatibility.
752 while test $# != 0 && test -z "$command"
755 add | foreach | init | update | status | summary | sync)
785 # No command word defaults to "status"
786 test -n "$command" || command=status
788 # "-b branch" is accepted only by "add"
789 if test -n "$branch" && test "$command" != add
794 # "--cached" is accepted only by "status" and "summary"
795 if test -n "$cached" && test "$command" != status -a "$command" != summary