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