default pull: forget about "newbie protection" for now.
[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.
5GIT_DIR=$(git-rev-parse --git-dir 2>/dev/null) || :;
ac4b0cff
JH
6
7get_data_source () {
8 case "$1" in
9 */*)
efe9bf0f 10 # Not so fast. This could be the partial URL shorthand...
f327dbce
MW
11 token=$(expr "z$1" : 'z\([^/]*\)/')
12 remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
73136b2e
JS
13 if test "$(git-repo-config --get "remote.$token.url")"
14 then
15 echo config-partial
16 elif test -f "$GIT_DIR/branches/$token"
ac4b0cff
JH
17 then
18 echo branches-partial
19 else
20 echo ''
21 fi
22 ;;
23 *)
73136b2e
JS
24 if test "$(git-repo-config --get "remote.$1.url")"
25 then
26 echo config
27 elif test -f "$GIT_DIR/remotes/$1"
ac4b0cff
JH
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" ;;
73136b2e
JS
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 ;;
ac4b0cff
JH
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)
f327dbce
MW
61 token=$(expr "z$1" : 'z\([^/]*\)/')
62 remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
ac4b0cff
JH
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
648ad18f
SB
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
ac4b0cff
JH
77get_remote_default_refs_for_push () {
78 data_source=$(get_data_source "$1")
79 case "$data_source" in
73136b2e 80 '' | config-partial | branches | branches-partial)
ac4b0cff 81 ;; # no default push mapping, just send matching refs.
73136b2e
JS
82 config)
83 git-repo-config --get-all "remote.$1.push" ;;
ac4b0cff
JH
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
5677882b
JH
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
d945d4be 102 lref=${ref#'+'}
5677882b 103 # a non glob pattern is given back as-is.
d945d4be 104 expr "z$lref" : 'zrefs/.*/\*:refs/.*/\*$' >/dev/null || {
5677882b
JH
105 echo "$ref"
106 continue
107 }
d945d4be
JH
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='+'
5677882b 113 echo "$ls_remote_result" |
0c7a97fa 114 sed -e '/\^{}$/d' |
5677882b
JH
115 (
116 IFS=' '
117 while read sha1 name
118 do
0c7a97fa 119 # ignore the ones that do not start with $from
5677882b 120 mapped=${name#"$from"}
0c7a97fa 121 test "z$name" = "z$mapped" && continue
d945d4be 122 echo "${local_force}${name}:${to}${mapped}"
5677882b
JH
123 done
124 )
125 done
126}
127
05dd8e2e 128# Subroutine to canonicalize remote:local notation.
ac4b0cff 129canon_refs_list_for_fetch () {
5372806a
SB
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
05dd8e2e 133 # to prevent the secondary branches to be merged by default.
5372806a 134 merge_branches=
62b339a5 135 curr_branch=
5372806a
SB
136 if test "$1" = "-d"
137 then
138 shift ; remote="$1" ; shift
d41cb273
JH
139 set x $(expand_refs_wildcard "$@")
140 shift
5372806a
SB
141 if test "$remote" = "$(get_default_remote)"
142 then
143 curr_branch=$(git-symbolic-ref HEAD | \
144 sed -e 's|^refs/heads/||')
145 merge_branches=$(git-repo-config \
9e115549 146 --get-all "branch.${curr_branch}.merge")
5372806a
SB
147 fi
148 fi
ac4b0cff
JH
149 for ref
150 do
efe9bf0f
JH
151 force=
152 case "$ref" in
153 +*)
dfdcb558 154 ref=$(expr "z$ref" : 'z+\(.*\)')
efe9bf0f
JH
155 force=+
156 ;;
157 esac
f327dbce
MW
158 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
159 remote=$(expr "z$ref" : 'z\([^:]*\):')
160 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
5372806a
SB
161 dot_prefix=.
162 if test -z "$merge_branches"
163 then
164 merge_branches=$remote
165 dot_prefix=
166 else
167 for merge_branch in $merge_branches
168 do
169 [ "$remote" = "$merge_branch" ] &&
170 dot_prefix= && break
171 done
172 fi
ac4b0cff
JH
173 case "$remote" in
174 '') remote=HEAD ;;
687b8be8
EW
175 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
176 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
ac4b0cff
JH
177 *) remote="refs/heads/$remote" ;;
178 esac
179 case "$local" in
180 '') local= ;;
687b8be8
EW
181 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
182 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
ac4b0cff
JH
183 *) local="refs/heads/$local" ;;
184 esac
d8a1deec 185
f327dbce 186 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
d8a1deec
JH
187 then
188 git-check-ref-format "$local_ref_name" ||
189 die "* refusing to create funny ref '$local_ref_name' locally"
190 fi
05dd8e2e 191 echo "${dot_prefix}${force}${remote}:${local}"
ac4b0cff
JH
192 done
193}
194
195# Returns list of src: (no store), or src:dst (store)
196get_remote_default_refs_for_fetch () {
197 data_source=$(get_data_source "$1")
198 case "$data_source" in
73136b2e 199 '' | config-partial | branches-partial)
ac4b0cff 200 echo "HEAD:" ;;
73136b2e 201 config)
5372806a 202 canon_refs_list_for_fetch -d "$1" \
73136b2e 203 $(git-repo-config --get-all "remote.$1.fetch") ;;
ac4b0cff
JH
204 branches)
205 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
206 case "$remote_branch" in '') remote_branch=master ;; esac
207 echo "refs/heads/${remote_branch}:refs/heads/$1"
208 ;;
209 remotes)
5372806a 210 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
ac4b0cff
JH
211 s///p
212 }' "$GIT_DIR/remotes/$1")
213 ;;
214 *)
215 die "internal error: get-remote-default-ref-for-push $1" ;;
216 esac
217}
218
219get_remote_refs_for_push () {
220 case "$#" in
221 0) die "internal error: get-remote-refs-for-push." ;;
222 1) get_remote_default_refs_for_push "$@" ;;
223 *) shift; echo "$@" ;;
224 esac
225}
226
227get_remote_refs_for_fetch () {
228 case "$#" in
229 0)
230 die "internal error: get-remote-refs-for-fetch." ;;
231 1)
232 get_remote_default_refs_for_fetch "$@" ;;
233 *)
234 shift
235 tag_just_seen=
236 for ref
237 do
238 if test "$tag_just_seen"
239 then
240 echo "refs/tags/${ref}:refs/tags/${ref}"
241 tag_just_seen=
242 continue
243 else
244 case "$ref" in
245 tag)
efe9bf0f 246 tag_just_seen=yes
ac4b0cff
JH
247 continue
248 ;;
249 esac
250 fi
efe9bf0f 251 canon_refs_list_for_fetch "$ref"
ac4b0cff
JH
252 done
253 ;;
254 esac
255}
4447badc
JH
256
257resolve_alternates () {
258 # original URL (xxx.git)
f327dbce 259 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
4447badc
JH
260 while read path
261 do
262 case "$path" in
263 \#* | '')
264 continue ;;
265 /*)
266 echo "$top_$path/" ;;
267 ../*)
268 # relative -- ugly but seems to work.
269 echo "$1/objects/$path/" ;;
270 *)
271 # exit code may not be caught by the reader.
272 echo "bad alternate: $path"
273 exit 1 ;;
274 esac
275 done
276}