contrib/thunderbird-patch-inline: do not require bash to run the script
[git/git.git] / git-parse-remote.sh
CommitLineData
ac4b0cff
JH
1#!/bin/sh
2
e8cc80d0
JH
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.
7bd93c1c 5GIT_DIR=$(git rev-parse -q --git-dir) || :;
ac4b0cff
JH
6
7get_data_source () {
8 case "$1" in
9 */*)
ea560e6d 10 echo ''
ac4b0cff 11 ;;
9debc324
PB
12 .)
13 echo self
14 ;;
ac4b0cff 15 *)
5be60078 16 if test "$(git config --get "remote.$1.url")"
73136b2e
JS
17 then
18 echo config
19 elif test -f "$GIT_DIR/remotes/$1"
ac4b0cff
JH
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
29}
30
31get_remote_url () {
32 data_source=$(get_data_source "$1")
33 case "$data_source" in
34 '')
ea560e6d 35 echo "$1"
73136b2e 36 ;;
9debc324
PB
37 self)
38 echo "$1"
39 ;;
73136b2e 40 config)
5be60078 41 git config --get "remote.$1.url"
73136b2e 42 ;;
ac4b0cff
JH
43 remotes)
44 sed -ne '/^URL: */{
45 s///p
46 q
ea560e6d
JH
47 }' "$GIT_DIR/remotes/$1"
48 ;;
ac4b0cff 49 branches)
ea560e6d 50 sed -e 's/#.*//' "$GIT_DIR/branches/$1"
ac4b0cff
JH
51 ;;
52 *)
53 die "internal error: get-remote-url $1" ;;
54 esac
55}
56
648ad18f 57get_default_remote () {
9ecd3ada
SB
58 curr_branch=$(git symbolic-ref -q HEAD)
59 curr_branch="${cur_branch#refs/heads/}"
5be60078 60 origin=$(git config --get "branch.$curr_branch.remote")
648ad18f
SB
61 echo ${origin:-origin}
62}
63
97af7ff0
SB
64get_remote_merge_branch () {
65 case "$#" in
66 0|1)
e9460a66
SB
67 origin="$1"
68 default=$(get_default_remote)
69 test -z "$origin" && origin=$default
f864f261 70 curr_branch=$(git symbolic-ref -q HEAD) &&
e9460a66
SB
71 [ "$origin" = "$default" ] &&
72 echo $(git for-each-ref --format='%(upstream)' $curr_branch)
73 ;;
97af7ff0
SB
74 *)
75 repo=$1
76 shift
77 ref=$1
78 # FIXME: It should return the tracking branch
79 # Currently only works with the default mapping
80 case "$ref" in
81 +*)
82 ref=$(expr "z$ref" : 'z+\(.*\)')
83 ;;
84 esac
85 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
86 remote=$(expr "z$ref" : 'z\([^:]*\):')
87 case "$remote" in
88 '' | HEAD ) remote=HEAD ;;
89 heads/*) remote=${remote#heads/} ;;
90 refs/heads/*) remote=${remote#refs/heads/} ;;
91 refs/* | tags/* | remotes/* ) remote=
92 esac
fe249b42
MZ
93 [ -n "$remote" ] && case "$repo" in
94 .)
95 echo "refs/heads/$remote"
96 ;;
97 *)
98 echo "refs/remotes/$repo/$remote"
99 ;;
100 esac
97af7ff0
SB
101 esac
102}