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