Code

Merge branch 'jn/maint-fast-import-object-reuse' into maint
[git.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
7 dashless=$(basename "$0" | sed -e 's/-/ /')
8 USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
9    or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10    or: $dashless [--quiet] init [--] [<path>...]
11    or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12    or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
13    or: $dashless [--quiet] foreach [--recursive] <command>
14    or: $dashless [--quiet] sync [--] [<path>...]"
15 OPTIONS_SPEC=
16 . git-sh-setup
17 . git-parse-remote
18 require_work_tree
20 command=
21 branch=
22 force=
23 reference=
24 cached=
25 recursive=
26 init=
27 files=
28 nofetch=
29 update=
30 prefix=
32 # Resolve relative url by appending to parent's url
33 resolve_relative_url ()
34 {
35         remote=$(get_default_remote)
36         remoteurl=$(git config "remote.$remote.url") ||
37                 die "remote ($remote) does not have a url defined in .git/config"
38         url="$1"
39         remoteurl=${remoteurl%/}
40         while test -n "$url"
41         do
42                 case "$url" in
43                 ../*)
44                         url="${url#../}"
45                         remoteurl="${remoteurl%/*}"
46                         ;;
47                 ./*)
48                         url="${url#./}"
49                         ;;
50                 *)
51                         break;;
52                 esac
53         done
54         echo "$remoteurl/${url%/}"
55 }
57 #
58 # Get submodule info for registered submodules
59 # $@ = path to limit submodule list
60 #
61 module_list()
62 {
63         git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
64 }
66 #
67 # Map submodule path to submodule name
68 #
69 # $1 = path
70 #
71 module_name()
72 {
73         # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
74         re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
75         name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
76                 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
77        test -z "$name" &&
78        die "No submodule mapping found in .gitmodules for path '$path'"
79        echo "$name"
80 }
82 #
83 # Clone a submodule
84 #
85 # Prior to calling, cmd_update checks that a possibly existing
86 # path is not a git repository.
87 # Likewise, cmd_add checks that path does not exist at all,
88 # since it is the location of a new submodule.
89 #
90 module_clone()
91 {
92         path=$1
93         url=$2
94         reference="$3"
96         if test -n "$reference"
97         then
98                 git-clone "$reference" -n "$url" "$path"
99         else
100                 git-clone -n "$url" "$path"
101         fi ||
102         die "Clone of '$url' into submodule path '$path' failed"
106 # Add a new submodule to the working tree, .gitmodules and the index
108 # $@ = repo path
110 # optional branch is stored in global branch variable
112 cmd_add()
114         # parse $args after "submodule ... add".
115         while test $# -ne 0
116         do
117                 case "$1" in
118                 -b | --branch)
119                         case "$2" in '') usage ;; esac
120                         branch=$2
121                         shift
122                         ;;
123                 -f | --force)
124                         force=$1
125                         ;;
126                 -q|--quiet)
127                         GIT_QUIET=1
128                         ;;
129                 --reference)
130                         case "$2" in '') usage ;; esac
131                         reference="--reference=$2"
132                         shift
133                         ;;
134                 --reference=*)
135                         reference="$1"
136                         shift
137                         ;;
138                 --)
139                         shift
140                         break
141                         ;;
142                 -*)
143                         usage
144                         ;;
145                 *)
146                         break
147                         ;;
148                 esac
149                 shift
150         done
152         repo=$1
153         path=$2
155         if test -z "$path"; then
156                 path=$(echo "$repo" |
157                         sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
158         fi
160         if test -z "$repo" -o -z "$path"; then
161                 usage
162         fi
164         # assure repo is absolute or relative to parent
165         case "$repo" in
166         ./*|../*)
167                 # dereference source url relative to parent's url
168                 realrepo=$(resolve_relative_url "$repo") || exit
169                 ;;
170         *:*|/*)
171                 # absolute url
172                 realrepo=$repo
173                 ;;
174         *)
175                 die "repo URL: '$repo' must be absolute or begin with ./|../"
176         ;;
177         esac
179         # normalize path:
180         # multiple //; leading ./; /./; /../; trailing /
181         path=$(printf '%s/\n' "$path" |
182                 sed -e '
183                         s|//*|/|g
184                         s|^\(\./\)*||
185                         s|/\./|/|g
186                         :start
187                         s|\([^/]*\)/\.\./||
188                         tstart
189                         s|/*$||
190                 ')
191         git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
192         die "'$path' already exists in the index"
194         if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
195         then
196                 echo >&2 "The following path is ignored by one of your .gitignore files:" &&
197                 echo >&2 $path &&
198                 echo >&2 "Use -f if you really want to add it."
199                 exit 1
200         fi
202         # perhaps the path exists and is already a git repo, else clone it
203         if test -e "$path"
204         then
205                 if test -d "$path"/.git -o -f "$path"/.git
206                 then
207                         echo "Adding existing repo at '$path' to the index"
208                 else
209                         die "'$path' already exists and is not a valid git repo"
210                 fi
212                 case "$repo" in
213                 ./*|../*)
214                         url=$(resolve_relative_url "$repo") || exit
215                     ;;
216                 *)
217                         url="$repo"
218                         ;;
219                 esac
220                 git config submodule."$path".url "$url"
221         else
223                 module_clone "$path" "$realrepo" "$reference" || exit
224                 (
225                         clear_local_git_env
226                         cd "$path" &&
227                         # ash fails to wordsplit ${branch:+-b "$branch"...}
228                         case "$branch" in
229                         '') git checkout -f -q ;;
230                         ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
231                         esac
232                 ) || die "Unable to checkout submodule '$path'"
233         fi
235         git add $force "$path" ||
236         die "Failed to add submodule '$path'"
238         git config -f .gitmodules submodule."$path".path "$path" &&
239         git config -f .gitmodules submodule."$path".url "$repo" &&
240         git add --force .gitmodules ||
241         die "Failed to register submodule '$path'"
245 # Execute an arbitrary command sequence in each checked out
246 # submodule
248 # $@ = command to execute
250 cmd_foreach()
252         # parse $args after "submodule ... foreach".
253         while test $# -ne 0
254         do
255                 case "$1" in
256                 -q|--quiet)
257                         GIT_QUIET=1
258                         ;;
259                 --recursive)
260                         recursive=1
261                         ;;
262                 -*)
263                         usage
264                         ;;
265                 *)
266                         break
267                         ;;
268                 esac
269                 shift
270         done
272         toplevel=$(pwd)
274         module_list |
275         while read mode sha1 stage path
276         do
277                 if test -e "$path"/.git
278                 then
279                         say "Entering '$prefix$path'"
280                         name=$(module_name "$path")
281                         (
282                                 prefix="$prefix$path/"
283                                 clear_local_git_env
284                                 cd "$path" &&
285                                 eval "$@" &&
286                                 if test -n "$recursive"
287                                 then
288                                         cmd_foreach "--recursive" "$@"
289                                 fi
290                         ) ||
291                         die "Stopping at '$path'; script returned non-zero status."
292                 fi
293         done
297 # Register submodules in .git/config
299 # $@ = requested paths (default to all)
301 cmd_init()
303         # parse $args after "submodule ... init".
304         while test $# -ne 0
305         do
306                 case "$1" in
307                 -q|--quiet)
308                         GIT_QUIET=1
309                         ;;
310                 --)
311                         shift
312                         break
313                         ;;
314                 -*)
315                         usage
316                         ;;
317                 *)
318                         break
319                         ;;
320                 esac
321                 shift
322         done
324         module_list "$@" |
325         while read mode sha1 stage path
326         do
327                 # Skip already registered paths
328                 name=$(module_name "$path") || exit
329                 url=$(git config submodule."$name".url)
330                 test -z "$url" || continue
332                 url=$(git config -f .gitmodules submodule."$name".url)
333                 test -z "$url" &&
334                 die "No url found for submodule path '$path' in .gitmodules"
336                 # Possibly a url relative to parent
337                 case "$url" in
338                 ./*|../*)
339                         url=$(resolve_relative_url "$url") || exit
340                         ;;
341                 esac
343                 git config submodule."$name".url "$url" ||
344                 die "Failed to register url for submodule path '$path'"
346                 upd="$(git config -f .gitmodules submodule."$name".update)"
347                 test -z "$upd" ||
348                 git config submodule."$name".update "$upd" ||
349                 die "Failed to register update mode for submodule path '$path'"
351                 say "Submodule '$name' ($url) registered for path '$path'"
352         done
356 # Update each submodule path to correct revision, using clone and checkout as needed
358 # $@ = requested paths (default to all)
360 cmd_update()
362         # parse $args after "submodule ... update".
363         orig_args="$@"
364         while test $# -ne 0
365         do
366                 case "$1" in
367                 -q|--quiet)
368                         shift
369                         GIT_QUIET=1
370                         ;;
371                 -i|--init)
372                         init=1
373                         shift
374                         ;;
375                 -N|--no-fetch)
376                         shift
377                         nofetch=1
378                         ;;
379                 -r|--rebase)
380                         shift
381                         update="rebase"
382                         ;;
383                 --reference)
384                         case "$2" in '') usage ;; esac
385                         reference="--reference=$2"
386                         shift 2
387                         ;;
388                 --reference=*)
389                         reference="$1"
390                         shift
391                         ;;
392                 -m|--merge)
393                         shift
394                         update="merge"
395                         ;;
396                 --recursive)
397                         shift
398                         recursive=1
399                         ;;
400                 --)
401                         shift
402                         break
403                         ;;
404                 -*)
405                         usage
406                         ;;
407                 *)
408                         break
409                         ;;
410                 esac
411         done
413         if test -n "$init"
414         then
415                 cmd_init "--" "$@" || return
416         fi
418         module_list "$@" |
419         while read mode sha1 stage path
420         do
421                 name=$(module_name "$path") || exit
422                 url=$(git config submodule."$name".url)
423                 update_module=$(git config submodule."$name".update)
424                 if test -z "$url"
425                 then
426                         # Only mention uninitialized submodules when its
427                         # path have been specified
428                         test "$#" != "0" &&
429                         say "Submodule path '$path' not initialized" &&
430                         say "Maybe you want to use 'update --init'?"
431                         continue
432                 fi
434                 if ! test -d "$path"/.git -o -f "$path"/.git
435                 then
436                         module_clone "$path" "$url" "$reference"|| exit
437                         subsha1=
438                 else
439                         subsha1=$(clear_local_git_env; cd "$path" &&
440                                 git rev-parse --verify HEAD) ||
441                         die "Unable to find current revision in submodule path '$path'"
442                 fi
444                 if ! test -z "$update"
445                 then
446                         update_module=$update
447                 fi
449                 if test "$subsha1" != "$sha1"
450                 then
451                         force=
452                         if test -z "$subsha1"
453                         then
454                                 force="-f"
455                         fi
457                         if test -z "$nofetch"
458                         then
459                                 (clear_local_git_env; cd "$path" &&
460                                         git-fetch) ||
461                                 die "Unable to fetch in submodule path '$path'"
462                         fi
464                         case "$update_module" in
465                         rebase)
466                                 command="git rebase"
467                                 action="rebase"
468                                 msg="rebased onto"
469                                 ;;
470                         merge)
471                                 command="git merge"
472                                 action="merge"
473                                 msg="merged in"
474                                 ;;
475                         *)
476                                 command="git checkout $force -q"
477                                 action="checkout"
478                                 msg="checked out"
479                                 ;;
480                         esac
482                         (clear_local_git_env; cd "$path" && $command "$sha1") ||
483                         die "Unable to $action '$sha1' in submodule path '$path'"
484                         say "Submodule path '$path': $msg '$sha1'"
485                 fi
487                 if test -n "$recursive"
488                 then
489                         (clear_local_git_env; cd "$path" && cmd_update $orig_args) ||
490                         die "Failed to recurse into submodule path '$path'"
491                 fi
492         done
495 set_name_rev () {
496         revname=$( (
497                 clear_local_git_env
498                 cd "$1" && {
499                         git describe "$2" 2>/dev/null ||
500                         git describe --tags "$2" 2>/dev/null ||
501                         git describe --contains "$2" 2>/dev/null ||
502                         git describe --all --always "$2"
503                 }
504         ) )
505         test -z "$revname" || revname=" ($revname)"
508 # Show commit summary for submodules in index or working tree
510 # If '--cached' is given, show summary between index and given commit,
511 # or between working tree and given commit
513 # $@ = [commit (default 'HEAD'),] requested paths (default all)
515 cmd_summary() {
516         summary_limit=-1
517         for_status=
518         diff_cmd=diff-index
520         # parse $args after "submodule ... summary".
521         while test $# -ne 0
522         do
523                 case "$1" in
524                 --cached)
525                         cached="$1"
526                         ;;
527                 --files)
528                         files="$1"
529                         ;;
530                 --for-status)
531                         for_status="$1"
532                         ;;
533                 -n|--summary-limit)
534                         if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
535                         then
536                                 :
537                         else
538                                 usage
539                         fi
540                         shift
541                         ;;
542                 --)
543                         shift
544                         break
545                         ;;
546                 -*)
547                         usage
548                         ;;
549                 *)
550                         break
551                         ;;
552                 esac
553                 shift
554         done
556         test $summary_limit = 0 && return
558         if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
559         then
560                 head=$rev
561                 test $# = 0 || shift
562         elif test -z "$1" -o "$1" = "HEAD"
563         then
564                 # before the first commit: compare with an empty tree
565                 head=$(git hash-object -w -t tree --stdin </dev/null)
566                 test -z "$1" || shift
567         else
568                 head="HEAD"
569         fi
571         if [ -n "$files" ]
572         then
573                 test -n "$cached" &&
574                 die "--cached cannot be used with --files"
575                 diff_cmd=diff-files
576                 head=
577         fi
579         cd_to_toplevel
580         # Get modified modules cared by user
581         modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
582                 sane_egrep '^:([0-7]* )?160000' |
583                 while read mod_src mod_dst sha1_src sha1_dst status name
584                 do
585                         # Always show modules deleted or type-changed (blob<->module)
586                         test $status = D -o $status = T && echo "$name" && continue
587                         # Also show added or modified modules which are checked out
588                         GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
589                         echo "$name"
590                 done
591         )
593         test -z "$modules" && return
595         git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
596         sane_egrep '^:([0-7]* )?160000' |
597         cut -c2- |
598         while read mod_src mod_dst sha1_src sha1_dst status name
599         do
600                 if test -z "$cached" &&
601                         test $sha1_dst = 0000000000000000000000000000000000000000
602                 then
603                         case "$mod_dst" in
604                         160000)
605                                 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
606                                 ;;
607                         100644 | 100755 | 120000)
608                                 sha1_dst=$(git hash-object $name)
609                                 ;;
610                         000000)
611                                 ;; # removed
612                         *)
613                                 # unexpected type
614                                 echo >&2 "unexpected mode $mod_dst"
615                                 continue ;;
616                         esac
617                 fi
618                 missing_src=
619                 missing_dst=
621                 test $mod_src = 160000 &&
622                 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
623                 missing_src=t
625                 test $mod_dst = 160000 &&
626                 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
627                 missing_dst=t
629                 total_commits=
630                 case "$missing_src,$missing_dst" in
631                 t,)
632                         errmsg="  Warn: $name doesn't contain commit $sha1_src"
633                         ;;
634                 ,t)
635                         errmsg="  Warn: $name doesn't contain commit $sha1_dst"
636                         ;;
637                 t,t)
638                         errmsg="  Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
639                         ;;
640                 *)
641                         errmsg=
642                         total_commits=$(
643                         if test $mod_src = 160000 -a $mod_dst = 160000
644                         then
645                                 range="$sha1_src...$sha1_dst"
646                         elif test $mod_src = 160000
647                         then
648                                 range=$sha1_src
649                         else
650                                 range=$sha1_dst
651                         fi
652                         GIT_DIR="$name/.git" \
653                         git rev-list --first-parent $range -- | wc -l
654                         )
655                         total_commits=" ($(($total_commits + 0)))"
656                         ;;
657                 esac
659                 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
660                 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
661                 if test $status = T
662                 then
663                         if test $mod_dst = 160000
664                         then
665                                 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
666                         else
667                                 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
668                         fi
669                 else
670                         echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
671                 fi
672                 if test -n "$errmsg"
673                 then
674                         # Don't give error msg for modification whose dst is not submodule
675                         # i.e. deleted or changed to blob
676                         test $mod_dst = 160000 && echo "$errmsg"
677                 else
678                         if test $mod_src = 160000 -a $mod_dst = 160000
679                         then
680                                 limit=
681                                 test $summary_limit -gt 0 && limit="-$summary_limit"
682                                 GIT_DIR="$name/.git" \
683                                 git log $limit --pretty='format:  %m %s' \
684                                 --first-parent $sha1_src...$sha1_dst
685                         elif test $mod_dst = 160000
686                         then
687                                 GIT_DIR="$name/.git" \
688                                 git log --pretty='format:  > %s' -1 $sha1_dst
689                         else
690                                 GIT_DIR="$name/.git" \
691                                 git log --pretty='format:  < %s' -1 $sha1_src
692                         fi
693                         echo
694                 fi
695                 echo
696         done |
697         if test -n "$for_status"; then
698                 if [ -n "$files" ]; then
699                         echo "# Submodules changed but not updated:"
700                 else
701                         echo "# Submodule changes to be committed:"
702                 fi
703                 echo "#"
704                 sed -e 's|^|# |' -e 's|^# $|#|'
705         else
706                 cat
707         fi
710 # List all submodules, prefixed with:
711 #  - submodule not initialized
712 #  + different revision checked out
714 # If --cached was specified the revision in the index will be printed
715 # instead of the currently checked out revision.
717 # $@ = requested paths (default to all)
719 cmd_status()
721         # parse $args after "submodule ... status".
722         orig_args="$@"
723         while test $# -ne 0
724         do
725                 case "$1" in
726                 -q|--quiet)
727                         GIT_QUIET=1
728                         ;;
729                 --cached)
730                         cached=1
731                         ;;
732                 --recursive)
733                         recursive=1
734                         ;;
735                 --)
736                         shift
737                         break
738                         ;;
739                 -*)
740                         usage
741                         ;;
742                 *)
743                         break
744                         ;;
745                 esac
746                 shift
747         done
749         module_list "$@" |
750         while read mode sha1 stage path
751         do
752                 name=$(module_name "$path") || exit
753                 url=$(git config submodule."$name".url)
754                 displaypath="$prefix$path"
755                 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
756                 then
757                         say "-$sha1 $displaypath"
758                         continue;
759                 fi
760                 set_name_rev "$path" "$sha1"
761                 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
762                 then
763                         say " $sha1 $displaypath$revname"
764                 else
765                         if test -z "$cached"
766                         then
767                                 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
768                                 set_name_rev "$path" "$sha1"
769                         fi
770                         say "+$sha1 $displaypath$revname"
771                 fi
773                 if test -n "$recursive"
774                 then
775                         (
776                                 prefix="$displaypath/"
777                                 clear_local_git_env
778                                 cd "$path" &&
779                                 cmd_status $orig_args
780                         ) ||
781                         die "Failed to recurse into submodule path '$path'"
782                 fi
783         done
786 # Sync remote urls for submodules
787 # This makes the value for remote.$remote.url match the value
788 # specified in .gitmodules.
790 cmd_sync()
792         while test $# -ne 0
793         do
794                 case "$1" in
795                 -q|--quiet)
796                         GIT_QUIET=1
797                         shift
798                         ;;
799                 --)
800                         shift
801                         break
802                         ;;
803                 -*)
804                         usage
805                         ;;
806                 *)
807                         break
808                         ;;
809                 esac
810         done
811         cd_to_toplevel
812         module_list "$@" |
813         while read mode sha1 stage path
814         do
815                 name=$(module_name "$path")
816                 url=$(git config -f .gitmodules --get submodule."$name".url)
818                 # Possibly a url relative to parent
819                 case "$url" in
820                 ./*|../*)
821                         url=$(resolve_relative_url "$url") || exit
822                         ;;
823                 esac
825                 say "Synchronizing submodule url for '$name'"
826                 git config submodule."$name".url "$url"
828                 if test -e "$path"/.git
829                 then
830                 (
831                         clear_local_git_env
832                         cd "$path"
833                         remote=$(get_default_remote)
834                         git config remote."$remote".url "$url"
835                 )
836                 fi
837         done
840 # This loop parses the command line arguments to find the
841 # subcommand name to dispatch.  Parsing of the subcommand specific
842 # options are primarily done by the subcommand implementations.
843 # Subcommand specific options such as --branch and --cached are
844 # parsed here as well, for backward compatibility.
846 while test $# != 0 && test -z "$command"
847 do
848         case "$1" in
849         add | foreach | init | update | status | summary | sync)
850                 command=$1
851                 ;;
852         -q|--quiet)
853                 GIT_QUIET=1
854                 ;;
855         -b|--branch)
856                 case "$2" in
857                 '')
858                         usage
859                         ;;
860                 esac
861                 branch="$2"; shift
862                 ;;
863         --cached)
864                 cached="$1"
865                 ;;
866         --)
867                 break
868                 ;;
869         -*)
870                 usage
871                 ;;
872         *)
873                 break
874                 ;;
875         esac
876         shift
877 done
879 # No command word defaults to "status"
880 test -n "$command" || command=status
882 # "-b branch" is accepted only by "add"
883 if test -n "$branch" && test "$command" != add
884 then
885         usage
886 fi
888 # "--cached" is accepted only by "status" and "summary"
889 if test -n "$cached" && test "$command" != status -a "$command" != summary
890 then
891         usage
892 fi
894 "cmd_$command" "$@"