git-submodule.shon commit commit_ref_update(): remove the flags parameter (5d9b2de)
   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] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
   9   or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
  10   or: $dashless [--quiet] init [--] [<path>...]
  11   or: $dashless [--quiet] deinit [-f|--force] [--] <path>...
  12   or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--reference <repository>] [--recursive] [--] [<path>...]
  13   or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
  14   or: $dashless [--quiet] foreach [--recursive] <command>
  15   or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
  16OPTIONS_SPEC=
  17SUBDIRECTORY_OK=Yes
  18. git-sh-setup
  19. git-sh-i18n
  20. git-parse-remote
  21require_work_tree
  22wt_prefix=$(git rev-parse --show-prefix)
  23cd_to_toplevel
  24
  25# Restrict ourselves to a vanilla subset of protocols; the URLs
  26# we get are under control of a remote repository, and we do not
  27# want them kicking off arbitrary git-remote-* programs.
  28#
  29# If the user has already specified a set of allowed protocols,
  30# we assume they know what they're doing and use that instead.
  31: ${GIT_ALLOW_PROTOCOL=file:git:http:https:ssh}
  32export GIT_ALLOW_PROTOCOL
  33
  34command=
  35branch=
  36force=
  37reference=
  38cached=
  39recursive=
  40init=
  41files=
  42remote=
  43nofetch=
  44update=
  45prefix=
  46custom_name=
  47depth=
  48
  49# The function takes at most 2 arguments. The first argument is the
  50# URL that navigates to the submodule origin repo. When relative, this URL
  51# is relative to the superproject origin URL repo. The second up_path
  52# argument, if specified, is the relative path that navigates
  53# from the submodule working tree to the superproject working tree.
  54#
  55# The output of the function is the origin URL of the submodule.
  56#
  57# The output will either be an absolute URL or filesystem path (if the
  58# superproject origin URL is an absolute URL or filesystem path,
  59# respectively) or a relative file system path (if the superproject
  60# origin URL is a relative file system path).
  61#
  62# When the output is a relative file system path, the path is either
  63# relative to the submodule working tree, if up_path is specified, or to
  64# the superproject working tree otherwise.
  65resolve_relative_url ()
  66{
  67        remote=$(get_default_remote)
  68        remoteurl=$(git config "remote.$remote.url") ||
  69                remoteurl=$(pwd) # the repository is its own authoritative upstream
  70        url="$1"
  71        remoteurl=${remoteurl%/}
  72        sep=/
  73        up_path="$2"
  74
  75        case "$remoteurl" in
  76        *:*|/*)
  77                is_relative=
  78                ;;
  79        ./*|../*)
  80                is_relative=t
  81                ;;
  82        *)
  83                is_relative=t
  84                remoteurl="./$remoteurl"
  85                ;;
  86        esac
  87
  88        while test -n "$url"
  89        do
  90                case "$url" in
  91                ../*)
  92                        url="${url#../}"
  93                        case "$remoteurl" in
  94                        */*)
  95                                remoteurl="${remoteurl%/*}"
  96                                ;;
  97                        *:*)
  98                                remoteurl="${remoteurl%:*}"
  99                                sep=:
 100                                ;;
 101                        *)
 102                                if test -z "$is_relative" || test "." = "$remoteurl"
 103                                then
 104                                        die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
 105                                else
 106                                        remoteurl=.
 107                                fi
 108                                ;;
 109                        esac
 110                        ;;
 111                ./*)
 112                        url="${url#./}"
 113                        ;;
 114                *)
 115                        break;;
 116                esac
 117        done
 118        remoteurl="$remoteurl$sep${url%/}"
 119        echo "${is_relative:+${up_path}}${remoteurl#./}"
 120}
 121
 122# Resolve a path to be relative to another path.  This is intended for
 123# converting submodule paths when git-submodule is run in a subdirectory
 124# and only handles paths where the directory separator is '/'.
 125#
 126# The output is the first argument as a path relative to the second argument,
 127# which defaults to $wt_prefix if it is omitted.
 128relative_path ()
 129{
 130        local target curdir result
 131        target=$1
 132        curdir=${2-$wt_prefix}
 133        curdir=${curdir%/}
 134        result=
 135
 136        while test -n "$curdir"
 137        do
 138                case "$target" in
 139                "$curdir/"*)
 140                        target=${target#"$curdir"/}
 141                        break
 142                        ;;
 143                esac
 144
 145                result="${result}../"
 146                if test "$curdir" = "${curdir%/*}"
 147                then
 148                        curdir=
 149                else
 150                        curdir="${curdir%/*}"
 151                fi
 152        done
 153
 154        echo "$result$target"
 155}
 156
 157die_if_unmatched ()
 158{
 159        if test "$1" = "#unmatched"
 160        then
 161                exit 1
 162        fi
 163}
 164
 165#
 166# Print a submodule configuration setting
 167#
 168# $1 = submodule name
 169# $2 = option name
 170# $3 = default value
 171#
 172# Checks in the usual git-config places first (for overrides),
 173# otherwise it falls back on .gitmodules.  This allows you to
 174# distribute project-wide defaults in .gitmodules, while still
 175# customizing individual repositories if necessary.  If the option is
 176# not in .gitmodules either, print a default value.
 177#
 178get_submodule_config () {
 179        name="$1"
 180        option="$2"
 181        default="$3"
 182        value=$(git config submodule."$name"."$option")
 183        if test -z "$value"
 184        then
 185                value=$(git config -f .gitmodules submodule."$name"."$option")
 186        fi
 187        printf '%s' "${value:-$default}"
 188}
 189
 190isnumber()
 191{
 192        n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
 193}
 194
 195# Sanitize the local git environment for use within a submodule. We
 196# can't simply use clear_local_git_env since we want to preserve some
 197# of the settings from GIT_CONFIG_PARAMETERS.
 198sanitize_submodule_env()
 199{
 200        sanitized_config=$(git submodule--helper sanitize-config)
 201        clear_local_git_env
 202        GIT_CONFIG_PARAMETERS=$sanitized_config
 203}
 204
 205#
 206# Add a new submodule to the working tree, .gitmodules and the index
 207#
 208# $@ = repo path
 209#
 210# optional branch is stored in global branch variable
 211#
 212cmd_add()
 213{
 214        # parse $args after "submodule ... add".
 215        reference_path=
 216        while test $# -ne 0
 217        do
 218                case "$1" in
 219                -b | --branch)
 220                        case "$2" in '') usage ;; esac
 221                        branch=$2
 222                        shift
 223                        ;;
 224                -f | --force)
 225                        force=$1
 226                        ;;
 227                -q|--quiet)
 228                        GIT_QUIET=1
 229                        ;;
 230                --reference)
 231                        case "$2" in '') usage ;; esac
 232                        reference_path=$2
 233                        shift
 234                        ;;
 235                --reference=*)
 236                        reference_path="${1#--reference=}"
 237                        ;;
 238                --name)
 239                        case "$2" in '') usage ;; esac
 240                        custom_name=$2
 241                        shift
 242                        ;;
 243                --depth)
 244                        case "$2" in '') usage ;; esac
 245                        depth="--depth=$2"
 246                        shift
 247                        ;;
 248                --depth=*)
 249                        depth=$1
 250                        ;;
 251                --)
 252                        shift
 253                        break
 254                        ;;
 255                -*)
 256                        usage
 257                        ;;
 258                *)
 259                        break
 260                        ;;
 261                esac
 262                shift
 263        done
 264
 265        if test -n "$reference_path"
 266        then
 267                is_absolute_path "$reference_path" ||
 268                reference_path="$wt_prefix$reference_path"
 269
 270                reference="--reference=$reference_path"
 271        fi
 272
 273        repo=$1
 274        sm_path=$2
 275
 276        if test -z "$sm_path"; then
 277                sm_path=$(printf '%s\n' "$repo" |
 278                        sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 279        fi
 280
 281        if test -z "$repo" || test -z "$sm_path"; then
 282                usage
 283        fi
 284
 285        is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
 286
 287        # assure repo is absolute or relative to parent
 288        case "$repo" in
 289        ./*|../*)
 290                test -z "$wt_prefix" ||
 291                die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
 292
 293                # dereference source url relative to parent's url
 294                realrepo=$(resolve_relative_url "$repo") || exit
 295                ;;
 296        *:*|/*)
 297                # absolute url
 298                realrepo=$repo
 299                ;;
 300        *)
 301                die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
 302        ;;
 303        esac
 304
 305        # normalize path:
 306        # multiple //; leading ./; /./; /../; trailing /
 307        sm_path=$(printf '%s/\n' "$sm_path" |
 308                sed -e '
 309                        s|//*|/|g
 310                        s|^\(\./\)*||
 311                        s|/\(\./\)*|/|g
 312                        :start
 313                        s|\([^/]*\)/\.\./||
 314                        tstart
 315                        s|/*$||
 316                ')
 317        git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
 318        die "$(eval_gettext "'\$sm_path' already exists in the index")"
 319
 320        if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
 321        then
 322                eval_gettextln "The following path is ignored by one of your .gitignore files:
 323\$sm_path
 324Use -f if you really want to add it." >&2
 325                exit 1
 326        fi
 327
 328        if test -n "$custom_name"
 329        then
 330                sm_name="$custom_name"
 331        else
 332                sm_name="$sm_path"
 333        fi
 334
 335        # perhaps the path exists and is already a git repo, else clone it
 336        if test -e "$sm_path"
 337        then
 338                if test -d "$sm_path"/.git || test -f "$sm_path"/.git
 339                then
 340                        eval_gettextln "Adding existing repo at '\$sm_path' to the index"
 341                else
 342                        die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
 343                fi
 344
 345        else
 346                if test -d ".git/modules/$sm_name"
 347                then
 348                        if test -z "$force"
 349                        then
 350                                echo >&2 "$(eval_gettext "A git directory for '\$sm_name' is found locally with remote(s):")"
 351                                GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^,"  ", -e s,' (fetch)',, >&2
 352                                echo >&2 "$(eval_gettext "If you want to reuse this local git directory instead of cloning again from")"
 353                                echo >&2 "  $realrepo"
 354                                echo >&2 "$(eval_gettext "use the '--force' option. If the local git directory is not the correct repo")"
 355                                die "$(eval_gettext "or you are unsure what this means choose another name with the '--name' option.")"
 356                        else
 357                                echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
 358                        fi
 359                fi
 360                git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${depth:+"$depth"} || exit
 361                (
 362                        sanitize_submodule_env
 363                        cd "$sm_path" &&
 364                        # ash fails to wordsplit ${branch:+-b "$branch"...}
 365                        case "$branch" in
 366                        '') git checkout -f -q ;;
 367                        ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
 368                        esac
 369                ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
 370        fi
 371        git config submodule."$sm_name".url "$realrepo"
 372
 373        git add $force "$sm_path" ||
 374        die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
 375
 376        git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
 377        git config -f .gitmodules submodule."$sm_name".url "$repo" &&
 378        if test -n "$branch"
 379        then
 380                git config -f .gitmodules submodule."$sm_name".branch "$branch"
 381        fi &&
 382        git add --force .gitmodules ||
 383        die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
 384}
 385
 386#
 387# Execute an arbitrary command sequence in each checked out
 388# submodule
 389#
 390# $@ = command to execute
 391#
 392cmd_foreach()
 393{
 394        # parse $args after "submodule ... foreach".
 395        while test $# -ne 0
 396        do
 397                case "$1" in
 398                -q|--quiet)
 399                        GIT_QUIET=1
 400                        ;;
 401                --recursive)
 402                        recursive=1
 403                        ;;
 404                -*)
 405                        usage
 406                        ;;
 407                *)
 408                        break
 409                        ;;
 410                esac
 411                shift
 412        done
 413
 414        toplevel=$(pwd)
 415
 416        # dup stdin so that it can be restored when running the external
 417        # command in the subshell (and a recursive call to this function)
 418        exec 3<&0
 419
 420        git submodule--helper list --prefix "$wt_prefix"|
 421        while read mode sha1 stage sm_path
 422        do
 423                die_if_unmatched "$mode"
 424                if test -e "$sm_path"/.git
 425                then
 426                        displaypath=$(relative_path "$prefix$sm_path")
 427                        say "$(eval_gettext "Entering '\$displaypath'")"
 428                        name=$(git submodule--helper name "$sm_path")
 429                        (
 430                                prefix="$prefix$sm_path/"
 431                                sanitize_submodule_env
 432                                cd "$sm_path" &&
 433                                sm_path=$(relative_path "$sm_path") &&
 434                                # we make $path available to scripts ...
 435                                path=$sm_path &&
 436                                if test $# -eq 1
 437                                then
 438                                        eval "$1"
 439                                else
 440                                        "$@"
 441                                fi &&
 442                                if test -n "$recursive"
 443                                then
 444                                        cmd_foreach "--recursive" "$@"
 445                                fi
 446                        ) <&3 3<&- ||
 447                        die "$(eval_gettext "Stopping at '\$displaypath'; script returned non-zero status.")"
 448                fi
 449        done
 450}
 451
 452#
 453# Register submodules in .git/config
 454#
 455# $@ = requested paths (default to all)
 456#
 457cmd_init()
 458{
 459        # parse $args after "submodule ... init".
 460        while test $# -ne 0
 461        do
 462                case "$1" in
 463                -q|--quiet)
 464                        GIT_QUIET=1
 465                        ;;
 466                --)
 467                        shift
 468                        break
 469                        ;;
 470                -*)
 471                        usage
 472                        ;;
 473                *)
 474                        break
 475                        ;;
 476                esac
 477                shift
 478        done
 479
 480        git submodule--helper list --prefix "$wt_prefix" "$@" |
 481        while read mode sha1 stage sm_path
 482        do
 483                die_if_unmatched "$mode"
 484                name=$(git submodule--helper name "$sm_path") || exit
 485
 486                displaypath=$(relative_path "$prefix$sm_path")
 487
 488                # Copy url setting when it is not set yet
 489                if test -z "$(git config "submodule.$name.url")"
 490                then
 491                        url=$(git config -f .gitmodules submodule."$name".url)
 492                        test -z "$url" &&
 493                        die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")"
 494
 495                        # Possibly a url relative to parent
 496                        case "$url" in
 497                        ./*|../*)
 498                                url=$(resolve_relative_url "$url") || exit
 499                                ;;
 500                        esac
 501                        git config submodule."$name".url "$url" ||
 502                        die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")"
 503
 504                        say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")"
 505                fi
 506
 507                # Copy "update" setting when it is not set yet
 508                if upd="$(git config -f .gitmodules submodule."$name".update)" &&
 509                   test -n "$upd" &&
 510                   test -z "$(git config submodule."$name".update)"
 511                then
 512                        case "$upd" in
 513                        checkout | rebase | merge | none)
 514                                ;; # known modes of updating
 515                        *)
 516                                echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'"
 517                                upd=none
 518                                ;;
 519                        esac
 520                        git config submodule."$name".update "$upd" ||
 521                        die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
 522                fi
 523        done
 524}
 525
 526#
 527# Unregister submodules from .git/config and remove their work tree
 528#
 529# $@ = requested paths (use '.' to deinit all submodules)
 530#
 531cmd_deinit()
 532{
 533        # parse $args after "submodule ... deinit".
 534        while test $# -ne 0
 535        do
 536                case "$1" in
 537                -f|--force)
 538                        force=$1
 539                        ;;
 540                -q|--quiet)
 541                        GIT_QUIET=1
 542                        ;;
 543                --)
 544                        shift
 545                        break
 546                        ;;
 547                -*)
 548                        usage
 549                        ;;
 550                *)
 551                        break
 552                        ;;
 553                esac
 554                shift
 555        done
 556
 557        if test $# = 0
 558        then
 559                die "$(eval_gettext "Use '.' if you really want to deinitialize all submodules")"
 560        fi
 561
 562        git submodule--helper list --prefix "$wt_prefix" "$@" |
 563        while read mode sha1 stage sm_path
 564        do
 565                die_if_unmatched "$mode"
 566                name=$(git submodule--helper name "$sm_path") || exit
 567
 568                displaypath=$(relative_path "$sm_path")
 569
 570                # Remove the submodule work tree (unless the user already did it)
 571                if test -d "$sm_path"
 572                then
 573                        # Protect submodules containing a .git directory
 574                        if test -d "$sm_path/.git"
 575                        then
 576                                echo >&2 "$(eval_gettext "Submodule work tree '\$displaypath' contains a .git directory")"
 577                                die "$(eval_gettext "(use 'rm -rf' if you really want to remove it including all of its history)")"
 578                        fi
 579
 580                        if test -z "$force"
 581                        then
 582                                git rm -qn "$sm_path" ||
 583                                die "$(eval_gettext "Submodule work tree '\$displaypath' contains local modifications; use '-f' to discard them")"
 584                        fi
 585                        rm -rf "$sm_path" &&
 586                        say "$(eval_gettext "Cleared directory '\$displaypath'")" ||
 587                        say "$(eval_gettext "Could not remove submodule work tree '\$displaypath'")"
 588                fi
 589
 590                mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$displaypath'")"
 591
 592                # Remove the .git/config entries (unless the user already did it)
 593                if test -n "$(git config --get-regexp submodule."$name\.")"
 594                then
 595                        # Remove the whole section so we have a clean state when
 596                        # the user later decides to init this submodule again
 597                        url=$(git config submodule."$name".url)
 598                        git config --remove-section submodule."$name" 2>/dev/null &&
 599                        say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$displaypath'")"
 600                fi
 601        done
 602}
 603
 604is_tip_reachable () (
 605        sanitize_submodule_env &&
 606        cd "$1" &&
 607        rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
 608        test -z "$rev"
 609)
 610
 611fetch_in_submodule () (
 612        sanitize_submodule_env &&
 613        cd "$1" &&
 614        case "$2" in
 615        '')
 616                git fetch ;;
 617        *)
 618                git fetch $(get_default_remote) "$2" ;;
 619        esac
 620)
 621
 622#
 623# Update each submodule path to correct revision, using clone and checkout as needed
 624#
 625# $@ = requested paths (default to all)
 626#
 627cmd_update()
 628{
 629        # parse $args after "submodule ... update".
 630        while test $# -ne 0
 631        do
 632                case "$1" in
 633                -q|--quiet)
 634                        GIT_QUIET=1
 635                        ;;
 636                -i|--init)
 637                        init=1
 638                        ;;
 639                --remote)
 640                        remote=1
 641                        ;;
 642                -N|--no-fetch)
 643                        nofetch=1
 644                        ;;
 645                -f|--force)
 646                        force=$1
 647                        ;;
 648                -r|--rebase)
 649                        update="rebase"
 650                        ;;
 651                --reference)
 652                        case "$2" in '') usage ;; esac
 653                        reference="--reference=$2"
 654                        shift
 655                        ;;
 656                --reference=*)
 657                        reference="$1"
 658                        ;;
 659                -m|--merge)
 660                        update="merge"
 661                        ;;
 662                --recursive)
 663                        recursive=1
 664                        ;;
 665                --checkout)
 666                        update="checkout"
 667                        ;;
 668                --depth)
 669                        case "$2" in '') usage ;; esac
 670                        depth="--depth=$2"
 671                        shift
 672                        ;;
 673                --depth=*)
 674                        depth=$1
 675                        ;;
 676                -j|--jobs)
 677                        case "$2" in '') usage ;; esac
 678                        jobs="--jobs=$2"
 679                        shift
 680                        ;;
 681                --jobs=*)
 682                        jobs=$1
 683                        ;;
 684                --)
 685                        shift
 686                        break
 687                        ;;
 688                -*)
 689                        usage
 690                        ;;
 691                *)
 692                        break
 693                        ;;
 694                esac
 695                shift
 696        done
 697
 698        if test -n "$init"
 699        then
 700                cmd_init "--" "$@" || return
 701        fi
 702
 703        {
 704        git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
 705                ${wt_prefix:+--prefix "$wt_prefix"} \
 706                ${prefix:+--recursive-prefix "$prefix"} \
 707                ${update:+--update "$update"} \
 708                ${reference:+--reference "$reference"} \
 709                ${depth:+--depth "$depth"} \
 710                ${jobs:+$jobs} \
 711                "$@" || echo "#unmatched"
 712        } | {
 713        err=
 714        while read mode sha1 stage just_cloned sm_path
 715        do
 716                die_if_unmatched "$mode"
 717
 718                name=$(git submodule--helper name "$sm_path") || exit
 719                url=$(git config submodule."$name".url)
 720                branch=$(get_submodule_config "$name" branch master)
 721                if ! test -z "$update"
 722                then
 723                        update_module=$update
 724                else
 725                        update_module=$(git config submodule."$name".update)
 726                        if test -z "$update_module"
 727                        then
 728                                update_module="checkout"
 729                        fi
 730                fi
 731
 732                displaypath=$(relative_path "$prefix$sm_path")
 733
 734                if test $just_cloned -eq 1
 735                then
 736                        subsha1=
 737                        update_module=checkout
 738                else
 739                        subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
 740                                git rev-parse --verify HEAD) ||
 741                        die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
 742                fi
 743
 744                if test -n "$remote"
 745                then
 746                        if test -z "$nofetch"
 747                        then
 748                                # Fetch remote before determining tracking $sha1
 749                                (sanitize_submodule_env; cd "$sm_path" && git-fetch) ||
 750                                die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
 751                        fi
 752                        remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
 753                        sha1=$(sanitize_submodule_env; cd "$sm_path" &&
 754                                git rev-parse --verify "${remote_name}/${branch}") ||
 755                        die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
 756                fi
 757
 758                if test "$subsha1" != "$sha1" || test -n "$force"
 759                then
 760                        subforce=$force
 761                        # If we don't already have a -f flag and the submodule has never been checked out
 762                        if test -z "$subsha1" && test -z "$force"
 763                        then
 764                                subforce="-f"
 765                        fi
 766
 767                        if test -z "$nofetch"
 768                        then
 769                                # Run fetch only if $sha1 isn't present or it
 770                                # is not reachable from a ref.
 771                                is_tip_reachable "$sm_path" "$sha1" ||
 772                                fetch_in_submodule "$sm_path" ||
 773                                die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
 774
 775                                # Now we tried the usual fetch, but $sha1 may
 776                                # not be reachable from any of the refs
 777                                is_tip_reachable "$sm_path" "$sha1" ||
 778                                fetch_in_submodule "$sm_path" "$sha1" ||
 779                                die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain $sha1. Direct fetching of that commit failed.")"
 780                        fi
 781
 782                        must_die_on_failure=
 783                        case "$update_module" in
 784                        checkout)
 785                                command="git checkout $subforce -q"
 786                                die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
 787                                say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
 788                                ;;
 789                        rebase)
 790                                command="git rebase"
 791                                die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
 792                                say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
 793                                must_die_on_failure=yes
 794                                ;;
 795                        merge)
 796                                command="git merge"
 797                                die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
 798                                say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
 799                                must_die_on_failure=yes
 800                                ;;
 801                        !*)
 802                                command="${update_module#!}"
 803                                die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$displaypath'")"
 804                                say_msg="$(eval_gettext "Submodule path '\$displaypath': '\$command \$sha1'")"
 805                                must_die_on_failure=yes
 806                                ;;
 807                        *)
 808                                die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
 809                        esac
 810
 811                        if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
 812                        then
 813                                say "$say_msg"
 814                        elif test -n "$must_die_on_failure"
 815                        then
 816                                die_with_status 2 "$die_msg"
 817                        else
 818                                err="${err};$die_msg"
 819                                continue
 820                        fi
 821                fi
 822
 823                if test -n "$recursive"
 824                then
 825                        (
 826                                prefix="$prefix$sm_path/"
 827                                sanitize_submodule_env
 828                                cd "$sm_path" &&
 829                                eval cmd_update
 830                        )
 831                        res=$?
 832                        if test $res -gt 0
 833                        then
 834                                die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
 835                                if test $res -eq 1
 836                                then
 837                                        err="${err};$die_msg"
 838                                        continue
 839                                else
 840                                        die_with_status $res "$die_msg"
 841                                fi
 842                        fi
 843                fi
 844        done
 845
 846        if test -n "$err"
 847        then
 848                OIFS=$IFS
 849                IFS=';'
 850                for e in $err
 851                do
 852                        if test -n "$e"
 853                        then
 854                                echo >&2 "$e"
 855                        fi
 856                done
 857                IFS=$OIFS
 858                exit 1
 859        fi
 860        }
 861}
 862
 863set_name_rev () {
 864        revname=$( (
 865                sanitize_submodule_env
 866                cd "$1" && {
 867                        git describe "$2" 2>/dev/null ||
 868                        git describe --tags "$2" 2>/dev/null ||
 869                        git describe --contains "$2" 2>/dev/null ||
 870                        git describe --all --always "$2"
 871                }
 872        ) )
 873        test -z "$revname" || revname=" ($revname)"
 874}
 875#
 876# Show commit summary for submodules in index or working tree
 877#
 878# If '--cached' is given, show summary between index and given commit,
 879# or between working tree and given commit
 880#
 881# $@ = [commit (default 'HEAD'),] requested paths (default all)
 882#
 883cmd_summary() {
 884        summary_limit=-1
 885        for_status=
 886        diff_cmd=diff-index
 887
 888        # parse $args after "submodule ... summary".
 889        while test $# -ne 0
 890        do
 891                case "$1" in
 892                --cached)
 893                        cached="$1"
 894                        ;;
 895                --files)
 896                        files="$1"
 897                        ;;
 898                --for-status)
 899                        for_status="$1"
 900                        ;;
 901                -n|--summary-limit)
 902                        summary_limit="$2"
 903                        isnumber "$summary_limit" || usage
 904                        shift
 905                        ;;
 906                --summary-limit=*)
 907                        summary_limit="${1#--summary-limit=}"
 908                        isnumber "$summary_limit" || usage
 909                        ;;
 910                --)
 911                        shift
 912                        break
 913                        ;;
 914                -*)
 915                        usage
 916                        ;;
 917                *)
 918                        break
 919                        ;;
 920                esac
 921                shift
 922        done
 923
 924        test $summary_limit = 0 && return
 925
 926        if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
 927        then
 928                head=$rev
 929                test $# = 0 || shift
 930        elif test -z "$1" || test "$1" = "HEAD"
 931        then
 932                # before the first commit: compare with an empty tree
 933                head=$(git hash-object -w -t tree --stdin </dev/null)
 934                test -z "$1" || shift
 935        else
 936                head="HEAD"
 937        fi
 938
 939        if [ -n "$files" ]
 940        then
 941                test -n "$cached" &&
 942                die "$(gettext "The --cached option cannot be used with the --files option")"
 943                diff_cmd=diff-files
 944                head=
 945        fi
 946
 947        cd_to_toplevel
 948        eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
 949        # Get modified modules cared by user
 950        modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
 951                sane_egrep '^:([0-7]* )?160000' |
 952                while read mod_src mod_dst sha1_src sha1_dst status sm_path
 953                do
 954                        # Always show modules deleted or type-changed (blob<->module)
 955                        if test "$status" = D || test "$status" = T
 956                        then
 957                                printf '%s\n' "$sm_path"
 958                                continue
 959                        fi
 960                        # Respect the ignore setting for --for-status.
 961                        if test -n "$for_status"
 962                        then
 963                                name=$(git submodule--helper name "$sm_path")
 964                                ignore_config=$(get_submodule_config "$name" ignore none)
 965                                test $status != A && test $ignore_config = all && continue
 966                        fi
 967                        # Also show added or modified modules which are checked out
 968                        GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
 969                        printf '%s\n' "$sm_path"
 970                done
 971        )
 972
 973        test -z "$modules" && return
 974
 975        git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
 976        sane_egrep '^:([0-7]* )?160000' |
 977        cut -c2- |
 978        while read mod_src mod_dst sha1_src sha1_dst status name
 979        do
 980                if test -z "$cached" &&
 981                        test $sha1_dst = 0000000000000000000000000000000000000000
 982                then
 983                        case "$mod_dst" in
 984                        160000)
 985                                sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
 986                                ;;
 987                        100644 | 100755 | 120000)
 988                                sha1_dst=$(git hash-object $name)
 989                                ;;
 990                        000000)
 991                                ;; # removed
 992                        *)
 993                                # unexpected type
 994                                eval_gettextln "unexpected mode \$mod_dst" >&2
 995                                continue ;;
 996                        esac
 997                fi
 998                missing_src=
 999                missing_dst=
1000
1001                test $mod_src = 160000 &&
1002                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1003                missing_src=t
1004
1005                test $mod_dst = 160000 &&
1006                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1007                missing_dst=t
1008
1009                display_name=$(relative_path "$name")
1010
1011                total_commits=
1012                case "$missing_src,$missing_dst" in
1013                t,)
1014                        errmsg="$(eval_gettext "  Warn: \$display_name doesn't contain commit \$sha1_src")"
1015                        ;;
1016                ,t)
1017                        errmsg="$(eval_gettext "  Warn: \$display_name doesn't contain commit \$sha1_dst")"
1018                        ;;
1019                t,t)
1020                        errmsg="$(eval_gettext "  Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
1021                        ;;
1022                *)
1023                        errmsg=
1024                        total_commits=$(
1025                        if test $mod_src = 160000 && test $mod_dst = 160000
1026                        then
1027                                range="$sha1_src...$sha1_dst"
1028                        elif test $mod_src = 160000
1029                        then
1030                                range=$sha1_src
1031                        else
1032                                range=$sha1_dst
1033                        fi
1034                        GIT_DIR="$name/.git" \
1035                        git rev-list --first-parent $range -- | wc -l
1036                        )
1037                        total_commits=" ($(($total_commits + 0)))"
1038                        ;;
1039                esac
1040
1041                sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
1042                sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
1043                if test $status = T
1044                then
1045                        blob="$(gettext "blob")"
1046                        submodule="$(gettext "submodule")"
1047                        if test $mod_dst = 160000
1048                        then
1049                                echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
1050                        else
1051                                echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
1052                        fi
1053                else
1054                        echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
1055                fi
1056                if test -n "$errmsg"
1057                then
1058                        # Don't give error msg for modification whose dst is not submodule
1059                        # i.e. deleted or changed to blob
1060                        test $mod_dst = 160000 && echo "$errmsg"
1061                else
1062                        if test $mod_src = 160000 && test $mod_dst = 160000
1063                        then
1064                                limit=
1065                                test $summary_limit -gt 0 && limit="-$summary_limit"
1066                                GIT_DIR="$name/.git" \
1067                                git log $limit --pretty='format:  %m %s' \
1068                                --first-parent $sha1_src...$sha1_dst
1069                        elif test $mod_dst = 160000
1070                        then
1071                                GIT_DIR="$name/.git" \
1072                                git log --pretty='format:  > %s' -1 $sha1_dst
1073                        else
1074                                GIT_DIR="$name/.git" \
1075                                git log --pretty='format:  < %s' -1 $sha1_src
1076                        fi
1077                        echo
1078                fi
1079                echo
1080        done
1081}
1082#
1083# List all submodules, prefixed with:
1084#  - submodule not initialized
1085#  + different revision checked out
1086#
1087# If --cached was specified the revision in the index will be printed
1088# instead of the currently checked out revision.
1089#
1090# $@ = requested paths (default to all)
1091#
1092cmd_status()
1093{
1094        # parse $args after "submodule ... status".
1095        while test $# -ne 0
1096        do
1097                case "$1" in
1098                -q|--quiet)
1099                        GIT_QUIET=1
1100                        ;;
1101                --cached)
1102                        cached=1
1103                        ;;
1104                --recursive)
1105                        recursive=1
1106                        ;;
1107                --)
1108                        shift
1109                        break
1110                        ;;
1111                -*)
1112                        usage
1113                        ;;
1114                *)
1115                        break
1116                        ;;
1117                esac
1118                shift
1119        done
1120
1121        git submodule--helper list --prefix "$wt_prefix" "$@" |
1122        while read mode sha1 stage sm_path
1123        do
1124                die_if_unmatched "$mode"
1125                name=$(git submodule--helper name "$sm_path") || exit
1126                url=$(git config submodule."$name".url)
1127                displaypath=$(relative_path "$prefix$sm_path")
1128                if test "$stage" = U
1129                then
1130                        say "U$sha1 $displaypath"
1131                        continue
1132                fi
1133                if test -z "$url" ||
1134                {
1135                        ! test -d "$sm_path"/.git &&
1136                        ! test -f "$sm_path"/.git
1137                }
1138                then
1139                        say "-$sha1 $displaypath"
1140                        continue;
1141                fi
1142                if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1143                then
1144                        set_name_rev "$sm_path" "$sha1"
1145                        say " $sha1 $displaypath$revname"
1146                else
1147                        if test -z "$cached"
1148                        then
1149                                sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify HEAD)
1150                        fi
1151                        set_name_rev "$sm_path" "$sha1"
1152                        say "+$sha1 $displaypath$revname"
1153                fi
1154
1155                if test -n "$recursive"
1156                then
1157                        (
1158                                prefix="$displaypath/"
1159                                sanitize_submodule_env
1160                                wt_prefix=
1161                                cd "$sm_path" &&
1162                                eval cmd_status
1163                        ) ||
1164                        die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1165                fi
1166        done
1167}
1168#
1169# Sync remote urls for submodules
1170# This makes the value for remote.$remote.url match the value
1171# specified in .gitmodules.
1172#
1173cmd_sync()
1174{
1175        while test $# -ne 0
1176        do
1177                case "$1" in
1178                -q|--quiet)
1179                        GIT_QUIET=1
1180                        shift
1181                        ;;
1182                --recursive)
1183                        recursive=1
1184                        shift
1185                        ;;
1186                --)
1187                        shift
1188                        break
1189                        ;;
1190                -*)
1191                        usage
1192                        ;;
1193                *)
1194                        break
1195                        ;;
1196                esac
1197        done
1198        cd_to_toplevel
1199        git submodule--helper list --prefix "$wt_prefix" "$@" |
1200        while read mode sha1 stage sm_path
1201        do
1202                die_if_unmatched "$mode"
1203                name=$(git submodule--helper name "$sm_path")
1204                url=$(git config -f .gitmodules --get submodule."$name".url)
1205
1206                # Possibly a url relative to parent
1207                case "$url" in
1208                ./*|../*)
1209                        # rewrite foo/bar as ../.. to find path from
1210                        # submodule work tree to superproject work tree
1211                        up_path="$(printf '%s\n' "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1212                        # guarantee a trailing /
1213                        up_path=${up_path%/}/ &&
1214                        # path from submodule work tree to submodule origin repo
1215                        sub_origin_url=$(resolve_relative_url "$url" "$up_path") &&
1216                        # path from superproject work tree to submodule origin repo
1217                        super_config_url=$(resolve_relative_url "$url") || exit
1218                        ;;
1219                *)
1220                        sub_origin_url="$url"
1221                        super_config_url="$url"
1222                        ;;
1223                esac
1224
1225                if git config "submodule.$name.url" >/dev/null 2>/dev/null
1226                then
1227                        displaypath=$(relative_path "$prefix$sm_path")
1228                        say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
1229                        git config submodule."$name".url "$super_config_url"
1230
1231                        if test -e "$sm_path"/.git
1232                        then
1233                        (
1234                                sanitize_submodule_env
1235                                cd "$sm_path"
1236                                remote=$(get_default_remote)
1237                                git config remote."$remote".url "$sub_origin_url"
1238
1239                                if test -n "$recursive"
1240                                then
1241                                        prefix="$prefix$sm_path/"
1242                                        eval cmd_sync
1243                                fi
1244                        )
1245                        fi
1246                fi
1247        done
1248}
1249
1250# This loop parses the command line arguments to find the
1251# subcommand name to dispatch.  Parsing of the subcommand specific
1252# options are primarily done by the subcommand implementations.
1253# Subcommand specific options such as --branch and --cached are
1254# parsed here as well, for backward compatibility.
1255
1256while test $# != 0 && test -z "$command"
1257do
1258        case "$1" in
1259        add | foreach | init | deinit | update | status | summary | sync)
1260                command=$1
1261                ;;
1262        -q|--quiet)
1263                GIT_QUIET=1
1264                ;;
1265        -b|--branch)
1266                case "$2" in
1267                '')
1268                        usage
1269                        ;;
1270                esac
1271                branch="$2"; shift
1272                ;;
1273        --cached)
1274                cached="$1"
1275                ;;
1276        --)
1277                break
1278                ;;
1279        -*)
1280                usage
1281                ;;
1282        *)
1283                break
1284                ;;
1285        esac
1286        shift
1287done
1288
1289# No command word defaults to "status"
1290if test -z "$command"
1291then
1292    if test $# = 0
1293    then
1294        command=status
1295    else
1296        usage
1297    fi
1298fi
1299
1300# "-b branch" is accepted only by "add"
1301if test -n "$branch" && test "$command" != add
1302then
1303        usage
1304fi
1305
1306# "--cached" is accepted only by "status" and "summary"
1307if test -n "$cached" && test "$command" != status && test "$command" != summary
1308then
1309        usage
1310fi
1311
1312"cmd_$command" "$@"