]>
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 | */*) | |
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 | ||
39 | get_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 |
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 | } | |
76 | ||
ac4b0cff JH |
77 | get_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 | ||
05dd8e2e | 93 | # Subroutine to canonicalize remote:local notation. |
ac4b0cff | 94 | canon_refs_list_for_fetch () { |
5372806a SB |
95 | # If called from get_remote_default_refs_for_fetch |
96 | # leave the branches in branch.${curr_branch}.merge alone, | |
97 | # or the first one otherwise; add prefix . to the rest | |
05dd8e2e | 98 | # to prevent the secondary branches to be merged by default. |
5372806a SB |
99 | merge_branches= |
100 | if test "$1" = "-d" | |
101 | then | |
102 | shift ; remote="$1" ; shift | |
103 | if test "$remote" = "$(get_default_remote)" | |
104 | then | |
105 | curr_branch=$(git-symbolic-ref HEAD | \ | |
106 | sed -e 's|^refs/heads/||') | |
107 | merge_branches=$(git-repo-config \ | |
108 | --get-all "branch.${curr_branch}.merge") | |
109 | fi | |
110 | fi | |
ac4b0cff JH |
111 | for ref |
112 | do | |
efe9bf0f JH |
113 | force= |
114 | case "$ref" in | |
115 | +*) | |
dfdcb558 | 116 | ref=$(expr "z$ref" : 'z+\(.*\)') |
efe9bf0f JH |
117 | force=+ |
118 | ;; | |
119 | esac | |
f327dbce MW |
120 | expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:" |
121 | remote=$(expr "z$ref" : 'z\([^:]*\):') | |
122 | local=$(expr "z$ref" : 'z[^:]*:\(.*\)') | |
5372806a SB |
123 | dot_prefix=. |
124 | if test -z "$merge_branches" | |
125 | then | |
126 | merge_branches=$remote | |
127 | dot_prefix= | |
128 | else | |
129 | for merge_branch in $merge_branches | |
130 | do | |
131 | [ "$remote" = "$merge_branch" ] && | |
132 | dot_prefix= && break | |
133 | done | |
134 | fi | |
ac4b0cff JH |
135 | case "$remote" in |
136 | '') remote=HEAD ;; | |
687b8be8 EW |
137 | refs/heads/* | refs/tags/* | refs/remotes/*) ;; |
138 | heads/* | tags/* | remotes/* ) remote="refs/$remote" ;; | |
ac4b0cff JH |
139 | *) remote="refs/heads/$remote" ;; |
140 | esac | |
141 | case "$local" in | |
142 | '') local= ;; | |
687b8be8 EW |
143 | refs/heads/* | refs/tags/* | refs/remotes/*) ;; |
144 | heads/* | tags/* | remotes/* ) local="refs/$local" ;; | |
ac4b0cff JH |
145 | *) local="refs/heads/$local" ;; |
146 | esac | |
d8a1deec | 147 | |
f327dbce | 148 | if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)') |
d8a1deec JH |
149 | then |
150 | git-check-ref-format "$local_ref_name" || | |
151 | die "* refusing to create funny ref '$local_ref_name' locally" | |
152 | fi | |
05dd8e2e | 153 | echo "${dot_prefix}${force}${remote}:${local}" |
ac4b0cff JH |
154 | done |
155 | } | |
156 | ||
157 | # Returns list of src: (no store), or src:dst (store) | |
158 | get_remote_default_refs_for_fetch () { | |
159 | data_source=$(get_data_source "$1") | |
160 | case "$data_source" in | |
73136b2e | 161 | '' | config-partial | branches-partial) |
ac4b0cff | 162 | echo "HEAD:" ;; |
73136b2e | 163 | config) |
5372806a | 164 | canon_refs_list_for_fetch -d "$1" \ |
73136b2e | 165 | $(git-repo-config --get-all "remote.$1.fetch") ;; |
ac4b0cff JH |
166 | branches) |
167 | remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1") | |
168 | case "$remote_branch" in '') remote_branch=master ;; esac | |
169 | echo "refs/heads/${remote_branch}:refs/heads/$1" | |
170 | ;; | |
171 | remotes) | |
5372806a | 172 | canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{ |
ac4b0cff JH |
173 | s///p |
174 | }' "$GIT_DIR/remotes/$1") | |
175 | ;; | |
176 | *) | |
177 | die "internal error: get-remote-default-ref-for-push $1" ;; | |
178 | esac | |
179 | } | |
180 | ||
181 | get_remote_refs_for_push () { | |
182 | case "$#" in | |
183 | 0) die "internal error: get-remote-refs-for-push." ;; | |
184 | 1) get_remote_default_refs_for_push "$@" ;; | |
185 | *) shift; echo "$@" ;; | |
186 | esac | |
187 | } | |
188 | ||
189 | get_remote_refs_for_fetch () { | |
190 | case "$#" in | |
191 | 0) | |
192 | die "internal error: get-remote-refs-for-fetch." ;; | |
193 | 1) | |
194 | get_remote_default_refs_for_fetch "$@" ;; | |
195 | *) | |
196 | shift | |
197 | tag_just_seen= | |
198 | for ref | |
199 | do | |
200 | if test "$tag_just_seen" | |
201 | then | |
202 | echo "refs/tags/${ref}:refs/tags/${ref}" | |
203 | tag_just_seen= | |
204 | continue | |
205 | else | |
206 | case "$ref" in | |
207 | tag) | |
efe9bf0f | 208 | tag_just_seen=yes |
ac4b0cff JH |
209 | continue |
210 | ;; | |
211 | esac | |
212 | fi | |
efe9bf0f | 213 | canon_refs_list_for_fetch "$ref" |
ac4b0cff JH |
214 | done |
215 | ;; | |
216 | esac | |
217 | } | |
4447badc JH |
218 | |
219 | resolve_alternates () { | |
220 | # original URL (xxx.git) | |
f327dbce | 221 | top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'` |
4447badc JH |
222 | while read path |
223 | do | |
224 | case "$path" in | |
225 | \#* | '') | |
226 | continue ;; | |
227 | /*) | |
228 | echo "$top_$path/" ;; | |
229 | ../*) | |
230 | # relative -- ugly but seems to work. | |
231 | echo "$1/objects/$path/" ;; | |
232 | *) | |
233 | # exit code may not be caught by the reader. | |
234 | echo "bad alternate: $path" | |
235 | exit 1 ;; | |
236 | esac | |
237 | done | |
238 | } |