]> git.ipfire.org Git - thirdparty/git.git/blame - git-parse-remote.sh
vim syntax: follow recent changes to commit template
[thirdparty/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
JH
113 echo "$ls_remote_result" |
114 (
115 IFS=' '
116 while read sha1 name
117 do
118 mapped=${name#"$from"}
4cd75359 119 if test "z$name" != "z${name%'^{}'}" ||
5677882b
JH
120 test "z$name" = "z$mapped"
121 then
122 continue
123 fi
d945d4be 124 echo "${local_force}${name}:${to}${mapped}"
5677882b
JH
125 done
126 )
127 done
128}
129
05dd8e2e 130# Subroutine to canonicalize remote:local notation.
ac4b0cff 131canon_refs_list_for_fetch () {
5372806a
SB
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
05dd8e2e 135 # to prevent the secondary branches to be merged by default.
5372806a 136 merge_branches=
62b339a5
JW
137 found_mergeref=
138 curr_branch=
5372806a
SB
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 fi
5677882b
JH
149 set x $(expand_refs_wildcard "$@")
150 shift
5372806a 151 fi
ac4b0cff
JH
152 for ref
153 do
efe9bf0f
JH
154 force=
155 case "$ref" in
156 +*)
dfdcb558 157 ref=$(expr "z$ref" : 'z+\(.*\)')
efe9bf0f
JH
158 force=+
159 ;;
160 esac
f327dbce
MW
161 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
162 remote=$(expr "z$ref" : 'z\([^:]*\):')
163 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
5372806a
SB
164 dot_prefix=.
165 if test -z "$merge_branches"
166 then
167 merge_branches=$remote
168 dot_prefix=
169 else
170 for merge_branch in $merge_branches
171 do
172 [ "$remote" = "$merge_branch" ] &&
173 dot_prefix= && break
174 done
175 fi
62b339a5
JW
176 if test -z $dot_prefix
177 then
178 found_mergeref=true
179 fi
ac4b0cff
JH
180 case "$remote" in
181 '') remote=HEAD ;;
687b8be8
EW
182 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
183 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
ac4b0cff
JH
184 *) remote="refs/heads/$remote" ;;
185 esac
186 case "$local" in
187 '') local= ;;
687b8be8
EW
188 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
189 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
ac4b0cff
JH
190 *) local="refs/heads/$local" ;;
191 esac
d8a1deec 192
f327dbce 193 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
d8a1deec
JH
194 then
195 git-check-ref-format "$local_ref_name" ||
196 die "* refusing to create funny ref '$local_ref_name' locally"
197 fi
05dd8e2e 198 echo "${dot_prefix}${force}${remote}:${local}"
ac4b0cff 199 done
62b339a5
JW
200 if test -z "$found_mergeref" -a "$curr_branch"
201 then
202 echo >&2 "Warning: No merge candidate found because value of config option
203 \"branch.${curr_branch}.merge\" does not match any remote branch fetched."
204 fi
ac4b0cff
JH
205}
206
207# Returns list of src: (no store), or src:dst (store)
208get_remote_default_refs_for_fetch () {
209 data_source=$(get_data_source "$1")
210 case "$data_source" in
73136b2e 211 '' | config-partial | branches-partial)
ac4b0cff 212 echo "HEAD:" ;;
73136b2e 213 config)
5372806a 214 canon_refs_list_for_fetch -d "$1" \
73136b2e 215 $(git-repo-config --get-all "remote.$1.fetch") ;;
ac4b0cff
JH
216 branches)
217 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
218 case "$remote_branch" in '') remote_branch=master ;; esac
219 echo "refs/heads/${remote_branch}:refs/heads/$1"
220 ;;
221 remotes)
5372806a 222 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
ac4b0cff
JH
223 s///p
224 }' "$GIT_DIR/remotes/$1")
225 ;;
226 *)
227 die "internal error: get-remote-default-ref-for-push $1" ;;
228 esac
229}
230
231get_remote_refs_for_push () {
232 case "$#" in
233 0) die "internal error: get-remote-refs-for-push." ;;
234 1) get_remote_default_refs_for_push "$@" ;;
235 *) shift; echo "$@" ;;
236 esac
237}
238
239get_remote_refs_for_fetch () {
240 case "$#" in
241 0)
242 die "internal error: get-remote-refs-for-fetch." ;;
243 1)
244 get_remote_default_refs_for_fetch "$@" ;;
245 *)
246 shift
247 tag_just_seen=
248 for ref
249 do
250 if test "$tag_just_seen"
251 then
252 echo "refs/tags/${ref}:refs/tags/${ref}"
253 tag_just_seen=
254 continue
255 else
256 case "$ref" in
257 tag)
efe9bf0f 258 tag_just_seen=yes
ac4b0cff
JH
259 continue
260 ;;
261 esac
262 fi
efe9bf0f 263 canon_refs_list_for_fetch "$ref"
ac4b0cff
JH
264 done
265 ;;
266 esac
267}
4447badc
JH
268
269resolve_alternates () {
270 # original URL (xxx.git)
f327dbce 271 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
4447badc
JH
272 while read path
273 do
274 case "$path" in
275 \#* | '')
276 continue ;;
277 /*)
278 echo "$top_$path/" ;;
279 ../*)
280 # relative -- ugly but seems to work.
281 echo "$1/objects/$path/" ;;
282 *)
283 # exit code may not be caught by the reader.
284 echo "bad alternate: $path"
285 exit 1 ;;
286 esac
287 done
288}