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