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