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