builtin-upload-archive.c broken on openbsd
[git/git.git] / git-parse-remote.sh
CommitLineData
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.
5GIT_DIR=$(git-rev-parse --git-dir 2>/dev/null) || :;
ac4b0cff
JH
6
7get_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
39get_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
71get_remote_default_refs_for_push () {
72 data_source=$(get_data_source "$1")
73 case "$data_source" in
73136b2e 74 '' | config-partial | branches | branches-partial)
ac4b0cff 75 ;; # no default push mapping, just send matching refs.
73136b2e
JS
76 config)
77 git-repo-config --get-all "remote.$1.push" ;;
ac4b0cff
JH
78 remotes)
79 sed -ne '/^Push: */{
80 s///p
81 }' "$GIT_DIR/remotes/$1" ;;
82 *)
83 die "internal error: get-remote-default-ref-for-push $1" ;;
84 esac
85}
86
05dd8e2e 87# Subroutine to canonicalize remote:local notation.
ac4b0cff 88canon_refs_list_for_fetch () {
05dd8e2e
JH
89 # Leave only the first one alone; add prefix . to the rest
90 # to prevent the secondary branches to be merged by default.
91 dot_prefix=
ac4b0cff
JH
92 for ref
93 do
efe9bf0f
JH
94 force=
95 case "$ref" in
96 +*)
dfdcb558 97 ref=$(expr "z$ref" : 'z+\(.*\)')
efe9bf0f
JH
98 force=+
99 ;;
100 esac
f327dbce
MW
101 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
102 remote=$(expr "z$ref" : 'z\([^:]*\):')
103 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
ac4b0cff
JH
104 case "$remote" in
105 '') remote=HEAD ;;
687b8be8
EW
106 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
107 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
ac4b0cff
JH
108 *) remote="refs/heads/$remote" ;;
109 esac
110 case "$local" in
111 '') local= ;;
687b8be8
EW
112 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
113 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
ac4b0cff
JH
114 *) local="refs/heads/$local" ;;
115 esac
d8a1deec 116
f327dbce 117 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
d8a1deec
JH
118 then
119 git-check-ref-format "$local_ref_name" ||
120 die "* refusing to create funny ref '$local_ref_name' locally"
121 fi
05dd8e2e
JH
122 echo "${dot_prefix}${force}${remote}:${local}"
123 dot_prefix=.
ac4b0cff
JH
124 done
125}
126
127# Returns list of src: (no store), or src:dst (store)
128get_remote_default_refs_for_fetch () {
129 data_source=$(get_data_source "$1")
130 case "$data_source" in
73136b2e 131 '' | config-partial | branches-partial)
ac4b0cff 132 echo "HEAD:" ;;
73136b2e
JS
133 config)
134 canon_refs_list_for_fetch \
135 $(git-repo-config --get-all "remote.$1.fetch") ;;
ac4b0cff
JH
136 branches)
137 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
138 case "$remote_branch" in '') remote_branch=master ;; esac
139 echo "refs/heads/${remote_branch}:refs/heads/$1"
140 ;;
141 remotes)
05dd8e2e
JH
142 # This prefixes the second and later default refspecs
143 # with a '.', to signal git-fetch to mark them
144 # not-for-merge.
ac4b0cff
JH
145 canon_refs_list_for_fetch $(sed -ne '/^Pull: */{
146 s///p
147 }' "$GIT_DIR/remotes/$1")
148 ;;
149 *)
150 die "internal error: get-remote-default-ref-for-push $1" ;;
151 esac
152}
153
154get_remote_refs_for_push () {
155 case "$#" in
156 0) die "internal error: get-remote-refs-for-push." ;;
157 1) get_remote_default_refs_for_push "$@" ;;
158 *) shift; echo "$@" ;;
159 esac
160}
161
162get_remote_refs_for_fetch () {
163 case "$#" in
164 0)
165 die "internal error: get-remote-refs-for-fetch." ;;
166 1)
167 get_remote_default_refs_for_fetch "$@" ;;
168 *)
169 shift
170 tag_just_seen=
171 for ref
172 do
173 if test "$tag_just_seen"
174 then
175 echo "refs/tags/${ref}:refs/tags/${ref}"
176 tag_just_seen=
177 continue
178 else
179 case "$ref" in
180 tag)
efe9bf0f 181 tag_just_seen=yes
ac4b0cff
JH
182 continue
183 ;;
184 esac
185 fi
efe9bf0f 186 canon_refs_list_for_fetch "$ref"
ac4b0cff
JH
187 done
188 ;;
189 esac
190}
4447badc
JH
191
192resolve_alternates () {
193 # original URL (xxx.git)
f327dbce 194 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
4447badc
JH
195 while read path
196 do
197 case "$path" in
198 \#* | '')
199 continue ;;
200 /*)
201 echo "$top_$path/" ;;
202 ../*)
203 # relative -- ugly but seems to work.
204 echo "$1/objects/$path/" ;;
205 *)
206 # exit code may not be caught by the reader.
207 echo "bad alternate: $path"
208 exit 1 ;;
209 esac
210 done
211}