e281b7c6ebd5515d74201869574eed6077f37c9d
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 ''
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 }
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"
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 }
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}
75 }
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" ;;
88 *)
89 die "internal error: get-remote-default-ref-for-push $1" ;;
90 esac
91 }
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
101 do
102 # a non glob pattern is given back as-is.
103 expr "z$ref" : 'zrefs/.*/\*:refs/.*/\*$' >/dev/null || {
104 echo "$ref"
105 continue
106 }
107 from=`expr "z$ref" : 'z\(refs/.*/\)\*:refs/.*/\*$'`
108 to=`expr "z$ref" : 'zrefs/.*/\*:\(refs/.*/\)\*$'`
109 echo "$ls_remote_result" |
110 (
111 IFS=' '
112 while read sha1 name
113 do
114 mapped=${name#"$from"}
115 if test "z$name" != "z${name#'^{}'}" ||
116 test "z$name" = "z$mapped"
117 then
118 continue
119 fi
120 echo "${name}:${to}${mapped}"
121 done
122 )
123 done
124 }
126 # Subroutine to canonicalize remote:local notation.
127 canon_refs_list_for_fetch () {
128 # If called from get_remote_default_refs_for_fetch
129 # leave the branches in branch.${curr_branch}.merge alone,
130 # or the first one otherwise; add prefix . to the rest
131 # to prevent the secondary branches to be merged by default.
132 merge_branches=
133 if test "$1" = "-d"
134 then
135 shift ; remote="$1" ; shift
136 if test "$remote" = "$(get_default_remote)"
137 then
138 curr_branch=$(git-symbolic-ref HEAD | \
139 sed -e 's|^refs/heads/||')
140 merge_branches=$(git-repo-config \
141 --get-all "branch.${curr_branch}.merge")
142 fi
143 set x $(expand_refs_wildcard "$@")
144 shift
145 fi
146 for ref
147 do
148 force=
149 case "$ref" in
150 +*)
151 ref=$(expr "z$ref" : 'z+\(.*\)')
152 force=+
153 ;;
154 esac
155 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
156 remote=$(expr "z$ref" : 'z\([^:]*\):')
157 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
158 dot_prefix=.
159 if test -z "$merge_branches"
160 then
161 merge_branches=$remote
162 dot_prefix=
163 else
164 for merge_branch in $merge_branches
165 do
166 [ "$remote" = "$merge_branch" ] &&
167 dot_prefix= && break
168 done
169 fi
170 case "$remote" in
171 '') remote=HEAD ;;
172 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
173 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
174 *) remote="refs/heads/$remote" ;;
175 esac
176 case "$local" in
177 '') local= ;;
178 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
179 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
180 *) local="refs/heads/$local" ;;
181 esac
183 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
184 then
185 git-check-ref-format "$local_ref_name" ||
186 die "* refusing to create funny ref '$local_ref_name' locally"
187 fi
188 echo "${dot_prefix}${force}${remote}:${local}"
189 done
190 }
192 # Returns list of src: (no store), or src:dst (store)
193 get_remote_default_refs_for_fetch () {
194 data_source=$(get_data_source "$1")
195 case "$data_source" in
196 '' | config-partial | branches-partial)
197 echo "HEAD:" ;;
198 config)
199 canon_refs_list_for_fetch -d "$1" \
200 $(git-repo-config --get-all "remote.$1.fetch") ;;
201 branches)
202 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
203 case "$remote_branch" in '') remote_branch=master ;; esac
204 echo "refs/heads/${remote_branch}:refs/heads/$1"
205 ;;
206 remotes)
207 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
208 s///p
209 }' "$GIT_DIR/remotes/$1")
210 ;;
211 *)
212 die "internal error: get-remote-default-ref-for-push $1" ;;
213 esac
214 }
216 get_remote_refs_for_push () {
217 case "$#" in
218 0) die "internal error: get-remote-refs-for-push." ;;
219 1) get_remote_default_refs_for_push "$@" ;;
220 *) shift; echo "$@" ;;
221 esac
222 }
224 get_remote_refs_for_fetch () {
225 case "$#" in
226 0)
227 die "internal error: get-remote-refs-for-fetch." ;;
228 1)
229 get_remote_default_refs_for_fetch "$@" ;;
230 *)
231 shift
232 tag_just_seen=
233 for ref
234 do
235 if test "$tag_just_seen"
236 then
237 echo "refs/tags/${ref}:refs/tags/${ref}"
238 tag_just_seen=
239 continue
240 else
241 case "$ref" in
242 tag)
243 tag_just_seen=yes
244 continue
245 ;;
246 esac
247 fi
248 canon_refs_list_for_fetch "$ref"
249 done
250 ;;
251 esac
252 }
254 resolve_alternates () {
255 # original URL (xxx.git)
256 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
257 while read path
258 do
259 case "$path" in
260 \#* | '')
261 continue ;;
262 /*)
263 echo "$top_$path/" ;;
264 ../*)
265 # relative -- ugly but seems to work.
266 echo "$1/objects/$path/" ;;
267 *)
268 # exit code may not be caught by the reader.
269 echo "bad alternate: $path"
270 exit 1 ;;
271 esac
272 done
273 }