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
160 # dereference source url relative to parent's url
161 realrepo="$(resolve_relative_url $repo)" ;;
163 # Turn the source into an absolute path if
165 if base=$(get_repo_base "$repo"); then
172 # Guess path from repo if not specified or strip trailing slashes
173 if test -z "$path"; then
174 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
176 path=$(echo "$path" | sed -e 's|/*$||')
180 die "'$path' already exists"
182 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
183 die "'$path' already exists in the index"
185 module_clone "$path" "$realrepo" || exit
186 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
187 die "Unable to checkout submodule '$path'"
189 die "Failed to add submodule '$path'"
191 GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
192 GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
193 git add .gitmodules ||
194 die "Failed to register submodule '$path'"
198 # Register submodules in .git/config
200 # $@ = requested paths (default to all)
204 # parse $args after "submodule ... init".
225 git ls-files --stage -- "$@" | grep -e '^160000 ' |
226 while read mode sha1 stage path
228 # Skip already registered paths
229 name=$(module_name "$path") || exit
230 url=$(git config submodule."$name".url)
231 test -z "$url" || continue
233 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
235 die "No url found for submodule path '$path' in .gitmodules"
237 # Possibly a url relative to parent
240 url="$(resolve_relative_url "$url")"
244 git config submodule."$name".url "$url" ||
245 die "Failed to register url for submodule path '$path'"
247 say "Submodule '$name' ($url) registered for path '$path'"
252 # Update each submodule path to correct revision, using clone and checkout as needed
254 # $@ = requested paths (default to all)
258 # parse $args after "submodule ... update".
279 git ls-files --stage -- "$@" | grep -e '^160000 ' |
280 while read mode sha1 stage path
282 name=$(module_name "$path") || exit
283 url=$(git config submodule."$name".url)
286 # Only mention uninitialized submodules when its
287 # path have been specified
289 say "Submodule path '$path' not initialized"
293 if ! test -d "$path"/.git
295 module_clone "$path" "$url" || exit
298 subsha1=$(unset GIT_DIR; cd "$path" &&
299 git rev-parse --verify HEAD) ||
300 die "Unable to find current revision in submodule path '$path'"
303 if test "$subsha1" != "$sha1"
305 (unset GIT_DIR; cd "$path" && git-fetch &&
306 git-checkout -q "$sha1") ||
307 die "Unable to checkout '$sha1' in submodule path '$path'"
309 say "Submodule path '$path': checked out '$sha1'"
318 git describe "$2" 2>/dev/null ||
319 git describe --tags "$2" 2>/dev/null ||
320 git describe --contains --tags "$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)
336 # parse $args after "submodule ... summary".
344 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
366 test $summary_limit = 0 && return
368 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
377 # Get modified modules cared by user
378 modules=$(git diff-index $cached --raw $head -- "$@" |
379 grep -e '^:160000' -e '^:[0-7]* 160000' |
380 while read mod_src mod_dst sha1_src sha1_dst status name
382 # Always show modules deleted or type-changed (blob<->module)
383 test $status = D -o $status = T && echo "$name" && continue
384 # Also show added or modified modules which are checked out
385 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
390 test -n "$modules" &&
391 git diff-index $cached --raw $head -- $modules |
392 grep -e '^:160000' -e '^:[0-7]* 160000' |
394 while read mod_src mod_dst sha1_src sha1_dst status name
396 if test -z "$cached" &&
397 test $sha1_dst = 0000000000000000000000000000000000000000
401 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
403 100644 | 100755 | 120000)
404 sha1_dst=$(git hash-object $name)
410 echo >&2 "unexpected mode $mod_dst"
417 test $mod_src = 160000 &&
418 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
421 test $mod_dst = 160000 &&
422 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
426 case "$missing_src,$missing_dst" in
428 errmsg=" Warn: $name doesn't contain commit $sha1_src"
431 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
434 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
439 if test $mod_src = 160000 -a $mod_dst = 160000
441 range="$sha1_src...$sha1_dst"
442 elif test $mod_src = 160000
448 GIT_DIR="$name/.git" \
449 git log --pretty=oneline --first-parent $range | wc -l
451 total_commits=" ($(($total_commits + 0)))"
455 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
456 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
459 if test $mod_dst = 160000
461 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
463 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
466 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
470 # Don't give error msg for modification whose dst is not submodule
471 # i.e. deleted or changed to blob
472 test $mod_dst = 160000 && echo "$errmsg"
474 if test $mod_src = 160000 -a $mod_dst = 160000
477 test $summary_limit -gt 0 && limit="-$summary_limit"
478 GIT_DIR="$name/.git" \
479 git log $limit --pretty='format: %m %s' \
480 --first-parent $sha1_src...$sha1_dst
481 elif test $mod_dst = 160000
483 GIT_DIR="$name/.git" \
484 git log --pretty='format: > %s' -1 $sha1_dst
486 GIT_DIR="$name/.git" \
487 git log --pretty='format: < %s' -1 $sha1_src
495 # List all submodules, prefixed with:
496 # - submodule not initialized
497 # + different revision checked out
499 # If --cached was specified the revision in the index will be printed
500 # instead of the currently checked out revision.
502 # $@ = requested paths (default to all)
506 # parse $args after "submodule ... status".
530 git ls-files --stage -- "$@" | grep -e '^160000 ' |
531 while read mode sha1 stage path
533 name=$(module_name "$path") || exit
534 url=$(git config submodule."$name".url)
535 if test -z "$url" || ! test -d "$path"/.git
540 set_name_rev "$path" "$sha1"
541 if git diff-files --quiet -- "$path"
543 say " $sha1 $path$revname"
547 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
548 set_name_rev "$path" "$sha1"
550 say "+$sha1 $path$revname"
555 # This loop parses the command line arguments to find the
556 # subcommand name to dispatch. Parsing of the subcommand specific
557 # options are primarily done by the subcommand implementations.
558 # Subcommand specific options such as --branch and --cached are
559 # parsed here as well, for backward compatibility.
561 while test $# != 0 && test -z "$command"
564 add | init | update | status | summary)
594 # No command word defaults to "status"
595 test -n "$command" || command=status
597 # "-b branch" is accepted only by "add"
598 if test -n "$branch" && test "$command" != add
603 # "--cached" is accepted only by "status" and "summary"
604 if test -n "$cached" && test "$command" != status -a "$command" != summary