get_remote_url(): use the same data source as ls-remote to get remote urls
[git.git] / git-parse-remote.sh
blob0ab1192f81217fe22715fd351395e3c9b05c86a0
1 #!/bin/sh
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.
5 GIT_DIR=$(git rev-parse -q --git-dir) || :;
7 get_remote_url () {
8 git ls-remote --get-url "$1"
11 get_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}
17 get_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)
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+\(.*\)')
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
48 echo "refs/heads/$remote"
51 echo "refs/remotes/$repo/$remote"
53 esac
54 esac