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