Documentation/git-merge.txt: Partial rewrite of How Merge Works
[git/dkf.git] / git-submodule.sh
blob9228f56bee7fd82d530a6865f1f8669fd9b99d8f
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 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 # Map submodule path to submodule name
59 # $1 = path
61 module_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' )
67 test -z "$name" &&
68 die "No submodule mapping found in .gitmodules for path '$path'"
69 echo "$name"
73 # Clone a submodule
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.
80 module_clone()
82 path=$1
83 url=$2
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.
90 if test -d "$path"
91 then
92 rmdir "$path" 2>/dev/null ||
93 die "Directory '$path' exist, but is neither empty nor a git repository"
96 test -e "$path" &&
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
106 # $@ = repo path
108 # optional branch is stored in global branch variable
110 cmd_add()
112 # parse $args after "submodule ... add".
113 while test $# -ne 0
115 case "$1" in
116 -b | --branch)
117 case "$2" in '') usage ;; esac
118 branch=$2
119 shift
121 -q|--quiet)
122 quiet=1
125 shift
126 break
129 usage
132 break
134 esac
135 shift
136 done
138 repo=$1
139 path=$2
141 if test -z "$repo" -o -z "$path"; then
142 usage
145 # assure repo is absolute or relative to parent
146 case "$repo" in
147 ./*|../*)
148 # dereference source url relative to parent's url
149 realrepo=$(resolve_relative_url "$repo") || exit
151 *:*|/*)
152 # absolute url
153 realrepo=$repo
156 die "repo URL: '$repo' must be absolute or begin with ./|../"
158 esac
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
167 if test -e "$path"
168 then
169 if test -d "$path"/.git -o -f "$path"/.git
170 then
171 echo "Adding existing repo at '$path' to the index"
172 else
173 die "'$path' already exists and is not a valid git repo"
176 case "$repo" in
177 ./*|../*)
178 url=$(resolve_relative_url "$repo") || exit
181 url="$repo"
183 esac
184 git config submodule."$path".url "$url"
185 else
187 module_clone "$path" "$realrepo" || exit
188 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
189 die "Unable to checkout submodule '$path'"
192 git add "$path" ||
193 die "Failed to add submodule '$path'"
195 git config -f .gitmodules submodule."$path".path "$path" &&
196 git config -f .gitmodules submodule."$path".url "$repo" &&
197 git add .gitmodules ||
198 die "Failed to register submodule '$path'"
202 # Register submodules in .git/config
204 # $@ = requested paths (default to all)
206 cmd_init()
208 # parse $args after "submodule ... init".
209 while test $# -ne 0
211 case "$1" in
212 -q|--quiet)
213 quiet=1
216 shift
217 break
220 usage
223 break
225 esac
226 shift
227 done
229 git ls-files --stage -- "$@" | grep '^160000 ' |
230 while read mode sha1 stage path
232 # Skip already registered paths
233 name=$(module_name "$path") || exit
234 url=$(git config submodule."$name".url)
235 test -z "$url" || continue
237 url=$(git config -f .gitmodules submodule."$name".url)
238 test -z "$url" &&
239 die "No url found for submodule path '$path' in .gitmodules"
241 # Possibly a url relative to parent
242 case "$url" in
243 ./*|../*)
244 url=$(resolve_relative_url "$url") || exit
246 esac
248 git config submodule."$name".url "$url" ||
249 die "Failed to register url for submodule path '$path'"
251 say "Submodule '$name' ($url) registered for path '$path'"
252 done
256 # Update each submodule path to correct revision, using clone and checkout as needed
258 # $@ = requested paths (default to all)
260 cmd_update()
262 # parse $args after "submodule ... update".
263 while test $# -ne 0
265 case "$1" in
266 -q|--quiet)
267 quiet=1
269 -i|--init)
270 shift
271 cmd_init "$@" || return
274 shift
275 break
278 usage
281 break
283 esac
284 shift
285 done
287 git ls-files --stage -- "$@" | grep '^160000 ' |
288 while read mode sha1 stage path
290 name=$(module_name "$path") || exit
291 url=$(git config submodule."$name".url)
292 if test -z "$url"
293 then
294 # Only mention uninitialized submodules when its
295 # path have been specified
296 test "$#" != "0" &&
297 say "Submodule path '$path' not initialized"
298 say "Maybe you want to use 'update --init'?"
299 continue
302 if ! test -d "$path"/.git -o -f "$path"/.git
303 then
304 module_clone "$path" "$url" || exit
305 subsha1=
306 else
307 subsha1=$(unset GIT_DIR; cd "$path" &&
308 git rev-parse --verify HEAD) ||
309 die "Unable to find current revision in submodule path '$path'"
312 if test "$subsha1" != "$sha1"
313 then
314 (unset GIT_DIR; cd "$path" && git-fetch &&
315 git-checkout -q "$sha1") ||
316 die "Unable to checkout '$sha1' in submodule path '$path'"
318 say "Submodule path '$path': checked out '$sha1'"
320 done
323 set_name_rev () {
324 revname=$( (
325 unset GIT_DIR
326 cd "$1" && {
327 git describe "$2" 2>/dev/null ||
328 git describe --tags "$2" 2>/dev/null ||
329 git describe --contains "$2" 2>/dev/null ||
330 git describe --all --always "$2"
333 test -z "$revname" || revname=" ($revname)"
336 # Show commit summary for submodules in index or working tree
338 # If '--cached' is given, show summary between index and given commit,
339 # or between working tree and given commit
341 # $@ = [commit (default 'HEAD'),] requested paths (default all)
343 cmd_summary() {
344 summary_limit=-1
345 for_status=
347 # parse $args after "submodule ... summary".
348 while test $# -ne 0
350 case "$1" in
351 --cached)
352 cached="$1"
354 --for-status)
355 for_status="$1"
357 -n|--summary-limit)
358 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
359 then
361 else
362 usage
364 shift
367 shift
368 break
371 usage
374 break
376 esac
377 shift
378 done
380 test $summary_limit = 0 && return
382 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
383 then
384 head=$rev
385 shift
386 else
387 head=HEAD
390 cd_to_toplevel
391 # Get modified modules cared by user
392 modules=$(git diff-index $cached --raw $head -- "$@" |
393 grep -e '^:160000' -e '^:[0-7]* 160000' |
394 while read mod_src mod_dst sha1_src sha1_dst status name
396 # Always show modules deleted or type-changed (blob<->module)
397 test $status = D -o $status = T && echo "$name" && continue
398 # Also show added or modified modules which are checked out
399 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
400 echo "$name"
401 done
404 test -z "$modules" && return
406 git diff-index $cached --raw $head -- $modules |
407 grep -e '^:160000' -e '^:[0-7]* 160000' |
408 cut -c2- |
409 while read mod_src mod_dst sha1_src sha1_dst status name
411 if test -z "$cached" &&
412 test $sha1_dst = 0000000000000000000000000000000000000000
413 then
414 case "$mod_dst" in
415 160000)
416 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
418 100644 | 100755 | 120000)
419 sha1_dst=$(git hash-object $name)
421 000000)
422 ;; # removed
424 # unexpected type
425 echo >&2 "unexpected mode $mod_dst"
426 continue ;;
427 esac
429 missing_src=
430 missing_dst=
432 test $mod_src = 160000 &&
433 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
434 missing_src=t
436 test $mod_dst = 160000 &&
437 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
438 missing_dst=t
440 total_commits=
441 case "$missing_src,$missing_dst" in
443 errmsg=" Warn: $name doesn't contain commit $sha1_src"
446 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
448 t,t)
449 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
452 errmsg=
453 total_commits=$(
454 if test $mod_src = 160000 -a $mod_dst = 160000
455 then
456 range="$sha1_src...$sha1_dst"
457 elif test $mod_src = 160000
458 then
459 range=$sha1_src
460 else
461 range=$sha1_dst
463 GIT_DIR="$name/.git" \
464 git log --pretty=oneline --first-parent $range | wc -l
466 total_commits=" ($(($total_commits + 0)))"
468 esac
470 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
471 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
472 if test $status = T
473 then
474 if test $mod_dst = 160000
475 then
476 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
477 else
478 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
480 else
481 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
483 if test -n "$errmsg"
484 then
485 # Don't give error msg for modification whose dst is not submodule
486 # i.e. deleted or changed to blob
487 test $mod_dst = 160000 && echo "$errmsg"
488 else
489 if test $mod_src = 160000 -a $mod_dst = 160000
490 then
491 limit=
492 test $summary_limit -gt 0 && limit="-$summary_limit"
493 GIT_DIR="$name/.git" \
494 git log $limit --pretty='format: %m %s' \
495 --first-parent $sha1_src...$sha1_dst
496 elif test $mod_dst = 160000
497 then
498 GIT_DIR="$name/.git" \
499 git log --pretty='format: > %s' -1 $sha1_dst
500 else
501 GIT_DIR="$name/.git" \
502 git log --pretty='format: < %s' -1 $sha1_src
504 echo
506 echo
507 done |
508 if test -n "$for_status"; then
509 echo "# Modified submodules:"
510 echo "#"
511 sed -e 's|^|# |' -e 's|^# $|#|'
512 else
517 # List all submodules, prefixed with:
518 # - submodule not initialized
519 # + different revision checked out
521 # If --cached was specified the revision in the index will be printed
522 # instead of the currently checked out revision.
524 # $@ = requested paths (default to all)
526 cmd_status()
528 # parse $args after "submodule ... status".
529 while test $# -ne 0
531 case "$1" in
532 -q|--quiet)
533 quiet=1
535 --cached)
536 cached=1
539 shift
540 break
543 usage
546 break
548 esac
549 shift
550 done
552 git ls-files --stage -- "$@" | grep '^160000 ' |
553 while read mode sha1 stage path
555 name=$(module_name "$path") || exit
556 url=$(git config submodule."$name".url)
557 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
558 then
559 say "-$sha1 $path"
560 continue;
562 set_name_rev "$path" "$sha1"
563 if git diff-files --quiet -- "$path"
564 then
565 say " $sha1 $path$revname"
566 else
567 if test -z "$cached"
568 then
569 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
570 set_name_rev "$path" "$sha1"
572 say "+$sha1 $path$revname"
574 done
577 # This loop parses the command line arguments to find the
578 # subcommand name to dispatch. Parsing of the subcommand specific
579 # options are primarily done by the subcommand implementations.
580 # Subcommand specific options such as --branch and --cached are
581 # parsed here as well, for backward compatibility.
583 while test $# != 0 && test -z "$command"
585 case "$1" in
586 add | init | update | status | summary)
587 command=$1
589 -q|--quiet)
590 quiet=1
592 -b|--branch)
593 case "$2" in
595 usage
597 esac
598 branch="$2"; shift
600 --cached)
601 cached="$1"
604 break
607 usage
610 break
612 esac
613 shift
614 done
616 # No command word defaults to "status"
617 test -n "$command" || command=status
619 # "-b branch" is accepted only by "add"
620 if test -n "$branch" && test "$command" != add
621 then
622 usage
625 # "--cached" is accepted only by "status" and "summary"
626 if test -n "$cached" && test "$command" != status -a "$command" != summary
627 then
628 usage
631 "cmd_$command" "$@"