Change check_ref_format() to take a flags argument
[git/git.git] / transport.c
1 #include "cache.h"
2 #include "transport.h"
3 #include "run-command.h"
4 #include "pkt-line.h"
5 #include "fetch-pack.h"
6 #include "send-pack.h"
7 #include "walker.h"
8 #include "bundle.h"
9 #include "dir.h"
10 #include "refs.h"
11 #include "branch.h"
12 #include "url.h"
13 #include "submodule.h"
14
15 /* rsync support */
16
17 /*
18 * We copy packed-refs and refs/ into a temporary file, then read the
19 * loose refs recursively (sorting whenever possible), and then inserting
20 * those packed refs that are not yet in the list (not validating, but
21 * assuming that the file is sorted).
22 *
23 * Appears refactoring this from refs.c is too cumbersome.
24 */
25
26 static int str_cmp(const void *a, const void *b)
27 {
28 const char *s1 = a;
29 const char *s2 = b;
30
31 return strcmp(s1, s2);
32 }
33
34 /* path->buf + name_offset is expected to point to "refs/" */
35
36 static int read_loose_refs(struct strbuf *path, int name_offset,
37 struct ref **tail)
38 {
39 DIR *dir = opendir(path->buf);
40 struct dirent *de;
41 struct {
42 char **entries;
43 int nr, alloc;
44 } list;
45 int i, pathlen;
46
47 if (!dir)
48 return -1;
49
50 memset (&list, 0, sizeof(list));
51
52 while ((de = readdir(dir))) {
53 if (is_dot_or_dotdot(de->d_name))
54 continue;
55 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
56 list.entries[list.nr++] = xstrdup(de->d_name);
57 }
58 closedir(dir);
59
60 /* sort the list */
61
62 qsort(list.entries, list.nr, sizeof(char *), str_cmp);
63
64 pathlen = path->len;
65 strbuf_addch(path, '/');
66
67 for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
68 strbuf_addstr(path, list.entries[i]);
69 if (read_loose_refs(path, name_offset, tail)) {
70 int fd = open(path->buf, O_RDONLY);
71 char buffer[40];
72 struct ref *next;
73
74 if (fd < 0)
75 continue;
76 next = alloc_ref(path->buf + name_offset);
77 if (read_in_full(fd, buffer, 40) != 40 ||
78 get_sha1_hex(buffer, next->old_sha1)) {
79 close(fd);
80 free(next);
81 continue;
82 }
83 close(fd);
84 (*tail)->next = next;
85 *tail = next;
86 }
87 }
88 strbuf_setlen(path, pathlen);
89
90 for (i = 0; i < list.nr; i++)
91 free(list.entries[i]);
92 free(list.entries);
93
94 return 0;
95 }
96
97 /* insert the packed refs for which no loose refs were found */
98
99 static void insert_packed_refs(const char *packed_refs, struct ref **list)
100 {
101 FILE *f = fopen(packed_refs, "r");
102 static char buffer[PATH_MAX];
103
104 if (!f)
105 return;
106
107 for (;;) {
108 int cmp = cmp, len;
109
110 if (!fgets(buffer, sizeof(buffer), f)) {
111 fclose(f);
112 return;
113 }
114
115 if (hexval(buffer[0]) > 0xf)
116 continue;
117 len = strlen(buffer);
118 if (len && buffer[len - 1] == '\n')
119 buffer[--len] = '\0';
120 if (len < 41)
121 continue;
122 while ((*list)->next &&
123 (cmp = strcmp(buffer + 41,
124 (*list)->next->name)) > 0)
125 list = &(*list)->next;
126 if (!(*list)->next || cmp < 0) {
127 struct ref *next = alloc_ref(buffer + 41);
128 buffer[40] = '\0';
129 if (get_sha1_hex(buffer, next->old_sha1)) {
130 warning ("invalid SHA-1: %s", buffer);
131 free(next);
132 continue;
133 }
134 next->next = (*list)->next;
135 (*list)->next = next;
136 list = &(*list)->next;
137 }
138 }
139 }
140
141 static void set_upstreams(struct transport *transport, struct ref *refs,
142 int pretend)
143 {
144 struct ref *ref;
145 for (ref = refs; ref; ref = ref->next) {
146 const char *localname;
147 const char *tmp;
148 const char *remotename;
149 unsigned char sha[20];
150 int flag = 0;
151 /*
152 * Check suitability for tracking. Must be successful /
153 * already up-to-date ref create/modify (not delete).
154 */
155 if (ref->status != REF_STATUS_OK &&
156 ref->status != REF_STATUS_UPTODATE)
157 continue;
158 if (!ref->peer_ref)
159 continue;
160 if (is_null_sha1(ref->new_sha1))
161 continue;
162
163 /* Follow symbolic refs (mainly for HEAD). */
164 localname = ref->peer_ref->name;
165 remotename = ref->name;
166 tmp = resolve_ref(localname, sha, 1, &flag);
167 if (tmp && flag & REF_ISSYMREF &&
168 !prefixcmp(tmp, "refs/heads/"))
169 localname = tmp;
170
171 /* Both source and destination must be local branches. */
172 if (!localname || prefixcmp(localname, "refs/heads/"))
173 continue;
174 if (!remotename || prefixcmp(remotename, "refs/heads/"))
175 continue;
176
177 if (!pretend)
178 install_branch_config(BRANCH_CONFIG_VERBOSE,
179 localname + 11, transport->remote->name,
180 remotename);
181 else
182 printf("Would set upstream of '%s' to '%s' of '%s'\n",
183 localname + 11, remotename + 11,
184 transport->remote->name);
185 }
186 }
187
188 static const char *rsync_url(const char *url)
189 {
190 return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
191 }
192
193 static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
194 {
195 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
196 struct ref dummy = {NULL}, *tail = &dummy;
197 struct child_process rsync;
198 const char *args[5];
199 int temp_dir_len;
200
201 if (for_push)
202 return NULL;
203
204 /* copy the refs to the temporary directory */
205
206 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
207 if (!mkdtemp(temp_dir.buf))
208 die_errno ("Could not make temporary directory");
209 temp_dir_len = temp_dir.len;
210
211 strbuf_addstr(&buf, rsync_url(transport->url));
212 strbuf_addstr(&buf, "/refs");
213
214 memset(&rsync, 0, sizeof(rsync));
215 rsync.argv = args;
216 rsync.stdout_to_stderr = 1;
217 args[0] = "rsync";
218 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
219 args[2] = buf.buf;
220 args[3] = temp_dir.buf;
221 args[4] = NULL;
222
223 if (run_command(&rsync))
224 die ("Could not run rsync to get refs");
225
226 strbuf_reset(&buf);
227 strbuf_addstr(&buf, rsync_url(transport->url));
228 strbuf_addstr(&buf, "/packed-refs");
229
230 args[2] = buf.buf;
231
232 if (run_command(&rsync))
233 die ("Could not run rsync to get refs");
234
235 /* read the copied refs */
236
237 strbuf_addstr(&temp_dir, "/refs");
238 read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
239 strbuf_setlen(&temp_dir, temp_dir_len);
240
241 tail = &dummy;
242 strbuf_addstr(&temp_dir, "/packed-refs");
243 insert_packed_refs(temp_dir.buf, &tail);
244 strbuf_setlen(&temp_dir, temp_dir_len);
245
246 if (remove_dir_recursively(&temp_dir, 0))
247 warning ("Error removing temporary directory %s.",
248 temp_dir.buf);
249
250 strbuf_release(&buf);
251 strbuf_release(&temp_dir);
252
253 return dummy.next;
254 }
255
256 static int fetch_objs_via_rsync(struct transport *transport,
257 int nr_objs, struct ref **to_fetch)
258 {
259 struct strbuf buf = STRBUF_INIT;
260 struct child_process rsync;
261 const char *args[8];
262 int result;
263
264 strbuf_addstr(&buf, rsync_url(transport->url));
265 strbuf_addstr(&buf, "/objects/");
266
267 memset(&rsync, 0, sizeof(rsync));
268 rsync.argv = args;
269 rsync.stdout_to_stderr = 1;
270 args[0] = "rsync";
271 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
272 args[2] = "--ignore-existing";
273 args[3] = "--exclude";
274 args[4] = "info";
275 args[5] = buf.buf;
276 args[6] = get_object_directory();
277 args[7] = NULL;
278
279 /* NEEDSWORK: handle one level of alternates */
280 result = run_command(&rsync);
281
282 strbuf_release(&buf);
283
284 return result;
285 }
286
287 static int write_one_ref(const char *name, const unsigned char *sha1,
288 int flags, void *data)
289 {
290 struct strbuf *buf = data;
291 int len = buf->len;
292 FILE *f;
293
294 /* when called via for_each_ref(), flags is non-zero */
295 if (flags && prefixcmp(name, "refs/heads/") &&
296 prefixcmp(name, "refs/tags/"))
297 return 0;
298
299 strbuf_addstr(buf, name);
300 if (safe_create_leading_directories(buf->buf) ||
301 !(f = fopen(buf->buf, "w")) ||
302 fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
303 fclose(f))
304 return error("problems writing temporary file %s", buf->buf);
305 strbuf_setlen(buf, len);
306 return 0;
307 }
308
309 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
310 int refspec_nr, const char **refspec)
311 {
312 int i;
313
314 for (i = 0; i < refspec_nr; i++) {
315 unsigned char sha1[20];
316 char *ref;
317
318 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
319 return error("Could not get ref %s", refspec[i]);
320
321 if (write_one_ref(ref, sha1, 0, temp_dir)) {
322 free(ref);
323 return -1;
324 }
325 free(ref);
326 }
327 return 0;
328 }
329
330 static int rsync_transport_push(struct transport *transport,
331 int refspec_nr, const char **refspec, int flags)
332 {
333 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
334 int result = 0, i;
335 struct child_process rsync;
336 const char *args[10];
337
338 if (flags & TRANSPORT_PUSH_MIRROR)
339 return error("rsync transport does not support mirror mode");
340
341 /* first push the objects */
342
343 strbuf_addstr(&buf, rsync_url(transport->url));
344 strbuf_addch(&buf, '/');
345
346 memset(&rsync, 0, sizeof(rsync));
347 rsync.argv = args;
348 rsync.stdout_to_stderr = 1;
349 i = 0;
350 args[i++] = "rsync";
351 args[i++] = "-a";
352 if (flags & TRANSPORT_PUSH_DRY_RUN)
353 args[i++] = "--dry-run";
354 if (transport->verbose > 0)
355 args[i++] = "-v";
356 args[i++] = "--ignore-existing";
357 args[i++] = "--exclude";
358 args[i++] = "info";
359 args[i++] = get_object_directory();
360 args[i++] = buf.buf;
361 args[i++] = NULL;
362
363 if (run_command(&rsync))
364 return error("Could not push objects to %s",
365 rsync_url(transport->url));
366
367 /* copy the refs to the temporary directory; they could be packed. */
368
369 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
370 if (!mkdtemp(temp_dir.buf))
371 die_errno ("Could not make temporary directory");
372 strbuf_addch(&temp_dir, '/');
373
374 if (flags & TRANSPORT_PUSH_ALL) {
375 if (for_each_ref(write_one_ref, &temp_dir))
376 return -1;
377 } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
378 return -1;
379
380 i = 2;
381 if (flags & TRANSPORT_PUSH_DRY_RUN)
382 args[i++] = "--dry-run";
383 if (!(flags & TRANSPORT_PUSH_FORCE))
384 args[i++] = "--ignore-existing";
385 args[i++] = temp_dir.buf;
386 args[i++] = rsync_url(transport->url);
387 args[i++] = NULL;
388 if (run_command(&rsync))
389 result = error("Could not push to %s",
390 rsync_url(transport->url));
391
392 if (remove_dir_recursively(&temp_dir, 0))
393 warning ("Could not remove temporary directory %s.",
394 temp_dir.buf);
395
396 strbuf_release(&buf);
397 strbuf_release(&temp_dir);
398
399 return result;
400 }
401
402 struct bundle_transport_data {
403 int fd;
404 struct bundle_header header;
405 };
406
407 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
408 {
409 struct bundle_transport_data *data = transport->data;
410 struct ref *result = NULL;
411 int i;
412
413 if (for_push)
414 return NULL;
415
416 if (data->fd > 0)
417 close(data->fd);
418 data->fd = read_bundle_header(transport->url, &data->header);
419 if (data->fd < 0)
420 die ("Could not read bundle '%s'.", transport->url);
421 for (i = 0; i < data->header.references.nr; i++) {
422 struct ref_list_entry *e = data->header.references.list + i;
423 struct ref *ref = alloc_ref(e->name);
424 hashcpy(ref->old_sha1, e->sha1);
425 ref->next = result;
426 result = ref;
427 }
428 return result;
429 }
430
431 static int fetch_refs_from_bundle(struct transport *transport,
432 int nr_heads, struct ref **to_fetch)
433 {
434 struct bundle_transport_data *data = transport->data;
435 return unbundle(&data->header, data->fd);
436 }
437
438 static int close_bundle(struct transport *transport)
439 {
440 struct bundle_transport_data *data = transport->data;
441 if (data->fd > 0)
442 close(data->fd);
443 free(data);
444 return 0;
445 }
446
447 struct git_transport_data {
448 struct git_transport_options options;
449 struct child_process *conn;
450 int fd[2];
451 unsigned got_remote_heads : 1;
452 struct extra_have_objects extra_have;
453 };
454
455 static int set_git_option(struct git_transport_options *opts,
456 const char *name, const char *value)
457 {
458 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
459 opts->uploadpack = value;
460 return 0;
461 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
462 opts->receivepack = value;
463 return 0;
464 } else if (!strcmp(name, TRANS_OPT_THIN)) {
465 opts->thin = !!value;
466 return 0;
467 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
468 opts->followtags = !!value;
469 return 0;
470 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
471 opts->keep = !!value;
472 return 0;
473 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
474 if (!value)
475 opts->depth = 0;
476 else
477 opts->depth = atoi(value);
478 return 0;
479 }
480 return 1;
481 }
482
483 static int connect_setup(struct transport *transport, int for_push, int verbose)
484 {
485 struct git_transport_data *data = transport->data;
486
487 if (data->conn)
488 return 0;
489
490 data->conn = git_connect(data->fd, transport->url,
491 for_push ? data->options.receivepack :
492 data->options.uploadpack,
493 verbose ? CONNECT_VERBOSE : 0);
494
495 return 0;
496 }
497
498 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
499 {
500 struct git_transport_data *data = transport->data;
501 struct ref *refs;
502
503 connect_setup(transport, for_push, 0);
504 get_remote_heads(data->fd[0], &refs, 0, NULL,
505 for_push ? REF_NORMAL : 0, &data->extra_have);
506 data->got_remote_heads = 1;
507
508 return refs;
509 }
510
511 static int fetch_refs_via_pack(struct transport *transport,
512 int nr_heads, struct ref **to_fetch)
513 {
514 struct git_transport_data *data = transport->data;
515 char **heads = xmalloc(nr_heads * sizeof(*heads));
516 char **origh = xmalloc(nr_heads * sizeof(*origh));
517 const struct ref *refs;
518 char *dest = xstrdup(transport->url);
519 struct fetch_pack_args args;
520 int i;
521 struct ref *refs_tmp = NULL;
522
523 memset(&args, 0, sizeof(args));
524 args.uploadpack = data->options.uploadpack;
525 args.keep_pack = data->options.keep;
526 args.lock_pack = 1;
527 args.use_thin_pack = data->options.thin;
528 args.include_tag = data->options.followtags;
529 args.verbose = (transport->verbose > 0);
530 args.quiet = (transport->verbose < 0);
531 args.no_progress = !transport->progress;
532 args.depth = data->options.depth;
533
534 for (i = 0; i < nr_heads; i++)
535 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
536
537 if (!data->got_remote_heads) {
538 connect_setup(transport, 0, 0);
539 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
540 data->got_remote_heads = 1;
541 }
542
543 refs = fetch_pack(&args, data->fd, data->conn,
544 refs_tmp ? refs_tmp : transport->remote_refs,
545 dest, nr_heads, heads, &transport->pack_lockfile);
546 close(data->fd[0]);
547 close(data->fd[1]);
548 if (finish_connect(data->conn))
549 refs = NULL;
550 data->conn = NULL;
551 data->got_remote_heads = 0;
552
553 free_refs(refs_tmp);
554
555 for (i = 0; i < nr_heads; i++)
556 free(origh[i]);
557 free(origh);
558 free(heads);
559 free(dest);
560 return (refs ? 0 : -1);
561 }
562
563 static int push_had_errors(struct ref *ref)
564 {
565 for (; ref; ref = ref->next) {
566 switch (ref->status) {
567 case REF_STATUS_NONE:
568 case REF_STATUS_UPTODATE:
569 case REF_STATUS_OK:
570 break;
571 default:
572 return 1;
573 }
574 }
575 return 0;
576 }
577
578 int transport_refs_pushed(struct ref *ref)
579 {
580 for (; ref; ref = ref->next) {
581 switch(ref->status) {
582 case REF_STATUS_NONE:
583 case REF_STATUS_UPTODATE:
584 break;
585 default:
586 return 1;
587 }
588 }
589 return 0;
590 }
591
592 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
593 {
594 struct refspec rs;
595
596 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
597 return;
598
599 rs.src = ref->name;
600 rs.dst = NULL;
601
602 if (!remote_find_tracking(remote, &rs)) {
603 if (verbose)
604 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
605 if (ref->deletion) {
606 delete_ref(rs.dst, NULL, 0);
607 } else
608 update_ref("update by push", rs.dst,
609 ref->new_sha1, NULL, 0, 0);
610 free(rs.dst);
611 }
612 }
613
614 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
615 {
616 if (porcelain) {
617 if (from)
618 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
619 else
620 fprintf(stdout, "%c\t:%s\t", flag, to->name);
621 if (msg)
622 fprintf(stdout, "%s (%s)\n", summary, msg);
623 else
624 fprintf(stdout, "%s\n", summary);
625 } else {
626 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
627 if (from)
628 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
629 else
630 fputs(prettify_refname(to->name), stderr);
631 if (msg) {
632 fputs(" (", stderr);
633 fputs(msg, stderr);
634 fputc(')', stderr);
635 }
636 fputc('\n', stderr);
637 }
638 }
639
640 static const char *status_abbrev(unsigned char sha1[20])
641 {
642 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
643 }
644
645 static void print_ok_ref_status(struct ref *ref, int porcelain)
646 {
647 if (ref->deletion)
648 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
649 else if (is_null_sha1(ref->old_sha1))
650 print_ref_status('*',
651 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
652 "[new branch]"),
653 ref, ref->peer_ref, NULL, porcelain);
654 else {
655 char quickref[84];
656 char type;
657 const char *msg;
658
659 strcpy(quickref, status_abbrev(ref->old_sha1));
660 if (ref->nonfastforward) {
661 strcat(quickref, "...");
662 type = '+';
663 msg = "forced update";
664 } else {
665 strcat(quickref, "..");
666 type = ' ';
667 msg = NULL;
668 }
669 strcat(quickref, status_abbrev(ref->new_sha1));
670
671 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
672 }
673 }
674
675 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
676 {
677 if (!count)
678 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
679
680 switch(ref->status) {
681 case REF_STATUS_NONE:
682 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
683 break;
684 case REF_STATUS_REJECT_NODELETE:
685 print_ref_status('!', "[rejected]", ref, NULL,
686 "remote does not support deleting refs", porcelain);
687 break;
688 case REF_STATUS_UPTODATE:
689 print_ref_status('=', "[up to date]", ref,
690 ref->peer_ref, NULL, porcelain);
691 break;
692 case REF_STATUS_REJECT_NONFASTFORWARD:
693 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
694 "non-fast-forward", porcelain);
695 break;
696 case REF_STATUS_REMOTE_REJECT:
697 print_ref_status('!', "[remote rejected]", ref,
698 ref->deletion ? NULL : ref->peer_ref,
699 ref->remote_status, porcelain);
700 break;
701 case REF_STATUS_EXPECTING_REPORT:
702 print_ref_status('!', "[remote failure]", ref,
703 ref->deletion ? NULL : ref->peer_ref,
704 "remote failed to report status", porcelain);
705 break;
706 case REF_STATUS_OK:
707 print_ok_ref_status(ref, porcelain);
708 break;
709 }
710
711 return 1;
712 }
713
714 void transport_print_push_status(const char *dest, struct ref *refs,
715 int verbose, int porcelain, int *nonfastforward)
716 {
717 struct ref *ref;
718 int n = 0;
719
720 if (verbose) {
721 for (ref = refs; ref; ref = ref->next)
722 if (ref->status == REF_STATUS_UPTODATE)
723 n += print_one_push_status(ref, dest, n, porcelain);
724 }
725
726 for (ref = refs; ref; ref = ref->next)
727 if (ref->status == REF_STATUS_OK)
728 n += print_one_push_status(ref, dest, n, porcelain);
729
730 *nonfastforward = 0;
731 for (ref = refs; ref; ref = ref->next) {
732 if (ref->status != REF_STATUS_NONE &&
733 ref->status != REF_STATUS_UPTODATE &&
734 ref->status != REF_STATUS_OK)
735 n += print_one_push_status(ref, dest, n, porcelain);
736 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
737 *nonfastforward = 1;
738 }
739 }
740
741 void transport_verify_remote_names(int nr_heads, const char **heads)
742 {
743 int i;
744
745 for (i = 0; i < nr_heads; i++) {
746 const char *local = heads[i];
747 const char *remote = strrchr(heads[i], ':');
748
749 if (*local == '+')
750 local++;
751
752 /* A matching refspec is okay. */
753 if (remote == local && remote[1] == '\0')
754 continue;
755
756 remote = remote ? (remote + 1) : local;
757 if (check_refname_format(remote,
758 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
759 die("remote part of refspec is not a valid name in %s",
760 heads[i]);
761 }
762 }
763
764 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
765 {
766 struct git_transport_data *data = transport->data;
767 struct send_pack_args args;
768 int ret;
769
770 if (!data->got_remote_heads) {
771 struct ref *tmp_refs;
772 connect_setup(transport, 1, 0);
773
774 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
775 NULL);
776 data->got_remote_heads = 1;
777 }
778
779 memset(&args, 0, sizeof(args));
780 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
781 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
782 args.use_thin_pack = data->options.thin;
783 args.verbose = (transport->verbose > 0);
784 args.quiet = (transport->verbose < 0);
785 args.progress = transport->progress;
786 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
787 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
788
789 ret = send_pack(&args, data->fd, data->conn, remote_refs,
790 &data->extra_have);
791
792 close(data->fd[1]);
793 close(data->fd[0]);
794 ret |= finish_connect(data->conn);
795 data->conn = NULL;
796 data->got_remote_heads = 0;
797
798 return ret;
799 }
800
801 static int connect_git(struct transport *transport, const char *name,
802 const char *executable, int fd[2])
803 {
804 struct git_transport_data *data = transport->data;
805 data->conn = git_connect(data->fd, transport->url,
806 executable, 0);
807 fd[0] = data->fd[0];
808 fd[1] = data->fd[1];
809 return 0;
810 }
811
812 static int disconnect_git(struct transport *transport)
813 {
814 struct git_transport_data *data = transport->data;
815 if (data->conn) {
816 if (data->got_remote_heads)
817 packet_flush(data->fd[1]);
818 close(data->fd[0]);
819 close(data->fd[1]);
820 finish_connect(data->conn);
821 }
822
823 free(data);
824 return 0;
825 }
826
827 void transport_take_over(struct transport *transport,
828 struct child_process *child)
829 {
830 struct git_transport_data *data;
831
832 if (!transport->smart_options)
833 die("Bug detected: Taking over transport requires non-NULL "
834 "smart_options field.");
835
836 data = xcalloc(1, sizeof(*data));
837 data->options = *transport->smart_options;
838 data->conn = child;
839 data->fd[0] = data->conn->out;
840 data->fd[1] = data->conn->in;
841 data->got_remote_heads = 0;
842 transport->data = data;
843
844 transport->set_option = NULL;
845 transport->get_refs_list = get_refs_via_connect;
846 transport->fetch = fetch_refs_via_pack;
847 transport->push = NULL;
848 transport->push_refs = git_transport_push;
849 transport->disconnect = disconnect_git;
850 transport->smart_options = &(data->options);
851 }
852
853 static int is_local(const char *url)
854 {
855 const char *colon = strchr(url, ':');
856 const char *slash = strchr(url, '/');
857 return !colon || (slash && slash < colon) ||
858 has_dos_drive_prefix(url);
859 }
860
861 static int is_file(const char *url)
862 {
863 struct stat buf;
864 if (stat(url, &buf))
865 return 0;
866 return S_ISREG(buf.st_mode);
867 }
868
869 static int external_specification_len(const char *url)
870 {
871 return strchr(url, ':') - url;
872 }
873
874 struct transport *transport_get(struct remote *remote, const char *url)
875 {
876 const char *helper;
877 struct transport *ret = xcalloc(1, sizeof(*ret));
878
879 ret->progress = isatty(2);
880
881 if (!remote)
882 die("No remote provided to transport_get()");
883
884 ret->got_remote_refs = 0;
885 ret->remote = remote;
886 helper = remote->foreign_vcs;
887
888 if (!url && remote->url)
889 url = remote->url[0];
890 ret->url = url;
891
892 /* maybe it is a foreign URL? */
893 if (url) {
894 const char *p = url;
895
896 while (is_urlschemechar(p == url, *p))
897 p++;
898 if (!prefixcmp(p, "::"))
899 helper = xstrndup(url, p - url);
900 }
901
902 if (helper) {
903 transport_helper_init(ret, helper);
904 } else if (!prefixcmp(url, "rsync:")) {
905 ret->get_refs_list = get_refs_via_rsync;
906 ret->fetch = fetch_objs_via_rsync;
907 ret->push = rsync_transport_push;
908 ret->smart_options = NULL;
909 } else if (is_local(url) && is_file(url)) {
910 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
911 ret->data = data;
912 ret->get_refs_list = get_refs_from_bundle;
913 ret->fetch = fetch_refs_from_bundle;
914 ret->disconnect = close_bundle;
915 ret->smart_options = NULL;
916 } else if (!is_url(url)
917 || !prefixcmp(url, "file://")
918 || !prefixcmp(url, "git://")
919 || !prefixcmp(url, "ssh://")
920 || !prefixcmp(url, "git+ssh://")
921 || !prefixcmp(url, "ssh+git://")) {
922 /* These are builtin smart transports. */
923 struct git_transport_data *data = xcalloc(1, sizeof(*data));
924 ret->data = data;
925 ret->set_option = NULL;
926 ret->get_refs_list = get_refs_via_connect;
927 ret->fetch = fetch_refs_via_pack;
928 ret->push_refs = git_transport_push;
929 ret->connect = connect_git;
930 ret->disconnect = disconnect_git;
931 ret->smart_options = &(data->options);
932
933 data->conn = NULL;
934 data->got_remote_heads = 0;
935 } else {
936 /* Unknown protocol in URL. Pass to external handler. */
937 int len = external_specification_len(url);
938 char *handler = xmalloc(len + 1);
939 handler[len] = 0;
940 strncpy(handler, url, len);
941 transport_helper_init(ret, handler);
942 }
943
944 if (ret->smart_options) {
945 ret->smart_options->thin = 1;
946 ret->smart_options->uploadpack = "git-upload-pack";
947 if (remote->uploadpack)
948 ret->smart_options->uploadpack = remote->uploadpack;
949 ret->smart_options->receivepack = "git-receive-pack";
950 if (remote->receivepack)
951 ret->smart_options->receivepack = remote->receivepack;
952 }
953
954 return ret;
955 }
956
957 int transport_set_option(struct transport *transport,
958 const char *name, const char *value)
959 {
960 int git_reports = 1, protocol_reports = 1;
961
962 if (transport->smart_options)
963 git_reports = set_git_option(transport->smart_options,
964 name, value);
965
966 if (transport->set_option)
967 protocol_reports = transport->set_option(transport, name,
968 value);
969
970 /* If either report is 0, report 0 (success). */
971 if (!git_reports || !protocol_reports)
972 return 0;
973 /* If either reports -1 (invalid value), report -1. */
974 if ((git_reports == -1) || (protocol_reports == -1))
975 return -1;
976 /* Otherwise if both report unknown, report unknown. */
977 return 1;
978 }
979
980 void transport_set_verbosity(struct transport *transport, int verbosity,
981 int force_progress)
982 {
983 if (verbosity >= 2)
984 transport->verbose = verbosity <= 3 ? verbosity : 3;
985 if (verbosity < 0)
986 transport->verbose = -1;
987
988 /**
989 * Rules used to determine whether to report progress (processing aborts
990 * when a rule is satisfied):
991 *
992 * 1. Report progress, if force_progress is 1 (ie. --progress).
993 * 2. Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
994 * 3. Report progress if isatty(2) is 1.
995 **/
996 transport->progress = force_progress || (verbosity >= 0 && isatty(2));
997 }
998
999 int transport_push(struct transport *transport,
1000 int refspec_nr, const char **refspec, int flags,
1001 int *nonfastforward)
1002 {
1003 *nonfastforward = 0;
1004 transport_verify_remote_names(refspec_nr, refspec);
1005
1006 if (transport->push) {
1007 /* Maybe FIXME. But no important transport uses this case. */
1008 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1009 die("This transport does not support using --set-upstream");
1010
1011 return transport->push(transport, refspec_nr, refspec, flags);
1012 } else if (transport->push_refs) {
1013 struct ref *remote_refs =
1014 transport->get_refs_list(transport, 1);
1015 struct ref *local_refs = get_local_heads();
1016 int match_flags = MATCH_REFS_NONE;
1017 int verbose = (transport->verbose > 0);
1018 int quiet = (transport->verbose < 0);
1019 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1020 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1021 int push_ret, ret, err;
1022
1023 if (flags & TRANSPORT_PUSH_ALL)
1024 match_flags |= MATCH_REFS_ALL;
1025 if (flags & TRANSPORT_PUSH_MIRROR)
1026 match_flags |= MATCH_REFS_MIRROR;
1027
1028 if (match_refs(local_refs, &remote_refs,
1029 refspec_nr, refspec, match_flags)) {
1030 return -1;
1031 }
1032
1033 set_ref_status_for_push(remote_refs,
1034 flags & TRANSPORT_PUSH_MIRROR,
1035 flags & TRANSPORT_PUSH_FORCE);
1036
1037 if ((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) && !is_bare_repository()) {
1038 struct ref *ref = remote_refs;
1039 for (; ref; ref = ref->next)
1040 if (!is_null_sha1(ref->new_sha1) &&
1041 check_submodule_needs_pushing(ref->new_sha1,transport->remote->name))
1042 die("There are unpushed submodules, aborting.");
1043 }
1044
1045 push_ret = transport->push_refs(transport, remote_refs, flags);
1046 err = push_had_errors(remote_refs);
1047 ret = push_ret | err;
1048
1049 if (!quiet || err)
1050 transport_print_push_status(transport->url, remote_refs,
1051 verbose | porcelain, porcelain,
1052 nonfastforward);
1053
1054 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1055 set_upstreams(transport, remote_refs, pretend);
1056
1057 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1058 struct ref *ref;
1059 for (ref = remote_refs; ref; ref = ref->next)
1060 transport_update_tracking_ref(transport->remote, ref, verbose);
1061 }
1062
1063 if (porcelain && !push_ret)
1064 puts("Done");
1065 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1066 fprintf(stderr, "Everything up-to-date\n");
1067
1068 return ret;
1069 }
1070 return 1;
1071 }
1072
1073 const struct ref *transport_get_remote_refs(struct transport *transport)
1074 {
1075 if (!transport->got_remote_refs) {
1076 transport->remote_refs = transport->get_refs_list(transport, 0);
1077 transport->got_remote_refs = 1;
1078 }
1079
1080 return transport->remote_refs;
1081 }
1082
1083 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1084 {
1085 int rc;
1086 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1087 struct ref **heads = NULL;
1088 struct ref *rm;
1089
1090 for (rm = refs; rm; rm = rm->next) {
1091 nr_refs++;
1092 if (rm->peer_ref &&
1093 !is_null_sha1(rm->old_sha1) &&
1094 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1095 continue;
1096 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1097 heads[nr_heads++] = rm;
1098 }
1099
1100 if (!nr_heads) {
1101 /*
1102 * When deepening of a shallow repository is requested,
1103 * then local and remote refs are likely to still be equal.
1104 * Just feed them all to the fetch method in that case.
1105 * This condition shouldn't be met in a non-deepening fetch
1106 * (see builtin-fetch.c:quickfetch()).
1107 */
1108 heads = xmalloc(nr_refs * sizeof(*heads));
1109 for (rm = refs; rm; rm = rm->next)
1110 heads[nr_heads++] = rm;
1111 }
1112
1113 rc = transport->fetch(transport, nr_heads, heads);
1114
1115 free(heads);
1116 return rc;
1117 }
1118
1119 void transport_unlock_pack(struct transport *transport)
1120 {
1121 if (transport->pack_lockfile) {
1122 unlink_or_warn(transport->pack_lockfile);
1123 free(transport->pack_lockfile);
1124 transport->pack_lockfile = NULL;
1125 }
1126 }
1127
1128 int transport_connect(struct transport *transport, const char *name,
1129 const char *exec, int fd[2])
1130 {
1131 if (transport->connect)
1132 return transport->connect(transport, name, exec, fd);
1133 else
1134 die("Operation not supported by protocol");
1135 }
1136
1137 int transport_disconnect(struct transport *transport)
1138 {
1139 int ret = 0;
1140 if (transport->disconnect)
1141 ret = transport->disconnect(transport);
1142 free(transport);
1143 return ret;
1144 }
1145
1146 /*
1147 * Strip username (and password) from an url and return
1148 * it in a newly allocated string.
1149 */
1150 char *transport_anonymize_url(const char *url)
1151 {
1152 char *anon_url, *scheme_prefix, *anon_part;
1153 size_t anon_len, prefix_len = 0;
1154
1155 anon_part = strchr(url, '@');
1156 if (is_local(url) || !anon_part)
1157 goto literal_copy;
1158
1159 anon_len = strlen(++anon_part);
1160 scheme_prefix = strstr(url, "://");
1161 if (!scheme_prefix) {
1162 if (!strchr(anon_part, ':'))
1163 /* cannot be "me@there:/path/name" */
1164 goto literal_copy;
1165 } else {
1166 const char *cp;
1167 /* make sure scheme is reasonable */
1168 for (cp = url; cp < scheme_prefix; cp++) {
1169 switch (*cp) {
1170 /* RFC 1738 2.1 */
1171 case '+': case '.': case '-':
1172 break; /* ok */
1173 default:
1174 if (isalnum(*cp))
1175 break;
1176 /* it isn't */
1177 goto literal_copy;
1178 }
1179 }
1180 /* @ past the first slash does not count */
1181 cp = strchr(scheme_prefix + 3, '/');
1182 if (cp && cp < anon_part)
1183 goto literal_copy;
1184 prefix_len = scheme_prefix - url + 3;
1185 }
1186 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1187 memcpy(anon_url, url, prefix_len);
1188 memcpy(anon_url + prefix_len, anon_part, anon_len);
1189 return anon_url;
1190 literal_copy:
1191 return xstrdup(url);
1192 }
1193
1194 struct alternate_refs_data {
1195 alternate_ref_fn *fn;
1196 void *data;
1197 };
1198
1199 static int refs_from_alternate_cb(struct alternate_object_database *e,
1200 void *data)
1201 {
1202 char *other;
1203 size_t len;
1204 struct remote *remote;
1205 struct transport *transport;
1206 const struct ref *extra;
1207 struct alternate_refs_data *cb = data;
1208
1209 e->name[-1] = '\0';
1210 other = xstrdup(real_path(e->base));
1211 e->name[-1] = '/';
1212 len = strlen(other);
1213
1214 while (other[len-1] == '/')
1215 other[--len] = '\0';
1216 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1217 return 0;
1218 /* Is this a git repository with refs? */
1219 memcpy(other + len - 8, "/refs", 6);
1220 if (!is_directory(other))
1221 return 0;
1222 other[len - 8] = '\0';
1223 remote = remote_get(other);
1224 transport = transport_get(remote, other);
1225 for (extra = transport_get_remote_refs(transport);
1226 extra;
1227 extra = extra->next)
1228 cb->fn(extra, cb->data);
1229 transport_disconnect(transport);
1230 free(other);
1231 return 0;
1232 }
1233
1234 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1235 {
1236 struct alternate_refs_data cb;
1237 cb.fn = fn;
1238 cb.data = data;
1239 foreach_alt_odb(refs_from_alternate_cb, &cb);
1240 }