3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE="[--quiet] [--cached|--files] \
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>...]]"
23 # Resolve relative url by appending to parent's url
24 resolve_relative_url ()
26 remote=$(get_default_remote)
27 remoteurl=$(git config "remote.$remote.url") ||
28 die "remote ($remote) does not have a url defined in .git/config"
30 remoteurl=${remoteurl%/}
36 remoteurl="${remoteurl%/*}"
45 echo "$remoteurl/${url%/}"
49 # Get submodule info for registered submodules
50 # $@ = path to limit submodule list
54 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
58 # Map submodule path to submodule name
64 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
65 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
66 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
67 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
69 die "No submodule mapping found in .gitmodules for path '$path'"
76 # Prior to calling, cmd_update checks that a possibly existing
77 # path is not a git repository.
78 # Likewise, cmd_add checks that path does not exist at all,
79 # since it is the location of a new submodule.
87 # If there already is a directory at the submodule path,
88 # expect it to be empty (since that is the default checkout
89 # action) and try to remove it.
90 # Note: if $path is a symlink to a directory the test will
91 # succeed but the rmdir will fail. We might want to fix this.
94 rmdir "$path" 2>/dev/null ||
95 die "Directory '$path' exist, but is neither empty nor a git repository"
99 die "A file already exist at path '$path'"
101 if test -n "$reference"
103 git-clone "$reference" -n "$url" "$path"
105 git-clone -n "$url" "$path"
107 die "Clone of '$url' into submodule path '$path' failed"
111 # Add a new submodule to the working tree, .gitmodules and the index
115 # optional branch is stored in global branch variable
119 # parse $args after "submodule ... add".
124 case "$2" in '') usage ;; esac
132 case "$2" in '') usage ;; esac
133 reference="--reference=$2"
157 if test -z "$repo" -o -z "$path"; then
161 # assure repo is absolute or relative to parent
164 # dereference source url relative to parent's url
165 realrepo=$(resolve_relative_url "$repo") || exit
172 die "repo URL: '$repo' must be absolute or begin with ./|../"
177 # multiple //; leading ./; /./; /../; trailing /
178 path=$(printf '%s/\n' "$path" |
188 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
189 die "'$path' already exists in the index"
191 # perhaps the path exists and is already a git repo, else clone it
194 if test -d "$path"/.git -o -f "$path"/.git
196 echo "Adding existing repo at '$path' to the index"
198 die "'$path' already exists and is not a valid git repo"
203 url=$(resolve_relative_url "$repo") || exit
209 git config submodule."$path".url "$url"
212 module_clone "$path" "$realrepo" "$reference" || exit
216 # ash fails to wordsplit ${branch:+-b "$branch"...}
218 '') git checkout -f -q ;;
219 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
221 ) || die "Unable to checkout submodule '$path'"
225 die "Failed to add submodule '$path'"
227 git config -f .gitmodules submodule."$path".path "$path" &&
228 git config -f .gitmodules submodule."$path".url "$repo" &&
229 git add .gitmodules ||
230 die "Failed to register submodule '$path'"
234 # Execute an arbitrary command sequence in each checked out
237 # $@ = command to execute
242 while read mode sha1 stage path
244 if test -e "$path"/.git
246 say "Entering '$path'"
247 (cd "$path" && eval "$@") ||
248 die "Stopping at '$path'; script returned non-zero status."
254 # Register submodules in .git/config
256 # $@ = requested paths (default to all)
260 # parse $args after "submodule ... init".
282 while read mode sha1 stage path
284 # Skip already registered paths
285 name=$(module_name "$path") || exit
286 url=$(git config submodule."$name".url)
287 test -z "$url" || continue
289 url=$(git config -f .gitmodules submodule."$name".url)
291 die "No url found for submodule path '$path' in .gitmodules"
293 # Possibly a url relative to parent
296 url=$(resolve_relative_url "$url") || exit
300 git config submodule."$name".url "$url" ||
301 die "Failed to register url for submodule path '$path'"
303 upd="$(git config -f .gitmodules submodule."$name".update)"
305 git config submodule."$name".update "$upd" ||
306 die "Failed to register update mode for submodule path '$path'"
308 say "Submodule '$name' ($url) registered for path '$path'"
313 # Update each submodule path to correct revision, using clone and checkout as needed
315 # $@ = requested paths (default to all)
319 # parse $args after "submodule ... update".
340 case "$2" in '') usage ;; esac
341 reference="--reference=$2"
367 cmd_init "--" "$@" || return
371 while read mode sha1 stage path
373 name=$(module_name "$path") || exit
374 url=$(git config submodule."$name".url)
375 update_module=$(git config submodule."$name".update)
378 # Only mention uninitialized submodules when its
379 # path have been specified
381 say "Submodule path '$path' not initialized" &&
382 say "Maybe you want to use 'update --init'?"
386 if ! test -d "$path"/.git -o -f "$path"/.git
388 module_clone "$path" "$url" "$reference"|| exit
391 subsha1=$(unset GIT_DIR; cd "$path" &&
392 git rev-parse --verify HEAD) ||
393 die "Unable to find current revision in submodule path '$path'"
396 if ! test -z "$update"
398 update_module=$update
401 if test "$subsha1" != "$sha1"
404 if test -z "$subsha1"
409 if test -z "$nofetch"
411 (unset GIT_DIR; cd "$path" &&
413 die "Unable to fetch in submodule path '$path'"
416 case "$update_module" in
428 command="git checkout $force -q"
434 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
435 die "Unable to $action '$sha1' in submodule path '$path'"
436 say "Submodule path '$path': $msg '$sha1'"
445 git describe "$2" 2>/dev/null ||
446 git describe --tags "$2" 2>/dev/null ||
447 git describe --contains "$2" 2>/dev/null ||
448 git describe --all --always "$2"
451 test -z "$revname" || revname=" ($revname)"
454 # Show commit summary for submodules in index or working tree
456 # If '--cached' is given, show summary between index and given commit,
457 # or between working tree and given commit
459 # $@ = [commit (default 'HEAD'),] requested paths (default all)
466 # parse $args after "submodule ... summary".
480 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
502 test $summary_limit = 0 && return
504 if rev=$(git rev-parse -q --verify "$1^0")
515 die "--cached cannot be used with --files"
521 # Get modified modules cared by user
522 modules=$(git $diff_cmd $cached --raw $head -- "$@" |
523 egrep '^:([0-7]* )?160000' |
524 while read mod_src mod_dst sha1_src sha1_dst status name
526 # Always show modules deleted or type-changed (blob<->module)
527 test $status = D -o $status = T && echo "$name" && continue
528 # Also show added or modified modules which are checked out
529 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
534 test -z "$modules" && return
536 git $diff_cmd $cached --raw $head -- $modules |
537 egrep '^:([0-7]* )?160000' |
539 while read mod_src mod_dst sha1_src sha1_dst status name
541 if test -z "$cached" &&
542 test $sha1_dst = 0000000000000000000000000000000000000000
546 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
548 100644 | 100755 | 120000)
549 sha1_dst=$(git hash-object $name)
555 echo >&2 "unexpected mode $mod_dst"
562 test $mod_src = 160000 &&
563 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
566 test $mod_dst = 160000 &&
567 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
571 case "$missing_src,$missing_dst" in
573 errmsg=" Warn: $name doesn't contain commit $sha1_src"
576 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
579 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
584 if test $mod_src = 160000 -a $mod_dst = 160000
586 range="$sha1_src...$sha1_dst"
587 elif test $mod_src = 160000
593 GIT_DIR="$name/.git" \
594 git log --pretty=oneline --first-parent $range | wc -l
596 total_commits=" ($(($total_commits + 0)))"
600 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
601 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
604 if test $mod_dst = 160000
606 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
608 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
611 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
615 # Don't give error msg for modification whose dst is not submodule
616 # i.e. deleted or changed to blob
617 test $mod_dst = 160000 && echo "$errmsg"
619 if test $mod_src = 160000 -a $mod_dst = 160000
622 test $summary_limit -gt 0 && limit="-$summary_limit"
623 GIT_DIR="$name/.git" \
624 git log $limit --pretty='format: %m %s' \
625 --first-parent $sha1_src...$sha1_dst
626 elif test $mod_dst = 160000
628 GIT_DIR="$name/.git" \
629 git log --pretty='format: > %s' -1 $sha1_dst
631 GIT_DIR="$name/.git" \
632 git log --pretty='format: < %s' -1 $sha1_src
638 if test -n "$for_status"; then
639 echo "# Modified submodules:"
641 sed -e 's|^|# |' -e 's|^# $|#|'
647 # List all submodules, prefixed with:
648 # - submodule not initialized
649 # + different revision checked out
651 # If --cached was specified the revision in the index will be printed
652 # instead of the currently checked out revision.
654 # $@ = requested paths (default to all)
658 # parse $args after "submodule ... status".
683 while read mode sha1 stage path
685 name=$(module_name "$path") || exit
686 url=$(git config submodule."$name".url)
687 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
692 set_name_rev "$path" "$sha1"
693 if git diff-files --quiet -- "$path"
695 say " $sha1 $path$revname"
699 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
700 set_name_rev "$path" "$sha1"
702 say "+$sha1 $path$revname"
707 # Sync remote urls for submodules
708 # This makes the value for remote.$remote.url match the value
709 # specified in .gitmodules.
734 while read mode sha1 stage path
736 name=$(module_name "$path")
737 url=$(git config -f .gitmodules --get submodule."$name".url)
739 # Possibly a url relative to parent
742 url=$(resolve_relative_url "$url") || exit
746 if test -e "$path"/.git
751 remote=$(get_default_remote)
752 say "Synchronizing submodule url for '$name'"
753 git config remote."$remote".url "$url"
759 # This loop parses the command line arguments to find the
760 # subcommand name to dispatch. Parsing of the subcommand specific
761 # options are primarily done by the subcommand implementations.
762 # Subcommand specific options such as --branch and --cached are
763 # parsed here as well, for backward compatibility.
765 while test $# != 0 && test -z "$command"
768 add | foreach | init | update | status | summary | sync)
798 # No command word defaults to "status"
799 test -n "$command" || command=status
801 # "-b branch" is accepted only by "add"
802 if test -n "$branch" && test "$command" != add
807 # "--cached" is accepted only by "status" and "summary"
808 if test -n "$cached" && test "$command" != status -a "$command" != summary