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