Merge branch 'jk/grep-double-dash'
[git.git] / git-parse-remote.sh
blob5f47b18141a0758c3d0dae2cfe764495c92139c5
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_data_source () {
8 case "$1" in
9 */*)
10 echo ''
13 echo self
16 if test "$(git config --get "remote.$1.url")"
17 then
18 echo config
19 elif test -f "$GIT_DIR/remotes/$1"
20 then
21 echo remotes
22 elif test -f "$GIT_DIR/branches/$1"
23 then
24 echo branches
25 else
26 echo ''
27 fi ;;
28 esac
31 get_remote_url () {
32 data_source=$(get_data_source "$1")
33 case "$data_source" in
34 '')
35 echo "$1"
37 self)
38 echo "$1"
40 config)
41 git config --get "remote.$1.url"
43 remotes)
44 sed -ne '/^URL: */{
45 s///p
47 }' "$GIT_DIR/remotes/$1"
49 branches)
50 sed -e 's/#.*//' "$GIT_DIR/branches/$1"
53 die "internal error: get-remote-url $1" ;;
54 esac
57 get_default_remote () {
58 curr_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')
59 origin=$(git config --get "branch.$curr_branch.remote")
60 echo ${origin:-origin}
63 get_remote_merge_branch () {
64 case "$#" in
65 0|1)
66 origin="$1"
67 default=$(get_default_remote)
68 test -z "$origin" && origin=$default
69 curr_branch=$(git symbolic-ref -q HEAD)
70 [ "$origin" = "$default" ] &&
71 echo $(git for-each-ref --format='%(upstream)' $curr_branch)
74 repo=$1
75 shift
76 ref=$1
77 # FIXME: It should return the tracking branch
78 # Currently only works with the default mapping
79 case "$ref" in
80 +*)
81 ref=$(expr "z$ref" : 'z+\(.*\)')
83 esac
84 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
85 remote=$(expr "z$ref" : 'z\([^:]*\):')
86 case "$remote" in
87 '' | HEAD ) remote=HEAD ;;
88 heads/*) remote=${remote#heads/} ;;
89 refs/heads/*) remote=${remote#refs/heads/} ;;
90 refs/* | tags/* | remotes/* ) remote=
91 esac
93 [ -n "$remote" ] && echo "refs/remotes/$repo/$remote"
94 esac