Teach git list-objects logic not to follow gitlinks
[git/builtin-gsoc.git] / builtin-push.c
blobcb78401c946eac9019c2e4e953c3b87ef9e214f3
1 /*
2 * "git push"
3 */
4 #include "cache.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "builtin.h"
9 #define MAX_URI (16)
11 static const char push_usage[] = "git-push [--all] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]";
13 static int all, tags, force, thin = 1, verbose;
14 static const char *receivepack;
16 #define BUF_SIZE (2084)
17 static char buffer[BUF_SIZE];
19 static const char **refspec;
20 static int refspec_nr;
22 static void add_refspec(const char *ref)
24 int nr = refspec_nr + 1;
25 refspec = xrealloc(refspec, nr * sizeof(char *));
26 refspec[nr-1] = ref;
27 refspec_nr = nr;
30 static int expand_one_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data)
32 /* Ignore the "refs/" at the beginning of the refname */
33 ref += 5;
35 if (!prefixcmp(ref, "tags/"))
36 add_refspec(xstrdup(ref));
37 return 0;
40 static void expand_refspecs(void)
42 if (all) {
43 if (refspec_nr)
44 die("cannot mix '--all' and a refspec");
47 * No need to expand "--all" - we'll just use
48 * the "--all" flag to send-pack
50 return;
52 if (!tags)
53 return;
54 for_each_ref(expand_one_ref, NULL);
57 struct wildcard_cb {
58 const char *from_prefix;
59 int from_prefix_len;
60 const char *to_prefix;
61 int to_prefix_len;
62 int force;
65 static int expand_wildcard_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data)
67 struct wildcard_cb *cb = cb_data;
68 int len = strlen(ref);
69 char *expanded, *newref;
71 if (len < cb->from_prefix_len ||
72 memcmp(cb->from_prefix, ref, cb->from_prefix_len))
73 return 0;
74 expanded = xmalloc(len * 2 + cb->force +
75 (cb->to_prefix_len - cb->from_prefix_len) + 2);
76 newref = expanded + cb->force;
77 if (cb->force)
78 expanded[0] = '+';
79 memcpy(newref, ref, len);
80 newref[len] = ':';
81 memcpy(newref + len + 1, cb->to_prefix, cb->to_prefix_len);
82 strcpy(newref + len + 1 + cb->to_prefix_len,
83 ref + cb->from_prefix_len);
84 add_refspec(expanded);
85 return 0;
88 static int wildcard_ref(const char *ref)
90 int len;
91 const char *colon;
92 struct wildcard_cb cb;
94 memset(&cb, 0, sizeof(cb));
95 if (ref[0] == '+') {
96 cb.force = 1;
97 ref++;
99 len = strlen(ref);
100 colon = strchr(ref, ':');
101 if (! (colon && ref < colon &&
102 colon[-2] == '/' && colon[-1] == '*' &&
103 /* "<mine>/<asterisk>:<yours>/<asterisk>" is at least 7 bytes */
104 7 <= len &&
105 ref[len-2] == '/' && ref[len-1] == '*') )
106 return 0 ;
107 cb.from_prefix = ref;
108 cb.from_prefix_len = colon - ref - 1;
109 cb.to_prefix = colon + 1;
110 cb.to_prefix_len = len - (colon - ref) - 2;
111 for_each_ref(expand_wildcard_ref, &cb);
112 return 1;
115 static void set_refspecs(const char **refs, int nr)
117 if (nr) {
118 int i;
119 for (i = 0; i < nr; i++) {
120 const char *ref = refs[i];
121 if (!strcmp("tag", ref)) {
122 char *tag;
123 int len;
124 if (nr <= ++i)
125 die("tag shorthand without <tag>");
126 len = strlen(refs[i]) + 11;
127 tag = xmalloc(len);
128 strcpy(tag, "refs/tags/");
129 strcat(tag, refs[i]);
130 ref = tag;
132 else if (wildcard_ref(ref))
133 continue;
134 add_refspec(ref);
137 expand_refspecs();
140 static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
142 int n = 0;
143 FILE *f = fopen(git_path("remotes/%s", repo), "r");
144 int has_explicit_refspec = refspec_nr || all || tags;
146 if (!f)
147 return -1;
148 while (fgets(buffer, BUF_SIZE, f)) {
149 int is_refspec;
150 char *s, *p;
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;
161 /* Remove whitespace at the head.. */
162 while (isspace(*s))
163 s++;
164 if (!*s)
165 continue;
167 /* ..and at the end */
168 p = s + strlen(s);
169 while (isspace(p[-1]))
170 *--p = 0;
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);
178 else if (is_refspec && !has_explicit_refspec) {
179 if (!wildcard_ref(s))
180 add_refspec(xstrdup(s));
183 fclose(f);
184 if (!n)
185 die("remote '%s' has no URL", repo);
186 return n;
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;
196 static int get_remote_config(const char* key, const char* value)
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);
206 else if (config_get_refspecs &&
207 !strcmp(key + 7 + config_repo_len, ".push")) {
208 if (!wildcard_ref(value))
209 add_refspec(xstrdup(value));
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");
221 return 0;
224 static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI])
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);
233 git_config(get_remote_config);
234 return config_current_uri;
237 static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
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;
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;
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
275 static int read_config(const char *repo, const char *uri[MAX_URI])
277 int n;
279 if (*repo != '/') {
280 n = get_remotes_uri(repo, uri);
281 if (n > 0)
282 return n;
284 n = get_config_remotes_uri(repo, uri);
285 if (n > 0)
286 return n;
288 n = get_branches_uri(repo, uri);
289 if (n > 0)
290 return n;
293 uri[0] = repo;
294 return 1;
297 static int do_push(const char *repo)
299 const char *uri[MAX_URI];
300 int i, n, errs;
301 int common_argc;
302 const char **argv;
303 int argc;
305 n = read_config(repo, uri);
306 if (n <= 0)
307 die("bad repository '%s'", repo);
309 argv = xmalloc((refspec_nr + 10) * sizeof(char *));
310 argv[0] = "dummy-send-pack";
311 argc = 1;
312 if (all)
313 argv[argc++] = "--all";
314 if (force)
315 argv[argc++] = "--force";
316 if (receivepack)
317 argv[argc++] = receivepack;
318 common_argc = argc;
320 errs = 0;
321 for (i = 0; i < n; i++) {
322 int err;
323 int dest_argc = common_argc;
324 int dest_refspec_nr = refspec_nr;
325 const char **dest_refspec = refspec;
326 const char *dest = uri[i];
327 const char *sender = "send-pack";
328 if (!prefixcmp(dest, "http://") ||
329 !prefixcmp(dest, "https://"))
330 sender = "http-push";
331 else if (thin)
332 argv[dest_argc++] = "--thin";
333 argv[0] = sender;
334 argv[dest_argc++] = dest;
335 while (dest_refspec_nr--)
336 argv[dest_argc++] = *dest_refspec++;
337 argv[dest_argc] = NULL;
338 if (verbose)
339 fprintf(stderr, "Pushing to %s\n", dest);
340 err = run_command_v_opt(argv, RUN_GIT_CMD);
341 if (!err)
342 continue;
344 error("failed to push to '%s'", uri[i]);
345 switch (err) {
346 case -ERR_RUN_COMMAND_FORK:
347 error("unable to fork for %s", sender);
348 case -ERR_RUN_COMMAND_EXEC:
349 error("unable to exec %s", sender);
350 break;
351 case -ERR_RUN_COMMAND_WAITPID:
352 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
353 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
354 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
355 error("%s died with strange error", sender);
357 errs++;
359 return !!errs;
362 int cmd_push(int argc, const char **argv, const char *prefix)
364 int i;
365 const char *repo = "origin"; /* default repository */
367 for (i = 1; i < argc; i++) {
368 const char *arg = argv[i];
370 if (arg[0] != '-') {
371 repo = arg;
372 i++;
373 break;
375 if (!strcmp(arg, "-v")) {
376 verbose=1;
377 continue;
379 if (!prefixcmp(arg, "--repo=")) {
380 repo = arg+7;
381 continue;
383 if (!strcmp(arg, "--all")) {
384 all = 1;
385 continue;
387 if (!strcmp(arg, "--tags")) {
388 tags = 1;
389 continue;
391 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
392 force = 1;
393 continue;
395 if (!strcmp(arg, "--thin")) {
396 thin = 1;
397 continue;
399 if (!strcmp(arg, "--no-thin")) {
400 thin = 0;
401 continue;
403 if (!prefixcmp(arg, "--receive-pack=")) {
404 receivepack = arg;
405 continue;
407 if (!prefixcmp(arg, "--exec=")) {
408 receivepack = arg;
409 continue;
411 usage(push_usage);
413 set_refspecs(argv + i, argc - i);
414 return do_push(repo);