Shell utilities: Guard against expr' magic tokens.
[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[^/]*/\(.*\)')
ac4b0cff
JH
13 if test -f "$GIT_DIR/branches/$token"
14 then
15 echo branches-partial
16 else
17 echo ''
18 fi
19 ;;
20 *)
21 if test -f "$GIT_DIR/remotes/$1"
22 then
23 echo remotes
24 elif test -f "$GIT_DIR/branches/$1"
25 then
26 echo branches
27 else
28 echo ''
29 fi ;;
30 esac
31}
32
33get_remote_url () {
34 data_source=$(get_data_source "$1")
35 case "$data_source" in
36 '')
37 echo "$1" ;;
38 remotes)
39 sed -ne '/^URL: */{
40 s///p
41 q
42 }' "$GIT_DIR/remotes/$1" ;;
43 branches)
44 sed -e 's/#.*//' "$GIT_DIR/branches/$1" ;;
45 branches-partial)
f327dbce
MW
46 token=$(expr "z$1" : 'z\([^/]*\)/')
47 remainder=$(expr "z$1" : 'z[^/]*/\(.*\)')
ac4b0cff
JH
48 url=$(sed -e 's/#.*//' "$GIT_DIR/branches/$token")
49 echo "$url/$remainder"
50 ;;
51 *)
52 die "internal error: get-remote-url $1" ;;
53 esac
54}
55
56get_remote_default_refs_for_push () {
57 data_source=$(get_data_source "$1")
58 case "$data_source" in
59 '' | branches | branches-partial)
60 ;; # no default push mapping, just send matching refs.
61 remotes)
62 sed -ne '/^Push: */{
63 s///p
64 }' "$GIT_DIR/remotes/$1" ;;
65 *)
66 die "internal error: get-remote-default-ref-for-push $1" ;;
67 esac
68}
69
05dd8e2e 70# Subroutine to canonicalize remote:local notation.
ac4b0cff 71canon_refs_list_for_fetch () {
05dd8e2e
JH
72 # Leave only the first one alone; add prefix . to the rest
73 # to prevent the secondary branches to be merged by default.
74 dot_prefix=
ac4b0cff
JH
75 for ref
76 do
efe9bf0f
JH
77 force=
78 case "$ref" in
79 +*)
f327dbce 80 ref=$(expr "z$ref" : 'z\+\(.*\)')
efe9bf0f
JH
81 force=+
82 ;;
83 esac
f327dbce
MW
84 expr "z$ref" : 'z.*:' >/dev/null || ref="${ref}:"
85 remote=$(expr "z$ref" : 'z\([^:]*\):')
86 local=$(expr "z$ref" : 'z[^:]*:\(.*\)')
ac4b0cff
JH
87 case "$remote" in
88 '') remote=HEAD ;;
687b8be8
EW
89 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
90 heads/* | tags/* | remotes/* ) remote="refs/$remote" ;;
ac4b0cff
JH
91 *) remote="refs/heads/$remote" ;;
92 esac
93 case "$local" in
94 '') local= ;;
687b8be8
EW
95 refs/heads/* | refs/tags/* | refs/remotes/*) ;;
96 heads/* | tags/* | remotes/* ) local="refs/$local" ;;
ac4b0cff
JH
97 *) local="refs/heads/$local" ;;
98 esac
d8a1deec 99
f327dbce 100 if local_ref_name=$(expr "z$local" : 'zrefs/\(.*\)')
d8a1deec
JH
101 then
102 git-check-ref-format "$local_ref_name" ||
103 die "* refusing to create funny ref '$local_ref_name' locally"
104 fi
05dd8e2e
JH
105 echo "${dot_prefix}${force}${remote}:${local}"
106 dot_prefix=.
ac4b0cff
JH
107 done
108}
109
110# Returns list of src: (no store), or src:dst (store)
111get_remote_default_refs_for_fetch () {
112 data_source=$(get_data_source "$1")
113 case "$data_source" in
114 '' | branches-partial)
115 echo "HEAD:" ;;
116 branches)
117 remote_branch=$(sed -ne '/#/s/.*#//p' "$GIT_DIR/branches/$1")
118 case "$remote_branch" in '') remote_branch=master ;; esac
119 echo "refs/heads/${remote_branch}:refs/heads/$1"
120 ;;
121 remotes)
05dd8e2e
JH
122 # This prefixes the second and later default refspecs
123 # with a '.', to signal git-fetch to mark them
124 # not-for-merge.
ac4b0cff
JH
125 canon_refs_list_for_fetch $(sed -ne '/^Pull: */{
126 s///p
127 }' "$GIT_DIR/remotes/$1")
128 ;;
129 *)
130 die "internal error: get-remote-default-ref-for-push $1" ;;
131 esac
132}
133
134get_remote_refs_for_push () {
135 case "$#" in
136 0) die "internal error: get-remote-refs-for-push." ;;
137 1) get_remote_default_refs_for_push "$@" ;;
138 *) shift; echo "$@" ;;
139 esac
140}
141
142get_remote_refs_for_fetch () {
143 case "$#" in
144 0)
145 die "internal error: get-remote-refs-for-fetch." ;;
146 1)
147 get_remote_default_refs_for_fetch "$@" ;;
148 *)
149 shift
150 tag_just_seen=
151 for ref
152 do
153 if test "$tag_just_seen"
154 then
155 echo "refs/tags/${ref}:refs/tags/${ref}"
156 tag_just_seen=
157 continue
158 else
159 case "$ref" in
160 tag)
efe9bf0f 161 tag_just_seen=yes
ac4b0cff
JH
162 continue
163 ;;
164 esac
165 fi
efe9bf0f 166 canon_refs_list_for_fetch "$ref"
ac4b0cff
JH
167 done
168 ;;
169 esac
170}
4447badc
JH
171
172resolve_alternates () {
173 # original URL (xxx.git)
f327dbce 174 top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
4447badc
JH
175 while read path
176 do
177 case "$path" in
178 \#* | '')
179 continue ;;
180 /*)
181 echo "$top_$path/" ;;
182 ../*)
183 # relative -- ugly but seems to work.
184 echo "$1/objects/$path/" ;;
185 *)
186 # exit code may not be caught by the reader.
187 echo "bad alternate: $path"
188 exit 1 ;;
189 esac
190 done
191}