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 | *) | |
e0d10e1c | 13 | if test "$(git-config --get "remote.$1.url")" |
73136b2e JS |
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) | |
e0d10e1c | 35 | git-config --get "remote.$1.url" |
73136b2e | 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 | 51 | get_default_remote () { |
a0f4280f | 52 | curr_branch=$(git-symbolic-ref -q HEAD | sed -e 's|^refs/heads/||') |
e0d10e1c | 53 | origin=$(git-config --get "branch.$curr_branch.remote") |
648ad18f SB |
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 | 62 | config) |
e0d10e1c | 63 | git-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. | |
fbc90123 JH |
79 | # |
80 | # The first token returned is either "explicit" or "glob"; this | |
81 | # is to help prevent randomly "globbed" ref from being chosen as | |
82 | # a merge candidate | |
5677882b | 83 | expand_refs_wildcard () { |
95339912 JP |
84 | echo "$ls_remote_result" | |
85 | git fetch--tool expand-refs-wildcard "-" "$@" | |
5677882b JH |
86 | } |
87 | ||
05dd8e2e | 88 | # Subroutine to canonicalize remote:local notation. |
ac4b0cff | 89 | canon_refs_list_for_fetch () { |
5372806a SB |
90 | # If called from get_remote_default_refs_for_fetch |
91 | # leave the branches in branch.${curr_branch}.merge alone, | |
92 | # or the first one otherwise; add prefix . to the rest | |
05dd8e2e | 93 | # to prevent the secondary branches to be merged by default. |
5372806a | 94 | merge_branches= |
62b339a5 | 95 | curr_branch= |
5372806a SB |
96 | if test "$1" = "-d" |
97 | then | |
98 | shift ; remote="$1" ; shift | |
ae9c6ffe | 99 | set $(expand_refs_wildcard "$remote" "$@") |
fbc90123 | 100 | is_explicit="$1" |
d41cb273 | 101 | shift |
5372806a SB |
102 | if test "$remote" = "$(get_default_remote)" |
103 | then | |
a0f4280f | 104 | curr_branch=$(git-symbolic-ref -q HEAD | \ |
5372806a | 105 | sed -e 's|^refs/heads/||') |
e0d10e1c | 106 | merge_branches=$(git-config \ |
9e115549 | 107 | --get-all "branch.${curr_branch}.merge") |
5372806a | 108 | fi |
fbc90123 JH |
109 | if test -z "$merge_branches" && test $is_explicit != explicit |
110 | then | |
111 | merge_branches=..this.will.never.match.any.ref.. | |
112 | fi | |
5372806a | 113 | fi |
ac4b0cff JH |
114 | for ref |
115 | do | |
efe9bf0f JH |
116 | force= |
117 | case "$ref" in | |
118 | +*) | |
dfdcb558 | 119 | ref=$(expr "z$ref" : 'z+\(.*\)') |
efe9bf0f JH |
120 | force=+ |
121 | ;; | |
122 | esac | |
f327dbce MW |
123 | expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:" |
124 | remote=$(expr "z$ref" : 'z\([^:]*\):') | |
125 | local=$(expr "z$ref" : 'z[^:]*:\(.*\)') | |
5372806a SB |
126 | dot_prefix=. |
127 | if test -z "$merge_branches" | |
128 | then | |
129 | merge_branches=$remote | |
130 | dot_prefix= | |
131 | else | |
132 | for merge_branch in $merge_branches | |
133 | do | |
756373da JH |
134 | [ "$remote" = "$merge_branch" ] && |
135 | dot_prefix= && break | |
5372806a SB |
136 | done |
137 | fi | |
ac4b0cff | 138 | case "$remote" in |
153e98d2 | 139 | '' | HEAD ) remote=HEAD ;; |
687b8be8 EW |
140 | refs/heads/* | refs/tags/* | refs/remotes/*) ;; |
141 | heads/* | tags/* | remotes/* ) remote="refs/$remote" ;; | |
ac4b0cff JH |
142 | *) remote="refs/heads/$remote" ;; |
143 | esac | |
144 | case "$local" in | |
145 | '') local= ;; | |
687b8be8 EW |
146 | refs/heads/* | refs/tags/* | refs/remotes/*) ;; |
147 | heads/* | tags/* | remotes/* ) local="refs/$local" ;; | |
ac4b0cff JH |
148 | *) local="refs/heads/$local" ;; |
149 | esac | |
d8a1deec | 150 | |
f327dbce | 151 | if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)') |
d8a1deec JH |
152 | then |
153 | git-check-ref-format "$local_ref_name" || | |
154 | die "* refusing to create funny ref '$local_ref_name' locally" | |
155 | fi | |
05dd8e2e | 156 | echo "${dot_prefix}${force}${remote}:${local}" |
ac4b0cff JH |
157 | done |
158 | } | |
159 | ||
160 | # Returns list of src: (no store), or src:dst (store) | |
161 | get_remote_default_refs_for_fetch () { | |
162 | data_source=$(get_data_source "$1") | |
163 | case "$data_source" in | |
ea560e6d | 164 | '') |
ac4b0cff | 165 | echo "HEAD:" ;; |
73136b2e | 166 | config) |
5372806a | 167 | canon_refs_list_for_fetch -d "$1" \ |
e0d10e1c | 168 | $(git-config --get-all "remote.$1.fetch") ;; |
ac4b0cff JH |
169 | branches) |
170 | remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1") | |
171 | case "$remote_branch" in '') remote_branch=master ;; esac | |
172 | echo "refs/heads/${remote_branch}:refs/heads/$1" | |
173 | ;; | |
174 | remotes) | |
5372806a | 175 | canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{ |
ac4b0cff JH |
176 | s///p |
177 | }' "$GIT_DIR/remotes/$1") | |
178 | ;; | |
179 | *) | |
180 | die "internal error: get-remote-default-ref-for-push $1" ;; | |
181 | esac | |
182 | } | |
183 | ||
184 | get_remote_refs_for_push () { | |
185 | case "$#" in | |
186 | 0) die "internal error: get-remote-refs-for-push." ;; | |
187 | 1) get_remote_default_refs_for_push "$@" ;; | |
188 | *) shift; echo "$@" ;; | |
189 | esac | |
190 | } | |
191 | ||
192 | get_remote_refs_for_fetch () { | |
193 | case "$#" in | |
194 | 0) | |
195 | die "internal error: get-remote-refs-for-fetch." ;; | |
196 | 1) | |
197 | get_remote_default_refs_for_fetch "$@" ;; | |
198 | *) | |
199 | shift | |
200 | tag_just_seen= | |
201 | for ref | |
202 | do | |
203 | if test "$tag_just_seen" | |
204 | then | |
205 | echo "refs/tags/${ref}:refs/tags/${ref}" | |
206 | tag_just_seen= | |
207 | continue | |
208 | else | |
209 | case "$ref" in | |
210 | tag) | |
efe9bf0f | 211 | tag_just_seen=yes |
ac4b0cff JH |
212 | continue |
213 | ;; | |
214 | esac | |
215 | fi | |
efe9bf0f | 216 | canon_refs_list_for_fetch "$ref" |
ac4b0cff JH |
217 | done |
218 | ;; | |
219 | esac | |
220 | } | |
4447badc JH |
221 | |
222 | resolve_alternates () { | |
223 | # original URL (xxx.git) | |
f327dbce | 224 | top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'` |
4447badc JH |
225 | while read path |
226 | do | |
227 | case "$path" in | |
228 | \#* | '') | |
229 | continue ;; | |
230 | /*) | |
231 | echo "$top_$path/" ;; | |
232 | ../*) | |
233 | # relative -- ugly but seems to work. | |
234 | echo "$1/objects/$path/" ;; | |
235 | *) | |
236 | # exit code may not be caught by the reader. | |
237 | echo "bad alternate: $path" | |
238 | exit 1 ;; | |
239 | esac | |
240 | done | |
241 | } | |
5dee29ac UKK |
242 | |
243 | get_uploadpack () { | |
244 | data_source=$(get_data_source "$1") | |
245 | case "$data_source" in | |
246 | config) | |
e0d10e1c | 247 | uplp=$(git-config --get "remote.$1.uploadpack") |
5dee29ac UKK |
248 | echo ${uplp:-git-upload-pack} |
249 | ;; | |
250 | *) | |
251 | echo "git-upload-pack" | |
252 | ;; | |
253 | esac | |
254 | } |