3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE="[--quiet] [--cached] \
8 [add <repo> [-b branch]|status|init|update|summary [-n|--summary-limit <n>] [<commit>]] \
20 # print stuff on stdout unless -q was specified
30 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
34 cd "$1" || cd "$1.git" &&
42 # Resolve relative url by appending to parent's url
43 resolve_relative_url ()
45 branch="$(git symbolic-ref HEAD 2>/dev/null)"
46 remote="$(git config branch.${branch#refs/heads/}.remote)"
47 remote="${remote:-origin}"
48 remoteurl="$(git config remote.$remote.url)" ||
49 die "remote ($remote) does not have a url in .git/config"
56 remoteurl="${remoteurl%/*}"
65 echo "$remoteurl/$url"
69 # Map submodule path to submodule name
75 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
76 re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
77 name=$( GIT_CONFIG=.gitmodules \
78 git config --get-regexp '^submodule\..*\.path$' |
79 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
81 die "No submodule mapping found in .gitmodules for path '$path'"
88 # Prior to calling, cmd_update checks that a possibly existing
89 # path is not a git repository.
90 # Likewise, cmd_add checks that path does not exist at all,
91 # 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 git-clone -n "$url" "$path" ||
113 die "Clone of '$url' into submodule path '$path' failed"
117 # Add a new submodule to the working tree, .gitmodules and the index
121 # optional branch is stored in global branch variable
125 # parse $args after "submodule ... add".
130 case "$2" in '') usage ;; esac
154 if test -z "$repo"; then
158 # Guess path from repo if not specified or strip trailing slashes
159 if test -z "$path"; then
160 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
162 path=$(echo "$path" | sed -e 's|/*$||')
165 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
166 die "'$path' already exists in the index"
168 # perhaps the path exists and is already a git repo, else clone it
171 if test -d "$path/.git" &&
172 test "$(unset GIT_DIR; cd $path; git rev-parse --git-dir)" = ".git"
174 echo "Adding existing repo at '$path' to the index"
176 die "'$path' already exists and is not a valid git repo"
181 # dereference source url relative to parent's url
182 realrepo="$(resolve_relative_url $repo)" ;;
184 # Turn the source into an absolute path if
186 if base=$(get_repo_base "$repo"); then
193 module_clone "$path" "$realrepo" || exit
194 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
195 die "Unable to checkout submodule '$path'"
199 die "Failed to add submodule '$path'"
201 GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
202 GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
203 git add .gitmodules ||
204 die "Failed to register submodule '$path'"
208 # Register submodules in .git/config
210 # $@ = requested paths (default to all)
214 # parse $args after "submodule ... init".
235 git ls-files --stage -- "$@" | grep '^160000 ' |
236 while read mode sha1 stage path
238 # Skip already registered paths
239 name=$(module_name "$path") || exit
240 url=$(git config submodule."$name".url)
241 test -z "$url" || continue
243 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
245 die "No url found for submodule path '$path' in .gitmodules"
247 # Possibly a url relative to parent
250 url="$(resolve_relative_url "$url")"
254 git config submodule."$name".url "$url" ||
255 die "Failed to register url for submodule path '$path'"
257 say "Submodule '$name' ($url) registered for path '$path'"
262 # Update each submodule path to correct revision, using clone and checkout as needed
264 # $@ = requested paths (default to all)
268 # parse $args after "submodule ... update".
289 git ls-files --stage -- "$@" | grep '^160000 ' |
290 while read mode sha1 stage path
292 name=$(module_name "$path") || exit
293 url=$(git config submodule."$name".url)
296 # Only mention uninitialized submodules when its
297 # path have been specified
299 say "Submodule path '$path' not initialized"
303 if ! test -d "$path"/.git
305 module_clone "$path" "$url" || exit
308 subsha1=$(unset GIT_DIR; cd "$path" &&
309 git rev-parse --verify HEAD) ||
310 die "Unable to find current revision in submodule path '$path'"
313 if test "$subsha1" != "$sha1"
315 (unset GIT_DIR; cd "$path" && git-fetch &&
316 git-checkout -q "$sha1") ||
317 die "Unable to checkout '$sha1' in submodule path '$path'"
319 say "Submodule path '$path': checked out '$sha1'"
328 git describe "$2" 2>/dev/null ||
329 git describe --tags "$2" 2>/dev/null ||
330 git describe --contains "$2" 2>/dev/null ||
331 git describe --all --always "$2"
334 test -z "$revname" || revname=" ($revname)"
337 # Show commit summary for submodules in index or working tree
339 # If '--cached' is given, show summary between index and given commit,
340 # or between working tree and given commit
342 # $@ = [commit (default 'HEAD'),] requested paths (default all)
348 # parse $args after "submodule ... summary".
359 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
381 test $summary_limit = 0 && return
383 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
392 # Get modified modules cared by user
393 modules=$(git diff-index $cached --raw $head -- "$@" |
394 grep -e '^:160000' -e '^:[0-7]* 160000' |
395 while read mod_src mod_dst sha1_src sha1_dst status name
397 # Always show modules deleted or type-changed (blob<->module)
398 test $status = D -o $status = T && echo "$name" && continue
399 # Also show added or modified modules which are checked out
400 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
405 test -z "$modules" && return
407 git diff-index $cached --raw $head -- $modules |
408 grep -e '^:160000' -e '^:[0-7]* 160000' |
410 while read mod_src mod_dst sha1_src sha1_dst status name
412 if test -z "$cached" &&
413 test $sha1_dst = 0000000000000000000000000000000000000000
417 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
419 100644 | 100755 | 120000)
420 sha1_dst=$(git hash-object $name)
426 echo >&2 "unexpected mode $mod_dst"
433 test $mod_src = 160000 &&
434 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
437 test $mod_dst = 160000 &&
438 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
442 case "$missing_src,$missing_dst" in
444 errmsg=" Warn: $name doesn't contain commit $sha1_src"
447 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
450 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
455 if test $mod_src = 160000 -a $mod_dst = 160000
457 range="$sha1_src...$sha1_dst"
458 elif test $mod_src = 160000
464 GIT_DIR="$name/.git" \
465 git log --pretty=oneline --first-parent $range | wc -l
467 total_commits=" ($(($total_commits + 0)))"
471 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
472 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
475 if test $mod_dst = 160000
477 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
479 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
482 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
486 # Don't give error msg for modification whose dst is not submodule
487 # i.e. deleted or changed to blob
488 test $mod_dst = 160000 && echo "$errmsg"
490 if test $mod_src = 160000 -a $mod_dst = 160000
493 test $summary_limit -gt 0 && limit="-$summary_limit"
494 GIT_DIR="$name/.git" \
495 git log $limit --pretty='format: %m %s' \
496 --first-parent $sha1_src...$sha1_dst
497 elif test $mod_dst = 160000
499 GIT_DIR="$name/.git" \
500 git log --pretty='format: > %s' -1 $sha1_dst
502 GIT_DIR="$name/.git" \
503 git log --pretty='format: < %s' -1 $sha1_src
509 if test -n "$for_status"; then
510 echo "# Modified submodules:"
512 sed -e 's|^|# |' -e 's|^# $|#|'
518 # List all submodules, prefixed with:
519 # - submodule not initialized
520 # + different revision checked out
522 # If --cached was specified the revision in the index will be printed
523 # instead of the currently checked out revision.
525 # $@ = requested paths (default to all)
529 # parse $args after "submodule ... status".
553 git ls-files --stage -- "$@" | grep '^160000 ' |
554 while read mode sha1 stage path
556 name=$(module_name "$path") || exit
557 url=$(git config submodule."$name".url)
558 if test -z "$url" || ! test -d "$path"/.git
563 set_name_rev "$path" "$sha1"
564 if git diff-files --quiet -- "$path"
566 say " $sha1 $path$revname"
570 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
571 set_name_rev "$path" "$sha1"
573 say "+$sha1 $path$revname"
578 # This loop parses the command line arguments to find the
579 # subcommand name to dispatch. Parsing of the subcommand specific
580 # options are primarily done by the subcommand implementations.
581 # Subcommand specific options such as --branch and --cached are
582 # parsed here as well, for backward compatibility.
584 while test $# != 0 && test -z "$command"
587 add | init | update | status | summary)
617 # No command word defaults to "status"
618 test -n "$command" || command=status
620 # "-b branch" is accepted only by "add"
621 if test -n "$branch" && test "$command" != add
626 # "--cached" is accepted only by "status" and "summary"
627 if test -n "$cached" && test "$command" != status -a "$command" != summary