git-submodule.shon commit t5300: add test for "unpack-objects --strict" (c0e809e)
   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] [add <repo> [-b branch]|status|init|update] [--] [<path>...]'
   8OPTIONS_SPEC=
   9. git-sh-setup
  10require_work_tree
  11
  12command=
  13branch=
  14quiet=
  15cached=
  16
  17#
  18# print stuff on stdout unless -q was specified
  19#
  20say()
  21{
  22        if test -z "$quiet"
  23        then
  24                echo "$@"
  25        fi
  26}
  27
  28# NEEDSWORK: identical function exists in get_repo_base in clone.sh
  29get_repo_base() {
  30        (
  31                cd "`/bin/pwd`" &&
  32                cd "$1" || cd "$1.git" &&
  33                {
  34                        cd .git
  35                        pwd
  36                }
  37        ) 2>/dev/null
  38}
  39
  40# Resolve relative url by appending to parent's url
  41resolve_relative_url ()
  42{
  43        branch="$(git symbolic-ref HEAD 2>/dev/null)"
  44        remote="$(git config branch.${branch#refs/heads/}.remote)"
  45        remote="${remote:-origin}"
  46        remoteurl="$(git config remote.$remote.url)" ||
  47                die "remote ($remote) does not have a url in .git/config"
  48        url="$1"
  49        while test -n "$url"
  50        do
  51                case "$url" in
  52                ../*)
  53                        url="${url#../}"
  54                        remoteurl="${remoteurl%/*}"
  55                        ;;
  56                ./*)
  57                        url="${url#./}"
  58                        ;;
  59                *)
  60                        break;;
  61                esac
  62        done
  63        echo "$remoteurl/$url"
  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' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
  75        name=$( GIT_CONFIG=.gitmodules \
  76                git config --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"; then
 153                usage
 154        fi
 155
 156        case "$repo" in
 157        ./*|../*)
 158                # dereference source url relative to parent's url
 159                realrepo="$(resolve_relative_url $repo)" ;;
 160        *)
 161                # Turn the source into an absolute path if
 162                # it is local
 163                if base=$(get_repo_base "$repo"); then
 164                        repo="$base"
 165                fi
 166                realrepo=$repo
 167                ;;
 168        esac
 169
 170        # Guess path from repo if not specified or strip trailing slashes
 171        if test -z "$path"; then
 172                path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 173        else
 174                path=$(echo "$path" | sed -e 's|/*$||')
 175        fi
 176
 177        test -e "$path" &&
 178        die "'$path' already exists"
 179
 180        git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
 181        die "'$path' already exists in the index"
 182
 183        module_clone "$path" "$realrepo" || exit
 184        (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
 185        die "Unable to checkout submodule '$path'"
 186        git add "$path" ||
 187        die "Failed to add submodule '$path'"
 188
 189        GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
 190        GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
 191        git add .gitmodules ||
 192        die "Failed to register submodule '$path'"
 193}
 194
 195#
 196# Register submodules in .git/config
 197#
 198# $@ = requested paths (default to all)
 199#
 200cmd_init()
 201{
 202        # parse $args after "submodule ... init".
 203        while test $# -ne 0
 204        do
 205                case "$1" in
 206                -q|--quiet)
 207                        quiet=1
 208                        ;;
 209                --)
 210                        shift
 211                        break
 212                        ;;
 213                -*)
 214                        usage
 215                        ;;
 216                *)
 217                        break
 218                        ;;
 219                esac
 220                shift
 221        done
 222
 223        git ls-files --stage -- "$@" | grep -e '^160000 ' |
 224        while read mode sha1 stage path
 225        do
 226                # Skip already registered paths
 227                name=$(module_name "$path") || exit
 228                url=$(git config submodule."$name".url)
 229                test -z "$url" || continue
 230
 231                url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
 232                test -z "$url" &&
 233                die "No url found for submodule path '$path' in .gitmodules"
 234
 235                # Possibly a url relative to parent
 236                case "$url" in
 237                ./*|../*)
 238                        url="$(resolve_relative_url "$url")"
 239                        ;;
 240                esac
 241
 242                git config submodule."$name".url "$url" ||
 243                die "Failed to register url for submodule path '$path'"
 244
 245                say "Submodule '$name' ($url) registered for path '$path'"
 246        done
 247}
 248
 249#
 250# Update each submodule path to correct revision, using clone and checkout as needed
 251#
 252# $@ = requested paths (default to all)
 253#
 254cmd_update()
 255{
 256        # parse $args after "submodule ... update".
 257        while test $# -ne 0
 258        do
 259                case "$1" in
 260                -q|--quiet)
 261                        quiet=1
 262                        ;;
 263                --)
 264                        shift
 265                        break
 266                        ;;
 267                -*)
 268                        usage
 269                        ;;
 270                *)
 271                        break
 272                        ;;
 273                esac
 274                shift
 275        done
 276
 277        git ls-files --stage -- "$@" | grep -e '^160000 ' |
 278        while read mode sha1 stage path
 279        do
 280                name=$(module_name "$path") || exit
 281                url=$(git config submodule."$name".url)
 282                if test -z "$url"
 283                then
 284                        # Only mention uninitialized submodules when its
 285                        # path have been specified
 286                        test "$#" != "0" &&
 287                        say "Submodule path '$path' not initialized"
 288                        continue
 289                fi
 290
 291                if ! test -d "$path"/.git
 292                then
 293                        module_clone "$path" "$url" || exit
 294                        subsha1=
 295                else
 296                        subsha1=$(unset GIT_DIR; cd "$path" &&
 297                                git rev-parse --verify HEAD) ||
 298                        die "Unable to find current revision in submodule path '$path'"
 299                fi
 300
 301                if test "$subsha1" != "$sha1"
 302                then
 303                        (unset GIT_DIR; cd "$path" && git-fetch &&
 304                                git-checkout -q "$sha1") ||
 305                        die "Unable to checkout '$sha1' in submodule path '$path'"
 306
 307                        say "Submodule path '$path': checked out '$sha1'"
 308                fi
 309        done
 310}
 311
 312set_name_rev () {
 313        revname=$( (
 314                unset GIT_DIR
 315                cd "$1" && {
 316                        git describe "$2" 2>/dev/null ||
 317                        git describe --tags "$2" 2>/dev/null ||
 318                        git describe --contains --tags "$2"
 319                }
 320        ) )
 321        test -z "$revname" || revname=" ($revname)"
 322}
 323
 324#
 325# List all submodules, prefixed with:
 326#  - submodule not initialized
 327#  + different revision checked out
 328#
 329# If --cached was specified the revision in the index will be printed
 330# instead of the currently checked out revision.
 331#
 332# $@ = requested paths (default to all)
 333#
 334cmd_status()
 335{
 336        # parse $args after "submodule ... status".
 337        while test $# -ne 0
 338        do
 339                case "$1" in
 340                -q|--quiet)
 341                        quiet=1
 342                        ;;
 343                --cached)
 344                        cached=1
 345                        ;;
 346                --)
 347                        shift
 348                        break
 349                        ;;
 350                -*)
 351                        usage
 352                        ;;
 353                *)
 354                        break
 355                        ;;
 356                esac
 357                shift
 358        done
 359
 360        git ls-files --stage -- "$@" | grep -e '^160000 ' |
 361        while read mode sha1 stage path
 362        do
 363                name=$(module_name "$path") || exit
 364                url=$(git config submodule."$name".url)
 365                if test -z "$url" || ! test -d "$path"/.git
 366                then
 367                        say "-$sha1 $path"
 368                        continue;
 369                fi
 370                set_name_rev "$path" "$sha1"
 371                if git diff-files --quiet -- "$path"
 372                then
 373                        say " $sha1 $path$revname"
 374                else
 375                        if test -z "$cached"
 376                        then
 377                                sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
 378                                set_name_rev "$path" "$sha1"
 379                        fi
 380                        say "+$sha1 $path$revname"
 381                fi
 382        done
 383}
 384
 385# This loop parses the command line arguments to find the
 386# subcommand name to dispatch.  Parsing of the subcommand specific
 387# options are primarily done by the subcommand implementations.
 388# Subcommand specific options such as --branch and --cached are
 389# parsed here as well, for backward compatibility.
 390
 391while test $# != 0 && test -z "$command"
 392do
 393        case "$1" in
 394        add | init | update | status)
 395                command=$1
 396                ;;
 397        -q|--quiet)
 398                quiet=1
 399                ;;
 400        -b|--branch)
 401                case "$2" in
 402                '')
 403                        usage
 404                        ;;
 405                esac
 406                branch="$2"; shift
 407                ;;
 408        --cached)
 409                cached=1
 410                ;;
 411        --)
 412                break
 413                ;;
 414        -*)
 415                usage
 416                ;;
 417        *)
 418                break
 419                ;;
 420        esac
 421        shift
 422done
 423
 424# No command word defaults to "status"
 425test -n "$command" || command=status
 426
 427# "-b branch" is accepted only by "add"
 428if test -n "$branch" && test "$command" != add
 429then
 430        usage
 431fi
 432
 433# "--cached" is accepted only by "status"
 434if test -n "$cached" && test "$command" != status
 435then
 436        usage
 437fi
 438
 439"cmd_$command" "$@"