git-submodule.shon commit Merge branch 'jc/maint-checkout-fileglob-doc' into maint-1.7.11 (6711759)
   1#!/bin/sh
   2#
   3# git-submodule.sh: add, init, update or list git submodules
   4#
   5# Copyright (c) 2007 Lars Hjemli
   6
   7dashless=$(basename "$0" | sed -e 's/-/ /')
   8USAGE="[--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] [-f|--force] [--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>...]"
  15OPTIONS_SPEC=
  16. git-sh-setup
  17. git-sh-i18n
  18. git-parse-remote
  19require_work_tree
  20
  21command=
  22branch=
  23force=
  24reference=
  25cached=
  26recursive=
  27init=
  28files=
  29nofetch=
  30update=
  31prefix=
  32
  33# Resolve relative url by appending to parent's url
  34resolve_relative_url ()
  35{
  36        remote=$(get_default_remote)
  37        remoteurl=$(git config "remote.$remote.url") ||
  38                remoteurl=$(pwd) # the repository is its own authoritative upstream
  39        url="$1"
  40        remoteurl=${remoteurl%/}
  41        sep=/
  42        while test -n "$url"
  43        do
  44                case "$url" in
  45                ../*)
  46                        url="${url#../}"
  47                        case "$remoteurl" in
  48                        */*)
  49                                remoteurl="${remoteurl%/*}"
  50                                ;;
  51                        *:*)
  52                                remoteurl="${remoteurl%:*}"
  53                                sep=:
  54                                ;;
  55                        *)
  56                                die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
  57                                ;;
  58                        esac
  59                        ;;
  60                ./*)
  61                        url="${url#./}"
  62                        ;;
  63                *)
  64                        break;;
  65                esac
  66        done
  67        echo "$remoteurl$sep${url%/}"
  68}
  69
  70#
  71# Get submodule info for registered submodules
  72# $@ = path to limit submodule list
  73#
  74module_list()
  75{
  76        (
  77                git ls-files --error-unmatch --stage -- "$@" ||
  78                echo "unmatched pathspec exists"
  79        ) |
  80        perl -e '
  81        my %unmerged = ();
  82        my ($null_sha1) = ("0" x 40);
  83        my @out = ();
  84        my $unmatched = 0;
  85        while (<STDIN>) {
  86                if (/^unmatched pathspec/) {
  87                        $unmatched = 1;
  88                        next;
  89                }
  90                chomp;
  91                my ($mode, $sha1, $stage, $path) =
  92                        /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
  93                next unless $mode eq "160000";
  94                if ($stage ne "0") {
  95                        if (!$unmerged{$path}++) {
  96                                push @out, "$mode $null_sha1 U\t$path\n";
  97                        }
  98                        next;
  99                }
 100                push @out, "$_\n";
 101        }
 102        if ($unmatched) {
 103                print "#unmatched\n";
 104        } else {
 105                print for (@out);
 106        }
 107        '
 108}
 109
 110die_if_unmatched ()
 111{
 112        if test "$1" = "#unmatched"
 113        then
 114                exit 1
 115        fi
 116}
 117
 118#
 119# Map submodule path to submodule name
 120#
 121# $1 = path
 122#
 123module_name()
 124{
 125        # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
 126        sm_path="$1"
 127        re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
 128        name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
 129                sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
 130        test -z "$name" &&
 131        die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
 132        echo "$name"
 133}
 134
 135#
 136# Clone a submodule
 137#
 138# Prior to calling, cmd_update checks that a possibly existing
 139# path is not a git repository.
 140# Likewise, cmd_add checks that path does not exist at all,
 141# since it is the location of a new submodule.
 142#
 143module_clone()
 144{
 145        sm_path=$1
 146        url=$2
 147        reference="$3"
 148        quiet=
 149        if test -n "$GIT_QUIET"
 150        then
 151                quiet=-q
 152        fi
 153
 154        gitdir=
 155        gitdir_base=
 156        name=$(module_name "$sm_path" 2>/dev/null)
 157        test -n "$name" || name="$sm_path"
 158        base_name=$(dirname "$name")
 159
 160        gitdir=$(git rev-parse --git-dir)
 161        gitdir_base="$gitdir/modules/$base_name"
 162        gitdir="$gitdir/modules/$name"
 163
 164        if test -d "$gitdir"
 165        then
 166                mkdir -p "$sm_path"
 167                rm -f "$gitdir/index"
 168        else
 169                mkdir -p "$gitdir_base"
 170                git clone $quiet -n ${reference:+"$reference"} \
 171                        --separate-git-dir "$gitdir" "$url" "$sm_path" ||
 172                die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
 173        fi
 174
 175        # We already are at the root of the work tree but cd_to_toplevel will
 176        # resolve any symlinks that might be present in $PWD
 177        a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
 178        b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
 179        # normalize Windows-style absolute paths to POSIX-style absolute paths
 180        case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
 181        case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
 182        # Remove all common leading directories after a sanity check
 183        if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
 184                die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
 185        fi
 186        while test "${a%%/*}" = "${b%%/*}"
 187        do
 188                a=${a#*/}
 189                b=${b#*/}
 190        done
 191        # Now chop off the trailing '/'s that were added in the beginning
 192        a=${a%/}
 193        b=${b%/}
 194
 195        # Turn each leading "*/" component into "../"
 196        rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
 197        echo "gitdir: $rel/$a" >"$sm_path/.git"
 198
 199        rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
 200        (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
 201}
 202
 203#
 204# Add a new submodule to the working tree, .gitmodules and the index
 205#
 206# $@ = repo path
 207#
 208# optional branch is stored in global branch variable
 209#
 210cmd_add()
 211{
 212        # parse $args after "submodule ... add".
 213        while test $# -ne 0
 214        do
 215                case "$1" in
 216                -b | --branch)
 217                        case "$2" in '') usage ;; esac
 218                        branch=$2
 219                        shift
 220                        ;;
 221                -f | --force)
 222                        force=$1
 223                        ;;
 224                -q|--quiet)
 225                        GIT_QUIET=1
 226                        ;;
 227                --reference)
 228                        case "$2" in '') usage ;; esac
 229                        reference="--reference=$2"
 230                        shift
 231                        ;;
 232                --reference=*)
 233                        reference="$1"
 234                        shift
 235                        ;;
 236                --)
 237                        shift
 238                        break
 239                        ;;
 240                -*)
 241                        usage
 242                        ;;
 243                *)
 244                        break
 245                        ;;
 246                esac
 247                shift
 248        done
 249
 250        repo=$1
 251        sm_path=$2
 252
 253        if test -z "$sm_path"; then
 254                sm_path=$(echo "$repo" |
 255                        sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 256        fi
 257
 258        if test -z "$repo" -o -z "$sm_path"; then
 259                usage
 260        fi
 261
 262        # assure repo is absolute or relative to parent
 263        case "$repo" in
 264        ./*|../*)
 265                # dereference source url relative to parent's url
 266                realrepo=$(resolve_relative_url "$repo") || exit
 267                ;;
 268        *:*|/*)
 269                # absolute url
 270                realrepo=$repo
 271                ;;
 272        *)
 273                die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
 274        ;;
 275        esac
 276
 277        # normalize path:
 278        # multiple //; leading ./; /./; /../; trailing /
 279        sm_path=$(printf '%s/\n' "$sm_path" |
 280                sed -e '
 281                        s|//*|/|g
 282                        s|^\(\./\)*||
 283                        s|/\./|/|g
 284                        :start
 285                        s|\([^/]*\)/\.\./||
 286                        tstart
 287                        s|/*$||
 288                ')
 289        git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
 290        die "$(eval_gettext "'\$sm_path' already exists in the index")"
 291
 292        if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
 293        then
 294                eval_gettextln "The following path is ignored by one of your .gitignore files:
 295\$sm_path
 296Use -f if you really want to add it." >&2
 297                exit 1
 298        fi
 299
 300        # perhaps the path exists and is already a git repo, else clone it
 301        if test -e "$sm_path"
 302        then
 303                if test -d "$sm_path"/.git -o -f "$sm_path"/.git
 304                then
 305                        eval_gettextln "Adding existing repo at '\$sm_path' to the index"
 306                else
 307                        die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
 308                fi
 309
 310        else
 311
 312                module_clone "$sm_path" "$realrepo" "$reference" || exit
 313                (
 314                        clear_local_git_env
 315                        cd "$sm_path" &&
 316                        # ash fails to wordsplit ${branch:+-b "$branch"...}
 317                        case "$branch" in
 318                        '') git checkout -f -q ;;
 319                        ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
 320                        esac
 321                ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
 322        fi
 323        git config submodule."$sm_path".url "$realrepo"
 324
 325        git add $force "$sm_path" ||
 326        die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
 327
 328        git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
 329        git config -f .gitmodules submodule."$sm_path".url "$repo" &&
 330        git add --force .gitmodules ||
 331        die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
 332}
 333
 334#
 335# Execute an arbitrary command sequence in each checked out
 336# submodule
 337#
 338# $@ = command to execute
 339#
 340cmd_foreach()
 341{
 342        # parse $args after "submodule ... foreach".
 343        while test $# -ne 0
 344        do
 345                case "$1" in
 346                -q|--quiet)
 347                        GIT_QUIET=1
 348                        ;;
 349                --recursive)
 350                        recursive=1
 351                        ;;
 352                -*)
 353                        usage
 354                        ;;
 355                *)
 356                        break
 357                        ;;
 358                esac
 359                shift
 360        done
 361
 362        toplevel=$(pwd)
 363
 364        # dup stdin so that it can be restored when running the external
 365        # command in the subshell (and a recursive call to this function)
 366        exec 3<&0
 367
 368        module_list |
 369        while read mode sha1 stage sm_path
 370        do
 371                die_if_unmatched "$mode"
 372                if test -e "$sm_path"/.git
 373                then
 374                        say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
 375                        name=$(module_name "$sm_path")
 376                        (
 377                                prefix="$prefix$sm_path/"
 378                                clear_local_git_env
 379                                # we make $path available to scripts ...
 380                                path=$sm_path
 381                                cd "$sm_path" &&
 382                                eval "$@" &&
 383                                if test -n "$recursive"
 384                                then
 385                                        cmd_foreach "--recursive" "$@"
 386                                fi
 387                        ) <&3 3<&- ||
 388                        die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
 389                fi
 390        done
 391}
 392
 393#
 394# Register submodules in .git/config
 395#
 396# $@ = requested paths (default to all)
 397#
 398cmd_init()
 399{
 400        # parse $args after "submodule ... init".
 401        while test $# -ne 0
 402        do
 403                case "$1" in
 404                -q|--quiet)
 405                        GIT_QUIET=1
 406                        ;;
 407                --)
 408                        shift
 409                        break
 410                        ;;
 411                -*)
 412                        usage
 413                        ;;
 414                *)
 415                        break
 416                        ;;
 417                esac
 418                shift
 419        done
 420
 421        module_list "$@" |
 422        while read mode sha1 stage sm_path
 423        do
 424                die_if_unmatched "$mode"
 425                name=$(module_name "$sm_path") || exit
 426
 427                # Copy url setting when it is not set yet
 428                if test -z "$(git config "submodule.$name.url")"
 429                then
 430                        url=$(git config -f .gitmodules submodule."$name".url)
 431                        test -z "$url" &&
 432                        die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
 433
 434                        # Possibly a url relative to parent
 435                        case "$url" in
 436                        ./*|../*)
 437                                url=$(resolve_relative_url "$url") || exit
 438                                ;;
 439                        esac
 440                        git config submodule."$name".url "$url" ||
 441                        die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
 442
 443                        say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
 444                fi
 445
 446                # Copy "update" setting when it is not set yet
 447                upd="$(git config -f .gitmodules submodule."$name".update)"
 448                test -z "$upd" ||
 449                test -n "$(git config submodule."$name".update)" ||
 450                git config submodule."$name".update "$upd" ||
 451                die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
 452        done
 453}
 454
 455#
 456# Update each submodule path to correct revision, using clone and checkout as needed
 457#
 458# $@ = requested paths (default to all)
 459#
 460cmd_update()
 461{
 462        # parse $args after "submodule ... update".
 463        orig_flags=
 464        while test $# -ne 0
 465        do
 466                case "$1" in
 467                -q|--quiet)
 468                        GIT_QUIET=1
 469                        ;;
 470                -i|--init)
 471                        init=1
 472                        ;;
 473                -N|--no-fetch)
 474                        nofetch=1
 475                        ;;
 476                -f|--force)
 477                        force=$1
 478                        ;;
 479                -r|--rebase)
 480                        update="rebase"
 481                        ;;
 482                --reference)
 483                        case "$2" in '') usage ;; esac
 484                        reference="--reference=$2"
 485                        orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 486                        shift
 487                        ;;
 488                --reference=*)
 489                        reference="$1"
 490                        ;;
 491                -m|--merge)
 492                        update="merge"
 493                        ;;
 494                --recursive)
 495                        recursive=1
 496                        ;;
 497                --checkout)
 498                        update="checkout"
 499                        ;;
 500                --)
 501                        shift
 502                        break
 503                        ;;
 504                -*)
 505                        usage
 506                        ;;
 507                *)
 508                        break
 509                        ;;
 510                esac
 511                orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 512                shift
 513        done
 514
 515        if test -n "$init"
 516        then
 517                cmd_init "--" "$@" || return
 518        fi
 519
 520        cloned_modules=
 521        module_list "$@" | {
 522        err=
 523        while read mode sha1 stage sm_path
 524        do
 525                die_if_unmatched "$mode"
 526                if test "$stage" = U
 527                then
 528                        echo >&2 "Skipping unmerged submodule $sm_path"
 529                        continue
 530                fi
 531                name=$(module_name "$sm_path") || exit
 532                url=$(git config submodule."$name".url)
 533                if ! test -z "$update"
 534                then
 535                        update_module=$update
 536                else
 537                        update_module=$(git config submodule."$name".update)
 538                fi
 539
 540                if test "$update_module" = "none"
 541                then
 542                        echo "Skipping submodule '$sm_path'"
 543                        continue
 544                fi
 545
 546                if test -z "$url"
 547                then
 548                        # Only mention uninitialized submodules when its
 549                        # path have been specified
 550                        test "$#" != "0" &&
 551                        say "$(eval_gettext "Submodule path '\$sm_path' not initialized
 552Maybe you want to use 'update --init'?")"
 553                        continue
 554                fi
 555
 556                if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
 557                then
 558                        module_clone "$sm_path" "$url" "$reference"|| exit
 559                        cloned_modules="$cloned_modules;$name"
 560                        subsha1=
 561                else
 562                        subsha1=$(clear_local_git_env; cd "$sm_path" &&
 563                                git rev-parse --verify HEAD) ||
 564                        die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
 565                fi
 566
 567                if test "$subsha1" != "$sha1" -o -n "$force"
 568                then
 569                        subforce=$force
 570                        # If we don't already have a -f flag and the submodule has never been checked out
 571                        if test -z "$subsha1" -a -z "$force"
 572                        then
 573                                subforce="-f"
 574                        fi
 575
 576                        if test -z "$nofetch"
 577                        then
 578                                # Run fetch only if $sha1 isn't present or it
 579                                # is not reachable from a ref.
 580                                (clear_local_git_env; cd "$sm_path" &&
 581                                        ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
 582                                         test -z "$rev") || git-fetch)) ||
 583                                die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
 584                        fi
 585
 586                        # Is this something we just cloned?
 587                        case ";$cloned_modules;" in
 588                        *";$name;"*)
 589                                # then there is no local change to integrate
 590                                update_module= ;;
 591                        esac
 592
 593                        must_die_on_failure=
 594                        case "$update_module" in
 595                        rebase)
 596                                command="git rebase"
 597                                die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
 598                                say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
 599                                must_die_on_failure=yes
 600                                ;;
 601                        merge)
 602                                command="git merge"
 603                                die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
 604                                say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
 605                                must_die_on_failure=yes
 606                                ;;
 607                        *)
 608                                command="git checkout $subforce -q"
 609                                die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
 610                                say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
 611                                ;;
 612                        esac
 613
 614                        if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
 615                        then
 616                                say "$say_msg"
 617                        elif test -n "$must_die_on_failure"
 618                        then
 619                                die_with_status 2 "$die_msg"
 620                        else
 621                                err="${err};$die_msg"
 622                                continue
 623                        fi
 624                fi
 625
 626                if test -n "$recursive"
 627                then
 628                        (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
 629                        res=$?
 630                        if test $res -gt 0
 631                        then
 632                                die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
 633                                if test $res -eq 1
 634                                then
 635                                        err="${err};$die_msg"
 636                                        continue
 637                                else
 638                                        die_with_status $res "$die_msg"
 639                                fi
 640                        fi
 641                fi
 642        done
 643
 644        if test -n "$err"
 645        then
 646                OIFS=$IFS
 647                IFS=';'
 648                for e in $err
 649                do
 650                        if test -n "$e"
 651                        then
 652                                echo >&2 "$e"
 653                        fi
 654                done
 655                IFS=$OIFS
 656                exit 1
 657        fi
 658        }
 659}
 660
 661set_name_rev () {
 662        revname=$( (
 663                clear_local_git_env
 664                cd "$1" && {
 665                        git describe "$2" 2>/dev/null ||
 666                        git describe --tags "$2" 2>/dev/null ||
 667                        git describe --contains "$2" 2>/dev/null ||
 668                        git describe --all --always "$2"
 669                }
 670        ) )
 671        test -z "$revname" || revname=" ($revname)"
 672}
 673#
 674# Show commit summary for submodules in index or working tree
 675#
 676# If '--cached' is given, show summary between index and given commit,
 677# or between working tree and given commit
 678#
 679# $@ = [commit (default 'HEAD'),] requested paths (default all)
 680#
 681cmd_summary() {
 682        summary_limit=-1
 683        for_status=
 684        diff_cmd=diff-index
 685
 686        # parse $args after "submodule ... summary".
 687        while test $# -ne 0
 688        do
 689                case "$1" in
 690                --cached)
 691                        cached="$1"
 692                        ;;
 693                --files)
 694                        files="$1"
 695                        ;;
 696                --for-status)
 697                        for_status="$1"
 698                        ;;
 699                -n|--summary-limit)
 700                        if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
 701                        then
 702                                :
 703                        else
 704                                usage
 705                        fi
 706                        shift
 707                        ;;
 708                --)
 709                        shift
 710                        break
 711                        ;;
 712                -*)
 713                        usage
 714                        ;;
 715                *)
 716                        break
 717                        ;;
 718                esac
 719                shift
 720        done
 721
 722        test $summary_limit = 0 && return
 723
 724        if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
 725        then
 726                head=$rev
 727                test $# = 0 || shift
 728        elif test -z "$1" -o "$1" = "HEAD"
 729        then
 730                # before the first commit: compare with an empty tree
 731                head=$(git hash-object -w -t tree --stdin </dev/null)
 732                test -z "$1" || shift
 733        else
 734                head="HEAD"
 735        fi
 736
 737        if [ -n "$files" ]
 738        then
 739                test -n "$cached" &&
 740                die "$(gettext -- "--cached cannot be used with --files")"
 741                diff_cmd=diff-files
 742                head=
 743        fi
 744
 745        cd_to_toplevel
 746        # Get modified modules cared by user
 747        modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
 748                sane_egrep '^:([0-7]* )?160000' |
 749                while read mod_src mod_dst sha1_src sha1_dst status name
 750                do
 751                        # Always show modules deleted or type-changed (blob<->module)
 752                        test $status = D -o $status = T && echo "$name" && continue
 753                        # Also show added or modified modules which are checked out
 754                        GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
 755                        echo "$name"
 756                done
 757        )
 758
 759        test -z "$modules" && return
 760
 761        git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
 762        sane_egrep '^:([0-7]* )?160000' |
 763        cut -c2- |
 764        while read mod_src mod_dst sha1_src sha1_dst status name
 765        do
 766                if test -z "$cached" &&
 767                        test $sha1_dst = 0000000000000000000000000000000000000000
 768                then
 769                        case "$mod_dst" in
 770                        160000)
 771                                sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
 772                                ;;
 773                        100644 | 100755 | 120000)
 774                                sha1_dst=$(git hash-object $name)
 775                                ;;
 776                        000000)
 777                                ;; # removed
 778                        *)
 779                                # unexpected type
 780                                eval_gettextln "unexpected mode \$mod_dst" >&2
 781                                continue ;;
 782                        esac
 783                fi
 784                missing_src=
 785                missing_dst=
 786
 787                test $mod_src = 160000 &&
 788                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
 789                missing_src=t
 790
 791                test $mod_dst = 160000 &&
 792                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
 793                missing_dst=t
 794
 795                total_commits=
 796                case "$missing_src,$missing_dst" in
 797                t,)
 798                        errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_src")"
 799                        ;;
 800                ,t)
 801                        errmsg="$(eval_gettext "  Warn: \$name doesn't contain commit \$sha1_dst")"
 802                        ;;
 803                t,t)
 804                        errmsg="$(eval_gettext "  Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
 805                        ;;
 806                *)
 807                        errmsg=
 808                        total_commits=$(
 809                        if test $mod_src = 160000 -a $mod_dst = 160000
 810                        then
 811                                range="$sha1_src...$sha1_dst"
 812                        elif test $mod_src = 160000
 813                        then
 814                                range=$sha1_src
 815                        else
 816                                range=$sha1_dst
 817                        fi
 818                        GIT_DIR="$name/.git" \
 819                        git rev-list --first-parent $range -- | wc -l
 820                        )
 821                        total_commits=" ($(($total_commits + 0)))"
 822                        ;;
 823                esac
 824
 825                sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
 826                sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
 827                if test $status = T
 828                then
 829                        blob="$(gettext "blob")"
 830                        submodule="$(gettext "submodule")"
 831                        if test $mod_dst = 160000
 832                        then
 833                                echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
 834                        else
 835                                echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
 836                        fi
 837                else
 838                        echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
 839                fi
 840                if test -n "$errmsg"
 841                then
 842                        # Don't give error msg for modification whose dst is not submodule
 843                        # i.e. deleted or changed to blob
 844                        test $mod_dst = 160000 && echo "$errmsg"
 845                else
 846                        if test $mod_src = 160000 -a $mod_dst = 160000
 847                        then
 848                                limit=
 849                                test $summary_limit -gt 0 && limit="-$summary_limit"
 850                                GIT_DIR="$name/.git" \
 851                                git log $limit --pretty='format:  %m %s' \
 852                                --first-parent $sha1_src...$sha1_dst
 853                        elif test $mod_dst = 160000
 854                        then
 855                                GIT_DIR="$name/.git" \
 856                                git log --pretty='format:  > %s' -1 $sha1_dst
 857                        else
 858                                GIT_DIR="$name/.git" \
 859                                git log --pretty='format:  < %s' -1 $sha1_src
 860                        fi
 861                        echo
 862                fi
 863                echo
 864        done |
 865        if test -n "$for_status"; then
 866                if [ -n "$files" ]; then
 867                        gettextln "# Submodules changed but not updated:"
 868                else
 869                        gettextln "# Submodule changes to be committed:"
 870                fi
 871                echo "#"
 872                sed -e 's|^|# |' -e 's|^# $|#|'
 873        else
 874                cat
 875        fi
 876}
 877#
 878# List all submodules, prefixed with:
 879#  - submodule not initialized
 880#  + different revision checked out
 881#
 882# If --cached was specified the revision in the index will be printed
 883# instead of the currently checked out revision.
 884#
 885# $@ = requested paths (default to all)
 886#
 887cmd_status()
 888{
 889        # parse $args after "submodule ... status".
 890        orig_flags=
 891        while test $# -ne 0
 892        do
 893                case "$1" in
 894                -q|--quiet)
 895                        GIT_QUIET=1
 896                        ;;
 897                --cached)
 898                        cached=1
 899                        ;;
 900                --recursive)
 901                        recursive=1
 902                        ;;
 903                --)
 904                        shift
 905                        break
 906                        ;;
 907                -*)
 908                        usage
 909                        ;;
 910                *)
 911                        break
 912                        ;;
 913                esac
 914                orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
 915                shift
 916        done
 917
 918        module_list "$@" |
 919        while read mode sha1 stage sm_path
 920        do
 921                die_if_unmatched "$mode"
 922                name=$(module_name "$sm_path") || exit
 923                url=$(git config submodule."$name".url)
 924                displaypath="$prefix$sm_path"
 925                if test "$stage" = U
 926                then
 927                        say "U$sha1 $displaypath"
 928                        continue
 929                fi
 930                if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
 931                then
 932                        say "-$sha1 $displaypath"
 933                        continue;
 934                fi
 935                set_name_rev "$sm_path" "$sha1"
 936                if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
 937                then
 938                        say " $sha1 $displaypath$revname"
 939                else
 940                        if test -z "$cached"
 941                        then
 942                                sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
 943                                set_name_rev "$sm_path" "$sha1"
 944                        fi
 945                        say "+$sha1 $displaypath$revname"
 946                fi
 947
 948                if test -n "$recursive"
 949                then
 950                        (
 951                                prefix="$displaypath/"
 952                                clear_local_git_env
 953                                cd "$sm_path" &&
 954                                eval cmd_status "$orig_args"
 955                        ) ||
 956                        die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
 957                fi
 958        done
 959}
 960#
 961# Sync remote urls for submodules
 962# This makes the value for remote.$remote.url match the value
 963# specified in .gitmodules.
 964#
 965cmd_sync()
 966{
 967        while test $# -ne 0
 968        do
 969                case "$1" in
 970                -q|--quiet)
 971                        GIT_QUIET=1
 972                        shift
 973                        ;;
 974                --)
 975                        shift
 976                        break
 977                        ;;
 978                -*)
 979                        usage
 980                        ;;
 981                *)
 982                        break
 983                        ;;
 984                esac
 985        done
 986        cd_to_toplevel
 987        module_list "$@" |
 988        while read mode sha1 stage sm_path
 989        do
 990                die_if_unmatched "$mode"
 991                name=$(module_name "$sm_path")
 992                url=$(git config -f .gitmodules --get submodule."$name".url)
 993
 994                # Possibly a url relative to parent
 995                case "$url" in
 996                ./*|../*)
 997                        url=$(resolve_relative_url "$url") || exit
 998                        ;;
 999                esac
1000
1001                if git config "submodule.$name.url" >/dev/null 2>/dev/null
1002                then
1003                        say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
1004                        git config submodule."$name".url "$url"
1005
1006                        if test -e "$sm_path"/.git
1007                        then
1008                        (
1009                                clear_local_git_env
1010                                cd "$sm_path"
1011                                remote=$(get_default_remote)
1012                                git config remote."$remote".url "$url"
1013                        )
1014                        fi
1015                fi
1016        done
1017}
1018
1019# This loop parses the command line arguments to find the
1020# subcommand name to dispatch.  Parsing of the subcommand specific
1021# options are primarily done by the subcommand implementations.
1022# Subcommand specific options such as --branch and --cached are
1023# parsed here as well, for backward compatibility.
1024
1025while test $# != 0 && test -z "$command"
1026do
1027        case "$1" in
1028        add | foreach | init | update | status | summary | sync)
1029                command=$1
1030                ;;
1031        -q|--quiet)
1032                GIT_QUIET=1
1033                ;;
1034        -b|--branch)
1035                case "$2" in
1036                '')
1037                        usage
1038                        ;;
1039                esac
1040                branch="$2"; shift
1041                ;;
1042        --cached)
1043                cached="$1"
1044                ;;
1045        --)
1046                break
1047                ;;
1048        -*)
1049                usage
1050                ;;
1051        *)
1052                break
1053                ;;
1054        esac
1055        shift
1056done
1057
1058# No command word defaults to "status"
1059test -n "$command" || command=status
1060
1061# "-b branch" is accepted only by "add"
1062if test -n "$branch" && test "$command" != add
1063then
1064        usage
1065fi
1066
1067# "--cached" is accepted only by "status" and "summary"
1068if test -n "$cached" && test "$command" != status -a "$command" != summary
1069then
1070        usage
1071fi
1072
1073"cmd_$command" "$@"