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