]> git.ipfire.org Git - thirdparty/git.git/blame - git-submodule.sh
git-submodule summary: show commit summary
[thirdparty/git.git] / git-submodule.sh
CommitLineData
70c7ac22
LH
1#!/bin/sh
2#
ecda0723 3# git-submodules.sh: add, init, update or list git submodules
70c7ac22
LH
4#
5# Copyright (c) 2007 Lars Hjemli
6
28f9af5d 7USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update|summary [<commit>]] [--] [<path>...]'
8f321a39 8OPTIONS_SPEC=
70c7ac22
LH
9. git-sh-setup
10require_work_tree
11
5c08dbbd 12command=
ecda0723 13branch=
70c7ac22
LH
14quiet=
15cached=
16
17#
18# print stuff on stdout unless -q was specified
19#
20say()
21{
22 if test -z "$quiet"
23 then
24 echo "$@"
25 fi
26}
27
ecda0723
SV
28# NEEDSWORK: identical function exists in get_repo_base in clone.sh
29get_repo_base() {
30 (
31 cd "`/bin/pwd`" &&
32 cd "$1" || cd "$1.git" &&
33 {
34 cd .git
35 pwd
36 }
37 ) 2>/dev/null
38}
39
f31a522a
ML
40# Resolve relative url by appending to parent's url
41resolve_relative_url ()
42{
43 branch="$(git symbolic-ref HEAD 2>/dev/null)"
44 remote="$(git config branch.${branch#refs/heads/}.remote)"
45 remote="${remote:-origin}"
46 remoteurl="$(git config remote.$remote.url)" ||
47 die "remote ($remote) does not have a url in .git/config"
48 url="$1"
49 while test -n "$url"
50 do
51 case "$url" in
52 ../*)
53 url="${url#../}"
54 remoteurl="${remoteurl%/*}"
55 ;;
56 ./*)
57 url="${url#./}"
58 ;;
59 *)
60 break;;
61 esac
62 done
63 echo "$remoteurl/$url"
64}
65
941987a5
LH
66#
67# Map submodule path to submodule name
68#
69# $1 = path
70#
71module_name()
72{
537601ac 73 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
4f2e94c0 74 re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
537601ac
JH
75 name=$( GIT_CONFIG=.gitmodules \
76 git config --get-regexp '^submodule\..*\.path$' |
77 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
941987a5
LH
78 test -z "$name" &&
79 die "No submodule mapping found in .gitmodules for path '$path'"
80 echo "$name"
81}
33aa6fff
LH
82
83#
84# Clone a submodule
85#
23a485e3 86# Prior to calling, cmd_update checks that a possibly existing
ecda0723 87# path is not a git repository.
23a485e3 88# Likewise, cmd_add checks that path does not exist at all,
ecda0723
SV
89# since it is the location of a new submodule.
90#
33aa6fff
LH
91module_clone()
92{
93 path=$1
94 url=$2
95
96 # If there already is a directory at the submodule path,
97 # expect it to be empty (since that is the default checkout
98 # action) and try to remove it.
99 # Note: if $path is a symlink to a directory the test will
100 # succeed but the rmdir will fail. We might want to fix this.
101 if test -d "$path"
102 then
103 rmdir "$path" 2>/dev/null ||
104 die "Directory '$path' exist, but is neither empty nor a git repository"
105 fi
106
107 test -e "$path" &&
108 die "A file already exist at path '$path'"
109
110 git-clone -n "$url" "$path" ||
941987a5 111 die "Clone of '$url' into submodule path '$path' failed"
33aa6fff
LH
112}
113
ecda0723
SV
114#
115# Add a new submodule to the working tree, .gitmodules and the index
116#
117# $@ = repo [path]
118#
119# optional branch is stored in global branch variable
120#
23a485e3 121cmd_add()
ecda0723 122{
5c08dbbd
JH
123 # parse $args after "submodule ... add".
124 while test $# -ne 0
125 do
126 case "$1" in
127 -b | --branch)
128 case "$2" in '') usage ;; esac
129 branch=$2
130 shift
131 ;;
132 -q|--quiet)
133 quiet=1
134 ;;
135 --)
136 shift
137 break
138 ;;
139 -*)
140 usage
141 ;;
142 *)
143 break
144 ;;
145 esac
146 shift
147 done
148
ecda0723
SV
149 repo=$1
150 path=$2
151
152 if test -z "$repo"; then
153 usage
154 fi
155
f31a522a
ML
156 case "$repo" in
157 ./*|../*)
158 # dereference source url relative to parent's url
159 realrepo="$(resolve_relative_url $repo)" ;;
160 *)
161 # Turn the source into an absolute path if
162 # it is local
163 if base=$(get_repo_base "$repo"); then
164 repo="$base"
f31a522a 165 fi
fe50eef5 166 realrepo=$repo
f31a522a
ML
167 ;;
168 esac
ecda0723
SV
169
170 # Guess path from repo if not specified or strip trailing slashes
171 if test -z "$path"; then
172 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
173 else
174 path=$(echo "$path" | sed -e 's|/*$||')
175 fi
176
177 test -e "$path" &&
178 die "'$path' already exists"
179
5be60078 180 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
ecda0723
SV
181 die "'$path' already exists in the index"
182
f31a522a 183 module_clone "$path" "$realrepo" || exit
51884080 184 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
ecda0723
SV
185 die "Unable to checkout submodule '$path'"
186 git add "$path" ||
187 die "Failed to add submodule '$path'"
188
189 GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
190 GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
191 git add .gitmodules ||
192 die "Failed to register submodule '$path'"
193}
194
70c7ac22 195#
211b7f19 196# Register submodules in .git/config
70c7ac22
LH
197#
198# $@ = requested paths (default to all)
199#
23a485e3 200cmd_init()
70c7ac22 201{
5c08dbbd
JH
202 # parse $args after "submodule ... init".
203 while test $# -ne 0
204 do
205 case "$1" in
206 -q|--quiet)
207 quiet=1
208 ;;
209 --)
210 shift
211 break
212 ;;
213 -*)
214 usage
215 ;;
216 *)
217 break
218 ;;
219 esac
220 shift
221 done
222
70c7ac22
LH
223 git ls-files --stage -- "$@" | grep -e '^160000 ' |
224 while read mode sha1 stage path
225 do
211b7f19 226 # Skip already registered paths
941987a5 227 name=$(module_name "$path") || exit
5be60078 228 url=$(git config submodule."$name".url)
211b7f19 229 test -z "$url" || continue
70c7ac22 230
5be60078 231 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
70c7ac22 232 test -z "$url" &&
941987a5 233 die "No url found for submodule path '$path' in .gitmodules"
70c7ac22 234
f31a522a
ML
235 # Possibly a url relative to parent
236 case "$url" in
237 ./*|../*)
238 url="$(resolve_relative_url "$url")"
239 ;;
240 esac
241
5be60078 242 git config submodule."$name".url "$url" ||
941987a5 243 die "Failed to register url for submodule path '$path'"
70c7ac22 244
941987a5 245 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac22
LH
246 done
247}
248
249#
211b7f19 250# Update each submodule path to correct revision, using clone and checkout as needed
70c7ac22
LH
251#
252# $@ = requested paths (default to all)
253#
23a485e3 254cmd_update()
70c7ac22 255{
5c08dbbd
JH
256 # parse $args after "submodule ... update".
257 while test $# -ne 0
258 do
259 case "$1" in
260 -q|--quiet)
261 quiet=1
262 ;;
263 --)
264 shift
265 break
266 ;;
267 -*)
268 usage
269 ;;
270 *)
271 break
272 ;;
273 esac
274 shift
275 done
276
70c7ac22
LH
277 git ls-files --stage -- "$@" | grep -e '^160000 ' |
278 while read mode sha1 stage path
279 do
941987a5 280 name=$(module_name "$path") || exit
5be60078 281 url=$(git config submodule."$name".url)
211b7f19 282 if test -z "$url"
70c7ac22
LH
283 then
284 # Only mention uninitialized submodules when its
285 # path have been specified
286 test "$#" != "0" &&
941987a5 287 say "Submodule path '$path' not initialized"
211b7f19
LH
288 continue
289 fi
290
291 if ! test -d "$path"/.git
292 then
293 module_clone "$path" "$url" || exit
bf2d8246
LH
294 subsha1=
295 else
51884080 296 subsha1=$(unset GIT_DIR; cd "$path" &&
5be60078 297 git rev-parse --verify HEAD) ||
941987a5 298 die "Unable to find current revision in submodule path '$path'"
70c7ac22 299 fi
211b7f19 300
70c7ac22
LH
301 if test "$subsha1" != "$sha1"
302 then
51884080 303 (unset GIT_DIR; cd "$path" && git-fetch &&
70c7ac22 304 git-checkout -q "$sha1") ||
941987a5 305 die "Unable to checkout '$sha1' in submodule path '$path'"
70c7ac22 306
941987a5 307 say "Submodule path '$path': checked out '$sha1'"
70c7ac22
LH
308 fi
309 done
310}
311
bffe71f4
EM
312set_name_rev () {
313 revname=$( (
51884080 314 unset GIT_DIR
bffe71f4 315 cd "$1" && {
5be60078
JH
316 git describe "$2" 2>/dev/null ||
317 git describe --tags "$2" 2>/dev/null ||
318 git describe --contains --tags "$2"
bffe71f4
EM
319 }
320 ) )
321 test -z "$revname" || revname=" ($revname)"
322}
28f9af5d
PY
323#
324# Show commit summary for submodules in index or working tree
325#
326# If '--cached' is given, show summary between index and given commit,
327# or between working tree and given commit
328#
329# $@ = [commit (default 'HEAD'),] requested paths (default all)
330#
331cmd_summary() {
332 # parse $args after "submodule ... summary".
333 while test $# -ne 0
334 do
335 case "$1" in
336 --cached)
337 cached="$1"
338 ;;
339 --)
340 shift
341 break
342 ;;
343 -*)
344 usage
345 ;;
346 *)
347 break
348 ;;
349 esac
350 shift
351 done
bffe71f4 352
28f9af5d
PY
353 if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
354 then
355 head=$rev
356 shift
357 else
358 head=HEAD
359 fi
360
361 cd_to_toplevel
362 # Get modified modules cared by user
363 modules=$(git diff-index $cached --raw $head -- "$@" |
364 grep -e '^:160000' -e '^:[0-7]* 160000' |
365 while read mod_src mod_dst sha1_src sha1_dst status name
366 do
367 # Always show modules deleted or type-changed (blob<->module)
368 test $status = D -o $status = T && echo "$name" && continue
369 # Also show added or modified modules which are checked out
370 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
371 echo "$name"
372 done
373 )
1cb639e6
PY
374
375 test -n "$modules" &&
376 git diff-index $cached --raw $head -- $modules |
377 grep -e '^:160000' -e '^:[0-7]* 160000' |
378 cut -c2- |
379 while read mod_src mod_dst sha1_src sha1_dst status name
380 do
381 if test -z "$cached" &&
382 test $sha1_dst = 0000000000000000000000000000000000000000
383 then
384 case "$mod_dst" in
385 160000)
386 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
387 ;;
388 100644 | 100755 | 120000)
389 sha1_dst=$(git hash-object $name)
390 ;;
391 000000)
392 ;; # removed
393 *)
394 # unexpected type
395 echo >&2 "unexpected mode $mod_dst"
396 continue ;;
397 esac
398 fi
399 missing_src=
400 missing_dst=
401
402 test $mod_src = 160000 &&
403 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
404 missing_src=t
405
406 test $mod_dst = 160000 &&
407 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
408 missing_dst=t
409
410 total_commits=
411 case "$missing_src,$missing_dst" in
412 t,)
413 errmsg=" Warn: $name doesn't contain commit $sha1_src"
414 ;;
415 ,t)
416 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
417 ;;
418 t,t)
419 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
420 ;;
421 *)
422 errmsg=
423 total_commits=$(
424 if test $mod_src = 160000 -a $mod_dst = 160000
425 then
426 range="$sha1_src...$sha1_dst"
427 elif test $mod_src = 160000
428 then
429 range=$sha1_src
430 else
431 range=$sha1_dst
432 fi
433 GIT_DIR="$name/.git" \
434 git log --pretty=oneline --first-parent $range | wc -l
435 )
436 total_commits=" ($total_commits)"
437 ;;
438 esac
439
440 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
441 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
442 if test $status = T
443 then
444 if test $mod_dst = 160000
445 then
446 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
447 else
448 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
449 fi
450 else
451 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
452 fi
453 if test -n "$errmsg"
454 then
455 # Don't give error msg for modification whose dst is not submodule
456 # i.e. deleted or changed to blob
457 test $mod_dst = 160000 && echo "$errmsg"
458 else
459 if test $mod_src = 160000 -a $mod_dst = 160000
460 then
461 GIT_DIR="$name/.git" \
462 git log --pretty='format: %m %s' \
463 --first-parent $sha1_src...$sha1_dst
464 elif test $mod_dst = 160000
465 then
466 GIT_DIR="$name/.git" \
467 git log --pretty='format: > %s' -1 $sha1_dst
468 else
469 GIT_DIR="$name/.git" \
470 git log --pretty='format: < %s' -1 $sha1_src
471 fi
472 echo
473 fi
474 echo
475 done
28f9af5d 476}
70c7ac22 477#
941987a5 478# List all submodules, prefixed with:
70c7ac22
LH
479# - submodule not initialized
480# + different revision checked out
481#
482# If --cached was specified the revision in the index will be printed
483# instead of the currently checked out revision.
484#
485# $@ = requested paths (default to all)
486#
23a485e3 487cmd_status()
70c7ac22 488{
5c08dbbd
JH
489 # parse $args after "submodule ... status".
490 while test $# -ne 0
491 do
492 case "$1" in
493 -q|--quiet)
494 quiet=1
495 ;;
496 --cached)
497 cached=1
498 ;;
499 --)
500 shift
501 break
502 ;;
503 -*)
504 usage
505 ;;
506 *)
507 break
508 ;;
509 esac
510 shift
511 done
512
70c7ac22
LH
513 git ls-files --stage -- "$@" | grep -e '^160000 ' |
514 while read mode sha1 stage path
515 do
941987a5 516 name=$(module_name "$path") || exit
5be60078 517 url=$(git config submodule."$name".url)
fcbcfe70 518 if test -z "$url" || ! test -d "$path"/.git
70c7ac22
LH
519 then
520 say "-$sha1 $path"
521 continue;
522 fi
41c7c1bd 523 set_name_rev "$path" "$sha1"
70c7ac22
LH
524 if git diff-files --quiet -- "$path"
525 then
bffe71f4 526 say " $sha1 $path$revname"
70c7ac22
LH
527 else
528 if test -z "$cached"
529 then
51884080 530 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
41c7c1bd 531 set_name_rev "$path" "$sha1"
70c7ac22 532 fi
bffe71f4 533 say "+$sha1 $path$revname"
70c7ac22
LH
534 fi
535 done
536}
537
5c08dbbd
JH
538# This loop parses the command line arguments to find the
539# subcommand name to dispatch. Parsing of the subcommand specific
540# options are primarily done by the subcommand implementations.
541# Subcommand specific options such as --branch and --cached are
542# parsed here as well, for backward compatibility.
543
544while test $# != 0 && test -z "$command"
70c7ac22
LH
545do
546 case "$1" in
28f9af5d 547 add | init | update | status | summary)
5c08dbbd 548 command=$1
70c7ac22
LH
549 ;;
550 -q|--quiet)
551 quiet=1
552 ;;
ecda0723
SV
553 -b|--branch)
554 case "$2" in
555 '')
556 usage
557 ;;
558 esac
559 branch="$2"; shift
560 ;;
70c7ac22 561 --cached)
28f9af5d 562 cached="$1"
70c7ac22
LH
563 ;;
564 --)
565 break
566 ;;
567 -*)
568 usage
569 ;;
570 *)
571 break
572 ;;
573 esac
574 shift
575done
576
5c08dbbd
JH
577# No command word defaults to "status"
578test -n "$command" || command=status
579
580# "-b branch" is accepted only by "add"
581if test -n "$branch" && test "$command" != add
582then
ecda0723 583 usage
5c08dbbd
JH
584fi
585
28f9af5d
PY
586# "--cached" is accepted only by "status" and "summary"
587if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbbd 588then
70c7ac22 589 usage
5c08dbbd
JH
590fi
591
592"cmd_$command" "$@"