1#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "pkt-line.h"
5#include "sideband.h"
6#include "run-command.h"
7#include "remote.h"
8#include "send-pack.h"
9#include "quote.h"
10
11static const char send_pack_usage[] =
12"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
13" --all and explicit <ref> specification are mutually exclusive.";
14
15static struct send_pack_args args;
16
17static int feed_object(const unsigned char *sha1, int fd, int negative)
18{
19 char buf[42];
20
21 if (negative && !has_sha1_file(sha1))
22 return 1;
23
24 memcpy(buf + negative, sha1_to_hex(sha1), 40);
25 if (negative)
26 buf[0] = '^';
27 buf[40 + negative] = '\n';
28 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
29}
30
31/*
32 * Make a pack stream and spit it out into file descriptor fd
33 */
34static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
35{
36 /*
37 * The child becomes pack-objects --revs; we feed
38 * the revision parameters to it via its stdin and
39 * let its stdout go back to the other end.
40 */
41 const char *argv[] = {
42 "pack-objects",
43 "--all-progress-implied",
44 "--revs",
45 "--stdout",
46 NULL,
47 NULL,
48 NULL,
49 NULL,
50 };
51 struct child_process po;
52 int i;
53
54 i = 4;
55 if (args->use_thin_pack)
56 argv[i++] = "--thin";
57 if (args->use_ofs_delta)
58 argv[i++] = "--delta-base-offset";
59 if (args->quiet)
60 argv[i++] = "-q";
61 memset(&po, 0, sizeof(po));
62 po.argv = argv;
63 po.in = -1;
64 po.out = args->stateless_rpc ? -1 : fd;
65 po.git_cmd = 1;
66 if (start_command(&po))
67 die_errno("git pack-objects failed");
68
69 /*
70 * We feed the pack-objects we just spawned with revision
71 * parameters by writing to the pipe.
72 */
73 for (i = 0; i < extra->nr; i++)
74 if (!feed_object(extra->array[i], po.in, 1))
75 break;
76
77 while (refs) {
78 if (!is_null_sha1(refs->old_sha1) &&
79 !feed_object(refs->old_sha1, po.in, 1))
80 break;
81 if (!is_null_sha1(refs->new_sha1) &&
82 !feed_object(refs->new_sha1, po.in, 0))
83 break;
84 refs = refs->next;
85 }
86
87 close(po.in);
88
89 if (args->stateless_rpc) {
90 char *buf = xmalloc(LARGE_PACKET_MAX);
91 while (1) {
92 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
93 if (n <= 0)
94 break;
95 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
96 }
97 free(buf);
98 close(po.out);
99 po.out = -1;
100 }
101
102 if (finish_command(&po))
103 return error("pack-objects died with strange error");
104 return 0;
105}
106
107static int receive_status(int in, struct ref *refs)
108{
109 struct ref *hint;
110 char line[1000];
111 int ret = 0;
112 int len = packet_read_line(in, line, sizeof(line));
113 if (len < 10 || memcmp(line, "unpack ", 7))
114 return error("did not receive remote status");
115 if (memcmp(line, "unpack ok\n", 10)) {
116 char *p = line + strlen(line) - 1;
117 if (*p == '\n')
118 *p = '\0';
119 error("unpack failed: %s", line + 7);
120 ret = -1;
121 }
122 hint = NULL;
123 while (1) {
124 char *refname;
125 char *msg;
126 len = packet_read_line(in, line, sizeof(line));
127 if (!len)
128 break;
129 if (len < 3 ||
130 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
131 fprintf(stderr, "protocol error: %s\n", line);
132 ret = -1;
133 break;
134 }
135
136 line[strlen(line)-1] = '\0';
137 refname = line + 3;
138 msg = strchr(refname, ' ');
139 if (msg)
140 *msg++ = '\0';
141
142 /* first try searching at our hint, falling back to all refs */
143 if (hint)
144 hint = find_ref_by_name(hint, refname);
145 if (!hint)
146 hint = find_ref_by_name(refs, refname);
147 if (!hint) {
148 warning("remote reported status on unknown ref: %s",
149 refname);
150 continue;
151 }
152 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
153 warning("remote reported status on unexpected ref: %s",
154 refname);
155 continue;
156 }
157
158 if (line[0] == 'o' && line[1] == 'k')
159 hint->status = REF_STATUS_OK;
160 else {
161 hint->status = REF_STATUS_REMOTE_REJECT;
162 ret = -1;
163 }
164 if (msg)
165 hint->remote_status = xstrdup(msg);
166 /* start our next search from the next ref */
167 hint = hint->next;
168 }
169 return ret;
170}
171
172static void update_tracking_ref(struct remote *remote, struct ref *ref)
173{
174 struct refspec rs;
175
176 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
177 return;
178
179 rs.src = ref->name;
180 rs.dst = NULL;
181
182 if (!remote_find_tracking(remote, &rs)) {
183 if (args.verbose)
184 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
185 if (ref->deletion) {
186 delete_ref(rs.dst, NULL, 0);
187 } else
188 update_ref("update by push", rs.dst,
189 ref->new_sha1, NULL, 0, 0);
190 free(rs.dst);
191 }
192}
193
194#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
195
196static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
197{
198 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
199 if (from)
200 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
201 else
202 fputs(prettify_refname(to->name), stderr);
203 if (msg) {
204 fputs(" (", stderr);
205 fputs(msg, stderr);
206 fputc(')', stderr);
207 }
208 fputc('\n', stderr);
209}
210
211static const char *status_abbrev(unsigned char sha1[20])
212{
213 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
214}
215
216static void print_ok_ref_status(struct ref *ref)
217{
218 if (ref->deletion)
219 print_ref_status('-', "[deleted]", ref, NULL, NULL);
220 else if (is_null_sha1(ref->old_sha1))
221 print_ref_status('*',
222 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
223 "[new branch]"),
224 ref, ref->peer_ref, NULL);
225 else {
226 char quickref[84];
227 char type;
228 const char *msg;
229
230 strcpy(quickref, status_abbrev(ref->old_sha1));
231 if (ref->nonfastforward) {
232 strcat(quickref, "...");
233 type = '+';
234 msg = "forced update";
235 } else {
236 strcat(quickref, "..");
237 type = ' ';
238 msg = NULL;
239 }
240 strcat(quickref, status_abbrev(ref->new_sha1));
241
242 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
243 }
244}
245
246static int print_one_push_status(struct ref *ref, const char *dest, int count)
247{
248 if (!count)
249 fprintf(stderr, "To %s\n", dest);
250
251 switch(ref->status) {
252 case REF_STATUS_NONE:
253 print_ref_status('X', "[no match]", ref, NULL, NULL);
254 break;
255 case REF_STATUS_REJECT_NODELETE:
256 print_ref_status('!', "[rejected]", ref, NULL,
257 "remote does not support deleting refs");
258 break;
259 case REF_STATUS_UPTODATE:
260 print_ref_status('=', "[up to date]", ref,
261 ref->peer_ref, NULL);
262 break;
263 case REF_STATUS_REJECT_NONFASTFORWARD:
264 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
265 "non-fast-forward");
266 break;
267 case REF_STATUS_REMOTE_REJECT:
268 print_ref_status('!', "[remote rejected]", ref,
269 ref->deletion ? NULL : ref->peer_ref,
270 ref->remote_status);
271 break;
272 case REF_STATUS_EXPECTING_REPORT:
273 print_ref_status('!', "[remote failure]", ref,
274 ref->deletion ? NULL : ref->peer_ref,
275 "remote failed to report status");
276 break;
277 case REF_STATUS_OK:
278 print_ok_ref_status(ref);
279 break;
280 }
281
282 return 1;
283}
284
285static void print_push_status(const char *dest, struct ref *refs)
286{
287 struct ref *ref;
288 int n = 0;
289
290 if (args.verbose) {
291 for (ref = refs; ref; ref = ref->next)
292 if (ref->status == REF_STATUS_UPTODATE)
293 n += print_one_push_status(ref, dest, n);
294 }
295
296 for (ref = refs; ref; ref = ref->next)
297 if (ref->status == REF_STATUS_OK)
298 n += print_one_push_status(ref, dest, n);
299
300 for (ref = refs; ref; ref = ref->next) {
301 if (ref->status != REF_STATUS_NONE &&
302 ref->status != REF_STATUS_UPTODATE &&
303 ref->status != REF_STATUS_OK)
304 n += print_one_push_status(ref, dest, n);
305 }
306}
307
308static int refs_pushed(struct ref *ref)
309{
310 for (; ref; ref = ref->next) {
311 switch(ref->status) {
312 case REF_STATUS_NONE:
313 case REF_STATUS_UPTODATE:
314 break;
315 default:
316 return 1;
317 }
318 }
319 return 0;
320}
321
322static void print_helper_status(struct ref *ref)
323{
324 struct strbuf buf = STRBUF_INIT;
325
326 for (; ref; ref = ref->next) {
327 const char *msg = NULL;
328 const char *res;
329
330 switch(ref->status) {
331 case REF_STATUS_NONE:
332 res = "error";
333 msg = "no match";
334 break;
335
336 case REF_STATUS_OK:
337 res = "ok";
338 break;
339
340 case REF_STATUS_UPTODATE:
341 res = "ok";
342 msg = "up to date";
343 break;
344
345 case REF_STATUS_REJECT_NONFASTFORWARD:
346 res = "error";
347 msg = "non-fast forward";
348 break;
349
350 case REF_STATUS_REJECT_NODELETE:
351 case REF_STATUS_REMOTE_REJECT:
352 res = "error";
353 break;
354
355 case REF_STATUS_EXPECTING_REPORT:
356 default:
357 continue;
358 }
359
360 strbuf_reset(&buf);
361 strbuf_addf(&buf, "%s %s", res, ref->name);
362 if (ref->remote_status)
363 msg = ref->remote_status;
364 if (msg) {
365 strbuf_addch(&buf, ' ');
366 quote_two_c_style(&buf, "", msg, 0);
367 }
368 strbuf_addch(&buf, '\n');
369
370 safe_write(1, buf.buf, buf.len);
371 }
372 strbuf_release(&buf);
373}
374
375static int sideband_demux(int in, int out, void *data)
376{
377 int *fd = data, ret;
378#ifndef WIN32
379 close(fd[1]);
380#endif
381 ret = recv_sideband("send-pack", fd[0], out);
382 close(out);
383 return ret;
384}
385
386int send_pack(struct send_pack_args *args,
387 int fd[], struct child_process *conn,
388 struct ref *remote_refs,
389 struct extra_have_objects *extra_have)
390{
391 int in = fd[0];
392 int out = fd[1];
393 struct strbuf req_buf = STRBUF_INIT;
394 struct ref *ref;
395 int new_refs;
396 int allow_deleting_refs = 0;
397 int status_report = 0;
398 int use_sideband = 0;
399 unsigned cmds_sent = 0;
400 int ret;
401 struct async demux;
402
403 /* Does the other end support the reporting? */
404 if (server_supports("report-status"))
405 status_report = 1;
406 if (server_supports("delete-refs"))
407 allow_deleting_refs = 1;
408 if (server_supports("ofs-delta"))
409 args->use_ofs_delta = 1;
410 if (server_supports("side-band-64k"))
411 use_sideband = 1;
412
413 if (!remote_refs) {
414 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
415 "Perhaps you should specify a branch such as 'master'.\n");
416 return 0;
417 }
418
419 /*
420 * Finally, tell the other end!
421 */
422 new_refs = 0;
423 for (ref = remote_refs; ref; ref = ref->next) {
424
425 if (ref->peer_ref)
426 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
427 else if (!args->send_mirror)
428 continue;
429
430 ref->deletion = is_null_sha1(ref->new_sha1);
431 if (ref->deletion && !allow_deleting_refs) {
432 ref->status = REF_STATUS_REJECT_NODELETE;
433 continue;
434 }
435 if (!ref->deletion &&
436 !hashcmp(ref->old_sha1, ref->new_sha1)) {
437 ref->status = REF_STATUS_UPTODATE;
438 continue;
439 }
440
441 /* This part determines what can overwrite what.
442 * The rules are:
443 *
444 * (0) you can always use --force or +A:B notation to
445 * selectively force individual ref pairs.
446 *
447 * (1) if the old thing does not exist, it is OK.
448 *
449 * (2) if you do not have the old thing, you are not allowed
450 * to overwrite it; you would not know what you are losing
451 * otherwise.
452 *
453 * (3) if both new and old are commit-ish, and new is a
454 * descendant of old, it is OK.
455 *
456 * (4) regardless of all of the above, removing :B is
457 * always allowed.
458 */
459
460 ref->nonfastforward =
461 !ref->deletion &&
462 !is_null_sha1(ref->old_sha1) &&
463 (!has_sha1_file(ref->old_sha1)
464 || !ref_newer(ref->new_sha1, ref->old_sha1));
465
466 if (ref->nonfastforward && !ref->force && !args->force_update) {
467 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
468 continue;
469 }
470
471 if (!ref->deletion)
472 new_refs++;
473
474 if (args->dry_run) {
475 ref->status = REF_STATUS_OK;
476 } else {
477 char *old_hex = sha1_to_hex(ref->old_sha1);
478 char *new_hex = sha1_to_hex(ref->new_sha1);
479
480 if (!cmds_sent && (status_report || use_sideband)) {
481 packet_buf_write(&req_buf, "%s %s %s%c%s%s",
482 old_hex, new_hex, ref->name, 0,
483 status_report ? " report-status" : "",
484 use_sideband ? " side-band-64k" : "");
485 }
486 else
487 packet_buf_write(&req_buf, "%s %s %s",
488 old_hex, new_hex, ref->name);
489 ref->status = status_report ?
490 REF_STATUS_EXPECTING_REPORT :
491 REF_STATUS_OK;
492 cmds_sent++;
493 }
494 }
495
496 if (args->stateless_rpc) {
497 if (!args->dry_run && cmds_sent) {
498 packet_buf_flush(&req_buf);
499 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
500 }
501 } else {
502 safe_write(out, req_buf.buf, req_buf.len);
503 packet_flush(out);
504 }
505 strbuf_release(&req_buf);
506
507 if (use_sideband && cmds_sent) {
508 memset(&demux, 0, sizeof(demux));
509 demux.proc = sideband_demux;
510 demux.data = fd;
511 demux.out = -1;
512 if (start_async(&demux))
513 die("receive-pack: unable to fork off sideband demultiplexer");
514 in = demux.out;
515 }
516
517 if (new_refs && cmds_sent) {
518 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
519 for (ref = remote_refs; ref; ref = ref->next)
520 ref->status = REF_STATUS_NONE;
521 if (args->stateless_rpc)
522 close(out);
523 if (git_connection_is_socket(conn))
524 shutdown(fd[0], SHUT_WR);
525 if (use_sideband)
526 finish_async(&demux);
527 return -1;
528 }
529 }
530 if (args->stateless_rpc && cmds_sent)
531 packet_flush(out);
532
533 if (status_report && cmds_sent)
534 ret = receive_status(in, remote_refs);
535 else
536 ret = 0;
537 if (args->stateless_rpc)
538 packet_flush(out);
539
540 if (use_sideband && cmds_sent) {
541 if (finish_async(&demux)) {
542 error("error in sideband demultiplexer");
543 ret = -1;
544 }
545 close(demux.out);
546 }
547
548 if (ret < 0)
549 return ret;
550 for (ref = remote_refs; ref; ref = ref->next) {
551 switch (ref->status) {
552 case REF_STATUS_NONE:
553 case REF_STATUS_UPTODATE:
554 case REF_STATUS_OK:
555 break;
556 default:
557 return -1;
558 }
559 }
560 return 0;
561}
562
563static void verify_remote_names(int nr_heads, const char **heads)
564{
565 int i;
566
567 for (i = 0; i < nr_heads; i++) {
568 const char *local = heads[i];
569 const char *remote = strrchr(heads[i], ':');
570
571 if (*local == '+')
572 local++;
573
574 /* A matching refspec is okay. */
575 if (remote == local && remote[1] == '\0')
576 continue;
577
578 remote = remote ? (remote + 1) : local;
579 switch (check_ref_format(remote)) {
580 case 0: /* ok */
581 case CHECK_REF_FORMAT_ONELEVEL:
582 /* ok but a single level -- that is fine for
583 * a match pattern.
584 */
585 case CHECK_REF_FORMAT_WILDCARD:
586 /* ok but ends with a pattern-match character */
587 continue;
588 }
589 die("remote part of refspec is not a valid name in %s",
590 heads[i]);
591 }
592}
593
594int cmd_send_pack(int argc, const char **argv, const char *prefix)
595{
596 int i, nr_refspecs = 0;
597 const char **refspecs = NULL;
598 const char *remote_name = NULL;
599 struct remote *remote = NULL;
600 const char *dest = NULL;
601 int fd[2];
602 struct child_process *conn;
603 struct extra_have_objects extra_have;
604 struct ref *remote_refs, *local_refs;
605 int ret;
606 int helper_status = 0;
607 int send_all = 0;
608 const char *receivepack = "git-receive-pack";
609 int flags;
610
611 argv++;
612 for (i = 1; i < argc; i++, argv++) {
613 const char *arg = *argv;
614
615 if (*arg == '-') {
616 if (!prefixcmp(arg, "--receive-pack=")) {
617 receivepack = arg + 15;
618 continue;
619 }
620 if (!prefixcmp(arg, "--exec=")) {
621 receivepack = arg + 7;
622 continue;
623 }
624 if (!prefixcmp(arg, "--remote=")) {
625 remote_name = arg + 9;
626 continue;
627 }
628 if (!strcmp(arg, "--all")) {
629 send_all = 1;
630 continue;
631 }
632 if (!strcmp(arg, "--dry-run")) {
633 args.dry_run = 1;
634 continue;
635 }
636 if (!strcmp(arg, "--mirror")) {
637 args.send_mirror = 1;
638 continue;
639 }
640 if (!strcmp(arg, "--force")) {
641 args.force_update = 1;
642 continue;
643 }
644 if (!strcmp(arg, "--verbose")) {
645 args.verbose = 1;
646 continue;
647 }
648 if (!strcmp(arg, "--thin")) {
649 args.use_thin_pack = 1;
650 continue;
651 }
652 if (!strcmp(arg, "--stateless-rpc")) {
653 args.stateless_rpc = 1;
654 continue;
655 }
656 if (!strcmp(arg, "--helper-status")) {
657 helper_status = 1;
658 continue;
659 }
660 usage(send_pack_usage);
661 }
662 if (!dest) {
663 dest = arg;
664 continue;
665 }
666 refspecs = (const char **) argv;
667 nr_refspecs = argc - i;
668 break;
669 }
670 if (!dest)
671 usage(send_pack_usage);
672 /*
673 * --all and --mirror are incompatible; neither makes sense
674 * with any refspecs.
675 */
676 if ((refspecs && (send_all || args.send_mirror)) ||
677 (send_all && args.send_mirror))
678 usage(send_pack_usage);
679
680 if (remote_name) {
681 remote = remote_get(remote_name);
682 if (!remote_has_url(remote, dest)) {
683 die("Destination %s is not a uri for %s",
684 dest, remote_name);
685 }
686 }
687
688 if (args.stateless_rpc) {
689 conn = NULL;
690 fd[0] = 0;
691 fd[1] = 1;
692 } else {
693 conn = git_connect(fd, dest, receivepack,
694 args.verbose ? CONNECT_VERBOSE : 0);
695 }
696
697 memset(&extra_have, 0, sizeof(extra_have));
698
699 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
700 &extra_have);
701
702 verify_remote_names(nr_refspecs, refspecs);
703
704 local_refs = get_local_heads();
705
706 flags = MATCH_REFS_NONE;
707
708 if (send_all)
709 flags |= MATCH_REFS_ALL;
710 if (args.send_mirror)
711 flags |= MATCH_REFS_MIRROR;
712
713 /* match them up */
714 if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
715 return -1;
716
717 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
718
719 if (helper_status)
720 print_helper_status(remote_refs);
721
722 close(fd[1]);
723 close(fd[0]);
724
725 ret |= finish_connect(conn);
726
727 if (!helper_status)
728 print_push_status(dest, remote_refs);
729
730 if (!args.dry_run && remote) {
731 struct ref *ref;
732 for (ref = remote_refs; ref; ref = ref->next)
733 update_tracking_ref(remote, ref);
734 }
735
736 if (!ret && !refs_pushed(remote_refs))
737 fprintf(stderr, "Everything up-to-date\n");
738
739 return ret;
740}