git-parse-remote.shon commit Teach diff -a as shorthand for --text (63ac450)
   1#!/bin/sh
   2
   3# git-ls-remote could be called from outside a git managed repository;
   4# this would fail in that case and would issue an error message.
   5GIT_DIR=$(git-rev-parse --git-dir 2>/dev/null) || :;
   6
   7get_data_source () {
   8        case "$1" in
   9        */*)
  10                # Not so fast.  This could be the partial URL shorthand...
  11                token=$(expr "z$1" : 'z\([^/]*\)/')
  12                remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
  13                if test "$(git-repo-config --get "remote.$token.url")"
  14                then
  15                        echo config-partial
  16                elif test -f "$GIT_DIR/branches/$token"
  17                then
  18                        echo branches-partial
  19                else
  20                        echo ''
  21                fi
  22                ;;
  23        *)
  24                if test "$(git-repo-config --get "remote.$1.url")"
  25                then
  26                        echo config
  27                elif test -f "$GIT_DIR/remotes/$1"
  28                then
  29                        echo remotes
  30                elif test -f "$GIT_DIR/branches/$1"
  31                then
  32                        echo branches
  33                else
  34                        echo ''
  35                fi ;;
  36        esac
  37}
  38
  39get_remote_url () {
  40        data_source=$(get_data_source "$1")
  41        case "$data_source" in
  42        '')
  43                echo "$1" ;;
  44        config-partial)
  45                token=$(expr "z$1" : 'z\([^/]*\)/')
  46                remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
  47                url=$(git-repo-config --get "remote.$token.url")
  48                echo "$url/$remainder"
  49                ;;
  50        config)
  51                git-repo-config --get "remote.$1.url"
  52                ;;
  53        remotes)
  54                sed -ne '/^URL: */{
  55                        s///p
  56                        q
  57                }' "$GIT_DIR/remotes/$1" ;;
  58        branches)
  59                sed -e 's/#.*//' "$GIT_DIR/branches/$1" ;;
  60        branches-partial)
  61                token=$(expr "z$1" : 'z\([^/]*\)/')
  62                remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
  63                url=$(sed -e 's/#.*//' "$GIT_DIR/branches/$token")
  64                echo "$url/$remainder"
  65                ;;
  66        *)
  67                die "internal error: get-remote-url $1" ;;
  68        esac
  69}
  70
  71get_remote_default_refs_for_push () {
  72        data_source=$(get_data_source "$1")
  73        case "$data_source" in
  74        '' | config-partial | branches | branches-partial)
  75                ;; # no default push mapping, just send matching refs.
  76        config)
  77                git-repo-config --get-all "remote.$1.push" ;;
  78        remotes)
  79                sed -ne '/^Push: */{
  80                        s///p
  81                }' "$GIT_DIR/remotes/$1" ;;
  82        *)
  83                die "internal error: get-remote-default-ref-for-push $1" ;;
  84        esac
  85}
  86
  87# Subroutine to canonicalize remote:local notation.
  88canon_refs_list_for_fetch () {
  89        # Leave only the first one alone; add prefix . to the rest
  90        # to prevent the secondary branches to be merged by default.
  91        dot_prefix=
  92        for ref
  93        do
  94                force=
  95                case "$ref" in
  96                +*)
  97                        ref=$(expr "z$ref" : 'z+\(.*\)')
  98                        force=+
  99                        ;;
 100                esac
 101                expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
 102                remote=$(expr "z$ref" : 'z\([^:]*\):')
 103                local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
 104                case "$remote" in
 105                '') remote=HEAD ;;
 106                refs/heads/* | refs/tags/* | refs/remotes/*) ;;
 107                heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
 108                *) remote="refs/heads/$remote" ;;
 109                esac
 110                case "$local" in
 111                '') local= ;;
 112                refs/heads/* | refs/tags/* | refs/remotes/*) ;;
 113                heads/* | tags/* | remotes/* ) local="refs/$local" ;;
 114                *) local="refs/heads/$local" ;;
 115                esac
 116
 117                if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
 118                then
 119                   git-check-ref-format "$local_ref_name" ||
 120                   die "* refusing to create funny ref '$local_ref_name' locally"
 121                fi
 122                echo "${dot_prefix}${force}${remote}:${local}"
 123                dot_prefix=.
 124        done
 125}
 126
 127# Returns list of src: (no store), or src:dst (store)
 128get_remote_default_refs_for_fetch () {
 129        data_source=$(get_data_source "$1")
 130        case "$data_source" in
 131        '' | config-partial | branches-partial)
 132                echo "HEAD:" ;;
 133        config)
 134                canon_refs_list_for_fetch \
 135                        $(git-repo-config --get-all "remote.$1.fetch") ;;
 136        branches)
 137                remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
 138                case "$remote_branch" in '') remote_branch=master ;; esac
 139                echo "refs/heads/${remote_branch}:refs/heads/$1"
 140                ;;
 141        remotes)
 142                # This prefixes the second and later default refspecs
 143                # with a '.', to signal git-fetch to mark them
 144                # not-for-merge.
 145                canon_refs_list_for_fetch $(sed -ne '/^Pull: */{
 146                                                s///p
 147                                        }' "$GIT_DIR/remotes/$1")
 148                ;;
 149        *)
 150                die "internal error: get-remote-default-ref-for-push $1" ;;
 151        esac
 152}
 153
 154get_remote_refs_for_push () {
 155        case "$#" in
 156        0) die "internal error: get-remote-refs-for-push." ;;
 157        1) get_remote_default_refs_for_push "$@" ;;
 158        *) shift; echo "$@" ;;
 159        esac
 160}
 161
 162get_remote_refs_for_fetch () {
 163        case "$#" in
 164        0)
 165            die "internal error: get-remote-refs-for-fetch." ;;
 166        1)
 167            get_remote_default_refs_for_fetch "$@" ;;
 168        *)
 169            shift
 170            tag_just_seen=
 171            for ref
 172            do
 173                if test "$tag_just_seen"
 174                then
 175                    echo "refs/tags/${ref}:refs/tags/${ref}"
 176                    tag_just_seen=
 177                    continue
 178                else
 179                    case "$ref" in
 180                    tag)
 181                        tag_just_seen=yes
 182                        continue
 183                        ;;
 184                    esac
 185                fi
 186                canon_refs_list_for_fetch "$ref"
 187            done
 188            ;;
 189        esac
 190}
 191
 192resolve_alternates () {
 193        # original URL (xxx.git)
 194        top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
 195        while read path
 196        do
 197                case "$path" in
 198                \#* | '')
 199                        continue ;;
 200                /*)
 201                        echo "$top_$path/" ;;
 202                ../*)
 203                        # relative -- ugly but seems to work.
 204                        echo "$1/objects/$path/" ;;
 205                *)
 206                        # exit code may not be caught by the reader.
 207                        echo "bad alternate: $path"
 208                        exit 1 ;;
 209                esac
 210        done
 211}