git-parse-remote.shon commit config_rename_section: fix FILE* leak (fc1905b)
   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_default_remote () {
  72        curr_branch=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
  73        origin=$(git-repo-config --get "branch.$curr_branch.remote")
  74        echo ${origin:-origin}
  75}
  76
  77get_remote_default_refs_for_push () {
  78        data_source=$(get_data_source "$1")
  79        case "$data_source" in
  80        '' | config-partial | branches | branches-partial)
  81                ;; # no default push mapping, just send matching refs.
  82        config)
  83                git-repo-config --get-all "remote.$1.push" ;;
  84        remotes)
  85                sed -ne '/^Push: */{
  86                        s///p
  87                }' "$GIT_DIR/remotes/$1" ;;
  88        *)
  89                die "internal error: get-remote-default-ref-for-push $1" ;;
  90        esac
  91}
  92
  93# Called from canon_refs_list_for_fetch -d "$remote", which
  94# is called from get_remote_default_refs_for_fetch to grok
  95# refspecs that are retrieved from the configuration, but not
  96# from get_remote_refs_for_fetch when it deals with refspecs
  97# supplied on the command line.  $ls_remote_result has the list
  98# of refs available at remote.
  99expand_refs_wildcard () {
 100        for ref
 101        do
 102                lref=${ref#'+'}
 103                # a non glob pattern is given back as-is.
 104                expr "z$lref" : 'zrefs/.*/\*:refs/.*/\*$' >/dev/null || {
 105                        echo "$ref"
 106                        continue
 107                }
 108
 109                from=`expr "z$lref" : 'z\(refs/.*/\)\*:refs/.*/\*$'`
 110                to=`expr "z$lref" : 'zrefs/.*/\*:\(refs/.*/\)\*$'`
 111                local_force=
 112                test "z$lref" = "z$ref" || local_force='+'
 113                echo "$ls_remote_result" |
 114                sed -e '/\^{}$/d' |
 115                (
 116                        IFS='   '
 117                        while read sha1 name
 118                        do
 119                                # ignore the ones that do not start with $from
 120                                mapped=${name#"$from"}
 121                                test "z$name" = "z$mapped" && continue
 122                                echo "${local_force}${name}:${to}${mapped}"
 123                        done
 124                )
 125        done
 126}
 127
 128# Subroutine to canonicalize remote:local notation.
 129canon_refs_list_for_fetch () {
 130        # If called from get_remote_default_refs_for_fetch
 131        # leave the branches in branch.${curr_branch}.merge alone,
 132        # or the first one otherwise; add prefix . to the rest
 133        # to prevent the secondary branches to be merged by default.
 134        merge_branches=
 135        found_mergeref=
 136        curr_branch=
 137        if test "$1" = "-d"
 138        then
 139                shift ; remote="$1" ; shift
 140                if test "$remote" = "$(get_default_remote)"
 141                then
 142                        curr_branch=$(git-symbolic-ref HEAD | \
 143                            sed -e 's|^refs/heads/||')
 144                        merge_branches=$(git-repo-config \
 145                            --get-all "branch.${curr_branch}.merge")
 146                fi
 147                set x $(expand_refs_wildcard "$@")
 148                shift
 149        fi
 150        for ref
 151        do
 152                force=
 153                case "$ref" in
 154                +*)
 155                        ref=$(expr "z$ref" : 'z+\(.*\)')
 156                        force=+
 157                        ;;
 158                esac
 159                expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
 160                remote=$(expr "z$ref" : 'z\([^:]*\):')
 161                local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
 162                dot_prefix=.
 163                if test -z "$merge_branches"
 164                then
 165                        merge_branches=$remote
 166                        dot_prefix=
 167                else
 168                        for merge_branch in $merge_branches
 169                        do
 170                            [ "$remote" = "$merge_branch" ] &&
 171                            dot_prefix= && break
 172                        done
 173                fi
 174                if test -z $dot_prefix
 175                then
 176                        found_mergeref=true
 177                fi
 178                case "$remote" in
 179                '') remote=HEAD ;;
 180                refs/heads/* | refs/tags/* | refs/remotes/*) ;;
 181                heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
 182                *) remote="refs/heads/$remote" ;;
 183                esac
 184                case "$local" in
 185                '') local= ;;
 186                refs/heads/* | refs/tags/* | refs/remotes/*) ;;
 187                heads/* | tags/* | remotes/* ) local="refs/$local" ;;
 188                *) local="refs/heads/$local" ;;
 189                esac
 190
 191                if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
 192                then
 193                   git-check-ref-format "$local_ref_name" ||
 194                   die "* refusing to create funny ref '$local_ref_name' locally"
 195                fi
 196                echo "${dot_prefix}${force}${remote}:${local}"
 197        done
 198        if test -z "$found_mergeref" -a "$curr_branch"
 199        then
 200                echo >&2 "Warning: No merge candidate found because value of config option
 201         \"branch.${curr_branch}.merge\" does not match any remote branch fetched."
 202        fi
 203}
 204
 205# Returns list of src: (no store), or src:dst (store)
 206get_remote_default_refs_for_fetch () {
 207        data_source=$(get_data_source "$1")
 208        case "$data_source" in
 209        '' | config-partial | branches-partial)
 210                echo "HEAD:" ;;
 211        config)
 212                canon_refs_list_for_fetch -d "$1" \
 213                        $(git-repo-config --get-all "remote.$1.fetch") ;;
 214        branches)
 215                remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
 216                case "$remote_branch" in '') remote_branch=master ;; esac
 217                echo "refs/heads/${remote_branch}:refs/heads/$1"
 218                ;;
 219        remotes)
 220                canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
 221                                                s///p
 222                                        }' "$GIT_DIR/remotes/$1")
 223                ;;
 224        *)
 225                die "internal error: get-remote-default-ref-for-push $1" ;;
 226        esac
 227}
 228
 229get_remote_refs_for_push () {
 230        case "$#" in
 231        0) die "internal error: get-remote-refs-for-push." ;;
 232        1) get_remote_default_refs_for_push "$@" ;;
 233        *) shift; echo "$@" ;;
 234        esac
 235}
 236
 237get_remote_refs_for_fetch () {
 238        case "$#" in
 239        0)
 240            die "internal error: get-remote-refs-for-fetch." ;;
 241        1)
 242            get_remote_default_refs_for_fetch "$@" ;;
 243        *)
 244            shift
 245            tag_just_seen=
 246            for ref
 247            do
 248                if test "$tag_just_seen"
 249                then
 250                    echo "refs/tags/${ref}:refs/tags/${ref}"
 251                    tag_just_seen=
 252                    continue
 253                else
 254                    case "$ref" in
 255                    tag)
 256                        tag_just_seen=yes
 257                        continue
 258                        ;;
 259                    esac
 260                fi
 261                canon_refs_list_for_fetch "$ref"
 262            done
 263            ;;
 264        esac
 265}
 266
 267resolve_alternates () {
 268        # original URL (xxx.git)
 269        top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
 270        while read path
 271        do
 272                case "$path" in
 273                \#* | '')
 274                        continue ;;
 275                /*)
 276                        echo "$top_$path/" ;;
 277                ../*)
 278                        # relative -- ugly but seems to work.
 279                        echo "$1/objects/$path/" ;;
 280                *)
 281                        # exit code may not be caught by the reader.
 282                        echo "bad alternate: $path"
 283                        exit 1 ;;
 284                esac
 285        done
 286}