1#!/bin/sh
2
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.
5GIT_DIR=$(git-rev-parse --git-dir 2>/dev/null) || :;
6
7get_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}
38
39get_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}
70
71get_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
77get_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}
92
93# Subroutine to canonicalize remote:local notation.
94canon_refs_list_for_fetch () {
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
98 # to prevent the secondary branches to be merged by default.
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
111 for ref
112 do
113 force=
114 case "$ref" in
115 +*)
116 ref=$(expr "z$ref" : 'z+\(.*\)')
117 force=+
118 ;;
119 esac
120 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
121 remote=$(expr "z$ref" : 'z\([^:]*\):')
122 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
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
135 case "$remote" in
136 '') remote=HEAD ;;
137 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
138 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
139 *) remote="refs/heads/$remote" ;;
140 esac
141 case "$local" in
142 '') local= ;;
143 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
144 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
145 *) local="refs/heads/$local" ;;
146 esac
147
148 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
149 then
150 git-check-ref-format "$local_ref_name" ||
151 die "* refusing to create funny ref '$local_ref_name' locally"
152 fi
153 echo "${dot_prefix}${force}${remote}:${local}"
154 done
155}
156
157# Returns list of src: (no store), or src:dst (store)
158get_remote_default_refs_for_fetch () {
159 data_source=$(get_data_source "$1")
160 case "$data_source" in
161 '' | config-partial | branches-partial)
162 echo "HEAD:" ;;
163 config)
164 canon_refs_list_for_fetch -d "$1" \
165 $(git-repo-config --get-all "remote.$1.fetch") ;;
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)
172 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
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
181get_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
189get_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)
208 tag_just_seen=yes
209 continue
210 ;;
211 esac
212 fi
213 canon_refs_list_for_fetch "$ref"
214 done
215 ;;
216 esac
217}
218
219resolve_alternates () {
220 # original URL (xxx.git)
221 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
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}