@matheusbsbs / matheus-bagliano / commits / 5751f49010
Fork 0

Move remote parsing into a library file out of builtin-push.

The new parser is different from the one in builtin-push in two ways: the default is to use the current branch's remote, if there is one, before "origin"; and config is used in preference to remotes. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Daniel Barkalow committed May 12, 2007 at 11:45 UTC 5751f49010ec54164b93529e31165e71f5996856
4 files changed +264 -172
Makefile
+3 -2
@@ -296,7 +296,8 @@ LIB_H = \
296 diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \
297 run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
298 tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
299 - utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h mailmap.h
299 + utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h \
300 + mailmap.h remote.h
301
302 DIFF_OBJS = \
303 diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -318,7 +319,7 @@ LIB_OBJS = \
319 write_or_die.o trace.o list-objects.o grep.o match-trees.o \
320 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
321 color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
321 - convert.o attr.o decorate.o progress.o mailmap.o symlinks.o
322 + convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o
323
324 BUILTIN_OBJS = \
325 builtin-add.o \
builtin-push.c
+20 -170
@@ -5,17 +5,13 @@
5 #include "refs.h"
6 #include "run-command.h"
7 #include "builtin.h"
8 -
9 -#define MAX_URI (16)
8 +#include "remote.h"
9
10 static const char push_usage[] = "git-push [--all] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]";
11
12 static int all, tags, force, thin = 1, verbose;
13 static const char *receivepack;
14
16 -#define BUF_SIZE (2084)
17 -static char buffer[BUF_SIZE];
18 -
15 static const char **refspec;
16 static int refspec_nr;
17
@@ -137,175 +133,29 @@ static void set_refspecs(const char **refs, int nr)
133 expand_refspecs();
134 }
135
140 -static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
141 -{
142 - int n = 0;
143 - FILE *f = fopen(git_path("remotes/%s", repo), "r");
144 - int has_explicit_refspec = refspec_nr || all || tags;
145 -
146 - if (!f)
147 - return -1;
148 - while (fgets(buffer, BUF_SIZE, f)) {
149 - int is_refspec;
150 - char *s, *p;
151 -
152 - if (!prefixcmp(buffer, "URL:")) {
153 - is_refspec = 0;
154 - s = buffer + 4;
155 - } else if (!prefixcmp(buffer, "Push:")) {
156 - is_refspec = 1;
157 - s = buffer + 5;
158 - } else
159 - continue;
160 -
161 - /* Remove whitespace at the head.. */
162 - while (isspace(*s))
163 - s++;
164 - if (!*s)
165 - continue;
166 -
167 - /* ..and at the end */
168 - p = s + strlen(s);
169 - while (isspace(p[-1]))
170 - *--p = 0;
171 -
172 - if (!is_refspec) {
173 - if (n < MAX_URI)
174 - uri[n++] = xstrdup(s);
175 - else
176 - error("more than %d URL's specified, ignoring the rest", MAX_URI);
177 - }
178 - else if (is_refspec && !has_explicit_refspec) {
179 - if (!wildcard_ref(s))
180 - add_refspec(xstrdup(s));
181 - }
182 - }
183 - fclose(f);
184 - if (!n)
185 - die("remote '%s' has no URL", repo);
186 - return n;
187 -}
188 -
189 -static const char **config_uri;
190 -static const char *config_repo;
191 -static int config_repo_len;
192 -static int config_current_uri;
193 -static int config_get_refspecs;
194 -static int config_get_receivepack;
195 -
196 -static int get_remote_config(const char* key, const char* value)
197 -{
198 - if (!prefixcmp(key, "remote.") &&
199 - !strncmp(key + 7, config_repo, config_repo_len)) {
200 - if (!strcmp(key + 7 + config_repo_len, ".url")) {
201 - if (config_current_uri < MAX_URI)
202 - config_uri[config_current_uri++] = xstrdup(value);
203 - else
204 - error("more than %d URL's specified, ignoring the rest", MAX_URI);
205 - }
206 - else if (config_get_refspecs &&
207 - !strcmp(key + 7 + config_repo_len, ".push")) {
208 - if (!wildcard_ref(value))
209 - add_refspec(xstrdup(value));
210 - }
211 - else if (config_get_receivepack &&
212 - !strcmp(key + 7 + config_repo_len, ".receivepack")) {
213 - if (!receivepack) {
214 - char *rp = xmalloc(strlen(value) + 16);
215 - sprintf(rp, "--receive-pack=%s", value);
216 - receivepack = rp;
217 - } else
218 - error("more than one receivepack given, using the first");
219 - }
220 - }
221 - return 0;
222 -}
223 -
224 -static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI])
225 -{
226 - config_repo_len = strlen(repo);
227 - config_repo = repo;
228 - config_current_uri = 0;
229 - config_uri = uri;
230 - config_get_refspecs = !(refspec_nr || all || tags);
231 - config_get_receivepack = (receivepack == NULL);
232 -
233 - git_config(get_remote_config);
234 - return config_current_uri;
235 -}
236 -
237 -static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
238 -{
239 - const char *slash = strchr(repo, '/');
240 - int n = slash ? slash - repo : 1000;
241 - FILE *f = fopen(git_path("branches/%.*s", n, repo), "r");
242 - char *s, *p;
243 - int len;
244 -
245 - if (!f)
246 - return 0;
247 - s = fgets(buffer, BUF_SIZE, f);
248 - fclose(f);
249 - if (!s)
250 - return 0;
251 - while (isspace(*s))
252 - s++;
253 - if (!*s)
254 - return 0;
255 - p = s + strlen(s);
256 - while (isspace(p[-1]))
257 - *--p = 0;
258 - len = p - s;
259 - if (slash)
260 - len += strlen(slash);
261 - p = xmalloc(len + 1);
262 - strcpy(p, s);
263 - if (slash)
264 - strcat(p, slash);
265 - uri[0] = p;
266 - return 1;
267 -}
268 -
269 -/*
270 - * Read remotes and branches file, fill the push target URI
271 - * list. If there is no command line refspecs, read Push: lines
272 - * to set up the *refspec list as well.
273 - * return the number of push target URIs
274 - */
275 -static int read_config(const char *repo, const char *uri[MAX_URI])
276 -{
277 - int n;
278 -
279 - if (*repo != '/') {
280 - n = get_remotes_uri(repo, uri);
281 - if (n > 0)
282 - return n;
283 -
284 - n = get_config_remotes_uri(repo, uri);
285 - if (n > 0)
286 - return n;
287 -
288 - n = get_branches_uri(repo, uri);
289 - if (n > 0)
290 - return n;
291 - }
292 -
293 - uri[0] = repo;
294 - return 1;
295 -}
296 -
136 static int do_push(const char *repo)
137 {
299 - const char *uri[MAX_URI];
300 - int i, n, errs;
138 + int i, errs;
139 int common_argc;
140 const char **argv;
141 int argc;
142 + struct remote *remote = remote_get(repo);
143
305 - n = read_config(repo, uri);
306 - if (n <= 0)
144 + if (!remote)
145 die("bad repository '%s'", repo);
146
147 + if (remote->receivepack) {
148 + char *rp = xmalloc(strlen(remote->receivepack) + 16);
149 + sprintf(rp, "--receive-pack=%s", remote->receivepack);
150 + receivepack = rp;
151 + }
152 + if (!refspec && !all && !tags && remote->push_refspec_nr) {
153 + for (i = 0; i < remote->push_refspec_nr; i++) {
154 + if (!wildcard_ref(remote->push_refspec[i]))
155 + add_refspec(remote->push_refspec[i]);
156 + }
157 + }
158 +
159 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
160 argv[0] = "dummy-send-pack";
161 argc = 1;
@@ -318,12 +168,12 @@ static int do_push(const char *repo)
168 common_argc = argc;
169
170 errs = 0;
321 - for (i = 0; i < n; i++) {
171 + for (i = 0; i < remote->uri_nr; i++) {
172 int err;
173 int dest_argc = common_argc;
174 int dest_refspec_nr = refspec_nr;
175 const char **dest_refspec = refspec;
326 - const char *dest = uri[i];
176 + const char *dest = remote->uri[i];
177 const char *sender = "send-pack";
178 if (!prefixcmp(dest, "http://") ||
179 !prefixcmp(dest, "https://"))
@@ -341,7 +191,7 @@ static int do_push(const char *repo)
191 if (!err)
192 continue;
193
344 - error("failed to push to '%s'", uri[i]);
194 + error("failed to push to '%s'", remote->uri[i]);
195 switch (err) {
196 case -ERR_RUN_COMMAND_FORK:
197 error("unable to fork for %s", sender);
@@ -362,7 +212,7 @@ static int do_push(const char *repo)
212 int cmd_push(int argc, const char **argv, const char *prefix)
213 {
214 int i;
365 - const char *repo = "origin"; /* default repository */
215 + const char *repo = NULL; /* default repository */
216
217 for (i = 1; i < argc; i++) {
218 const char *arg = argv[i];
remote.c new
+223
@@ -0,0 +1,223 @@
1 +#include "cache.h"
2 +#include "remote.h"
3 +#include "refs.h"
4 +
5 +static struct remote **remotes;
6 +static int allocated_remotes;
7 +
8 +#define BUF_SIZE (2048)
9 +static char buffer[BUF_SIZE];
10 +
11 +static void add_push_refspec(struct remote *remote, const char *ref)
12 +{
13 + int nr = remote->push_refspec_nr + 1;
14 + remote->push_refspec =
15 + xrealloc(remote->push_refspec, nr * sizeof(char *));
16 + remote->push_refspec[nr-1] = ref;
17 + remote->push_refspec_nr = nr;
18 +}
19 +
20 +static void add_uri(struct remote *remote, const char *uri)
21 +{
22 + int nr = remote->uri_nr + 1;
23 + remote->uri =
24 + xrealloc(remote->uri, nr * sizeof(char *));
25 + remote->uri[nr-1] = uri;
26 + remote->uri_nr = nr;
27 +}
28 +
29 +static struct remote *make_remote(const char *name, int len)
30 +{
31 + int i, empty = -1;
32 +
33 + for (i = 0; i < allocated_remotes; i++) {
34 + if (!remotes[i]) {
35 + if (empty < 0)
36 + empty = i;
37 + } else {
38 + if (len ? (!strncmp(name, remotes[i]->name, len) &&
39 + !remotes[i]->name[len]) :
40 + !strcmp(name, remotes[i]->name))
41 + return remotes[i];
42 + }
43 + }
44 +
45 + if (empty < 0) {
46 + empty = allocated_remotes;
47 + allocated_remotes += allocated_remotes ? allocated_remotes : 1;
48 + remotes = xrealloc(remotes,
49 + sizeof(*remotes) * allocated_remotes);
50 + memset(remotes + empty, 0,
51 + (allocated_remotes - empty) * sizeof(*remotes));
52 + }
53 + remotes[empty] = xcalloc(1, sizeof(struct remote));
54 + if (len)
55 + remotes[empty]->name = xstrndup(name, len);
56 + else
57 + remotes[empty]->name = xstrdup(name);
58 + return remotes[empty];
59 +}
60 +
61 +static void read_remotes_file(struct remote *remote)
62 +{
63 + FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
64 +
65 + if (!f)
66 + return;
67 + while (fgets(buffer, BUF_SIZE, f)) {
68 + int value_list;
69 + char *s, *p;
70 +
71 + if (!prefixcmp(buffer, "URL:")) {
72 + value_list = 0;
73 + s = buffer + 4;
74 + } else if (!prefixcmp(buffer, "Push:")) {
75 + value_list = 1;
76 + s = buffer + 5;
77 + } else
78 + continue;
79 +
80 + while (isspace(*s))
81 + s++;
82 + if (!*s)
83 + continue;
84 +
85 + p = s + strlen(s);
86 + while (isspace(p[-1]))
87 + *--p = 0;
88 +
89 + switch (value_list) {
90 + case 0:
91 + add_uri(remote, xstrdup(s));
92 + break;
93 + case 1:
94 + add_push_refspec(remote, xstrdup(s));
95 + break;
96 + }
97 + }
98 + fclose(f);
99 +}
100 +
101 +static void read_branches_file(struct remote *remote)
102 +{
103 + const char *slash = strchr(remote->name, '/');
104 + int n = slash ? slash - remote->name : 1000;
105 + FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
106 + char *s, *p;
107 + int len;
108 +
109 + if (!f)
110 + return;
111 + s = fgets(buffer, BUF_SIZE, f);
112 + fclose(f);
113 + if (!s)
114 + return;
115 + while (isspace(*s))
116 + s++;
117 + if (!*s)
118 + return;
119 + p = s + strlen(s);
120 + while (isspace(p[-1]))
121 + *--p = 0;
122 + len = p - s;
123 + if (slash)
124 + len += strlen(slash);
125 + p = xmalloc(len + 1);
126 + strcpy(p, s);
127 + if (slash)
128 + strcat(p, slash);
129 + add_uri(remote, p);
130 +}
131 +
132 +static char *default_remote_name = NULL;
133 +static const char *current_branch = NULL;
134 +static int current_branch_len = 0;
135 +
136 +static int handle_config(const char *key, const char *value)
137 +{
138 + const char *name;
139 + const char *subkey;
140 + struct remote *remote;
141 + if (!prefixcmp(key, "branch.") && current_branch &&
142 + !strncmp(key + 7, current_branch, current_branch_len) &&
143 + !strcmp(key + 7 + current_branch_len, ".remote")) {
144 + free(default_remote_name);
145 + default_remote_name = xstrdup(value);
146 + }
147 + if (prefixcmp(key, "remote."))
148 + return 0;
149 + name = key + 7;
150 + subkey = strrchr(name, '.');
151 + if (!subkey)
152 + return error("Config with no key for remote %s", name);
153 + if (*subkey == '/') {
154 + warning("Config remote shorthand cannot begin with '/': %s", name);
155 + return 0;
156 + }
157 + remote = make_remote(name, subkey - name);
158 + if (!value) {
159 + /* if we ever have a boolean variable, e.g. "remote.*.disabled"
160 + * [remote "frotz"]
161 + * disabled
162 + * is a valid way to set it to true; we get NULL in value so
163 + * we need to handle it here.
164 + *
165 + * if (!strcmp(subkey, ".disabled")) {
166 + * val = git_config_bool(key, value);
167 + * return 0;
168 + * } else
169 + *
170 + */
171 + return 0; /* ignore unknown booleans */
172 + }
173 + if (!strcmp(subkey, ".url")) {
174 + add_uri(remote, xstrdup(value));
175 + } else if (!strcmp(subkey, ".push")) {
176 + add_push_refspec(remote, xstrdup(value));
177 + } else if (!strcmp(subkey, ".receivepack")) {
178 + if (!remote->receivepack)
179 + remote->receivepack = xstrdup(value);
180 + else
181 + error("more than one receivepack given, using the first");
182 + }
183 + return 0;
184 +}
185 +
186 +static void read_config(void)
187 +{
188 + unsigned char sha1[20];
189 + const char *head_ref;
190 + int flag;
191 + if (default_remote_name) // did this already
192 + return;
193 + default_remote_name = xstrdup("origin");
194 + current_branch = NULL;
195 + head_ref = resolve_ref("HEAD", sha1, 0, &flag);
196 + if (head_ref && (flag & REF_ISSYMREF) &&
197 + !prefixcmp(head_ref, "refs/heads/")) {
198 + current_branch = head_ref + strlen("refs/heads/");
199 + current_branch_len = strlen(current_branch);
200 + }
201 + git_config(handle_config);
202 +}
203 +
204 +struct remote *remote_get(const char *name)
205 +{
206 + struct remote *ret;
207 +
208 + read_config();
209 + if (!name)
210 + name = default_remote_name;
211 + ret = make_remote(name, 0);
212 + if (name[0] != '/') {
213 + if (!ret->uri)
214 + read_remotes_file(ret);
215 + if (!ret->uri)
216 + read_branches_file(ret);
217 + }
218 + if (!ret->uri)
219 + add_uri(ret, name);
220 + if (!ret->uri)
221 + return NULL;
222 + return ret;
223 +}
remote.h new
+18
@@ -0,0 +1,18 @@
1 +#ifndef REMOTE_H
2 +#define REMOTE_H
3 +
4 +struct remote {
5 + const char *name;
6 +
7 + const char **uri;
8 + int uri_nr;
9 +
10 + const char **push_refspec;
11 + int push_refspec_nr;
12 +
13 + const char *receivepack;
14 +};
15 +
16 +struct remote *remote_get(const char *name);
17 +
18 +#endif