parse-options: add parse_options_check to validate option specs.
[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.
7bd93c1c 5GIT_DIR=$(git rev-parse -q --git-dir) || :;
ac4b0cff
JH
6
7get_data_source () {
8 case "$1" in
9 */*)
ea560e6d 10 echo ''
ac4b0cff 11 ;;
9debc324
PB
12 .)
13 echo self
14 ;;
ac4b0cff 15 *)
5be60078 16 if test "$(git config --get "remote.$1.url")"
73136b2e
JS
17 then
18 echo config
19 elif test -f "$GIT_DIR/remotes/$1"
ac4b0cff
JH
20 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 '')
ea560e6d 35 echo "$1"
73136b2e 36 ;;
9debc324
PB
37 self)
38 echo "$1"
39 ;;
73136b2e 40 config)
5be60078 41 git config --get "remote.$1.url"
73136b2e 42 ;;
ac4b0cff
JH
43 remotes)
44 sed -ne '/^URL: */{
45 s///p
46 q
ea560e6d
JH
47 }' "$GIT_DIR/remotes/$1"
48 ;;
ac4b0cff 49 branches)
ea560e6d 50 sed -e 's/#.*//' "$GIT_DIR/branches/$1"
ac4b0cff
JH
51 ;;
52 *)
53 die "internal error: get-remote-url $1" ;;
54 esac
55}
56
648ad18f 57get_default_remote () {
5be60078
JH
58 curr_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')
59 origin=$(git config --get "branch.$curr_branch.remote")
648ad18f
SB
60 echo ${origin:-origin}
61}
62
ac4b0cff
JH
63get_remote_default_refs_for_push () {
64 data_source=$(get_data_source "$1")
65 case "$data_source" in
9debc324 66 '' | branches | self)
ac4b0cff 67 ;; # no default push mapping, just send matching refs.
73136b2e 68 config)
5be60078 69 git config --get-all "remote.$1.push" ;;
ac4b0cff
JH
70 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
5677882b
JH
79# 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.
fbc90123
JH
85#
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
5677882b 89expand_refs_wildcard () {
95339912
JP
90 echo "$ls_remote_result" |
91 git fetch--tool expand-refs-wildcard "-" "$@"
5677882b
JH
92}
93
05dd8e2e 94# Subroutine to canonicalize remote:local notation.
ac4b0cff 95canon_refs_list_for_fetch () {
5372806a
SB
96 # 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
05dd8e2e 99 # to prevent the secondary branches to be merged by default.
5372806a 100 merge_branches=
62b339a5 101 curr_branch=
5372806a
SB
102 if test "$1" = "-d"
103 then
104 shift ; remote="$1" ; shift
ae9c6ffe 105 set $(expand_refs_wildcard "$remote" "$@")
fbc90123 106 is_explicit="$1"
d41cb273 107 shift
5372806a
SB
108 if test "$remote" = "$(get_default_remote)"
109 then
5be60078 110 curr_branch=$(git symbolic-ref -q HEAD | \
5372806a 111 sed -e 's|^refs/heads/||')
5be60078 112 merge_branches=$(git config \
9e115549 113 --get-all "branch.${curr_branch}.merge")
5372806a 114 fi
fbc90123
JH
115 if test -z "$merge_branches" && test $is_explicit != explicit
116 then
117 merge_branches=..this.will.never.match.any.ref..
118 fi
5372806a 119 fi
ac4b0cff
JH
120 for ref
121 do
efe9bf0f
JH
122 force=
123 case "$ref" in
124 +*)
dfdcb558 125 ref=$(expr "z$ref" : 'z+\(.*\)')
efe9bf0f
JH
126 force=+
127 ;;
128 esac
f327dbce
MW
129 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
130 remote=$(expr "z$ref" : 'z\([^:]*\):')
131 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
5372806a
SB
132 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
756373da
JH
140 [ "$remote" = "$merge_branch" ] &&
141 dot_prefix= && break
5372806a
SB
142 done
143 fi
ac4b0cff 144 case "$remote" in
153e98d2 145 '' | HEAD ) remote=HEAD ;;
96f12b54 146 refs/*) ;;
687b8be8 147 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
ac4b0cff
JH
148 *) remote="refs/heads/$remote" ;;
149 esac
150 case "$local" in
151 '') local= ;;
96f12b54 152 refs/*) ;;
687b8be8 153 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
ac4b0cff
JH
154 *) local="refs/heads/$local" ;;
155 esac
d8a1deec 156
f327dbce 157 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
d8a1deec 158 then
5be60078 159 git check-ref-format "$local_ref_name" ||
d8a1deec
JH
160 die "* refusing to create funny ref '$local_ref_name' locally"
161 fi
05dd8e2e 162 echo "${dot_prefix}${force}${remote}:${local}"
ac4b0cff
JH
163 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
ea560e6d 170 '')
ac4b0cff 171 echo "HEAD:" ;;
9debc324
PB
172 self)
173 canon_refs_list_for_fetch -d "$1" \
5be60078 174 $(git for-each-ref --format='%(refname):')
9debc324 175 ;;
73136b2e 176 config)
5372806a 177 canon_refs_list_for_fetch -d "$1" \
5be60078 178 $(git config --get-all "remote.$1.fetch") ;;
ac4b0cff
JH
179 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)
5372806a 185 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
ac4b0cff
JH
186 s///p
187 }' "$GIT_DIR/remotes/$1")
188 ;;
189 *)
9debc324 190 die "internal error: get-remote-default-ref-for-fetch $1" ;;
ac4b0cff
JH
191 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)
efe9bf0f 221 tag_just_seen=yes
ac4b0cff
JH
222 continue
223 ;;
224 esac
225 fi
efe9bf0f 226 canon_refs_list_for_fetch "$ref"
ac4b0cff
JH
227 done
228 ;;
229 esac
230}
4447badc
JH
231
232resolve_alternates () {
233 # original URL (xxx.git)
f327dbce 234 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
4447badc
JH
235 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}
5dee29ac
UKK
252
253get_uploadpack () {
254 data_source=$(get_data_source "$1")
255 case "$data_source" in
256 config)
5be60078 257 uplp=$(git config --get "remote.$1.uploadpack")
5dee29ac
UKK
258 echo ${uplp:-git-upload-pack}
259 ;;
260 *)
261 echo "git-upload-pack"
262 ;;
263 esac
264}