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