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