Windows ANSI - hook write()
[git/mingw/4msysgit/peterh.git] / git-parse-remote.sh
blob7c0d242eec97c06d744059155902e84ee0bd0582
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 echo ''
13 echo self
16 if test "$(git config --get "remote.$1.url")"
17 then
18 echo config
19 elif test -f "$GIT_DIR/remotes/$1"
20 then
21 echo remotes
22 elif test -f "$GIT_DIR/branches/$1"
23 then
24 echo branches
25 else
26 echo ''
27 fi ;;
28 esac
31 get_remote_url () {
32 data_source=$(get_data_source "$1")
33 case "$data_source" in
34 '')
35 echo "$1"
37 self)
38 echo "$1"
40 config)
41 git config --get "remote.$1.url"
43 remotes)
44 sed -ne '/^URL: */{
45 s///p
47 }' "$GIT_DIR/remotes/$1"
49 branches)
50 sed -e 's/#.*//' "$GIT_DIR/branches/$1"
53 die "internal error: get-remote-url $1" ;;
54 esac | sed "s|^\(.[^:][^:]*\):\(/[^/]\)|ssh://\1\2|"
55 # work around MinGW path mangling
58 get_default_remote () {
59 curr_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')
60 origin=$(git config --get "branch.$curr_branch.remote")
61 echo ${origin:-origin}
64 get_remote_default_refs_for_push () {
65 data_source=$(get_data_source "$1")
66 case "$data_source" in
67 '' | branches | self)
68 ;; # no default push mapping, just send matching refs.
69 config)
70 git config --get-all "remote.$1.push" ;;
71 remotes)
72 sed -ne '/^Push: */{
73 s///p
74 }' "$GIT_DIR/remotes/$1" ;;
76 die "internal error: get-remote-default-ref-for-push $1" ;;
77 esac
80 # Called from canon_refs_list_for_fetch -d "$remote", which
81 # is called from get_remote_default_refs_for_fetch to grok
82 # refspecs that are retrieved from the configuration, but not
83 # from get_remote_refs_for_fetch when it deals with refspecs
84 # supplied on the command line. $ls_remote_result has the list
85 # of refs available at remote.
87 # The first token returned is either "explicit" or "glob"; this
88 # is to help prevent randomly "globbed" ref from being chosen as
89 # a merge candidate
90 expand_refs_wildcard () {
91 echo "$ls_remote_result" |
92 git fetch--tool expand-refs-wildcard "-" "$@"
95 # Subroutine to canonicalize remote:local notation.
96 canon_refs_list_for_fetch () {
97 # If called from get_remote_default_refs_for_fetch
98 # leave the branches in branch.${curr_branch}.merge alone,
99 # or the first one otherwise; add prefix . to the rest
100 # to prevent the secondary branches to be merged by default.
101 merge_branches=
102 curr_branch=
103 if test "$1" = "-d"
104 then
105 shift ; remote="$1" ; shift
106 set $(expand_refs_wildcard "$remote" "$@")
107 is_explicit="$1"
108 shift
109 if test "$remote" = "$(get_default_remote)"
110 then
111 curr_branch=$(git symbolic-ref -q HEAD | \
112 sed -e 's|^refs/heads/||')
113 merge_branches=$(git config \
114 --get-all "branch.${curr_branch}.merge")
116 if test -z "$merge_branches" && test $is_explicit != explicit
117 then
118 merge_branches=..this.will.never.match.any.ref..
121 for ref
123 force=
124 case "$ref" in
126 ref=$(expr "z$ref" : 'z+\(.*\)')
127 force=+
129 esac
130 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
131 remote=$(expr "z$ref" : 'z\([^:]*\):')
132 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
133 dot_prefix=.
134 if test -z "$merge_branches"
135 then
136 merge_branches=$remote
137 dot_prefix=
138 else
139 for merge_branch in $merge_branches
141 [ "$remote" = "$merge_branch" ] &&
142 dot_prefix= && break
143 done
145 case "$remote" in
146 '' | HEAD ) remote=HEAD ;;
147 refs/*) ;;
148 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
149 *) remote="refs/heads/$remote" ;;
150 esac
151 case "$local" in
152 '') local= ;;
153 refs/*) ;;
154 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
155 *) local="refs/heads/$local" ;;
156 esac
158 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
159 then
160 git check-ref-format "$local_ref_name" ||
161 die "* refusing to create funny ref '$local_ref_name' locally"
163 echo "${dot_prefix}${force}${remote}:${local}"
164 done
167 # Returns list of src: (no store), or src:dst (store)
168 get_remote_default_refs_for_fetch () {
169 data_source=$(get_data_source "$1")
170 case "$data_source" in
172 echo "HEAD:" ;;
173 self)
174 canon_refs_list_for_fetch -d "$1" \
175 $(git for-each-ref --format='%(refname):')
177 config)
178 canon_refs_list_for_fetch -d "$1" \
179 $(git config --get-all "remote.$1.fetch") ;;
180 branches)
181 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
182 case "$remote_branch" in '') remote_branch=master ;; esac
183 echo "refs/heads/${remote_branch}:refs/heads/$1"
185 remotes)
186 canon_refs_list_for_fetch -d "$1" $(sed -ne '/^Pull: */{
187 s///p
188 }' "$GIT_DIR/remotes/$1")
191 die "internal error: get-remote-default-ref-for-fetch $1" ;;
192 esac
195 get_remote_refs_for_push () {
196 case "$#" in
197 0) die "internal error: get-remote-refs-for-push." ;;
198 1) get_remote_default_refs_for_push "$@" ;;
199 *) shift; echo "$@" ;;
200 esac
203 get_remote_refs_for_fetch () {
204 case "$#" in
206 die "internal error: get-remote-refs-for-fetch." ;;
208 get_remote_default_refs_for_fetch "$@" ;;
210 shift
211 tag_just_seen=
212 for ref
214 if test "$tag_just_seen"
215 then
216 echo "refs/tags/${ref}:refs/tags/${ref}"
217 tag_just_seen=
218 continue
219 else
220 case "$ref" in
221 tag)
222 tag_just_seen=yes
223 continue
225 esac
227 canon_refs_list_for_fetch "$ref"
228 done
230 esac
233 resolve_alternates () {
234 # original URL (xxx.git)
235 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
236 while read path
238 case "$path" in
239 \#* | '')
240 continue ;;
242 echo "$top_$path/" ;;
243 ../*)
244 # relative -- ugly but seems to work.
245 echo "$1/objects/$path/" ;;
247 # exit code may not be caught by the reader.
248 echo "bad alternate: $path"
249 exit 1 ;;
250 esac
251 done
254 get_uploadpack () {
255 data_source=$(get_data_source "$1")
256 case "$data_source" in
257 config)
258 uplp=$(git config --get "remote.$1.uploadpack")
259 echo ${uplp:-git-upload-pack}
262 echo "git-upload-pack"
264 esac