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] <path>]|[status|init|update [-i|--init]|summary [-n|--summary-limit <n>] [<commit>]] \
20 # print stuff on stdout unless -q was specified
30 # Resolve relative url by appending to parent's url
31 resolve_relative_url ()
33 branch="$(git symbolic-ref HEAD 2>/dev/null)"
34 remote="$(git config branch.${branch#refs/heads/}.remote)"
35 remote="${remote:-origin}"
36 remoteurl=$(git config "remote.$remote.url") ||
37 die "remote ($remote) does not have a url defined in .git/config"
44 remoteurl="${remoteurl%/*}"
53 echo "$remoteurl/$url"
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.
85 # If there already is a directory at the submodule path,
86 # expect it to be empty (since that is the default checkout
87 # action) and try to remove it.
88 # Note: if $path is a symlink to a directory the test will
89 # succeed but the rmdir will fail. We might want to fix this.
92 rmdir "$path" 2>/dev/null ||
93 die "Directory '$path' exist, but is neither empty nor a git repository"
97 die "A file already exist at path '$path'"
99 git-clone -n "$url" "$path" ||
100 die "Clone of '$url' into submodule path '$path' failed"
104 # Add a new submodule to the working tree, .gitmodules and the index
108 # optional branch is stored in global branch variable
112 # parse $args after "submodule ... add".
117 case "$2" in '') usage ;; esac
141 if test -z "$repo" -o -z "$path"; then
145 # assure repo is absolute or relative to parent
148 # dereference source url relative to parent's url
149 realrepo=$(resolve_relative_url "$repo") || exit
156 die "repo URL: '$repo' must be absolute or begin with ./|../"
160 # strip trailing slashes from path
161 path=$(echo "$path" | sed -e 's|/*$||')
163 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
164 die "'$path' already exists in the index"
166 # perhaps the path exists and is already a git repo, else clone it
169 if test -d "$path"/.git -o -f "$path"/.git
171 echo "Adding existing repo at '$path' to the index"
173 die "'$path' already exists and is not a valid git repo"
177 module_clone "$path" "$realrepo" || exit
178 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
179 die "Unable to checkout submodule '$path'"
183 die "Failed to add submodule '$path'"
185 git config -f .gitmodules submodule."$path".path "$path" &&
186 git config -f .gitmodules submodule."$path".url "$repo" &&
187 git add .gitmodules ||
188 die "Failed to register submodule '$path'"
192 # Register submodules in .git/config
194 # $@ = requested paths (default to all)
198 # parse $args after "submodule ... init".
219 git ls-files --stage -- "$@" | grep '^160000 ' |
220 while read mode sha1 stage path
222 # Skip already registered paths
223 name=$(module_name "$path") || exit
224 url=$(git config submodule."$name".url)
225 test -z "$url" || continue
227 url=$(git config -f .gitmodules submodule."$name".url)
229 die "No url found for submodule path '$path' in .gitmodules"
231 # Possibly a url relative to parent
234 url=$(resolve_relative_url "$url") || exit
238 git config submodule."$name".url "$url" ||
239 die "Failed to register url for submodule path '$path'"
241 say "Submodule '$name' ($url) registered for path '$path'"
246 # Update each submodule path to correct revision, using clone and checkout as needed
248 # $@ = requested paths (default to all)
252 # parse $args after "submodule ... update".
261 cmd_init "$@" || return
277 git ls-files --stage -- "$@" | grep '^160000 ' |
278 while read mode sha1 stage path
280 name=$(module_name "$path") || exit
281 url=$(git config submodule."$name".url)
284 # Only mention uninitialized submodules when its
285 # path have been specified
287 say "Submodule path '$path' not initialized"
288 say "Maybe you want to use 'update --init'?"
292 if ! test -d "$path"/.git -o -f "$path"/.git
294 module_clone "$path" "$url" || exit
297 subsha1=$(unset GIT_DIR; cd "$path" &&
298 git rev-parse --verify HEAD) ||
299 die "Unable to find current revision in submodule path '$path'"
302 if test "$subsha1" != "$sha1"
304 (unset GIT_DIR; cd "$path" && git-fetch &&
305 git-checkout -q "$sha1") ||
306 die "Unable to checkout '$sha1' in submodule path '$path'"
308 say "Submodule path '$path': checked out '$sha1'"
317 git describe "$2" 2>/dev/null ||
318 git describe --tags "$2" 2>/dev/null ||
319 git describe --contains "$2" 2>/dev/null ||
320 git describe --all --always "$2"
323 test -z "$revname" || revname=" ($revname)"
326 # Show commit summary for submodules in index or working tree
328 # If '--cached' is given, show summary between index and given commit,
329 # or between working tree and given commit
331 # $@ = [commit (default 'HEAD'),] requested paths (default all)
337 # parse $args after "submodule ... summary".
348 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
370 test $summary_limit = 0 && return
372 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
381 # Get modified modules cared by user
382 modules=$(git diff-index $cached --raw $head -- "$@" |
383 grep -e '^:160000' -e '^:[0-7]* 160000' |
384 while read mod_src mod_dst sha1_src sha1_dst status name
386 # Always show modules deleted or type-changed (blob<->module)
387 test $status = D -o $status = T && echo "$name" && continue
388 # Also show added or modified modules which are checked out
389 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
394 test -z "$modules" && return
396 git diff-index $cached --raw $head -- $modules |
397 grep -e '^:160000' -e '^:[0-7]* 160000' |
399 while read mod_src mod_dst sha1_src sha1_dst status name
401 if test -z "$cached" &&
402 test $sha1_dst = 0000000000000000000000000000000000000000
406 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
408 100644 | 100755 | 120000)
409 sha1_dst=$(git hash-object $name)
415 echo >&2 "unexpected mode $mod_dst"
422 test $mod_src = 160000 &&
423 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
426 test $mod_dst = 160000 &&
427 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
431 case "$missing_src,$missing_dst" in
433 errmsg=" Warn: $name doesn't contain commit $sha1_src"
436 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
439 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
444 if test $mod_src = 160000 -a $mod_dst = 160000
446 range="$sha1_src...$sha1_dst"
447 elif test $mod_src = 160000
453 GIT_DIR="$name/.git" \
454 git log --pretty=oneline --first-parent $range | wc -l
456 total_commits=" ($(($total_commits + 0)))"
460 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
461 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
464 if test $mod_dst = 160000
466 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
468 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
471 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
475 # Don't give error msg for modification whose dst is not submodule
476 # i.e. deleted or changed to blob
477 test $mod_dst = 160000 && echo "$errmsg"
479 if test $mod_src = 160000 -a $mod_dst = 160000
482 test $summary_limit -gt 0 && limit="-$summary_limit"
483 GIT_DIR="$name/.git" \
484 git log $limit --pretty='format: %m %s' \
485 --first-parent $sha1_src...$sha1_dst
486 elif test $mod_dst = 160000
488 GIT_DIR="$name/.git" \
489 git log --pretty='format: > %s' -1 $sha1_dst
491 GIT_DIR="$name/.git" \
492 git log --pretty='format: < %s' -1 $sha1_src
498 if test -n "$for_status"; then
499 echo "# Modified submodules:"
501 sed -e 's|^|# |' -e 's|^# $|#|'
507 # List all submodules, prefixed with:
508 # - submodule not initialized
509 # + different revision checked out
511 # If --cached was specified the revision in the index will be printed
512 # instead of the currently checked out revision.
514 # $@ = requested paths (default to all)
518 # parse $args after "submodule ... status".
542 git ls-files --stage -- "$@" | grep '^160000 ' |
543 while read mode sha1 stage path
545 name=$(module_name "$path") || exit
546 url=$(git config submodule."$name".url)
547 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
552 set_name_rev "$path" "$sha1"
553 if git diff-files --quiet -- "$path"
555 say " $sha1 $path$revname"
559 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
560 set_name_rev "$path" "$sha1"
562 say "+$sha1 $path$revname"
567 # This loop parses the command line arguments to find the
568 # subcommand name to dispatch. Parsing of the subcommand specific
569 # options are primarily done by the subcommand implementations.
570 # Subcommand specific options such as --branch and --cached are
571 # parsed here as well, for backward compatibility.
573 while test $# != 0 && test -z "$command"
576 add | init | update | status | summary)
606 # No command word defaults to "status"
607 test -n "$command" || command=status
609 # "-b branch" is accepted only by "add"
610 if test -n "$branch" && test "$command" != add
615 # "--cached" is accepted only by "status" and "summary"
616 if test -n "$cached" && test "$command" != status -a "$command" != summary