0ab1192f81217fe22715fd351395e3c9b05c86a0
   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 -q --git-dir) || :;
   6
   7get_remote_url () {
   8        git ls-remote --get-url "$1"
   9}
  10
  11get_default_remote () {
  12        curr_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')
  13        origin=$(git config --get "branch.$curr_branch.remote")
  14        echo ${origin:-origin}
  15}
  16
  17get_remote_merge_branch () {
  18        case "$#" in
  19        0|1)
  20            origin="$1"
  21            default=$(get_default_remote)
  22            test -z "$origin" && origin=$default
  23            curr_branch=$(git symbolic-ref -q HEAD) &&
  24            [ "$origin" = "$default" ] &&
  25            echo $(git for-each-ref --format='%(upstream)' $curr_branch)
  26            ;;
  27        *)
  28            repo=$1
  29            shift
  30            ref=$1
  31            # FIXME: It should return the tracking branch
  32            #        Currently only works with the default mapping
  33            case "$ref" in
  34            +*)
  35                ref=$(expr "z$ref" : 'z+\(.*\)')
  36                ;;
  37            esac
  38            expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
  39            remote=$(expr "z$ref" : 'z\([^:]*\):')
  40            case "$remote" in
  41            '' | HEAD ) remote=HEAD ;;
  42            heads/*) remote=${remote#heads/} ;;
  43            refs/heads/*) remote=${remote#refs/heads/} ;;
  44            refs/* | tags/* | remotes/* ) remote=
  45            esac
  46            [ -n "$remote" ] && case "$repo" in
  47                .)
  48                    echo "refs/heads/$remote"
  49                    ;;
  50                *)
  51                    echo "refs/remotes/$repo/$remote"
  52                    ;;
  53            esac
  54        esac
  55}