]>
Commit | Line | Data |
---|---|---|
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. | |
5 | GIT_DIR=$(git-rev-parse --git-dir 2>/dev/null) || :; | |
ac4b0cff JH |
6 | |
7 | get_data_source () { | |
8 | case "$1" in | |
9 | */*) | |
ea560e6d | 10 | echo '' |
ac4b0cff JH |
11 | ;; |
12 | *) | |
73136b2e JS |
13 | if test "$(git-repo-config --get "remote.$1.url")" |
14 | then | |
15 | echo config | |
16 | elif test -f "$GIT_DIR/remotes/$1" | |
ac4b0cff JH |
17 | then |
18 | echo remotes | |
19 | elif test -f "$GIT_DIR/branches/$1" | |
20 | then | |
21 | echo branches | |
22 | else | |
23 | echo '' | |
24 | fi ;; | |
25 | esac | |
26 | } | |
27 | ||
28 | get_remote_url () { | |
29 | data_source=$(get_data_source "$1") | |
30 | case "$data_source" in | |
31 | '') | |
ea560e6d | 32 | echo "$1" |
73136b2e JS |
33 | ;; |
34 | config) | |
35 | git-repo-config --get "remote.$1.url" | |
36 | ;; | |
ac4b0cff JH |
37 | remotes) |
38 | sed -ne '/^URL: */{ | |
39 | s///p | |
40 | q | |
ea560e6d JH |
41 | }' "$GIT_DIR/remotes/$1" |
42 | ;; | |
ac4b0cff | 43 | branches) |
ea560e6d | 44 | sed -e 's/#.*//' "$GIT_DIR/branches/$1" |
ac4b0cff JH |
45 | ;; |
46 | *) | |
47 | die "internal error: get-remote-url $1" ;; | |
48 | esac | |
49 | } | |
50 | ||
648ad18f SB |
51 | get_default_remote () { |
52 | curr_branch=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||') | |
53 | origin=$(git-repo-config --get "branch.$curr_branch.remote") | |
54 | echo ${origin:-origin} | |
55 | } | |
56 | ||
ac4b0cff JH |
57 | get_remote_default_refs_for_push () { |
58 | data_source=$(get_data_source "$1") | |
59 | case "$data_source" in | |
ea560e6d | 60 | '' | branches) |
ac4b0cff | 61 | ;; # no default push mapping, just send matching refs. |
73136b2e JS |
62 | config) |
63 | git-repo-config --get-all "remote.$1.push" ;; | |
ac4b0cff JH |
64 | remotes) |
65 | sed -ne '/^Push: */{ | |
66 | s///p | |
67 | }' "$GIT_DIR/remotes/$1" ;; | |
68 | *) | |
69 | die "internal error: get-remote-default-ref-for-push $1" ;; | |
70 | esac | |
71 | } | |
72 | ||
5677882b JH |
73 | # Called from canon_refs_list_for_fetch -d "$remote", which |
74 | # is called from get_remote_default_refs_for_fetch to grok | |
75 | # refspecs that are retrieved from the configuration, but not | |
76 | # from get_remote_refs_for_fetch when it deals with refspecs | |
77 | # supplied on the command line. $ls_remote_result has the list | |
78 | # of refs available at remote. | |
79 | expand_refs_wildcard () { | |
80 | for ref | |
81 | do | |
d945d4be | 82 | lref=${ref#'+'} |
5677882b | 83 | # a non glob pattern is given back as-is. |
d945d4be | 84 | expr "z$lref" : 'zrefs/.*/\*:refs/.*/\*$' >/dev/null || { |
5677882b JH |
85 | echo "$ref" |
86 | continue | |
87 | } | |
d945d4be JH |
88 | |
89 | from=`expr "z$lref" : 'z\(refs/.*/\)\*:refs/.*/\*$'` | |
90 | to=`expr "z$lref" : 'zrefs/.*/\*:\(refs/.*/\)\*$'` | |
91 | local_force= | |
92 | test "z$lref" = "z$ref" || local_force='+' | |
5677882b | 93 | echo "$ls_remote_result" | |
0c7a97fa | 94 | sed -e '/\^{}$/d' | |
5677882b JH |
95 | ( |
96 | IFS=' ' | |
97 | while read sha1 name | |
98 | do | |
0c7a97fa | 99 | # ignore the ones that do not start with $from |
5677882b | 100 | mapped=${name#"$from"} |
0c7a97fa | 101 | test "z$name" = "z$mapped" && continue |
d945d4be | 102 | echo "${local_force}${name}:${to}${mapped}" |
5677882b JH |
103 | done |
104 | ) | |
105 | done | |
106 | } | |
107 | ||
05dd8e2e | 108 | # Subroutine to canonicalize remote:local notation. |
ac4b0cff | 109 | canon_refs_list_for_fetch () { |
5372806a SB |
110 | # If called from get_remote_default_refs_for_fetch |
111 | # leave the branches in branch.${curr_branch}.merge alone, | |
112 | # or the first one otherwise; add prefix . to the rest | |
05dd8e2e | 113 | # to prevent the secondary branches to be merged by default. |
5372806a | 114 | merge_branches= |
62b339a5 | 115 | curr_branch= |
5372806a SB |
116 | if test "$1" = "-d" |
117 | then | |
118 | shift ; remote="$1" ; shift | |
d41cb273 JH |
119 | set x $(expand_refs_wildcard "$@") |
120 | shift | |
5372806a SB |
121 | if test "$remote" = "$(get_default_remote)" |
122 | then | |
123 | curr_branch=$(git-symbolic-ref HEAD | \ | |
124 | sed -e 's|^refs/heads/||') | |
125 | merge_branches=$(git-repo-config \ | |
9e115549 | 126 | --get-all "branch.${curr_branch}.merge") |
5372806a SB |
127 | fi |
128 | fi | |
ac4b0cff JH |
129 | for ref |
130 | do | |
efe9bf0f JH |
131 | force= |
132 | case "$ref" in | |
133 | +*) | |
dfdcb558 | 134 | ref=$(expr "z$ref" : 'z+\(.*\)') |
efe9bf0f JH |
135 | force=+ |
136 | ;; | |
137 | esac | |
f327dbce MW |
138 | expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:" |
139 | remote=$(expr "z$ref" : 'z\([^:]*\):') | |
140 | local=$(expr "z$ref" : 'z[^:]*:\(.*\)') | |
5372806a SB |
141 | dot_prefix=. |
142 | if test -z "$merge_branches" | |
143 | then | |
144 | merge_branches=$remote | |
145 | dot_prefix= | |
146 | else | |
147 | for merge_branch in $merge_branches | |
148 | do | |
80c79776 JH |
149 | if test "$remote" = "$merge_branch" || |
150 | test "$local" = "$merge_branch" | |
151 | then | |
152 | dot_prefix= | |
153 | break | |
154 | fi | |
5372806a SB |
155 | done |
156 | fi | |
ac4b0cff JH |
157 | case "$remote" in |
158 | '') remote=HEAD ;; | |
687b8be8 EW |
159 | refs/heads/* | refs/tags/* | refs/remotes/*) ;; |
160 | heads/* | tags/* | remotes/* ) remote="refs/$remote" ;; | |
ac4b0cff JH |
161 | *) remote="refs/heads/$remote" ;; |
162 | esac | |
163 | case "$local" in | |
164 | '') local= ;; | |
687b8be8 EW |
165 | refs/heads/* | refs/tags/* | refs/remotes/*) ;; |
166 | heads/* | tags/* | remotes/* ) local="refs/$local" ;; | |
ac4b0cff JH |
167 | *) local="refs/heads/$local" ;; |
168 | esac | |
d8a1deec | 169 | |
f327dbce | 170 | if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)') |
d8a1deec JH |
171 | then |
172 | git-check-ref-format "$local_ref_name" || | |
173 | die "* refusing to create funny ref '$local_ref_name' locally" | |
174 | fi | |
05dd8e2e | 175 | echo "${dot_prefix}${force}${remote}:${local}" |
ac4b0cff JH |
176 | done |
177 | } | |
178 | ||
179 | # Returns list of src: (no store), or src:dst (store) | |
180 | get_remote_default_refs_for_fetch () { | |
181 | data_source=$(get_data_source "$1") | |
182 | case "$data_source" in | |
ea560e6d | 183 | '') |
ac4b0cff | 184 | echo "HEAD:" ;; |
73136b2e | 185 | config) |
5372806a | 186 | canon_refs_list_for_fetch -d "$1" \ |
73136b2e | 187 | $(git-repo-config --get-all "remote.$1.fetch") ;; |
ac4b0cff JH |
188 | branches) |
189 | remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1") | |
190 | case "$remote_branch" in '') remote_branch=master ;; esac | |
191 | echo "refs/heads/${remote_branch}:refs/heads/$1" | |
192 | ;; | |
193 | remotes) | |
5372806a | 194 | canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{ |
ac4b0cff JH |
195 | s///p |
196 | }' "$GIT_DIR/remotes/$1") | |
197 | ;; | |
198 | *) | |
199 | die "internal error: get-remote-default-ref-for-push $1" ;; | |
200 | esac | |
201 | } | |
202 | ||
203 | get_remote_refs_for_push () { | |
204 | case "$#" in | |
205 | 0) die "internal error: get-remote-refs-for-push." ;; | |
206 | 1) get_remote_default_refs_for_push "$@" ;; | |
207 | *) shift; echo "$@" ;; | |
208 | esac | |
209 | } | |
210 | ||
211 | get_remote_refs_for_fetch () { | |
212 | case "$#" in | |
213 | 0) | |
214 | die "internal error: get-remote-refs-for-fetch." ;; | |
215 | 1) | |
216 | get_remote_default_refs_for_fetch "$@" ;; | |
217 | *) | |
218 | shift | |
219 | tag_just_seen= | |
220 | for ref | |
221 | do | |
222 | if test "$tag_just_seen" | |
223 | then | |
224 | echo "refs/tags/${ref}:refs/tags/${ref}" | |
225 | tag_just_seen= | |
226 | continue | |
227 | else | |
228 | case "$ref" in | |
229 | tag) | |
efe9bf0f | 230 | tag_just_seen=yes |
ac4b0cff JH |
231 | continue |
232 | ;; | |
233 | esac | |
234 | fi | |
efe9bf0f | 235 | canon_refs_list_for_fetch "$ref" |
ac4b0cff JH |
236 | done |
237 | ;; | |
238 | esac | |
239 | } | |
4447badc JH |
240 | |
241 | resolve_alternates () { | |
242 | # original URL (xxx.git) | |
f327dbce | 243 | top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'` |
4447badc JH |
244 | while read path |
245 | do | |
246 | case "$path" in | |
247 | \#* | '') | |
248 | continue ;; | |
249 | /*) | |
250 | echo "$top_$path/" ;; | |
251 | ../*) | |
252 | # relative -- ugly but seems to work. | |
253 | echo "$1/objects/$path/" ;; | |
254 | *) | |
255 | # exit code may not be caught by the reader. | |
256 | echo "bad alternate: $path" | |
257 | exit 1 ;; | |
258 | esac | |
259 | done | |
260 | } |