git-submodule.shon commit git-submodule: add support for --rebase. (ca2cedb)
   1#!/bin/sh
   2#
   3# git-submodules.sh: add, init, update or list git submodules
   4#
   5# Copyright (c) 2007 Lars Hjemli
   6
   7USAGE="[--quiet] [--cached] \
   8[add [-b branch] <repo> <path>]|[status|init|update [-i|--init] [-N|--no-fetch]|summary [-n|--summary-limit <n>] [<commit>]] \
   9[--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
  10OPTIONS_SPEC=
  11. git-sh-setup
  12. git-parse-remote
  13require_work_tree
  14
  15command=
  16branch=
  17quiet=
  18cached=
  19nofetch=
  20rebase=
  21
  22#
  23# print stuff on stdout unless -q was specified
  24#
  25say()
  26{
  27        if test -z "$quiet"
  28        then
  29                echo "$@"
  30        fi
  31}
  32
  33# Resolve relative url by appending to parent's url
  34resolve_relative_url ()
  35{
  36        remote=$(get_default_remote)
  37        remoteurl=$(git config "remote.$remote.url") ||
  38                die "remote ($remote) does not have a url defined in .git/config"
  39        url="$1"
  40        remoteurl=${remoteurl%/}
  41        while test -n "$url"
  42        do
  43                case "$url" in
  44                ../*)
  45                        url="${url#../}"
  46                        remoteurl="${remoteurl%/*}"
  47                        ;;
  48                ./*)
  49                        url="${url#./}"
  50                        ;;
  51                *)
  52                        break;;
  53                esac
  54        done
  55        echo "$remoteurl/${url%/}"
  56}
  57
  58#
  59# Get submodule info for registered submodules
  60# $@ = path to limit submodule list
  61#
  62module_list()
  63{
  64        git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
  65}
  66
  67#
  68# Map submodule path to submodule name
  69#
  70# $1 = path
  71#
  72module_name()
  73{
  74        # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
  75        re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
  76        name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
  77                sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
  78       test -z "$name" &&
  79       die "No submodule mapping found in .gitmodules for path '$path'"
  80       echo "$name"
  81}
  82
  83#
  84# Clone a submodule
  85#
  86# Prior to calling, cmd_update checks that a possibly existing
  87# path is not a git repository.
  88# Likewise, cmd_add checks that path does not exist at all,
  89# since it is the location of a new submodule.
  90#
  91module_clone()
  92{
  93        path=$1
  94        url=$2
  95
  96        # If there already is a directory at the submodule path,
  97        # expect it to be empty (since that is the default checkout
  98        # action) and try to remove it.
  99        # Note: if $path is a symlink to a directory the test will
 100        # succeed but the rmdir will fail. We might want to fix this.
 101        if test -d "$path"
 102        then
 103                rmdir "$path" 2>/dev/null ||
 104                die "Directory '$path' exist, but is neither empty nor a git repository"
 105        fi
 106
 107        test -e "$path" &&
 108        die "A file already exist at path '$path'"
 109
 110        git-clone -n "$url" "$path" ||
 111        die "Clone of '$url' into submodule path '$path' failed"
 112}
 113
 114#
 115# Add a new submodule to the working tree, .gitmodules and the index
 116#
 117# $@ = repo path
 118#
 119# optional branch is stored in global branch variable
 120#
 121cmd_add()
 122{
 123        # parse $args after "submodule ... add".
 124        while test $# -ne 0
 125        do
 126                case "$1" in
 127                -b | --branch)
 128                        case "$2" in '') usage ;; esac
 129                        branch=$2
 130                        shift
 131                        ;;
 132                -q|--quiet)
 133                        quiet=1
 134                        ;;
 135                --)
 136                        shift
 137                        break
 138                        ;;
 139                -*)
 140                        usage
 141                        ;;
 142                *)
 143                        break
 144                        ;;
 145                esac
 146                shift
 147        done
 148
 149        repo=$1
 150        path=$2
 151
 152        if test -z "$repo" -o -z "$path"; then
 153                usage
 154        fi
 155
 156        # assure repo is absolute or relative to parent
 157        case "$repo" in
 158        ./*|../*)
 159                # dereference source url relative to parent's url
 160                realrepo=$(resolve_relative_url "$repo") || exit
 161                ;;
 162        *:*|/*)
 163                # absolute url
 164                realrepo=$repo
 165                ;;
 166        *)
 167                die "repo URL: '$repo' must be absolute or begin with ./|../"
 168        ;;
 169        esac
 170
 171        # normalize path:
 172        # multiple //; leading ./; /./; /../; trailing /
 173        path=$(printf '%s/\n' "$path" |
 174                sed -e '
 175                        s|//*|/|g
 176                        s|^\(\./\)*||
 177                        s|/\./|/|g
 178                        :start
 179                        s|\([^/]*\)/\.\./||
 180                        tstart
 181                        s|/*$||
 182                ')
 183        git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
 184        die "'$path' already exists in the index"
 185
 186        # perhaps the path exists and is already a git repo, else clone it
 187        if test -e "$path"
 188        then
 189                if test -d "$path"/.git -o -f "$path"/.git
 190                then
 191                        echo "Adding existing repo at '$path' to the index"
 192                else
 193                        die "'$path' already exists and is not a valid git repo"
 194                fi
 195
 196                case "$repo" in
 197                ./*|../*)
 198                        url=$(resolve_relative_url "$repo") || exit
 199                    ;;
 200                *)
 201                        url="$repo"
 202                        ;;
 203                esac
 204                git config submodule."$path".url "$url"
 205        else
 206
 207                module_clone "$path" "$realrepo" || exit
 208                (
 209                        unset GIT_DIR
 210                        cd "$path" &&
 211                        # ash fails to wordsplit ${branch:+-b "$branch"...}
 212                        case "$branch" in
 213                        '') git checkout -f -q ;;
 214                        ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
 215                        esac
 216                ) || die "Unable to checkout submodule '$path'"
 217        fi
 218
 219        git add "$path" ||
 220        die "Failed to add submodule '$path'"
 221
 222        git config -f .gitmodules submodule."$path".path "$path" &&
 223        git config -f .gitmodules submodule."$path".url "$repo" &&
 224        git add .gitmodules ||
 225        die "Failed to register submodule '$path'"
 226}
 227
 228#
 229# Execute an arbitrary command sequence in each checked out
 230# submodule
 231#
 232# $@ = command to execute
 233#
 234cmd_foreach()
 235{
 236        module_list |
 237        while read mode sha1 stage path
 238        do
 239                if test -e "$path"/.git
 240                then
 241                        say "Entering '$path'"
 242                        (cd "$path" && eval "$@") ||
 243                        die "Stopping at '$path'; script returned non-zero status."
 244                fi
 245        done
 246}
 247
 248#
 249# Register submodules in .git/config
 250#
 251# $@ = requested paths (default to all)
 252#
 253cmd_init()
 254{
 255        # parse $args after "submodule ... init".
 256        while test $# -ne 0
 257        do
 258                case "$1" in
 259                -q|--quiet)
 260                        quiet=1
 261                        ;;
 262                --)
 263                        shift
 264                        break
 265                        ;;
 266                -*)
 267                        usage
 268                        ;;
 269                *)
 270                        break
 271                        ;;
 272                esac
 273                shift
 274        done
 275
 276        module_list "$@" |
 277        while read mode sha1 stage path
 278        do
 279                # Skip already registered paths
 280                name=$(module_name "$path") || exit
 281                url=$(git config submodule."$name".url)
 282                test -z "$url" || continue
 283
 284                url=$(git config -f .gitmodules submodule."$name".url)
 285                test -z "$url" &&
 286                die "No url found for submodule path '$path' in .gitmodules"
 287
 288                # Possibly a url relative to parent
 289                case "$url" in
 290                ./*|../*)
 291                        url=$(resolve_relative_url "$url") || exit
 292                        ;;
 293                esac
 294
 295                git config submodule."$name".url "$url" ||
 296                die "Failed to register url for submodule path '$path'"
 297
 298                test true != "$(git config -f .gitmodules --bool \
 299                        submodule."$name".rebase)" ||
 300                git config submodule."$name".rebase true ||
 301                die "Failed to register submodule path '$path' as rebasing"
 302
 303                say "Submodule '$name' ($url) registered for path '$path'"
 304        done
 305}
 306
 307#
 308# Update each submodule path to correct revision, using clone and checkout as needed
 309#
 310# $@ = requested paths (default to all)
 311#
 312cmd_update()
 313{
 314        # parse $args after "submodule ... update".
 315        while test $# -ne 0
 316        do
 317                case "$1" in
 318                -q|--quiet)
 319                        shift
 320                        quiet=1
 321                        ;;
 322                -i|--init)
 323                        shift
 324                        cmd_init "$@" || return
 325                        ;;
 326                -N|--no-fetch)
 327                        shift
 328                        nofetch=1
 329                        ;;
 330                -r|--rebase)
 331                        shift
 332                        rebase=true
 333                        ;;
 334                --)
 335                        shift
 336                        break
 337                        ;;
 338                -*)
 339                        usage
 340                        ;;
 341                *)
 342                        break
 343                        ;;
 344                esac
 345        done
 346
 347        module_list "$@" |
 348        while read mode sha1 stage path
 349        do
 350                name=$(module_name "$path") || exit
 351                url=$(git config submodule."$name".url)
 352                rebase_module=$(git config --bool submodule."$name".rebase)
 353                if test -z "$url"
 354                then
 355                        # Only mention uninitialized submodules when its
 356                        # path have been specified
 357                        test "$#" != "0" &&
 358                        say "Submodule path '$path' not initialized" &&
 359                        say "Maybe you want to use 'update --init'?"
 360                        continue
 361                fi
 362
 363                if ! test -d "$path"/.git -o -f "$path"/.git
 364                then
 365                        module_clone "$path" "$url" || exit
 366                        subsha1=
 367                else
 368                        subsha1=$(unset GIT_DIR; cd "$path" &&
 369                                git rev-parse --verify HEAD) ||
 370                        die "Unable to find current revision in submodule path '$path'"
 371                fi
 372
 373                if test true = "$rebase"
 374                then
 375                        rebase_module=true
 376                fi
 377
 378                if test "$subsha1" != "$sha1"
 379                then
 380                        force=
 381                        if test -z "$subsha1"
 382                        then
 383                                force="-f"
 384                        fi
 385
 386                        if test -z "$nofetch"
 387                        then
 388                                (unset GIT_DIR; cd "$path" &&
 389                                        git-fetch) ||
 390                                die "Unable to fetch in submodule path '$path'"
 391                        fi
 392
 393                        if test true = "$rebase_module"
 394                        then
 395                                command="git-rebase"
 396                                action="rebase"
 397                                msg="rebased onto"
 398                        else
 399                                command="git-checkout $force -q"
 400                                action="checkout"
 401                                msg="checked out"
 402                        fi
 403
 404                        (unset GIT_DIR; cd "$path" && $command "$sha1") ||
 405                        die "Unable to $action '$sha1' in submodule path '$path'"
 406                        say "Submodule path '$path': $msg '$sha1'"
 407                fi
 408        done
 409}
 410
 411set_name_rev () {
 412        revname=$( (
 413                unset GIT_DIR
 414                cd "$1" && {
 415                        git describe "$2" 2>/dev/null ||
 416                        git describe --tags "$2" 2>/dev/null ||
 417                        git describe --contains "$2" 2>/dev/null ||
 418                        git describe --all --always "$2"
 419                }
 420        ) )
 421        test -z "$revname" || revname=" ($revname)"
 422}
 423#
 424# Show commit summary for submodules in index or working tree
 425#
 426# If '--cached' is given, show summary between index and given commit,
 427# or between working tree and given commit
 428#
 429# $@ = [commit (default 'HEAD'),] requested paths (default all)
 430#
 431cmd_summary() {
 432        summary_limit=-1
 433        for_status=
 434
 435        # parse $args after "submodule ... summary".
 436        while test $# -ne 0
 437        do
 438                case "$1" in
 439                --cached)
 440                        cached="$1"
 441                        ;;
 442                --for-status)
 443                        for_status="$1"
 444                        ;;
 445                -n|--summary-limit)
 446                        if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
 447                        then
 448                                :
 449                        else
 450                                usage
 451                        fi
 452                        shift
 453                        ;;
 454                --)
 455                        shift
 456                        break
 457                        ;;
 458                -*)
 459                        usage
 460                        ;;
 461                *)
 462                        break
 463                        ;;
 464                esac
 465                shift
 466        done
 467
 468        test $summary_limit = 0 && return
 469
 470        if rev=$(git rev-parse -q --verify "$1^0")
 471        then
 472                head=$rev
 473                shift
 474        else
 475                head=HEAD
 476        fi
 477
 478        cd_to_toplevel
 479        # Get modified modules cared by user
 480        modules=$(git diff-index $cached --raw $head -- "$@" |
 481                egrep '^:([0-7]* )?160000' |
 482                while read mod_src mod_dst sha1_src sha1_dst status name
 483                do
 484                        # Always show modules deleted or type-changed (blob<->module)
 485                        test $status = D -o $status = T && echo "$name" && continue
 486                        # Also show added or modified modules which are checked out
 487                        GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
 488                        echo "$name"
 489                done
 490        )
 491
 492        test -z "$modules" && return
 493
 494        git diff-index $cached --raw $head -- $modules |
 495        egrep '^:([0-7]* )?160000' |
 496        cut -c2- |
 497        while read mod_src mod_dst sha1_src sha1_dst status name
 498        do
 499                if test -z "$cached" &&
 500                        test $sha1_dst = 0000000000000000000000000000000000000000
 501                then
 502                        case "$mod_dst" in
 503                        160000)
 504                                sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
 505                                ;;
 506                        100644 | 100755 | 120000)
 507                                sha1_dst=$(git hash-object $name)
 508                                ;;
 509                        000000)
 510                                ;; # removed
 511                        *)
 512                                # unexpected type
 513                                echo >&2 "unexpected mode $mod_dst"
 514                                continue ;;
 515                        esac
 516                fi
 517                missing_src=
 518                missing_dst=
 519
 520                test $mod_src = 160000 &&
 521                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
 522                missing_src=t
 523
 524                test $mod_dst = 160000 &&
 525                ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
 526                missing_dst=t
 527
 528                total_commits=
 529                case "$missing_src,$missing_dst" in
 530                t,)
 531                        errmsg="  Warn: $name doesn't contain commit $sha1_src"
 532                        ;;
 533                ,t)
 534                        errmsg="  Warn: $name doesn't contain commit $sha1_dst"
 535                        ;;
 536                t,t)
 537                        errmsg="  Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
 538                        ;;
 539                *)
 540                        errmsg=
 541                        total_commits=$(
 542                        if test $mod_src = 160000 -a $mod_dst = 160000
 543                        then
 544                                range="$sha1_src...$sha1_dst"
 545                        elif test $mod_src = 160000
 546                        then
 547                                range=$sha1_src
 548                        else
 549                                range=$sha1_dst
 550                        fi
 551                        GIT_DIR="$name/.git" \
 552                        git log --pretty=oneline --first-parent $range | wc -l
 553                        )
 554                        total_commits=" ($(($total_commits + 0)))"
 555                        ;;
 556                esac
 557
 558                sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
 559                sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
 560                if test $status = T
 561                then
 562                        if test $mod_dst = 160000
 563                        then
 564                                echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
 565                        else
 566                                echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
 567                        fi
 568                else
 569                        echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
 570                fi
 571                if test -n "$errmsg"
 572                then
 573                        # Don't give error msg for modification whose dst is not submodule
 574                        # i.e. deleted or changed to blob
 575                        test $mod_dst = 160000 && echo "$errmsg"
 576                else
 577                        if test $mod_src = 160000 -a $mod_dst = 160000
 578                        then
 579                                limit=
 580                                test $summary_limit -gt 0 && limit="-$summary_limit"
 581                                GIT_DIR="$name/.git" \
 582                                git log $limit --pretty='format:  %m %s' \
 583                                --first-parent $sha1_src...$sha1_dst
 584                        elif test $mod_dst = 160000
 585                        then
 586                                GIT_DIR="$name/.git" \
 587                                git log --pretty='format:  > %s' -1 $sha1_dst
 588                        else
 589                                GIT_DIR="$name/.git" \
 590                                git log --pretty='format:  < %s' -1 $sha1_src
 591                        fi
 592                        echo
 593                fi
 594                echo
 595        done |
 596        if test -n "$for_status"; then
 597                echo "# Modified submodules:"
 598                echo "#"
 599                sed -e 's|^|# |' -e 's|^# $|#|'
 600        else
 601                cat
 602        fi
 603}
 604#
 605# List all submodules, prefixed with:
 606#  - submodule not initialized
 607#  + different revision checked out
 608#
 609# If --cached was specified the revision in the index will be printed
 610# instead of the currently checked out revision.
 611#
 612# $@ = requested paths (default to all)
 613#
 614cmd_status()
 615{
 616        # parse $args after "submodule ... status".
 617        while test $# -ne 0
 618        do
 619                case "$1" in
 620                -q|--quiet)
 621                        quiet=1
 622                        ;;
 623                --cached)
 624                        cached=1
 625                        ;;
 626                --)
 627                        shift
 628                        break
 629                        ;;
 630                -*)
 631                        usage
 632                        ;;
 633                *)
 634                        break
 635                        ;;
 636                esac
 637                shift
 638        done
 639
 640        module_list "$@" |
 641        while read mode sha1 stage path
 642        do
 643                name=$(module_name "$path") || exit
 644                url=$(git config submodule."$name".url)
 645                if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
 646                then
 647                        say "-$sha1 $path"
 648                        continue;
 649                fi
 650                set_name_rev "$path" "$sha1"
 651                if git diff-files --quiet -- "$path"
 652                then
 653                        say " $sha1 $path$revname"
 654                else
 655                        if test -z "$cached"
 656                        then
 657                                sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
 658                                set_name_rev "$path" "$sha1"
 659                        fi
 660                        say "+$sha1 $path$revname"
 661                fi
 662        done
 663}
 664#
 665# Sync remote urls for submodules
 666# This makes the value for remote.$remote.url match the value
 667# specified in .gitmodules.
 668#
 669cmd_sync()
 670{
 671        while test $# -ne 0
 672        do
 673                case "$1" in
 674                -q|--quiet)
 675                        quiet=1
 676                        shift
 677                        ;;
 678                --)
 679                        shift
 680                        break
 681                        ;;
 682                -*)
 683                        usage
 684                        ;;
 685                *)
 686                        break
 687                        ;;
 688                esac
 689        done
 690        cd_to_toplevel
 691        module_list "$@" |
 692        while read mode sha1 stage path
 693        do
 694                name=$(module_name "$path")
 695                url=$(git config -f .gitmodules --get submodule."$name".url)
 696
 697                # Possibly a url relative to parent
 698                case "$url" in
 699                ./*|../*)
 700                        url=$(resolve_relative_url "$url") || exit
 701                        ;;
 702                esac
 703
 704                if test -e "$path"/.git
 705                then
 706                (
 707                        unset GIT_DIR
 708                        cd "$path"
 709                        remote=$(get_default_remote)
 710                        say "Synchronizing submodule url for '$name'"
 711                        git config remote."$remote".url "$url"
 712                )
 713                fi
 714        done
 715}
 716
 717# This loop parses the command line arguments to find the
 718# subcommand name to dispatch.  Parsing of the subcommand specific
 719# options are primarily done by the subcommand implementations.
 720# Subcommand specific options such as --branch and --cached are
 721# parsed here as well, for backward compatibility.
 722
 723while test $# != 0 && test -z "$command"
 724do
 725        case "$1" in
 726        add | foreach | init | update | status | summary | sync)
 727                command=$1
 728                ;;
 729        -q|--quiet)
 730                quiet=1
 731                ;;
 732        -b|--branch)
 733                case "$2" in
 734                '')
 735                        usage
 736                        ;;
 737                esac
 738                branch="$2"; shift
 739                ;;
 740        --cached)
 741                cached="$1"
 742                ;;
 743        --)
 744                break
 745                ;;
 746        -*)
 747                usage
 748                ;;
 749        *)
 750                break
 751                ;;
 752        esac
 753        shift
 754done
 755
 756# No command word defaults to "status"
 757test -n "$command" || command=status
 758
 759# "-b branch" is accepted only by "add"
 760if test -n "$branch" && test "$command" != add
 761then
 762        usage
 763fi
 764
 765# "--cached" is accepted only by "status" and "summary"
 766if test -n "$cached" && test "$command" != status -a "$command" != summary
 767then
 768        usage
 769fi
 770
 771"cmd_$command" "$@"