1#!/bin/sh
2
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.
5GIT_DIR=$(git-rev-parse --git-dir 2>/dev/null) || :;
6
7get_data_source () {
8 case "$1" in
9 */*)
10 # Not so fast. This could be the partial URL shorthand...
11 token=$(expr "z$1" : 'z\([^/]*\)/')
12 remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
13 if test "$(git-repo-config --get "remote.$token.url")"
14 then
15 echo config-partial
16 elif test -f "$GIT_DIR/branches/$token"
17 then
18 echo branches-partial
19 else
20 echo ''
21 fi
22 ;;
23 *)
24 if test "$(git-repo-config --get "remote.$1.url")"
25 then
26 echo config
27 elif test -f "$GIT_DIR/remotes/$1"
28 then
29 echo remotes
30 elif test -f "$GIT_DIR/branches/$1"
31 then
32 echo branches
33 else
34 echo ''
35 fi ;;
36 esac
37}
38
39get_remote_url () {
40 data_source=$(get_data_source "$1")
41 case "$data_source" in
42 '')
43 echo "$1" ;;
44 config-partial)
45 token=$(expr "z$1" : 'z\([^/]*\)/')
46 remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
47 url=$(git-repo-config --get "remote.$token.url")
48 echo "$url/$remainder"
49 ;;
50 config)
51 git-repo-config --get "remote.$1.url"
52 ;;
53 remotes)
54 sed -ne '/^URL: */{
55 s///p
56 q
57 }' "$GIT_DIR/remotes/$1" ;;
58 branches)
59 sed -e 's/#.*//' "$GIT_DIR/branches/$1" ;;
60 branches-partial)
61 token=$(expr "z$1" : 'z\([^/]*\)/')
62 remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
63 url=$(sed -e 's/#.*//' "$GIT_DIR/branches/$token")
64 echo "$url/$remainder"
65 ;;
66 *)
67 die "internal error: get-remote-url $1" ;;
68 esac
69}
70
71get_default_remote () {
72 curr_branch=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
73 origin=$(git-repo-config --get "branch.$curr_branch.remote")
74 echo ${origin:-origin}
75}
76
77get_remote_default_refs_for_push () {
78 data_source=$(get_data_source "$1")
79 case "$data_source" in
80 '' | config-partial | branches | branches-partial)
81 ;; # no default push mapping, just send matching refs.
82 config)
83 git-repo-config --get-all "remote.$1.push" ;;
84 remotes)
85 sed -ne '/^Push: */{
86 s///p
87 }' "$GIT_DIR/remotes/$1" ;;
88 *)
89 die "internal error: get-remote-default-ref-for-push $1" ;;
90 esac
91}
92
93# Called from canon_refs_list_for_fetch -d "$remote", which
94# is called from get_remote_default_refs_for_fetch to grok
95# refspecs that are retrieved from the configuration, but not
96# from get_remote_refs_for_fetch when it deals with refspecs
97# supplied on the command line. $ls_remote_result has the list
98# of refs available at remote.
99expand_refs_wildcard () {
100 for ref
101 do
102 lref=${ref#'+'}
103 # a non glob pattern is given back as-is.
104 expr "z$lref" : 'zrefs/.*/\*:refs/.*/\*$' >/dev/null || {
105 echo "$ref"
106 continue
107 }
108
109 from=`expr "z$lref" : 'z\(refs/.*/\)\*:refs/.*/\*$'`
110 to=`expr "z$lref" : 'zrefs/.*/\*:\(refs/.*/\)\*$'`
111 local_force=
112 test "z$lref" = "z$ref" || local_force='+'
113 echo "$ls_remote_result" |
114 (
115 IFS=' '
116 while read sha1 name
117 do
118 mapped=${name#"$from"}
119 if test "z$name" != "z${name%'^{}'}" ||
120 test "z$name" = "z$mapped"
121 then
122 continue
123 fi
124 echo "${local_force}${name}:${to}${mapped}"
125 done
126 )
127 done
128}
129
130# Subroutine to canonicalize remote:local notation.
131canon_refs_list_for_fetch () {
132 # If called from get_remote_default_refs_for_fetch
133 # leave the branches in branch.${curr_branch}.merge alone,
134 # or the first one otherwise; add prefix . to the rest
135 # to prevent the secondary branches to be merged by default.
136 merge_branches=
137 found_mergeref=
138 curr_branch=
139 if test "$1" = "-d"
140 then
141 shift ; remote="$1" ; shift
142 if test "$remote" = "$(get_default_remote)"
143 then
144 curr_branch=$(git-symbolic-ref HEAD | \
145 sed -e 's|^refs/heads/||')
146 merge_branches=$(git-repo-config \
147 --get-all "branch.${curr_branch}.merge") ||
148 merge_branches=.this.would.never.match.any.ref.
149 fi
150 set x $(expand_refs_wildcard "$@")
151 shift
152 fi
153 for ref
154 do
155 force=
156 case "$ref" in
157 +*)
158 ref=$(expr "z$ref" : 'z+\(.*\)')
159 force=+
160 ;;
161 esac
162 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
163 remote=$(expr "z$ref" : 'z\([^:]*\):')
164 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
165 dot_prefix=.
166 if test -z "$merge_branches"
167 then
168 merge_branches=$remote
169 dot_prefix=
170 else
171 for merge_branch in $merge_branches
172 do
173 [ "$remote" = "$merge_branch" ] &&
174 dot_prefix= && break
175 done
176 fi
177 if test -z $dot_prefix
178 then
179 found_mergeref=true
180 fi
181 case "$remote" in
182 '') remote=HEAD ;;
183 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
184 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
185 *) remote="refs/heads/$remote" ;;
186 esac
187 case "$local" in
188 '') local= ;;
189 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
190 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
191 *) local="refs/heads/$local" ;;
192 esac
193
194 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
195 then
196 git-check-ref-format "$local_ref_name" ||
197 die "* refusing to create funny ref '$local_ref_name' locally"
198 fi
199 echo "${dot_prefix}${force}${remote}:${local}"
200 done
201 if test -z "$found_mergeref" -a "$curr_branch"
202 then
203 echo >&2 "Warning: No merge candidate found because value of config option
204 \"branch.${curr_branch}.merge\" does not match any remote branch fetched."
205 fi
206}
207
208# Returns list of src: (no store), or src:dst (store)
209get_remote_default_refs_for_fetch () {
210 data_source=$(get_data_source "$1")
211 case "$data_source" in
212 '' | config-partial | branches-partial)
213 echo "HEAD:" ;;
214 config)
215 canon_refs_list_for_fetch -d "$1" \
216 $(git-repo-config --get-all "remote.$1.fetch") ;;
217 branches)
218 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
219 case "$remote_branch" in '') remote_branch=master ;; esac
220 echo "refs/heads/${remote_branch}:refs/heads/$1"
221 ;;
222 remotes)
223 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
224 s///p
225 }' "$GIT_DIR/remotes/$1")
226 ;;
227 *)
228 die "internal error: get-remote-default-ref-for-push $1" ;;
229 esac
230}
231
232get_remote_refs_for_push () {
233 case "$#" in
234 0) die "internal error: get-remote-refs-for-push." ;;
235 1) get_remote_default_refs_for_push "$@" ;;
236 *) shift; echo "$@" ;;
237 esac
238}
239
240get_remote_refs_for_fetch () {
241 case "$#" in
242 0)
243 die "internal error: get-remote-refs-for-fetch." ;;
244 1)
245 get_remote_default_refs_for_fetch "$@" ;;
246 *)
247 shift
248 tag_just_seen=
249 for ref
250 do
251 if test "$tag_just_seen"
252 then
253 echo "refs/tags/${ref}:refs/tags/${ref}"
254 tag_just_seen=
255 continue
256 else
257 case "$ref" in
258 tag)
259 tag_just_seen=yes
260 continue
261 ;;
262 esac
263 fi
264 canon_refs_list_for_fetch "$ref"
265 done
266 ;;
267 esac
268}
269
270resolve_alternates () {
271 # original URL (xxx.git)
272 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
273 while read path
274 do
275 case "$path" in
276 \#* | '')
277 continue ;;
278 /*)
279 echo "$top_$path/" ;;
280 ../*)
281 # relative -- ugly but seems to work.
282 echo "$1/objects/$path/" ;;
283 *)
284 # exit code may not be caught by the reader.
285 echo "bad alternate: $path"
286 exit 1 ;;
287 esac
288 done
289}