git-submodule.shon commit gtksourceview2 support for gitview (24ccd8b)
   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>...]'
   8. git-sh-setup
   9require_work_tree
  10
  11add=
  12branch=
  13init=
  14update=
  15status=
  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/\([^a-zA-Z0-9_]\)/\\\1/g')
  77        name=$( GIT_CONFIG=.gitmodules \
  78                git config --get-regexp '^submodule\..*\.path$' |
  79                sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
  80       test -z "$name" &&
  81       die "No submodule mapping found in .gitmodules for path '$path'"
  82       echo "$name"
  83}
  84
  85#
  86# Clone a submodule
  87#
  88# Prior to calling, modules_update checks that a possibly existing
  89# path is not a git repository.
  90# Likewise, module_add checks that path does not exist at all,
  91# since it is the location of a new submodule.
  92#
  93module_clone()
  94{
  95        path=$1
  96        url=$2
  97
  98        # If there already is a directory at the submodule path,
  99        # expect it to be empty (since that is the default checkout
 100        # action) and try to remove it.
 101        # Note: if $path is a symlink to a directory the test will
 102        # succeed but the rmdir will fail. We might want to fix this.
 103        if test -d "$path"
 104        then
 105                rmdir "$path" 2>/dev/null ||
 106                die "Directory '$path' exist, but is neither empty nor a git repository"
 107        fi
 108
 109        test -e "$path" &&
 110        die "A file already exist at path '$path'"
 111
 112        git-clone -n "$url" "$path" ||
 113        die "Clone of '$url' into submodule path '$path' failed"
 114}
 115
 116#
 117# Add a new submodule to the working tree, .gitmodules and the index
 118#
 119# $@ = repo [path]
 120#
 121# optional branch is stored in global branch variable
 122#
 123module_add()
 124{
 125        repo=$1
 126        path=$2
 127
 128        if test -z "$repo"; then
 129                usage
 130        fi
 131
 132        case "$repo" in
 133        ./*|../*)
 134                # dereference source url relative to parent's url
 135                realrepo="$(resolve_relative_url $repo)" ;;
 136        *)
 137                # Turn the source into an absolute path if
 138                # it is local
 139                if base=$(get_repo_base "$repo"); then
 140                        repo="$base"
 141                fi
 142                realrepo=$repo
 143                ;;
 144        esac
 145
 146        # Guess path from repo if not specified or strip trailing slashes
 147        if test -z "$path"; then
 148                path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
 149        else
 150                path=$(echo "$path" | sed -e 's|/*$||')
 151        fi
 152
 153        test -e "$path" &&
 154        die "'$path' already exists"
 155
 156        git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
 157        die "'$path' already exists in the index"
 158
 159        module_clone "$path" "$realrepo" || exit
 160        (unset GIT_DIR && cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
 161        die "Unable to checkout submodule '$path'"
 162        git add "$path" ||
 163        die "Failed to add submodule '$path'"
 164
 165        GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
 166        GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
 167        git add .gitmodules ||
 168        die "Failed to register submodule '$path'"
 169}
 170
 171#
 172# Register submodules in .git/config
 173#
 174# $@ = requested paths (default to all)
 175#
 176modules_init()
 177{
 178        git ls-files --stage -- "$@" | grep -e '^160000 ' |
 179        while read mode sha1 stage path
 180        do
 181                # Skip already registered paths
 182                name=$(module_name "$path") || exit
 183                url=$(git config submodule."$name".url)
 184                test -z "$url" || continue
 185
 186                url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
 187                test -z "$url" &&
 188                die "No url found for submodule path '$path' in .gitmodules"
 189
 190                # Possibly a url relative to parent
 191                case "$url" in
 192                ./*|../*)
 193                        url="$(resolve_relative_url "$url")"
 194                        ;;
 195                esac
 196
 197                git config submodule."$name".url "$url" ||
 198                die "Failed to register url for submodule path '$path'"
 199
 200                say "Submodule '$name' ($url) registered for path '$path'"
 201        done
 202}
 203
 204#
 205# Update each submodule path to correct revision, using clone and checkout as needed
 206#
 207# $@ = requested paths (default to all)
 208#
 209modules_update()
 210{
 211        git ls-files --stage -- "$@" | grep -e '^160000 ' |
 212        while read mode sha1 stage path
 213        do
 214                name=$(module_name "$path") || exit
 215                url=$(git config submodule."$name".url)
 216                if test -z "$url"
 217                then
 218                        # Only mention uninitialized submodules when its
 219                        # path have been specified
 220                        test "$#" != "0" &&
 221                        say "Submodule path '$path' not initialized"
 222                        continue
 223                fi
 224
 225                if ! test -d "$path"/.git
 226                then
 227                        module_clone "$path" "$url" || exit
 228                        subsha1=
 229                else
 230                        subsha1=$(unset GIT_DIR && cd "$path" &&
 231                                git rev-parse --verify HEAD) ||
 232                        die "Unable to find current revision in submodule path '$path'"
 233                fi
 234
 235                if test "$subsha1" != "$sha1"
 236                then
 237                        (unset GIT_DIR && cd "$path" && git-fetch &&
 238                                git-checkout -q "$sha1") ||
 239                        die "Unable to checkout '$sha1' in submodule path '$path'"
 240
 241                        say "Submodule path '$path': checked out '$sha1'"
 242                fi
 243        done
 244}
 245
 246set_name_rev () {
 247        revname=$( (
 248                unset GIT_DIR &&
 249                cd "$1" && {
 250                        git describe "$2" 2>/dev/null ||
 251                        git describe --tags "$2" 2>/dev/null ||
 252                        git describe --contains --tags "$2"
 253                }
 254        ) )
 255        test -z "$revname" || revname=" ($revname)"
 256}
 257
 258#
 259# List all submodules, prefixed with:
 260#  - submodule not initialized
 261#  + different revision checked out
 262#
 263# If --cached was specified the revision in the index will be printed
 264# instead of the currently checked out revision.
 265#
 266# $@ = requested paths (default to all)
 267#
 268modules_list()
 269{
 270        git ls-files --stage -- "$@" | grep -e '^160000 ' |
 271        while read mode sha1 stage path
 272        do
 273                name=$(module_name "$path") || exit
 274                url=$(git config submodule."$name".url)
 275                if test -z "url" || ! test -d "$path"/.git
 276                then
 277                        say "-$sha1 $path"
 278                        continue;
 279                fi
 280                set_name_rev "$path" "$sha1"
 281                if git diff-files --quiet -- "$path"
 282                then
 283                        say " $sha1 $path$revname"
 284                else
 285                        if test -z "$cached"
 286                        then
 287                                sha1=$(unset GIT_DIR && cd "$path" && git rev-parse --verify HEAD)
 288                                set_name_rev "$path" "$sha1"
 289                        fi
 290                        say "+$sha1 $path$revname"
 291                fi
 292        done
 293}
 294
 295while test $# != 0
 296do
 297        case "$1" in
 298        add)
 299                add=1
 300                ;;
 301        init)
 302                init=1
 303                ;;
 304        update)
 305                update=1
 306                ;;
 307        status)
 308                status=1
 309                ;;
 310        -q|--quiet)
 311                quiet=1
 312                ;;
 313        -b|--branch)
 314                case "$2" in
 315                '')
 316                        usage
 317                        ;;
 318                esac
 319                branch="$2"; shift
 320                ;;
 321        --cached)
 322                cached=1
 323                ;;
 324        --)
 325                break
 326                ;;
 327        -*)
 328                usage
 329                ;;
 330        *)
 331                break
 332                ;;
 333        esac
 334        shift
 335done
 336
 337case "$add,$branch" in
 3381,*)
 339        ;;
 340,)
 341        ;;
 342,*)
 343        usage
 344        ;;
 345esac
 346
 347case "$add,$init,$update,$status,$cached" in
 3481,,,,)
 349        module_add "$@"
 350        ;;
 351,1,,,)
 352        modules_init "$@"
 353        ;;
 354,,1,,)
 355        modules_update "$@"
 356        ;;
 357,,,*,*)
 358        modules_list "$@"
 359        ;;
 360*)
 361        usage
 362        ;;
 363esac