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