blob: 5f47b18141a0758c3d0dae2cfe764495c92139c5 [file] [log] [blame]
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -07001#!/bin/sh
2
Junio C Hamanoe8cc80d2005-11-23 23:46:13 -08003# 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.
Dan Loewenherz7bd93c12009-04-22 21:46:02 -04005GIT_DIR=$(git rev-parse -q --git-dir) || :;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -07006
7get_data_source () {
8 case "$1" in
9 */*)
Junio C Hamanoea560e62006-12-22 15:37:48 -080010 echo ''
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070011 ;;
Paolo Bonzini9debc322007-03-15 09:23:20 +010012 .)
13 echo self
14 ;;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070015 *)
Junio C Hamano5be60072007-07-02 22:52:14 -070016 if test "$(git config --get "remote.$1.url")"
Johannes Schindelin73136b22006-05-03 15:20:21 +020017 then
18 echo config
19 elif test -f "$GIT_DIR/remotes/$1"
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070020 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 '')
Junio C Hamanoea560e62006-12-22 15:37:48 -080035 echo "$1"
Johannes Schindelin73136b22006-05-03 15:20:21 +020036 ;;
Paolo Bonzini9debc322007-03-15 09:23:20 +010037 self)
38 echo "$1"
39 ;;
Johannes Schindelin73136b22006-05-03 15:20:21 +020040 config)
Junio C Hamano5be60072007-07-02 22:52:14 -070041 git config --get "remote.$1.url"
Johannes Schindelin73136b22006-05-03 15:20:21 +020042 ;;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070043 remotes)
44 sed -ne '/^URL: */{
45 s///p
46 q
Junio C Hamanoea560e62006-12-22 15:37:48 -080047 }' "$GIT_DIR/remotes/$1"
48 ;;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070049 branches)
Junio C Hamanoea560e62006-12-22 15:37:48 -080050 sed -e 's/#.*//' "$GIT_DIR/branches/$1"
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070051 ;;
52 *)
53 die "internal error: get-remote-url $1" ;;
54 esac
55}
56
Santi Béjar648ad182006-09-23 12:05:43 +020057get_default_remote () {
Junio C Hamano5be60072007-07-02 22:52:14 -070058 curr_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')
59 origin=$(git config --get "branch.$curr_branch.remote")
Santi Béjar648ad182006-09-23 12:05:43 +020060 echo ${origin:-origin}
61}
62
Santi Béjar97af7ff2009-06-12 00:39:18 +020063get_remote_merge_branch () {
64 case "$#" in
65 0|1)
Santi Béjare9460a62009-06-12 00:39:19 +020066 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)
72 ;;
Santi Béjar97af7ff2009-06-12 00:39:18 +020073 *)
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+\(.*\)')
82 ;;
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
92
93 [ -n "$remote" ] && echo "refs/remotes/$repo/$remote"
94 esac
95}