Add t5510 to test per branch configuration affecting git-fetch.
[git.git] / git-parse-remote.sh
blob69998169b7f2a2a07363a7d1416be129bca82e84
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_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}
77 get_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" ;;
89 die "internal error: get-remote-default-ref-for-push $1" ;;
90 esac
93 # Subroutine to canonicalize remote:local notation.
94 canon_refs_list_for_fetch () {
95 # Leave only the first one alone; add prefix . to the rest
96 # to prevent the secondary branches to be merged by default.
97 dot_prefix=
98 for ref
100 force=
101 case "$ref" in
103 ref=$(expr "z$ref" : 'z+\(.*\)')
104 force=+
106 esac
107 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
108 remote=$(expr "z$ref" : 'z\([^:]*\):')
109 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
110 case "$remote" in
111 '') remote=HEAD ;;
112 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
113 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
114 *) remote="refs/heads/$remote" ;;
115 esac
116 case "$local" in
117 '') local= ;;
118 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
119 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
120 *) local="refs/heads/$local" ;;
121 esac
123 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
124 then
125 git-check-ref-format "$local_ref_name" ||
126 die "* refusing to create funny ref '$local_ref_name' locally"
128 echo "${dot_prefix}${force}${remote}:${local}"
129 dot_prefix=.
130 done
133 # Returns list of src: (no store), or src:dst (store)
134 get_remote_default_refs_for_fetch () {
135 data_source=$(get_data_source "$1")
136 case "$data_source" in
137 '' | config-partial | branches-partial)
138 echo "HEAD:" ;;
139 config)
140 canon_refs_list_for_fetch \
141 $(git-repo-config --get-all "remote.$1.fetch") ;;
142 branches)
143 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
144 case "$remote_branch" in '') remote_branch=master ;; esac
145 echo "refs/heads/${remote_branch}:refs/heads/$1"
147 remotes)
148 # This prefixes the second and later default refspecs
149 # with a '.', to signal git-fetch to mark them
150 # not-for-merge.
151 canon_refs_list_for_fetch $(sed -ne '/^Pull: */{
152 s///p
153 }' "$GIT_DIR/remotes/$1")
156 die "internal error: get-remote-default-ref-for-push $1" ;;
157 esac
160 get_remote_refs_for_push () {
161 case "$#" in
162 0) die "internal error: get-remote-refs-for-push." ;;
163 1) get_remote_default_refs_for_push "$@" ;;
164 *) shift; echo "$@" ;;
165 esac
168 get_remote_refs_for_fetch () {
169 case "$#" in
171 die "internal error: get-remote-refs-for-fetch." ;;
173 get_remote_default_refs_for_fetch "$@" ;;
175 shift
176 tag_just_seen=
177 for ref
179 if test "$tag_just_seen"
180 then
181 echo "refs/tags/${ref}:refs/tags/${ref}"
182 tag_just_seen=
183 continue
184 else
185 case "$ref" in
186 tag)
187 tag_just_seen=yes
188 continue
190 esac
192 canon_refs_list_for_fetch "$ref"
193 done
195 esac
198 resolve_alternates () {
199 # original URL (xxx.git)
200 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
201 while read path
203 case "$path" in
204 \#* | '')
205 continue ;;
207 echo "$top_$path/" ;;
208 ../*)
209 # relative -- ugly but seems to work.
210 echo "$1/objects/$path/" ;;
212 # exit code may not be caught by the reader.
213 echo "bad alternate: $path"
214 exit 1 ;;
215 esac
216 done