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