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 [-i|--init]|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 defined 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\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
77 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
78 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
80 die "No submodule mapping found in .gitmodules for path '$path'"
87 # Prior to calling, cmd_update checks that a possibly existing
88 # path is not a git repository.
89 # Likewise, cmd_add checks that path does not exist at all,
90 # 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 git-clone -n "$url" "$path" ||
112 die "Clone of '$url' into submodule path '$path' failed"
116 # Add a new submodule to the working tree, .gitmodules and the index
120 # optional branch is stored in global branch variable
124 # parse $args after "submodule ... add".
129 case "$2" in '') usage ;; esac
153 if test -z "$repo"; then
157 # Guess path from repo if not specified or strip trailing slashes
158 if test -z "$path"; then
159 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
161 path=$(echo "$path" | sed -e 's|/*$||')
164 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
165 die "'$path' already exists in the index"
167 # perhaps the path exists and is already a git repo, else clone it
170 if test -d "$path"/.git -o -f "$path"/.git
172 echo "Adding existing repo at '$path' to the index"
174 die "'$path' already exists and is not a valid git repo"
179 # dereference source url relative to parent's url
180 realrepo=$(resolve_relative_url "$repo") || exit
183 # Turn the source into an absolute path if
185 if base=$(get_repo_base "$repo"); then
192 module_clone "$path" "$realrepo" || exit
193 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
194 die "Unable to checkout submodule '$path'"
198 die "Failed to add submodule '$path'"
200 git config -f .gitmodules submodule."$path".path "$path" &&
201 git config -f .gitmodules submodule."$path".url "$repo" &&
202 git add .gitmodules ||
203 die "Failed to register submodule '$path'"
207 # Register submodules in .git/config
209 # $@ = requested paths (default to all)
213 # parse $args after "submodule ... init".
234 git ls-files --stage -- "$@" | grep '^160000 ' |
235 while read mode sha1 stage path
237 # Skip already registered paths
238 name=$(module_name "$path") || exit
239 url=$(git config submodule."$name".url)
240 test -z "$url" || continue
242 url=$(git config -f .gitmodules submodule."$name".url)
244 die "No url found for submodule path '$path' in .gitmodules"
246 # Possibly a url relative to parent
249 url=$(resolve_relative_url "$url") || exit
253 git config submodule."$name".url "$url" ||
254 die "Failed to register url for submodule path '$path'"
256 say "Submodule '$name' ($url) registered for path '$path'"
261 # Update each submodule path to correct revision, using clone and checkout as needed
263 # $@ = requested paths (default to all)
267 # parse $args after "submodule ... update".
276 cmd_init "$@" || return
292 git ls-files --stage -- "$@" | grep '^160000 ' |
293 while read mode sha1 stage path
295 name=$(module_name "$path") || exit
296 url=$(git config submodule."$name".url)
299 # Only mention uninitialized submodules when its
300 # path have been specified
302 say "Submodule path '$path' not initialized"
303 say "Maybe you want to use 'update --init'?"
307 if ! test -d "$path"/.git -o -f "$path"/.git
309 module_clone "$path" "$url" || exit
312 subsha1=$(unset GIT_DIR; cd "$path" &&
313 git rev-parse --verify HEAD) ||
314 die "Unable to find current revision in submodule path '$path'"
317 if test "$subsha1" != "$sha1"
319 (unset GIT_DIR; cd "$path" && git-fetch &&
320 git-checkout -q "$sha1") ||
321 die "Unable to checkout '$sha1' in submodule path '$path'"
323 say "Submodule path '$path': checked out '$sha1'"
332 git describe "$2" 2>/dev/null ||
333 git describe --tags "$2" 2>/dev/null ||
334 git describe --contains "$2" 2>/dev/null ||
335 git describe --all --always "$2"
338 test -z "$revname" || revname=" ($revname)"
341 # Show commit summary for submodules in index or working tree
343 # If '--cached' is given, show summary between index and given commit,
344 # or between working tree and given commit
346 # $@ = [commit (default 'HEAD'),] requested paths (default all)
352 # parse $args after "submodule ... summary".
363 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
385 test $summary_limit = 0 && return
387 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
396 # Get modified modules cared by user
397 modules=$(git diff-index $cached --raw $head -- "$@" |
398 grep -e '^:160000' -e '^:[0-7]* 160000' |
399 while read mod_src mod_dst sha1_src sha1_dst status name
401 # Always show modules deleted or type-changed (blob<->module)
402 test $status = D -o $status = T && echo "$name" && continue
403 # Also show added or modified modules which are checked out
404 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
409 test -z "$modules" && return
411 git diff-index $cached --raw $head -- $modules |
412 grep -e '^:160000' -e '^:[0-7]* 160000' |
414 while read mod_src mod_dst sha1_src sha1_dst status name
416 if test -z "$cached" &&
417 test $sha1_dst = 0000000000000000000000000000000000000000
421 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
423 100644 | 100755 | 120000)
424 sha1_dst=$(git hash-object $name)
430 echo >&2 "unexpected mode $mod_dst"
437 test $mod_src = 160000 &&
438 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
441 test $mod_dst = 160000 &&
442 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
446 case "$missing_src,$missing_dst" in
448 errmsg=" Warn: $name doesn't contain commit $sha1_src"
451 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
454 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
459 if test $mod_src = 160000 -a $mod_dst = 160000
461 range="$sha1_src...$sha1_dst"
462 elif test $mod_src = 160000
468 GIT_DIR="$name/.git" \
469 git log --pretty=oneline --first-parent $range | wc -l
471 total_commits=" ($(($total_commits + 0)))"
475 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
476 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
479 if test $mod_dst = 160000
481 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
483 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
486 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
490 # Don't give error msg for modification whose dst is not submodule
491 # i.e. deleted or changed to blob
492 test $mod_dst = 160000 && echo "$errmsg"
494 if test $mod_src = 160000 -a $mod_dst = 160000
497 test $summary_limit -gt 0 && limit="-$summary_limit"
498 GIT_DIR="$name/.git" \
499 git log $limit --pretty='format: %m %s' \
500 --first-parent $sha1_src...$sha1_dst
501 elif test $mod_dst = 160000
503 GIT_DIR="$name/.git" \
504 git log --pretty='format: > %s' -1 $sha1_dst
506 GIT_DIR="$name/.git" \
507 git log --pretty='format: < %s' -1 $sha1_src
513 if test -n "$for_status"; then
514 echo "# Modified submodules:"
516 sed -e 's|^|# |' -e 's|^# $|#|'
522 # List all submodules, prefixed with:
523 # - submodule not initialized
524 # + different revision checked out
526 # If --cached was specified the revision in the index will be printed
527 # instead of the currently checked out revision.
529 # $@ = requested paths (default to all)
533 # parse $args after "submodule ... status".
557 git ls-files --stage -- "$@" | grep '^160000 ' |
558 while read mode sha1 stage path
560 name=$(module_name "$path") || exit
561 url=$(git config submodule."$name".url)
562 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
567 set_name_rev "$path" "$sha1"
568 if git diff-files --quiet -- "$path"
570 say " $sha1 $path$revname"
574 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
575 set_name_rev "$path" "$sha1"
577 say "+$sha1 $path$revname"
582 # This loop parses the command line arguments to find the
583 # subcommand name to dispatch. Parsing of the subcommand specific
584 # options are primarily done by the subcommand implementations.
585 # Subcommand specific options such as --branch and --cached are
586 # parsed here as well, for backward compatibility.
588 while test $# != 0 && test -z "$command"
591 add | init | update | status | summary)
621 # No command word defaults to "status"
622 test -n "$command" || command=status
624 # "-b branch" is accepted only by "add"
625 if test -n "$branch" && test "$command" != add
630 # "--cached" is accepted only by "status" and "summary"
631 if test -n "$cached" && test "$command" != status -a "$command" != summary