blob: 695a4094bb4230341618bd6f16d0bea9bff2e826 [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.
Junio C Hamano5be60072007-07-02 22:52:14 -07005GIT_DIR=$(git rev-parse --git-dir 2>/dev/null) || :;
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
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070063get_remote_default_refs_for_push () {
64 data_source=$(get_data_source "$1")
65 case "$data_source" in
Paolo Bonzini9debc322007-03-15 09:23:20 +010066 '' | branches | self)
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070067 ;; # no default push mapping, just send matching refs.
Johannes Schindelin73136b22006-05-03 15:20:21 +020068 config)
Junio C Hamano5be60072007-07-02 22:52:14 -070069 git config --get-all "remote.$1.push" ;;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070070 remotes)
71 sed -ne '/^Push: */{
72 s///p
73 }' "$GIT_DIR/remotes/$1" ;;
74 *)
75 die "internal error: get-remote-default-ref-for-push $1" ;;
76 esac
77}
78
Junio C Hamano56778822006-11-22 23:15:00 -080079# Called from canon_refs_list_for_fetch -d "$remote", which
80# is called from get_remote_default_refs_for_fetch to grok
81# refspecs that are retrieved from the configuration, but not
82# from get_remote_refs_for_fetch when it deals with refspecs
83# supplied on the command line. $ls_remote_result has the list
84# of refs available at remote.
Junio C Hamanofbc90122006-12-31 17:44:37 -080085#
86# The first token returned is either "explicit" or "glob"; this
87# is to help prevent randomly "globbed" ref from being chosen as
88# a merge candidate
Junio C Hamano56778822006-11-22 23:15:00 -080089expand_refs_wildcard () {
Julian Phillips95339912007-02-13 01:21:40 +000090 echo "$ls_remote_result" |
91 git fetch--tool expand-refs-wildcard "-" "$@"
Junio C Hamano56778822006-11-22 23:15:00 -080092}
93
Junio C Hamano05dd8e22005-09-25 22:54:23 -070094# Subroutine to canonicalize remote:local notation.
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -070095canon_refs_list_for_fetch () {
Santi Béjar53728062006-09-23 22:53:04 +020096 # If called from get_remote_default_refs_for_fetch
97 # leave the branches in branch.${curr_branch}.merge alone,
98 # or the first one otherwise; add prefix . to the rest
Junio C Hamano05dd8e22005-09-25 22:54:23 -070099 # to prevent the secondary branches to be merged by default.
Santi Béjar53728062006-09-23 22:53:04 +0200100 merge_branches=
Josef Weidendorfer62b339a2006-12-09 02:28:26 +0100101 curr_branch=
Santi Béjar53728062006-09-23 22:53:04 +0200102 if test "$1" = "-d"
103 then
104 shift ; remote="$1" ; shift
Junio C Hamanoae9c6ff2007-01-25 21:50:49 -0800105 set $(expand_refs_wildcard "$remote" "$@")
Junio C Hamanofbc90122006-12-31 17:44:37 -0800106 is_explicit="$1"
Junio C Hamanod41cb272006-12-21 22:39:09 -0800107 shift
Santi Béjar53728062006-09-23 22:53:04 +0200108 if test "$remote" = "$(get_default_remote)"
109 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700110 curr_branch=$(git symbolic-ref -q HEAD | \
Santi Béjar53728062006-09-23 22:53:04 +0200111 sed -e 's|^refs/heads/||')
Junio C Hamano5be60072007-07-02 22:52:14 -0700112 merge_branches=$(git config \
Junio C Hamano9e115542006-12-21 22:10:56 -0800113 --get-all "branch.${curr_branch}.merge")
Santi Béjar53728062006-09-23 22:53:04 +0200114 fi
Junio C Hamanofbc90122006-12-31 17:44:37 -0800115 if test -z "$merge_branches" && test $is_explicit != explicit
116 then
117 merge_branches=..this.will.never.match.any.ref..
118 fi
Santi Béjar53728062006-09-23 22:53:04 +0200119 fi
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700120 for ref
121 do
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700122 force=
123 case "$ref" in
124 +*)
Junio C Hamanodfdcb552006-04-13 19:05:38 -0700125 ref=$(expr "z$ref" : 'z+\(.*\)')
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700126 force=+
127 ;;
128 esac
Mark Woodingf327dbc2006-04-13 22:01:24 +0000129 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
130 remote=$(expr "z$ref" : 'z\([^:]*\):')
131 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
Santi Béjar53728062006-09-23 22:53:04 +0200132 dot_prefix=.
133 if test -z "$merge_branches"
134 then
135 merge_branches=$remote
136 dot_prefix=
137 else
138 for merge_branch in $merge_branches
139 do
Junio C Hamano756373d2007-02-03 16:23:38 -0800140 [ "$remote" = "$merge_branch" ] &&
141 dot_prefix= && break
Santi Béjar53728062006-09-23 22:53:04 +0200142 done
143 fi
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700144 case "$remote" in
Santi Béjar153e98d2007-01-30 10:36:24 +0100145 '' | HEAD ) remote=HEAD ;;
Alex Riesen96f12b52007-05-11 22:35:22 +0200146 refs/*) ;;
Eric Wong687b8be2006-03-10 04:19:07 -0800147 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700148 *) remote="refs/heads/$remote" ;;
149 esac
150 case "$local" in
151 '') local= ;;
Alex Riesen96f12b52007-05-11 22:35:22 +0200152 refs/*) ;;
Eric Wong687b8be2006-03-10 04:19:07 -0800153 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700154 *) local="refs/heads/$local" ;;
155 esac
Junio C Hamanod8a1dee2005-10-13 18:57:39 -0700156
Mark Woodingf327dbc2006-04-13 22:01:24 +0000157 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
Junio C Hamanod8a1dee2005-10-13 18:57:39 -0700158 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700159 git check-ref-format "$local_ref_name" ||
Junio C Hamanod8a1dee2005-10-13 18:57:39 -0700160 die "* refusing to create funny ref '$local_ref_name' locally"
161 fi
Junio C Hamano05dd8e22005-09-25 22:54:23 -0700162 echo "${dot_prefix}${force}${remote}:${local}"
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700163 done
164}
165
166# Returns list of src: (no store), or src:dst (store)
167get_remote_default_refs_for_fetch () {
168 data_source=$(get_data_source "$1")
169 case "$data_source" in
Junio C Hamanoea560e62006-12-22 15:37:48 -0800170 '')
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700171 echo "HEAD:" ;;
Paolo Bonzini9debc322007-03-15 09:23:20 +0100172 self)
173 canon_refs_list_for_fetch -d "$1" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700174 $(git for-each-ref --format='%(refname):')
Paolo Bonzini9debc322007-03-15 09:23:20 +0100175 ;;
Johannes Schindelin73136b22006-05-03 15:20:21 +0200176 config)
Santi Béjar53728062006-09-23 22:53:04 +0200177 canon_refs_list_for_fetch -d "$1" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700178 $(git config --get-all "remote.$1.fetch") ;;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700179 branches)
180 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
181 case "$remote_branch" in '') remote_branch=master ;; esac
182 echo "refs/heads/${remote_branch}:refs/heads/$1"
183 ;;
184 remotes)
Santi Béjar53728062006-09-23 22:53:04 +0200185 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700186 s///p
187 }' "$GIT_DIR/remotes/$1")
188 ;;
189 *)
Paolo Bonzini9debc322007-03-15 09:23:20 +0100190 die "internal error: get-remote-default-ref-for-fetch $1" ;;
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700191 esac
192}
193
194get_remote_refs_for_push () {
195 case "$#" in
196 0) die "internal error: get-remote-refs-for-push." ;;
197 1) get_remote_default_refs_for_push "$@" ;;
198 *) shift; echo "$@" ;;
199 esac
200}
201
202get_remote_refs_for_fetch () {
203 case "$#" in
204 0)
205 die "internal error: get-remote-refs-for-fetch." ;;
206 1)
207 get_remote_default_refs_for_fetch "$@" ;;
208 *)
209 shift
210 tag_just_seen=
211 for ref
212 do
213 if test "$tag_just_seen"
214 then
215 echo "refs/tags/${ref}:refs/tags/${ref}"
216 tag_just_seen=
217 continue
218 else
219 case "$ref" in
220 tag)
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700221 tag_just_seen=yes
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700222 continue
223 ;;
224 esac
225 fi
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700226 canon_refs_list_for_fetch "$ref"
Junio C Hamanoac4b0cf2005-08-20 02:52:24 -0700227 done
228 ;;
229 esac
230}
Junio C Hamano4447bad2005-09-17 11:56:41 -0700231
232resolve_alternates () {
233 # original URL (xxx.git)
Mark Woodingf327dbc2006-04-13 22:01:24 +0000234 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
Junio C Hamano4447bad2005-09-17 11:56:41 -0700235 while read path
236 do
237 case "$path" in
238 \#* | '')
239 continue ;;
240 /*)
241 echo "$top_$path/" ;;
242 ../*)
243 # relative -- ugly but seems to work.
244 echo "$1/objects/$path/" ;;
245 *)
246 # exit code may not be caught by the reader.
247 echo "bad alternate: $path"
248 exit 1 ;;
249 esac
250 done
251}
Uwe Kleine-König5dee29a2007-01-25 05:45:39 +0100252
253get_uploadpack () {
254 data_source=$(get_data_source "$1")
255 case "$data_source" in
256 config)
Junio C Hamano5be60072007-07-02 22:52:14 -0700257 uplp=$(git config --get "remote.$1.uploadpack")
Uwe Kleine-König5dee29a2007-01-25 05:45:39 +0100258 echo ${uplp:-git-upload-pack}
259 ;;
260 *)
261 echo "git-upload-pack"
262 ;;
263 esac
264}