Merge branch 'jc/test-clone' into jc/clone
[git/gitweb.git] / git-parse-remote.sh
blob11c4aba24448492f1ba1836cded54170a5dde08c
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 --git-dir 2>/dev/null) || :;
7 get_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 ''
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
39 get_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"
50 config)
51 git-repo-config --get "remote.$1.url"
53 remotes)
54 sed -ne '/^URL: */{
55 s///p
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"
67 die "internal error: get-remote-url $1" ;;
68 esac
71 get_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}
77 get_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" ;;
89 die "internal error: get-remote-default-ref-for-push $1" ;;
90 esac
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.
99 expand_refs_wildcard () {
100 for ref
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
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 sed -e '/\^{}$/d' |
116 IFS=' '
117 while read sha1 name
119 # ignore the ones that do not start with $from
120 mapped=${name#"$from"}
121 test "z$name" = "z$mapped" && continue
122 echo "${local_force}${name}:${to}${mapped}"
123 done
125 done
128 # Subroutine to canonicalize remote:local notation.
129 canon_refs_list_for_fetch () {
130 # If called from get_remote_default_refs_for_fetch
131 # leave the branches in branch.${curr_branch}.merge alone,
132 # or the first one otherwise; add prefix . to the rest
133 # to prevent the secondary branches to be merged by default.
134 merge_branches=
135 found_mergeref=
136 curr_branch=
137 if test "$1" = "-d"
138 then
139 shift ; remote="$1" ; shift
140 if test "$remote" = "$(get_default_remote)"
141 then
142 curr_branch=$(git-symbolic-ref HEAD | \
143 sed -e 's|^refs/heads/||')
144 merge_branches=$(git-repo-config \
145 --get-all "branch.${curr_branch}.merge") ||
146 merge_branches=.this.would.never.match.any.ref.
148 set x $(expand_refs_wildcard "$@")
149 shift
151 for ref
153 force=
154 case "$ref" in
156 ref=$(expr "z$ref" : 'z+\(.*\)')
157 force=+
159 esac
160 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
161 remote=$(expr "z$ref" : 'z\([^:]*\):')
162 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
163 dot_prefix=.
164 if test -z "$merge_branches"
165 then
166 merge_branches=$remote
167 dot_prefix=
168 else
169 for merge_branch in $merge_branches
171 [ "$remote" = "$merge_branch" ] &&
172 dot_prefix= && break
173 done
175 if test -z $dot_prefix
176 then
177 found_mergeref=true
179 case "$remote" in
180 '') remote=HEAD ;;
181 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
182 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
183 *) remote="refs/heads/$remote" ;;
184 esac
185 case "$local" in
186 '') local= ;;
187 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
188 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
189 *) local="refs/heads/$local" ;;
190 esac
192 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
193 then
194 git-check-ref-format "$local_ref_name" ||
195 die "* refusing to create funny ref '$local_ref_name' locally"
197 echo "${dot_prefix}${force}${remote}:${local}"
198 done
199 if test -z "$found_mergeref" -a "$curr_branch"
200 then
201 echo >&2 "Warning: No merge candidate found because value of config option
202 \"branch.${curr_branch}.merge\" does not match any remote branch fetched."
206 # Returns list of src: (no store), or src:dst (store)
207 get_remote_default_refs_for_fetch () {
208 data_source=$(get_data_source "$1")
209 case "$data_source" in
210 '' | config-partial | branches-partial)
211 echo "HEAD:" ;;
212 config)
213 canon_refs_list_for_fetch -d "$1" \
214 $(git-repo-config --get-all "remote.$1.fetch") ;;
215 branches)
216 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
217 case "$remote_branch" in '') remote_branch=master ;; esac
218 echo "refs/heads/${remote_branch}:refs/heads/$1"
220 remotes)
221 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
222 s///p
223 }' "$GIT_DIR/remotes/$1")
226 die "internal error: get-remote-default-ref-for-push $1" ;;
227 esac
230 get_remote_refs_for_push () {
231 case "$#" in
232 0) die "internal error: get-remote-refs-for-push." ;;
233 1) get_remote_default_refs_for_push "$@" ;;
234 *) shift; echo "$@" ;;
235 esac
238 get_remote_refs_for_fetch () {
239 case "$#" in
241 die "internal error: get-remote-refs-for-fetch." ;;
243 get_remote_default_refs_for_fetch "$@" ;;
245 shift
246 tag_just_seen=
247 for ref
249 if test "$tag_just_seen"
250 then
251 echo "refs/tags/${ref}:refs/tags/${ref}"
252 tag_just_seen=
253 continue
254 else
255 case "$ref" in
256 tag)
257 tag_just_seen=yes
258 continue
260 esac
262 canon_refs_list_for_fetch "$ref"
263 done
265 esac
268 resolve_alternates () {
269 # original URL (xxx.git)
270 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
271 while read path
273 case "$path" in
274 \#* | '')
275 continue ;;
277 echo "$top_$path/" ;;
278 ../*)
279 # relative -- ugly but seems to work.
280 echo "$1/objects/$path/" ;;
282 # exit code may not be caught by the reader.
283 echo "bad alternate: $path"
284 exit 1 ;;
285 esac
286 done