auto-detect changed prefix and/or changed build flags
[git/spearce.git] / git-parse-remote.sh
blob187f0883c9136772677088ddf61228291d4b41d1
1 #!/bin/sh
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) || :;
7 get_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 ''
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
39 get_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"
50 config)
51 git-repo-config --get "remote.$1.url"
53 remotes)
54 sed -ne '/^URL: */{
55 s///p
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"
67 die "internal error: get-remote-url $1" ;;
68 esac
71 get_remote_default_refs_for_push () {
72 data_source=$(get_data_source "$1")
73 case "$data_source" in
74 '' | config-partial | branches | branches-partial)
75 ;; # no default push mapping, just send matching refs.
76 config)
77 git-repo-config --get-all "remote.$1.push" ;;
78 remotes)
79 sed -ne '/^Push: */{
80 s///p
81 }' "$GIT_DIR/remotes/$1" ;;
83 die "internal error: get-remote-default-ref-for-push $1" ;;
84 esac
87 # Subroutine to canonicalize remote:local notation.
88 canon_refs_list_for_fetch () {
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=
92 for ref
94 force=
95 case "$ref" in
96 +*)
97 ref=$(expr "z$ref" : 'z+\(.*\)')
98 force=+
100 esac
101 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
102 remote=$(expr "z$ref" : 'z\([^:]*\):')
103 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
104 case "$remote" in
105 '') remote=HEAD ;;
106 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
107 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
108 *) remote="refs/heads/$remote" ;;
109 esac
110 case "$local" in
111 '') local= ;;
112 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
113 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
114 *) local="refs/heads/$local" ;;
115 esac
117 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
118 then
119 git-check-ref-format "$local_ref_name" ||
120 die "* refusing to create funny ref '$local_ref_name' locally"
122 echo "${dot_prefix}${force}${remote}:${local}"
123 dot_prefix=.
124 done
127 # Returns list of src: (no store), or src:dst (store)
128 get_remote_default_refs_for_fetch () {
129 data_source=$(get_data_source "$1")
130 case "$data_source" in
131 '' | config-partial | branches-partial)
132 echo "HEAD:" ;;
133 config)
134 canon_refs_list_for_fetch \
135 $(git-repo-config --get-all "remote.$1.fetch") ;;
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"
141 remotes)
142 # This prefixes the second and later default refspecs
143 # with a '.', to signal git-fetch to mark them
144 # not-for-merge.
145 canon_refs_list_for_fetch $(sed -ne '/^Pull: */{
146 s///p
147 }' "$GIT_DIR/remotes/$1")
150 die "internal error: get-remote-default-ref-for-push $1" ;;
151 esac
154 get_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
162 get_remote_refs_for_fetch () {
163 case "$#" in
165 die "internal error: get-remote-refs-for-fetch." ;;
167 get_remote_default_refs_for_fetch "$@" ;;
169 shift
170 tag_just_seen=
171 for ref
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)
181 tag_just_seen=yes
182 continue
184 esac
186 canon_refs_list_for_fetch "$ref"
187 done
189 esac
192 resolve_alternates () {
193 # original URL (xxx.git)
194 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
195 while read path
197 case "$path" in
198 \#* | '')
199 continue ;;
201 echo "$top_$path/" ;;
202 ../*)
203 # relative -- ugly but seems to work.
204 echo "$1/objects/$path/" ;;
206 # exit code may not be caught by the reader.
207 echo "bad alternate: $path"
208 exit 1 ;;
209 esac
210 done