]> asedeno.scripts.mit.edu Git - git.git/blob - git-parse-remote.sh
Fetch: default remote repository from branch properties
[git.git] / git-parse-remote.sh
1 #!/bin/sh
2
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) || :;
6
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 ''
21                 fi
22                 ;;
23         *)
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
37 }
38
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"
49                 ;;
50         config)
51                 git-repo-config --get "remote.$1.url"
52                 ;;
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)
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"
65                 ;;
66         *)
67                 die "internal error: get-remote-url $1" ;;
68         esac
69 }
70
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}
75 }
76
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" ;;
88         *)
89                 die "internal error: get-remote-default-ref-for-push $1" ;;
90         esac
91 }
92
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
99         do
100                 force=
101                 case "$ref" in
102                 +*)
103                         ref=$(expr "z$ref" : 'z+\(.*\)')
104                         force=+
105                         ;;
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
122
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"
127                 fi
128                 echo "${dot_prefix}${force}${remote}:${local}"
129                 dot_prefix=.
130         done
131 }
132
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"
146                 ;;
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")
154                 ;;
155         *)
156                 die "internal error: get-remote-default-ref-for-push $1" ;;
157         esac
158 }
159
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
166 }
167
168 get_remote_refs_for_fetch () {
169         case "$#" in
170         0)
171             die "internal error: get-remote-refs-for-fetch." ;;
172         1)
173             get_remote_default_refs_for_fetch "$@" ;;
174         *)
175             shift
176             tag_just_seen=
177             for ref
178             do
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
189                         ;;
190                     esac
191                 fi
192                 canon_refs_list_for_fetch "$ref"
193             done
194             ;;
195         esac
196 }
197
198 resolve_alternates () {
199         # original URL (xxx.git)
200         top_=`expr "z$1" : 'z\([^:]*:/*[^/]*\)/'`
201         while read path
202         do
203                 case "$path" in
204                 \#* | '')
205                         continue ;;
206                 /*)
207                         echo "$top_$path/" ;;
208                 ../*)
209                         # relative -- ugly but seems to work.
210                         echo "$1/objects/$path/" ;;
211                 *)
212                         # exit code may not be caught by the reader.
213                         echo "bad alternate: $path"
214                         exit 1 ;;
215                 esac
216         done
217 }