Update draft release notes to 1.7.9.1
[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"
d2b17b32 13#include "submodule.h"
cd547b48
JS
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
26static 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
36static 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))) {
8ca12c0d 53 if (is_dot_or_dotdot(de->d_name))
cd547b48
JS
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;
59c69c0c 76 next = alloc_ref(path->buf + name_offset);
cd547b48
JS
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);
cd547b48
JS
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
99static 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 (;;) {
47ec7943 108 int cmp = cmp, len;
cd547b48
JS
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);
872c930d 118 if (len && buffer[len - 1] == '\n')
cd547b48
JS
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) {
59c69c0c 127 struct ref *next = alloc_ref(buffer + 41);
cd547b48
JS
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 }
cd547b48
JS
134 next->next = (*list)->next;
135 (*list)->next = next;
136 list = &(*list)->next;
137 }
138 }
139}
140
e9fcd1e2
IL
141static 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;
5dd56489 160 if (is_null_sha1(ref->new_sha1))
e9fcd1e2
IL
161 continue;
162
163 /* Follow symbolic refs (mainly for HEAD). */
164 localname = ref->peer_ref->name;
165 remotename = ref->name;
8cad4744 166 tmp = resolve_ref_unsafe(localname, sha, 1, &flag);
e9fcd1e2
IL
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
7efaeba2
JS
188static const char *rsync_url(const char *url)
189{
190 return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
191}
192
64fcef2d 193static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
cd547b48
JS
194{
195 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
c2e86add 196 struct ref dummy = {NULL}, *tail = &dummy;
cd547b48
JS
197 struct child_process rsync;
198 const char *args[5];
199 int temp_dir_len;
200
64fcef2d
DB
201 if (for_push)
202 return NULL;
203
cd547b48
JS
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))
0721c314 208 die_errno ("Could not make temporary directory");
cd547b48
JS
209 temp_dir_len = temp_dir.len;
210
7efaeba2 211 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
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";
bd2c86ef 218 args[1] = (transport->verbose > 1) ? "-rv" : "-r";
cd547b48
JS
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);
7efaeba2 227 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
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
256static int fetch_objs_via_rsync(struct transport *transport,
37148311 257 int nr_objs, struct ref **to_fetch)
cd547b48
JS
258{
259 struct strbuf buf = STRBUF_INIT;
260 struct child_process rsync;
261 const char *args[8];
262 int result;
263
7efaeba2 264 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
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";
bd2c86ef 271 args[1] = (transport->verbose > 1) ? "-rv" : "-r";
cd547b48
JS
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
287static 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
309static 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
330static 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;
ee020f35 336 const char *args[10];
cd547b48 337
94c89ba6
AW
338 if (flags & TRANSPORT_PUSH_MIRROR)
339 return error("rsync transport does not support mirror mode");
340
cd547b48
JS
341 /* first push the objects */
342
7efaeba2 343 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
344 strbuf_addch(&buf, '/');
345
346 memset(&rsync, 0, sizeof(rsync));
347 rsync.argv = args;
348 rsync.stdout_to_stderr = 1;
ee020f35
SP
349 i = 0;
350 args[i++] = "rsync";
351 args[i++] = "-a";
352 if (flags & TRANSPORT_PUSH_DRY_RUN)
353 args[i++] = "--dry-run";
bd2c86ef 354 if (transport->verbose > 1)
ee020f35
SP
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;
cd547b48
JS
362
363 if (run_command(&rsync))
7efaeba2
JS
364 return error("Could not push objects to %s",
365 rsync_url(transport->url));
cd547b48
JS
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))
0721c314 371 die_errno ("Could not make temporary directory");
cd547b48
JS
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
ee020f35
SP
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";
cd547b48 385 args[i++] = temp_dir.buf;
7efaeba2 386 args[i++] = rsync_url(transport->url);
cd547b48
JS
387 args[i++] = NULL;
388 if (run_command(&rsync))
7efaeba2
JS
389 result = error("Could not push to %s",
390 rsync_url(transport->url));
cd547b48
JS
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}
c29727d5 401
c7a8a162
JS
402struct bundle_transport_data {
403 int fd;
404 struct bundle_header header;
405};
406
64fcef2d 407static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
c7a8a162
JS
408{
409 struct bundle_transport_data *data = transport->data;
410 struct ref *result = NULL;
411 int i;
412
64fcef2d
DB
413 if (for_push)
414 return NULL;
415
c7a8a162
JS
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;
59c69c0c 423 struct ref *ref = alloc_ref(e->name);
c7a8a162 424 hashcpy(ref->old_sha1, e->sha1);
c7a8a162
JS
425 ref->next = result;
426 result = ref;
427 }
428 return result;
429}
430
1788c39c 431static int fetch_refs_from_bundle(struct transport *transport,
37148311 432 int nr_heads, struct ref **to_fetch)
c7a8a162
JS
433{
434 struct bundle_transport_data *data = transport->data;
be042aff
JH
435 return unbundle(&data->header, data->fd,
436 transport->progress ? BUNDLE_VERBOSE : 0);
c7a8a162
JS
437}
438
439static int close_bundle(struct transport *transport)
440{
441 struct bundle_transport_data *data = transport->data;
442 if (data->fd > 0)
443 close(data->fd);
f4e95765 444 free(data);
c7a8a162
JS
445 return 0;
446}
447
9b288516 448struct git_transport_data {
aa5af974 449 struct git_transport_options options;
ba227857
DB
450 struct child_process *conn;
451 int fd[2];
61b075bd 452 unsigned got_remote_heads : 1;
64fcef2d 453 struct extra_have_objects extra_have;
9b288516
DB
454};
455
aa5af974 456static int set_git_option(struct git_transport_options *opts,
9b288516
DB
457 const char *name, const char *value)
458{
c29727d5 459 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
aa5af974 460 opts->uploadpack = value;
c29727d5
DB
461 return 0;
462 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
aa5af974 463 opts->receivepack = value;
9b288516
DB
464 return 0;
465 } else if (!strcmp(name, TRANS_OPT_THIN)) {
aa5af974 466 opts->thin = !!value;
9b288516 467 return 0;
41fa7d2e 468 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
aa5af974 469 opts->followtags = !!value;
41fa7d2e 470 return 0;
c29727d5 471 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
aa5af974 472 opts->keep = !!value;
c29727d5 473 return 0;
c29727d5
DB
474 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
475 if (!value)
aa5af974 476 opts->depth = 0;
e7622ce8
NTND
477 else {
478 char *end;
479 opts->depth = strtol(value, &end, 0);
480 if (*end)
481 die("transport: invalid depth option '%s'", value);
482 }
c29727d5 483 return 0;
9b288516
DB
484 }
485 return 1;
486}
487
64fcef2d 488static int connect_setup(struct transport *transport, int for_push, int verbose)
ba227857
DB
489{
490 struct git_transport_data *data = transport->data;
61b075bd
IL
491
492 if (data->conn)
493 return 0;
494
5a277f3f
JH
495 data->conn = git_connect(data->fd, transport->url,
496 for_push ? data->options.receivepack :
497 data->options.uploadpack,
64fcef2d 498 verbose ? CONNECT_VERBOSE : 0);
61b075bd 499
ba227857
DB
500 return 0;
501}
502
64fcef2d 503static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
c29727d5
DB
504{
505 struct git_transport_data *data = transport->data;
506 struct ref *refs;
c29727d5 507
64fcef2d 508 connect_setup(transport, for_push, 0);
afe7c5ff 509 get_remote_heads(data->fd[0], &refs,
64fcef2d 510 for_push ? REF_NORMAL : 0, &data->extra_have);
61b075bd 511 data->got_remote_heads = 1;
c29727d5
DB
512
513 return refs;
514}
515
1788c39c 516static int fetch_refs_via_pack(struct transport *transport,
37148311 517 int nr_heads, struct ref **to_fetch)
c29727d5
DB
518{
519 struct git_transport_data *data = transport->data;
425b1393 520 char **heads = xmalloc(nr_heads * sizeof(*heads));
e4cd6c7a 521 char **origh = xmalloc(nr_heads * sizeof(*origh));
ba227857 522 const struct ref *refs;
c29727d5
DB
523 char *dest = xstrdup(transport->url);
524 struct fetch_pack_args args;
425b1393 525 int i;
00183cbb 526 struct ref *refs_tmp = NULL;
c29727d5 527
fa740529 528 memset(&args, 0, sizeof(args));
aa5af974
IL
529 args.uploadpack = data->options.uploadpack;
530 args.keep_pack = data->options.keep;
fa740529 531 args.lock_pack = 1;
aa5af974
IL
532 args.use_thin_pack = data->options.thin;
533 args.include_tag = data->options.followtags;
bd2c86ef 534 args.verbose = (transport->verbose > 1);
fe8aa148 535 args.quiet = (transport->verbose < 0);
d01b3c02 536 args.no_progress = !transport->progress;
aa5af974 537 args.depth = data->options.depth;
c29727d5 538
425b1393 539 for (i = 0; i < nr_heads; i++)
e4cd6c7a 540 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
ba227857 541
61b075bd 542 if (!data->got_remote_heads) {
64fcef2d 543 connect_setup(transport, 0, 0);
afe7c5ff 544 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL);
61b075bd 545 data->got_remote_heads = 1;
ba227857
DB
546 }
547
00183cbb
DB
548 refs = fetch_pack(&args, data->fd, data->conn,
549 refs_tmp ? refs_tmp : transport->remote_refs,
ba227857
DB
550 dest, nr_heads, heads, &transport->pack_lockfile);
551 close(data->fd[0]);
552 close(data->fd[1]);
553 if (finish_connect(data->conn))
554 refs = NULL;
555 data->conn = NULL;
61b075bd 556 data->got_remote_heads = 0;
c29727d5 557
00183cbb
DB
558 free_refs(refs_tmp);
559
425b1393 560 for (i = 0; i < nr_heads; i++)
e4cd6c7a
SP
561 free(origh[i]);
562 free(origh);
563 free(heads);
c29727d5 564 free(dest);
c6bc4005 565 return (refs ? 0 : -1);
c29727d5
DB
566}
567
481c7a6d
JK
568static int push_had_errors(struct ref *ref)
569{
570 for (; ref; ref = ref->next) {
571 switch (ref->status) {
572 case REF_STATUS_NONE:
573 case REF_STATUS_UPTODATE:
574 case REF_STATUS_OK:
575 break;
576 default:
577 return 1;
578 }
579 }
580 return 0;
581}
582
f1863d0d 583int transport_refs_pushed(struct ref *ref)
64fcef2d
DB
584{
585 for (; ref; ref = ref->next) {
586 switch(ref->status) {
587 case REF_STATUS_NONE:
588 case REF_STATUS_UPTODATE:
589 break;
590 default:
591 return 1;
592 }
593 }
594 return 0;
595}
596
f1863d0d 597void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
64fcef2d
DB
598{
599 struct refspec rs;
600
601 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
602 return;
603
604 rs.src = ref->name;
605 rs.dst = NULL;
606
607 if (!remote_find_tracking(remote, &rs)) {
608 if (verbose)
609 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
610 if (ref->deletion) {
611 delete_ref(rs.dst, NULL, 0);
612 } else
613 update_ref("update by push", rs.dst,
614 ref->new_sha1, NULL, 0, 0);
615 free(rs.dst);
616 }
617}
618
1965ff74 619static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
64fcef2d 620{
1965ff74
LA
621 if (porcelain) {
622 if (from)
623 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
624 else
625 fprintf(stdout, "%c\t:%s\t", flag, to->name);
626 if (msg)
627 fprintf(stdout, "%s (%s)\n", summary, msg);
628 else
629 fprintf(stdout, "%s\n", summary);
630 } else {
f1863d0d 631 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
1965ff74
LA
632 if (from)
633 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
634 else
635 fputs(prettify_refname(to->name), stderr);
636 if (msg) {
637 fputs(" (", stderr);
638 fputs(msg, stderr);
639 fputc(')', stderr);
640 }
641 fputc('\n', stderr);
64fcef2d 642 }
64fcef2d
DB
643}
644
645static const char *status_abbrev(unsigned char sha1[20])
646{
647 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
648}
649
1965ff74 650static void print_ok_ref_status(struct ref *ref, int porcelain)
64fcef2d
DB
651{
652 if (ref->deletion)
1965ff74 653 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
64fcef2d
DB
654 else if (is_null_sha1(ref->old_sha1))
655 print_ref_status('*',
656 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
1965ff74
LA
657 "[new branch]"),
658 ref, ref->peer_ref, NULL, porcelain);
64fcef2d
DB
659 else {
660 char quickref[84];
661 char type;
662 const char *msg;
663
664 strcpy(quickref, status_abbrev(ref->old_sha1));
665 if (ref->nonfastforward) {
666 strcat(quickref, "...");
667 type = '+';
668 msg = "forced update";
669 } else {
670 strcat(quickref, "..");
671 type = ' ';
672 msg = NULL;
673 }
674 strcat(quickref, status_abbrev(ref->new_sha1));
675
1965ff74 676 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
64fcef2d
DB
677 }
678}
679
1965ff74 680static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
64fcef2d
DB
681{
682 if (!count)
60cfeb8e 683 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
64fcef2d
DB
684
685 switch(ref->status) {
686 case REF_STATUS_NONE:
1965ff74 687 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
64fcef2d
DB
688 break;
689 case REF_STATUS_REJECT_NODELETE:
690 print_ref_status('!', "[rejected]", ref, NULL,
1965ff74 691 "remote does not support deleting refs", porcelain);
64fcef2d
DB
692 break;
693 case REF_STATUS_UPTODATE:
694 print_ref_status('=', "[up to date]", ref,
1965ff74 695 ref->peer_ref, NULL, porcelain);
64fcef2d
DB
696 break;
697 case REF_STATUS_REJECT_NONFASTFORWARD:
698 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
a75d7b54 699 "non-fast-forward", porcelain);
64fcef2d
DB
700 break;
701 case REF_STATUS_REMOTE_REJECT:
702 print_ref_status('!', "[remote rejected]", ref,
1965ff74
LA
703 ref->deletion ? NULL : ref->peer_ref,
704 ref->remote_status, porcelain);
64fcef2d
DB
705 break;
706 case REF_STATUS_EXPECTING_REPORT:
707 print_ref_status('!', "[remote failure]", ref,
1965ff74
LA
708 ref->deletion ? NULL : ref->peer_ref,
709 "remote failed to report status", porcelain);
64fcef2d
DB
710 break;
711 case REF_STATUS_OK:
1965ff74 712 print_ok_ref_status(ref, porcelain);
64fcef2d
DB
713 break;
714 }
715
716 return 1;
717}
718
f1863d0d
ML
719void transport_print_push_status(const char *dest, struct ref *refs,
720 int verbose, int porcelain, int *nonfastforward)
64fcef2d
DB
721{
722 struct ref *ref;
723 int n = 0;
724
725 if (verbose) {
726 for (ref = refs; ref; ref = ref->next)
727 if (ref->status == REF_STATUS_UPTODATE)
1965ff74 728 n += print_one_push_status(ref, dest, n, porcelain);
64fcef2d
DB
729 }
730
731 for (ref = refs; ref; ref = ref->next)
732 if (ref->status == REF_STATUS_OK)
1965ff74 733 n += print_one_push_status(ref, dest, n, porcelain);
64fcef2d 734
07436e43 735 *nonfastforward = 0;
64fcef2d
DB
736 for (ref = refs; ref; ref = ref->next) {
737 if (ref->status != REF_STATUS_NONE &&
738 ref->status != REF_STATUS_UPTODATE &&
739 ref->status != REF_STATUS_OK)
1965ff74 740 n += print_one_push_status(ref, dest, n, porcelain);
07436e43
MM
741 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
742 *nonfastforward = 1;
64fcef2d
DB
743 }
744}
745
f1863d0d 746void transport_verify_remote_names(int nr_heads, const char **heads)
64fcef2d
DB
747{
748 int i;
749
750 for (i = 0; i < nr_heads; i++) {
751 const char *local = heads[i];
752 const char *remote = strrchr(heads[i], ':');
753
754 if (*local == '+')
755 local++;
756
757 /* A matching refspec is okay. */
758 if (remote == local && remote[1] == '\0')
759 continue;
760
761 remote = remote ? (remote + 1) : local;
8d9c5010
MH
762 if (check_refname_format(remote,
763 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
764 die("remote part of refspec is not a valid name in %s",
765 heads[i]);
64fcef2d
DB
766 }
767}
768
769static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
f3fa1838 770{
9b288516 771 struct git_transport_data *data = transport->data;
40cb4fab 772 struct send_pack_args args;
64fcef2d
DB
773 int ret;
774
61b075bd 775 if (!data->got_remote_heads) {
64fcef2d
DB
776 struct ref *tmp_refs;
777 connect_setup(transport, 1, 0);
778
afe7c5ff 779 get_remote_heads(data->fd[0], &tmp_refs, REF_NORMAL, NULL);
61b075bd 780 data->got_remote_heads = 1;
64fcef2d 781 }
9b288516 782
de1a2fdd 783 memset(&args, 0, sizeof(args));
94c89ba6 784 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
40cb4fab 785 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
aa5af974 786 args.use_thin_pack = data->options.thin;
8afd8dc0
TRC
787 args.verbose = (transport->verbose > 0);
788 args.quiet = (transport->verbose < 0);
d7c411b7 789 args.progress = transport->progress;
40cb4fab 790 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
77555854 791 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
40cb4fab 792
64fcef2d
DB
793 ret = send_pack(&args, data->fd, data->conn, remote_refs,
794 &data->extra_have);
795
796 close(data->fd[1]);
797 close(data->fd[0]);
798 ret |= finish_connect(data->conn);
799 data->conn = NULL;
61b075bd 800 data->got_remote_heads = 0;
64fcef2d
DB
801
802 return ret;
9b288516
DB
803}
804
b236752a
IL
805static int connect_git(struct transport *transport, const char *name,
806 const char *executable, int fd[2])
807{
808 struct git_transport_data *data = transport->data;
809 data->conn = git_connect(data->fd, transport->url,
810 executable, 0);
811 fd[0] = data->fd[0];
812 fd[1] = data->fd[1];
813 return 0;
814}
815
f4e95765
SP
816static int disconnect_git(struct transport *transport)
817{
ba227857
DB
818 struct git_transport_data *data = transport->data;
819 if (data->conn) {
61b075bd
IL
820 if (data->got_remote_heads)
821 packet_flush(data->fd[1]);
ba227857
DB
822 close(data->fd[0]);
823 close(data->fd[1]);
824 finish_connect(data->conn);
825 }
826
827 free(data);
f4e95765
SP
828 return 0;
829}
830
61b075bd
IL
831void transport_take_over(struct transport *transport,
832 struct child_process *child)
833{
834 struct git_transport_data *data;
835
836 if (!transport->smart_options)
837 die("Bug detected: Taking over transport requires non-NULL "
838 "smart_options field.");
839
840 data = xcalloc(1, sizeof(*data));
841 data->options = *transport->smart_options;
842 data->conn = child;
843 data->fd[0] = data->conn->out;
844 data->fd[1] = data->conn->in;
845 data->got_remote_heads = 0;
846 transport->data = data;
847
848 transport->set_option = NULL;
849 transport->get_refs_list = get_refs_via_connect;
850 transport->fetch = fetch_refs_via_pack;
851 transport->push = NULL;
852 transport->push_refs = git_transport_push;
853 transport->disconnect = disconnect_git;
854 transport->smart_options = &(data->options);
855}
856
9b288516
DB
857static int is_local(const char *url)
858{
859 const char *colon = strchr(url, ':');
860 const char *slash = strchr(url, '/');
be501813
JS
861 return !colon || (slash && slash < colon) ||
862 has_dos_drive_prefix(url);
9b288516
DB
863}
864
865static int is_file(const char *url)
866{
867 struct stat buf;
868 if (stat(url, &buf))
869 return 0;
870 return S_ISREG(buf.st_mode);
871}
872
25d5cc48
IL
873static int external_specification_len(const char *url)
874{
875 return strchr(url, ':') - url;
876}
877
e5f4e214 878struct transport *transport_get(struct remote *remote, const char *url)
9b288516 879{
4da50460 880 const char *helper;
8eb554ae
SP
881 struct transport *ret = xcalloc(1, sizeof(*ret));
882
d01b3c02
TRC
883 ret->progress = isatty(2);
884
c1d45cf7
DB
885 if (!remote)
886 die("No remote provided to transport_get()");
887
b0d66e15 888 ret->got_remote_refs = 0;
8eb554ae 889 ret->remote = remote;
4da50460 890 helper = remote->foreign_vcs;
fb0cc87e 891
cb21d8f0 892 if (!url && remote->url)
fb0cc87e 893 url = remote->url[0];
8eb554ae 894 ret->url = url;
8eb554ae 895
87422439
JS
896 /* maybe it is a foreign URL? */
897 if (url) {
898 const char *p = url;
899
638794cd 900 while (is_urlschemechar(p == url, *p))
87422439
JS
901 p++;
902 if (!prefixcmp(p, "::"))
4da50460 903 helper = xstrndup(url, p - url);
87422439
JS
904 }
905
4da50460
IL
906 if (helper) {
907 transport_helper_init(ret, helper);
25d5cc48 908 } else if (!prefixcmp(url, "rsync:")) {
cd547b48
JS
909 ret->get_refs_list = get_refs_via_rsync;
910 ret->fetch = fetch_objs_via_rsync;
911 ret->push = rsync_transport_push;
aa5af974 912 ret->smart_options = NULL;
2727b71f 913 } else if (is_local(url) && is_file(url) && is_bundle(url, 1)) {
c7a8a162 914 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
c7a8a162 915 ret->data = data;
824d5776
SP
916 ret->get_refs_list = get_refs_from_bundle;
917 ret->fetch = fetch_refs_from_bundle;
918 ret->disconnect = close_bundle;
aa5af974 919 ret->smart_options = NULL;
25d5cc48
IL
920 } else if (!is_url(url)
921 || !prefixcmp(url, "file://")
922 || !prefixcmp(url, "git://")
923 || !prefixcmp(url, "ssh://")
924 || !prefixcmp(url, "git+ssh://")
925 || !prefixcmp(url, "ssh+git://")) {
926 /* These are builtin smart transports. */
9b288516 927 struct git_transport_data *data = xcalloc(1, sizeof(*data));
9b288516 928 ret->data = data;
aa5af974 929 ret->set_option = NULL;
824d5776
SP
930 ret->get_refs_list = get_refs_via_connect;
931 ret->fetch = fetch_refs_via_pack;
64fcef2d 932 ret->push_refs = git_transport_push;
b236752a 933 ret->connect = connect_git;
f4e95765 934 ret->disconnect = disconnect_git;
aa5af974 935 ret->smart_options = &(data->options);
824d5776 936
ba227857 937 data->conn = NULL;
61b075bd 938 data->got_remote_heads = 0;
25d5cc48
IL
939 } else {
940 /* Unknown protocol in URL. Pass to external handler. */
941 int len = external_specification_len(url);
942 char *handler = xmalloc(len + 1);
943 handler[len] = 0;
944 strncpy(handler, url, len);
945 transport_helper_init(ret, handler);
9b288516 946 }
8eb554ae 947
aa5af974
IL
948 if (ret->smart_options) {
949 ret->smart_options->thin = 1;
950 ret->smart_options->uploadpack = "git-upload-pack";
951 if (remote->uploadpack)
952 ret->smart_options->uploadpack = remote->uploadpack;
953 ret->smart_options->receivepack = "git-receive-pack";
954 if (remote->receivepack)
955 ret->smart_options->receivepack = remote->receivepack;
956 }
957
9b288516
DB
958 return ret;
959}
960
961int transport_set_option(struct transport *transport,
962 const char *name, const char *value)
963{
aa5af974
IL
964 int git_reports = 1, protocol_reports = 1;
965
966 if (transport->smart_options)
967 git_reports = set_git_option(transport->smart_options,
968 name, value);
969
824d5776 970 if (transport->set_option)
aa5af974
IL
971 protocol_reports = transport->set_option(transport, name,
972 value);
973
974 /* If either report is 0, report 0 (success). */
975 if (!git_reports || !protocol_reports)
976 return 0;
977 /* If either reports -1 (invalid value), report -1. */
978 if ((git_reports == -1) || (protocol_reports == -1))
979 return -1;
980 /* Otherwise if both report unknown, report unknown. */
ab865e6e 981 return 1;
9b288516
DB
982}
983
d01b3c02
TRC
984void transport_set_verbosity(struct transport *transport, int verbosity,
985 int force_progress)
bde873c5 986{
bd2c86ef 987 if (verbosity >= 1)
bde873c5
TRC
988 transport->verbose = verbosity <= 3 ? verbosity : 3;
989 if (verbosity < 0)
990 transport->verbose = -1;
d01b3c02
TRC
991
992 /**
993 * Rules used to determine whether to report progress (processing aborts
994 * when a rule is satisfied):
995 *
996 * 1. Report progress, if force_progress is 1 (ie. --progress).
997 * 2. Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
998 * 3. Report progress if isatty(2) is 1.
999 **/
1000 transport->progress = force_progress || (verbosity >= 0 && isatty(2));
bde873c5
TRC
1001}
1002
9b288516 1003int transport_push(struct transport *transport,
07436e43 1004 int refspec_nr, const char **refspec, int flags,
bb8cccd0 1005 int *nonfastforward)
9b288516 1006{
bb8cccd0 1007 *nonfastforward = 0;
f1863d0d 1008 transport_verify_remote_names(refspec_nr, refspec);
64fcef2d 1009
61b075bd 1010 if (transport->push) {
e9fcd1e2
IL
1011 /* Maybe FIXME. But no important transport uses this case. */
1012 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1013 die("This transport does not support using --set-upstream");
1014
64fcef2d 1015 return transport->push(transport, refspec_nr, refspec, flags);
61b075bd 1016 } else if (transport->push_refs) {
64fcef2d
DB
1017 struct ref *remote_refs =
1018 transport->get_refs_list(transport, 1);
64fcef2d
DB
1019 struct ref *local_refs = get_local_heads();
1020 int match_flags = MATCH_REFS_NONE;
8afd8dc0
TRC
1021 int verbose = (transport->verbose > 0);
1022 int quiet = (transport->verbose < 0);
1965ff74 1023 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
e9fcd1e2 1024 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
77555854 1025 int push_ret, ret, err;
64fcef2d
DB
1026
1027 if (flags & TRANSPORT_PUSH_ALL)
1028 match_flags |= MATCH_REFS_ALL;
1029 if (flags & TRANSPORT_PUSH_MIRROR)
1030 match_flags |= MATCH_REFS_MIRROR;
1031
29753cdd
JH
1032 if (match_push_refs(local_refs, &remote_refs,
1033 refspec_nr, refspec, match_flags)) {
64fcef2d
DB
1034 return -1;
1035 }
1036
20e8b465
TRC
1037 set_ref_status_for_push(remote_refs,
1038 flags & TRANSPORT_PUSH_MIRROR,
1039 flags & TRANSPORT_PUSH_FORCE);
1040
d2b17b32
FG
1041 if ((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) && !is_bare_repository()) {
1042 struct ref *ref = remote_refs;
1043 for (; ref; ref = ref->next)
1044 if (!is_null_sha1(ref->new_sha1) &&
1045 check_submodule_needs_pushing(ref->new_sha1,transport->remote->name))
1046 die("There are unpushed submodules, aborting.");
1047 }
1048
77555854 1049 push_ret = transport->push_refs(transport, remote_refs, flags);
42328267 1050 err = push_had_errors(remote_refs);
77555854 1051 ret = push_ret | err;
64fcef2d 1052
42328267 1053 if (!quiet || err)
f1863d0d 1054 transport_print_push_status(transport->url, remote_refs,
6ffd7812
JH
1055 verbose | porcelain, porcelain,
1056 nonfastforward);
64fcef2d 1057
e9fcd1e2
IL
1058 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1059 set_upstreams(transport, remote_refs, pretend);
1060
64fcef2d
DB
1061 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1062 struct ref *ref;
1063 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 1064 transport_update_tracking_ref(transport->remote, ref, verbose);
64fcef2d
DB
1065 }
1066
77555854
LA
1067 if (porcelain && !push_ret)
1068 puts("Done");
66bce02e 1069 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
64fcef2d 1070 fprintf(stderr, "Everything up-to-date\n");
77555854 1071
64fcef2d
DB
1072 return ret;
1073 }
1074 return 1;
9b288516
DB
1075}
1076
4577370e 1077const struct ref *transport_get_remote_refs(struct transport *transport)
c29727d5 1078{
b0d66e15 1079 if (!transport->got_remote_refs) {
64fcef2d 1080 transport->remote_refs = transport->get_refs_list(transport, 0);
b0d66e15
TRC
1081 transport->got_remote_refs = 1;
1082 }
61b075bd 1083
c29727d5
DB
1084 return transport->remote_refs;
1085}
1086
37148311 1087int transport_fetch_refs(struct transport *transport, struct ref *refs)
c29727d5 1088{
425b1393 1089 int rc;
86386829 1090 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
37148311
DB
1091 struct ref **heads = NULL;
1092 struct ref *rm;
c29727d5
DB
1093
1094 for (rm = refs; rm; rm = rm->next) {
86386829 1095 nr_refs++;
c29727d5 1096 if (rm->peer_ref &&
37148311 1097 !is_null_sha1(rm->old_sha1) &&
c29727d5
DB
1098 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1099 continue;
7a2bff45 1100 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
425b1393 1101 heads[nr_heads++] = rm;
c29727d5
DB
1102 }
1103
86386829
NP
1104 if (!nr_heads) {
1105 /*
1106 * When deepening of a shallow repository is requested,
1107 * then local and remote refs are likely to still be equal.
1108 * Just feed them all to the fetch method in that case.
1109 * This condition shouldn't be met in a non-deepening fetch
1110 * (see builtin-fetch.c:quickfetch()).
1111 */
1112 heads = xmalloc(nr_refs * sizeof(*heads));
1113 for (rm = refs; rm; rm = rm->next)
1114 heads[nr_heads++] = rm;
1115 }
1116
824d5776 1117 rc = transport->fetch(transport, nr_heads, heads);
61b075bd 1118
c29727d5 1119 free(heads);
425b1393 1120 return rc;
c29727d5
DB
1121}
1122
1788c39c
SP
1123void transport_unlock_pack(struct transport *transport)
1124{
1125 if (transport->pack_lockfile) {
691f1a28 1126 unlink_or_warn(transport->pack_lockfile);
1788c39c
SP
1127 free(transport->pack_lockfile);
1128 transport->pack_lockfile = NULL;
1129 }
1130}
1131
b236752a
IL
1132int transport_connect(struct transport *transport, const char *name,
1133 const char *exec, int fd[2])
1134{
1135 if (transport->connect)
1136 return transport->connect(transport, name, exec, fd);
1137 else
1138 die("Operation not supported by protocol");
1139}
1140
9b288516
DB
1141int transport_disconnect(struct transport *transport)
1142{
1143 int ret = 0;
824d5776
SP
1144 if (transport->disconnect)
1145 ret = transport->disconnect(transport);
9b288516
DB
1146 free(transport);
1147 return ret;
1148}
47abd85b
AE
1149
1150/*
1151 * Strip username (and password) from an url and return
1152 * it in a newly allocated string.
1153 */
1154char *transport_anonymize_url(const char *url)
1155{
1156 char *anon_url, *scheme_prefix, *anon_part;
1157 size_t anon_len, prefix_len = 0;
1158
1159 anon_part = strchr(url, '@');
1160 if (is_local(url) || !anon_part)
1161 goto literal_copy;
1162
1163 anon_len = strlen(++anon_part);
1164 scheme_prefix = strstr(url, "://");
1165 if (!scheme_prefix) {
1166 if (!strchr(anon_part, ':'))
1167 /* cannot be "me@there:/path/name" */
1168 goto literal_copy;
1169 } else {
1170 const char *cp;
1171 /* make sure scheme is reasonable */
1172 for (cp = url; cp < scheme_prefix; cp++) {
1173 switch (*cp) {
1174 /* RFC 1738 2.1 */
1175 case '+': case '.': case '-':
1176 break; /* ok */
1177 default:
1178 if (isalnum(*cp))
1179 break;
1180 /* it isn't */
1181 goto literal_copy;
1182 }
1183 }
1184 /* @ past the first slash does not count */
1185 cp = strchr(scheme_prefix + 3, '/');
1186 if (cp && cp < anon_part)
1187 goto literal_copy;
1188 prefix_len = scheme_prefix - url + 3;
1189 }
1190 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1191 memcpy(anon_url, url, prefix_len);
1192 memcpy(anon_url + prefix_len, anon_part, anon_len);
1193 return anon_url;
1194literal_copy:
1195 return xstrdup(url);
1196}
36cfda15 1197
114a6a88
JK
1198struct alternate_refs_data {
1199 alternate_ref_fn *fn;
1200 void *data;
1201};
1202
1203static int refs_from_alternate_cb(struct alternate_object_database *e,
1204 void *data)
36cfda15
JH
1205{
1206 char *other;
1207 size_t len;
1208 struct remote *remote;
1209 struct transport *transport;
1210 const struct ref *extra;
114a6a88 1211 struct alternate_refs_data *cb = data;
36cfda15
JH
1212
1213 e->name[-1] = '\0';
91b3c7ce 1214 other = xstrdup(real_path(e->base));
36cfda15
JH
1215 e->name[-1] = '/';
1216 len = strlen(other);
1217
1218 while (other[len-1] == '/')
1219 other[--len] = '\0';
1220 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1221 return 0;
1222 /* Is this a git repository with refs? */
1223 memcpy(other + len - 8, "/refs", 6);
1224 if (!is_directory(other))
1225 return 0;
1226 other[len - 8] = '\0';
1227 remote = remote_get(other);
1228 transport = transport_get(remote, other);
1229 for (extra = transport_get_remote_refs(transport);
1230 extra;
1231 extra = extra->next)
114a6a88 1232 cb->fn(extra, cb->data);
36cfda15
JH
1233 transport_disconnect(transport);
1234 free(other);
1235 return 0;
1236}
114a6a88
JK
1237
1238void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1239{
1240 struct alternate_refs_data cb;
1241 cb.fn = fn;
1242 cb.data = data;
1243 foreach_alt_odb(refs_from_alternate_cb, &cb);
1244}