git-submodule.sh - Remove trailing / from URL if found
[git/mingw.git] / git-submodule.sh
blob46d75aba3114778f277cb62b5b0fe84866b326c9
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>...]"
10 OPTIONS_SPEC=
11 . git-sh-setup
12 require_work_tree
14 command=
15 branch=
16 quiet=
17 cached=
20 # print stuff on stdout unless -q was specified
22 say()
24 if test -z "$quiet"
25 then
26 echo "$@"
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"
38 url="$1"
39 remoteurl=${remoteurl%/}
40 while test -n "$url"
42 case "$url" in
43 ../*)
44 url="${url#../}"
45 remoteurl="${remoteurl%/*}"
47 ./*)
48 url="${url#./}"
51 break;;
52 esac
53 done
54 echo "$remoteurl/${url%/}"
58 # Map submodule path to submodule name
60 # $1 = path
62 module_name()
64 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
65 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
66 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
67 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
68 test -z "$name" &&
69 die "No submodule mapping found in .gitmodules for path '$path'"
70 echo "$name"
74 # Clone a submodule
76 # Prior to calling, cmd_update checks that a possibly existing
77 # path is not a git repository.
78 # Likewise, cmd_add checks that path does not exist at all,
79 # since it is the location of a new submodule.
81 module_clone()
83 path=$1
84 url=$2
86 # If there already is a directory at the submodule path,
87 # expect it to be empty (since that is the default checkout
88 # action) and try to remove it.
89 # Note: if $path is a symlink to a directory the test will
90 # succeed but the rmdir will fail. We might want to fix this.
91 if test -d "$path"
92 then
93 rmdir "$path" 2>/dev/null ||
94 die "Directory '$path' exist, but is neither empty nor a git repository"
97 test -e "$path" &&
98 die "A file already exist at path '$path'"
100 git-clone -n "$url" "$path" ||
101 die "Clone of '$url' into submodule path '$path' failed"
105 # Add a new submodule to the working tree, .gitmodules and the index
107 # $@ = repo path
109 # optional branch is stored in global branch variable
111 cmd_add()
113 # parse $args after "submodule ... add".
114 while test $# -ne 0
116 case "$1" in
117 -b | --branch)
118 case "$2" in '') usage ;; esac
119 branch=$2
120 shift
122 -q|--quiet)
123 quiet=1
126 shift
127 break
130 usage
133 break
135 esac
136 shift
137 done
139 repo=$1
140 path=$2
142 if test -z "$repo" -o -z "$path"; then
143 usage
146 # assure repo is absolute or relative to parent
147 case "$repo" in
148 ./*|../*)
149 # dereference source url relative to parent's url
150 realrepo=$(resolve_relative_url "$repo") || exit
152 *:*|/*)
153 # absolute url
154 realrepo=$repo
157 die "repo URL: '$repo' must be absolute or begin with ./|../"
159 esac
161 # strip trailing slashes from path
162 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
168 if test -e "$path"
169 then
170 if test -d "$path"/.git -o -f "$path"/.git
171 then
172 echo "Adding existing repo at '$path' to the index"
173 else
174 die "'$path' already exists and is not a valid git repo"
177 case "$repo" in
178 ./*|../*)
179 url=$(resolve_relative_url "$repo") || exit
182 url="$repo"
184 esac
185 git config submodule."$path".url "$url"
186 else
188 module_clone "$path" "$realrepo" || exit
189 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
190 die "Unable to checkout submodule '$path'"
193 git add "$path" ||
194 die "Failed to add submodule '$path'"
196 git config -f .gitmodules submodule."$path".path "$path" &&
197 git config -f .gitmodules submodule."$path".url "$repo" &&
198 git add .gitmodules ||
199 die "Failed to register submodule '$path'"
203 # Register submodules in .git/config
205 # $@ = requested paths (default to all)
207 cmd_init()
209 # parse $args after "submodule ... init".
210 while test $# -ne 0
212 case "$1" in
213 -q|--quiet)
214 quiet=1
217 shift
218 break
221 usage
224 break
226 esac
227 shift
228 done
230 git ls-files --stage -- "$@" | grep '^160000 ' |
231 while read mode sha1 stage path
233 # Skip already registered paths
234 name=$(module_name "$path") || exit
235 url=$(git config submodule."$name".url)
236 test -z "$url" || continue
238 url=$(git config -f .gitmodules submodule."$name".url)
239 test -z "$url" &&
240 die "No url found for submodule path '$path' in .gitmodules"
242 # Possibly a url relative to parent
243 case "$url" in
244 ./*|../*)
245 url=$(resolve_relative_url "$url") || exit
247 esac
249 git config submodule."$name".url "$url" ||
250 die "Failed to register url for submodule path '$path'"
252 say "Submodule '$name' ($url) registered for path '$path'"
253 done
257 # Update each submodule path to correct revision, using clone and checkout as needed
259 # $@ = requested paths (default to all)
261 cmd_update()
263 # parse $args after "submodule ... update".
264 while test $# -ne 0
266 case "$1" in
267 -q|--quiet)
268 shift
269 quiet=1
271 -i|--init)
272 shift
273 cmd_init "$@" || return
276 shift
277 break
280 usage
283 break
285 esac
286 done
288 git ls-files --stage -- "$@" | grep '^160000 ' |
289 while read mode sha1 stage path
291 name=$(module_name "$path") || exit
292 url=$(git config submodule."$name".url)
293 if test -z "$url"
294 then
295 # Only mention uninitialized submodules when its
296 # path have been specified
297 test "$#" != "0" &&
298 say "Submodule path '$path' not initialized"
299 say "Maybe you want to use 'update --init'?"
300 continue
303 if ! test -d "$path"/.git -o -f "$path"/.git
304 then
305 module_clone "$path" "$url" || exit
306 subsha1=
307 else
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"
314 then
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'"
321 done
324 set_name_rev () {
325 revname=$( (
326 unset GIT_DIR
327 cd "$1" && {
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)
344 cmd_summary() {
345 summary_limit=-1
346 for_status=
348 # parse $args after "submodule ... summary".
349 while test $# -ne 0
351 case "$1" in
352 --cached)
353 cached="$1"
355 --for-status)
356 for_status="$1"
358 -n|--summary-limit)
359 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
360 then
362 else
363 usage
365 shift
368 shift
369 break
372 usage
375 break
377 esac
378 shift
379 done
381 test $summary_limit = 0 && return
383 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
384 then
385 head=$rev
386 shift
387 else
388 head=HEAD
391 cd_to_toplevel
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 &&
401 echo "$name"
402 done
405 test -z "$modules" && return
407 git diff-index $cached --raw $head -- $modules |
408 grep -e '^:160000' -e '^:[0-7]* 160000' |
409 cut -c2- |
410 while read mod_src mod_dst sha1_src sha1_dst status name
412 if test -z "$cached" &&
413 test $sha1_dst = 0000000000000000000000000000000000000000
414 then
415 case "$mod_dst" in
416 160000)
417 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
419 100644 | 100755 | 120000)
420 sha1_dst=$(git hash-object $name)
422 000000)
423 ;; # removed
425 # unexpected type
426 echo >&2 "unexpected mode $mod_dst"
427 continue ;;
428 esac
430 missing_src=
431 missing_dst=
433 test $mod_src = 160000 &&
434 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
435 missing_src=t
437 test $mod_dst = 160000 &&
438 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
439 missing_dst=t
441 total_commits=
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"
449 t,t)
450 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
453 errmsg=
454 total_commits=$(
455 if test $mod_src = 160000 -a $mod_dst = 160000
456 then
457 range="$sha1_src...$sha1_dst"
458 elif test $mod_src = 160000
459 then
460 range=$sha1_src
461 else
462 range=$sha1_dst
464 GIT_DIR="$name/.git" \
465 git log --pretty=oneline --first-parent $range | wc -l
467 total_commits=" ($(($total_commits + 0)))"
469 esac
471 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
472 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
473 if test $status = T
474 then
475 if test $mod_dst = 160000
476 then
477 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
478 else
479 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
481 else
482 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
484 if test -n "$errmsg"
485 then
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"
489 else
490 if test $mod_src = 160000 -a $mod_dst = 160000
491 then
492 limit=
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
498 then
499 GIT_DIR="$name/.git" \
500 git log --pretty='format: > %s' -1 $sha1_dst
501 else
502 GIT_DIR="$name/.git" \
503 git log --pretty='format: < %s' -1 $sha1_src
505 echo
507 echo
508 done |
509 if test -n "$for_status"; then
510 echo "# Modified submodules:"
511 echo "#"
512 sed -e 's|^|# |' -e 's|^# $|#|'
513 else
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)
527 cmd_status()
529 # parse $args after "submodule ... status".
530 while test $# -ne 0
532 case "$1" in
533 -q|--quiet)
534 quiet=1
536 --cached)
537 cached=1
540 shift
541 break
544 usage
547 break
549 esac
550 shift
551 done
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 -o -f "$path"/.git
559 then
560 say "-$sha1 $path"
561 continue;
563 set_name_rev "$path" "$sha1"
564 if git diff-files --quiet -- "$path"
565 then
566 say " $sha1 $path$revname"
567 else
568 if test -z "$cached"
569 then
570 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
571 set_name_rev "$path" "$sha1"
573 say "+$sha1 $path$revname"
575 done
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"
586 case "$1" in
587 add | init | update | status | summary)
588 command=$1
590 -q|--quiet)
591 quiet=1
593 -b|--branch)
594 case "$2" in
596 usage
598 esac
599 branch="$2"; shift
601 --cached)
602 cached="$1"
605 break
608 usage
611 break
613 esac
614 shift
615 done
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
622 then
623 usage
626 # "--cached" is accepted only by "status" and "summary"
627 if test -n "$cached" && test "$command" != status -a "$command" != summary
628 then
629 usage
632 "cmd_$command" "$@"