git submodule: Fix adding of submodules at paths with ./, .. and //
[git/spearce.git] / git-submodule.sh
blob68d6afd15c94844c2435b65a197d678aa52314c9
1 #!/bin/sh
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>]] \
9 [--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
10 OPTIONS_SPEC=
11 . git-sh-setup
12 . git-parse-remote
13 require_work_tree
15 command=
16 branch=
17 quiet=
18 cached=
21 # print stuff on stdout unless -q was specified
23 say()
25 if test -z "$quiet"
26 then
27 echo "$@"
31 # Resolve relative url by appending to parent's url
32 resolve_relative_url ()
34 remote=$(get_default_remote)
35 remoteurl=$(git config "remote.$remote.url") ||
36 die "remote ($remote) does not have a url defined in .git/config"
37 url="$1"
38 remoteurl=${remoteurl%/}
39 while test -n "$url"
41 case "$url" in
42 ../*)
43 url="${url#../}"
44 remoteurl="${remoteurl%/*}"
46 ./*)
47 url="${url#./}"
50 break;;
51 esac
52 done
53 echo "$remoteurl/${url%/}"
57 # Get submodule info for registered submodules
58 # $@ = path to limit submodule list
60 module_list()
62 git ls-files --stage -- "$@" | grep '^160000 '
66 # Map submodule path to submodule name
68 # $1 = path
70 module_name()
72 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
73 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
74 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
75 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
76 test -z "$name" &&
77 die "No submodule mapping found in .gitmodules for path '$path'"
78 echo "$name"
82 # Clone a submodule
84 # Prior to calling, cmd_update checks that a possibly existing
85 # path is not a git repository.
86 # Likewise, cmd_add checks that path does not exist at all,
87 # since it is the location of a new submodule.
89 module_clone()
91 path=$1
92 url=$2
94 # If there already is a directory at the submodule path,
95 # expect it to be empty (since that is the default checkout
96 # action) and try to remove it.
97 # Note: if $path is a symlink to a directory the test will
98 # succeed but the rmdir will fail. We might want to fix this.
99 if test -d "$path"
100 then
101 rmdir "$path" 2>/dev/null ||
102 die "Directory '$path' exist, but is neither empty nor a git repository"
105 test -e "$path" &&
106 die "A file already exist at path '$path'"
108 git-clone -n "$url" "$path" ||
109 die "Clone of '$url' into submodule path '$path' failed"
113 # Add a new submodule to the working tree, .gitmodules and the index
115 # $@ = repo path
117 # optional branch is stored in global branch variable
119 cmd_add()
121 # parse $args after "submodule ... add".
122 while test $# -ne 0
124 case "$1" in
125 -b | --branch)
126 case "$2" in '') usage ;; esac
127 branch=$2
128 shift
130 -q|--quiet)
131 quiet=1
134 shift
135 break
138 usage
141 break
143 esac
144 shift
145 done
147 repo=$1
148 path=$2
150 if test -z "$repo" -o -z "$path"; then
151 usage
154 # assure repo is absolute or relative to parent
155 case "$repo" in
156 ./*|../*)
157 # dereference source url relative to parent's url
158 realrepo=$(resolve_relative_url "$repo") || exit
160 *:*|/*)
161 # absolute url
162 realrepo=$repo
165 die "repo URL: '$repo' must be absolute or begin with ./|../"
167 esac
169 # normalize path:
170 # multiple //; leading ./; /./; /../; trailing /
171 path=$(printf '%s/\n' "$path" |
172 sed -e '
173 s|//*|/|g
174 s|^\(\./\)*||
175 s|/\./|/|g
176 :start
177 s|\([^/]*\)/\.\./||
178 tstart
179 s|/*$||
181 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
182 die "'$path' already exists in the index"
184 # perhaps the path exists and is already a git repo, else clone it
185 if test -e "$path"
186 then
187 if test -d "$path"/.git -o -f "$path"/.git
188 then
189 echo "Adding existing repo at '$path' to the index"
190 else
191 die "'$path' already exists and is not a valid git repo"
194 case "$repo" in
195 ./*|../*)
196 url=$(resolve_relative_url "$repo") || exit
199 url="$repo"
201 esac
202 git config submodule."$path".url "$url"
203 else
205 module_clone "$path" "$realrepo" || exit
206 (unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b "$branch" "origin/$branch"}) ||
207 die "Unable to checkout submodule '$path'"
210 git add "$path" ||
211 die "Failed to add submodule '$path'"
213 git config -f .gitmodules submodule."$path".path "$path" &&
214 git config -f .gitmodules submodule."$path".url "$repo" &&
215 git add .gitmodules ||
216 die "Failed to register submodule '$path'"
220 # Execute an arbitrary command sequence in each checked out
221 # submodule
223 # $@ = command to execute
225 cmd_foreach()
227 module_list |
228 while read mode sha1 stage path
230 if test -e "$path"/.git
231 then
232 say "Entering '$path'"
233 (cd "$path" && eval "$@") ||
234 die "Stopping at '$path'; script returned non-zero status."
236 done
240 # Register submodules in .git/config
242 # $@ = requested paths (default to all)
244 cmd_init()
246 # parse $args after "submodule ... init".
247 while test $# -ne 0
249 case "$1" in
250 -q|--quiet)
251 quiet=1
254 shift
255 break
258 usage
261 break
263 esac
264 shift
265 done
267 module_list "$@" |
268 while read mode sha1 stage path
270 # Skip already registered paths
271 name=$(module_name "$path") || exit
272 url=$(git config submodule."$name".url)
273 test -z "$url" || continue
275 url=$(git config -f .gitmodules submodule."$name".url)
276 test -z "$url" &&
277 die "No url found for submodule path '$path' in .gitmodules"
279 # Possibly a url relative to parent
280 case "$url" in
281 ./*|../*)
282 url=$(resolve_relative_url "$url") || exit
284 esac
286 git config submodule."$name".url "$url" ||
287 die "Failed to register url for submodule path '$path'"
289 say "Submodule '$name' ($url) registered for path '$path'"
290 done
294 # Update each submodule path to correct revision, using clone and checkout as needed
296 # $@ = requested paths (default to all)
298 cmd_update()
300 # parse $args after "submodule ... update".
301 while test $# -ne 0
303 case "$1" in
304 -q|--quiet)
305 shift
306 quiet=1
308 -i|--init)
309 shift
310 cmd_init "$@" || return
313 shift
314 break
317 usage
320 break
322 esac
323 done
325 module_list "$@" |
326 while read mode sha1 stage path
328 name=$(module_name "$path") || exit
329 url=$(git config submodule."$name".url)
330 if test -z "$url"
331 then
332 # Only mention uninitialized submodules when its
333 # path have been specified
334 test "$#" != "0" &&
335 say "Submodule path '$path' not initialized" &&
336 say "Maybe you want to use 'update --init'?"
337 continue
340 if ! test -d "$path"/.git -o -f "$path"/.git
341 then
342 module_clone "$path" "$url" || exit
343 subsha1=
344 else
345 subsha1=$(unset GIT_DIR; cd "$path" &&
346 git rev-parse --verify HEAD) ||
347 die "Unable to find current revision in submodule path '$path'"
350 if test "$subsha1" != "$sha1"
351 then
352 force=
353 if test -z "$subsha1"
354 then
355 force="-f"
357 (unset GIT_DIR; cd "$path" && git-fetch &&
358 git-checkout $force -q "$sha1") ||
359 die "Unable to checkout '$sha1' in submodule path '$path'"
361 say "Submodule path '$path': checked out '$sha1'"
363 done
366 set_name_rev () {
367 revname=$( (
368 unset GIT_DIR
369 cd "$1" && {
370 git describe "$2" 2>/dev/null ||
371 git describe --tags "$2" 2>/dev/null ||
372 git describe --contains "$2" 2>/dev/null ||
373 git describe --all --always "$2"
376 test -z "$revname" || revname=" ($revname)"
379 # Show commit summary for submodules in index or working tree
381 # If '--cached' is given, show summary between index and given commit,
382 # or between working tree and given commit
384 # $@ = [commit (default 'HEAD'),] requested paths (default all)
386 cmd_summary() {
387 summary_limit=-1
388 for_status=
390 # parse $args after "submodule ... summary".
391 while test $# -ne 0
393 case "$1" in
394 --cached)
395 cached="$1"
397 --for-status)
398 for_status="$1"
400 -n|--summary-limit)
401 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
402 then
404 else
405 usage
407 shift
410 shift
411 break
414 usage
417 break
419 esac
420 shift
421 done
423 test $summary_limit = 0 && return
425 if rev=$(git rev-parse -q --verify "$1^0")
426 then
427 head=$rev
428 shift
429 else
430 head=HEAD
433 cd_to_toplevel
434 # Get modified modules cared by user
435 modules=$(git diff-index $cached --raw $head -- "$@" |
436 egrep '^:([0-7]* )?160000' |
437 while read mod_src mod_dst sha1_src sha1_dst status name
439 # Always show modules deleted or type-changed (blob<->module)
440 test $status = D -o $status = T && echo "$name" && continue
441 # Also show added or modified modules which are checked out
442 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
443 echo "$name"
444 done
447 test -z "$modules" && return
449 git diff-index $cached --raw $head -- $modules |
450 egrep '^:([0-7]* )?160000' |
451 cut -c2- |
452 while read mod_src mod_dst sha1_src sha1_dst status name
454 if test -z "$cached" &&
455 test $sha1_dst = 0000000000000000000000000000000000000000
456 then
457 case "$mod_dst" in
458 160000)
459 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
461 100644 | 100755 | 120000)
462 sha1_dst=$(git hash-object $name)
464 000000)
465 ;; # removed
467 # unexpected type
468 echo >&2 "unexpected mode $mod_dst"
469 continue ;;
470 esac
472 missing_src=
473 missing_dst=
475 test $mod_src = 160000 &&
476 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
477 missing_src=t
479 test $mod_dst = 160000 &&
480 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
481 missing_dst=t
483 total_commits=
484 case "$missing_src,$missing_dst" in
486 errmsg=" Warn: $name doesn't contain commit $sha1_src"
489 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
491 t,t)
492 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
495 errmsg=
496 total_commits=$(
497 if test $mod_src = 160000 -a $mod_dst = 160000
498 then
499 range="$sha1_src...$sha1_dst"
500 elif test $mod_src = 160000
501 then
502 range=$sha1_src
503 else
504 range=$sha1_dst
506 GIT_DIR="$name/.git" \
507 git log --pretty=oneline --first-parent $range | wc -l
509 total_commits=" ($(($total_commits + 0)))"
511 esac
513 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
514 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
515 if test $status = T
516 then
517 if test $mod_dst = 160000
518 then
519 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
520 else
521 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
523 else
524 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
526 if test -n "$errmsg"
527 then
528 # Don't give error msg for modification whose dst is not submodule
529 # i.e. deleted or changed to blob
530 test $mod_dst = 160000 && echo "$errmsg"
531 else
532 if test $mod_src = 160000 -a $mod_dst = 160000
533 then
534 limit=
535 test $summary_limit -gt 0 && limit="-$summary_limit"
536 GIT_DIR="$name/.git" \
537 git log $limit --pretty='format: %m %s' \
538 --first-parent $sha1_src...$sha1_dst
539 elif test $mod_dst = 160000
540 then
541 GIT_DIR="$name/.git" \
542 git log --pretty='format: > %s' -1 $sha1_dst
543 else
544 GIT_DIR="$name/.git" \
545 git log --pretty='format: < %s' -1 $sha1_src
547 echo
549 echo
550 done |
551 if test -n "$for_status"; then
552 echo "# Modified submodules:"
553 echo "#"
554 sed -e 's|^|# |' -e 's|^# $|#|'
555 else
560 # List all submodules, prefixed with:
561 # - submodule not initialized
562 # + different revision checked out
564 # If --cached was specified the revision in the index will be printed
565 # instead of the currently checked out revision.
567 # $@ = requested paths (default to all)
569 cmd_status()
571 # parse $args after "submodule ... status".
572 while test $# -ne 0
574 case "$1" in
575 -q|--quiet)
576 quiet=1
578 --cached)
579 cached=1
582 shift
583 break
586 usage
589 break
591 esac
592 shift
593 done
595 module_list "$@" |
596 while read mode sha1 stage path
598 name=$(module_name "$path") || exit
599 url=$(git config submodule."$name".url)
600 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
601 then
602 say "-$sha1 $path"
603 continue;
605 set_name_rev "$path" "$sha1"
606 if git diff-files --quiet -- "$path"
607 then
608 say " $sha1 $path$revname"
609 else
610 if test -z "$cached"
611 then
612 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
613 set_name_rev "$path" "$sha1"
615 say "+$sha1 $path$revname"
617 done
620 # Sync remote urls for submodules
621 # This makes the value for remote.$remote.url match the value
622 # specified in .gitmodules.
624 cmd_sync()
626 while test $# -ne 0
628 case "$1" in
629 -q|--quiet)
630 quiet=1
631 shift
634 shift
635 break
638 usage
641 break
643 esac
644 done
645 cd_to_toplevel
646 module_list "$@" |
647 while read mode sha1 stage path
649 name=$(module_name "$path")
650 url=$(git config -f .gitmodules --get submodule."$name".url)
652 # Possibly a url relative to parent
653 case "$url" in
654 ./*|../*)
655 url=$(resolve_relative_url "$url") || exit
657 esac
659 if test -e "$path"/.git
660 then
662 unset GIT_DIR
663 cd "$path"
664 remote=$(get_default_remote)
665 say "Synchronizing submodule url for '$name'"
666 git config remote."$remote".url "$url"
669 done
672 # This loop parses the command line arguments to find the
673 # subcommand name to dispatch. Parsing of the subcommand specific
674 # options are primarily done by the subcommand implementations.
675 # Subcommand specific options such as --branch and --cached are
676 # parsed here as well, for backward compatibility.
678 while test $# != 0 && test -z "$command"
680 case "$1" in
681 add | foreach | init | update | status | summary | sync)
682 command=$1
684 -q|--quiet)
685 quiet=1
687 -b|--branch)
688 case "$2" in
690 usage
692 esac
693 branch="$2"; shift
695 --cached)
696 cached="$1"
699 break
702 usage
705 break
707 esac
708 shift
709 done
711 # No command word defaults to "status"
712 test -n "$command" || command=status
714 # "-b branch" is accepted only by "add"
715 if test -n "$branch" && test "$command" != add
716 then
717 usage
720 # "--cached" is accepted only by "status" and "summary"
721 if test -n "$cached" && test "$command" != status -a "$command" != summary
722 then
723 usage
726 "cmd_$command" "$@"