6 #include "run-command.h"
11 static 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.";
15 static struct send_pack_args args;
17 static int feed_object(const unsigned char *sha1, int fd, int negative)
21 if (negative && !has_sha1_file(sha1))
24 memcpy(buf + negative, sha1_to_hex(sha1), 40);
27 buf[40 + negative] = '\n';
28 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
32 * Make a pack stream and spit it out into file descriptor fd
34 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
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.
41 const char *argv[] = {
43 "--all-progress-implied",
51 struct child_process po;
55 if (args->use_thin_pack)
57 if (args->use_ofs_delta)
58 argv[i++] = "--delta-base-offset";
61 memset(&po, 0, sizeof(po));
64 po.out = args->stateless_rpc ? -1 : fd;
66 if (start_command(&po))
67 die_errno("git pack-objects failed");
70 * We feed the pack-objects we just spawned with revision
71 * parameters by writing to the pipe.
73 for (i = 0; i < extra->nr; i++)
74 if (!feed_object(extra->array[i], po.in, 1))
78 if (!is_null_sha1(refs->old_sha1) &&
79 !feed_object(refs->old_sha1, po.in, 1))
81 if (!is_null_sha1(refs->new_sha1) &&
82 !feed_object(refs->new_sha1, po.in, 0))
89 if (args->stateless_rpc) {
90 char *buf = xmalloc(LARGE_PACKET_MAX);
92 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
95 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
103 if (finish_command(&po))
104 return error("pack-objects died with strange error");
108 static int receive_status(int in, struct ref *refs)
113 int len = packet_read_line(in, line, sizeof(line));
114 if (len < 10 || memcmp(line, "unpack ", 7))
115 return error("did not receive remote status");
116 if (memcmp(line, "unpack ok\n", 10)) {
117 char *p = line + strlen(line) - 1;
120 error("unpack failed: %s", line + 7);
127 len = packet_read_line(in, line, sizeof(line));
131 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
132 fprintf(stderr, "protocol error: %s\n", line);
137 line[strlen(line)-1] = '\0';
139 msg = strchr(refname, ' ');
143 /* first try searching at our hint, falling back to all refs */
145 hint = find_ref_by_name(hint, refname);
147 hint = find_ref_by_name(refs, refname);
149 warning("remote reported status on unknown ref: %s",
153 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
154 warning("remote reported status on unexpected ref: %s",
159 if (line[0] == 'o' && line[1] == 'k')
160 hint->status = REF_STATUS_OK;
162 hint->status = REF_STATUS_REMOTE_REJECT;
166 hint->remote_status = xstrdup(msg);
167 /* start our next search from the next ref */
173 static void update_tracking_ref(struct remote *remote, struct ref *ref)
177 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
183 if (!remote_find_tracking(remote, &rs)) {
185 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
187 delete_ref(rs.dst, NULL, 0);
189 update_ref("update by push", rs.dst,
190 ref->new_sha1, NULL, 0, 0);
195 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
197 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
199 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
201 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
203 fputs(prettify_refname(to->name), stderr);
212 static const char *status_abbrev(unsigned char sha1[20])
214 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
217 static void print_ok_ref_status(struct ref *ref)
220 print_ref_status('-', "[deleted]", ref, NULL, NULL);
221 else if (is_null_sha1(ref->old_sha1))
222 print_ref_status('*',
223 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
225 ref, ref->peer_ref, NULL);
231 strcpy(quickref, status_abbrev(ref->old_sha1));
232 if (ref->nonfastforward) {
233 strcat(quickref, "...");
235 msg = "forced update";
237 strcat(quickref, "..");
241 strcat(quickref, status_abbrev(ref->new_sha1));
243 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
247 static int print_one_push_status(struct ref *ref, const char *dest, int count)
250 fprintf(stderr, "To %s\n", dest);
252 switch(ref->status) {
253 case REF_STATUS_NONE:
254 print_ref_status('X', "[no match]", ref, NULL, NULL);
256 case REF_STATUS_REJECT_NODELETE:
257 print_ref_status('!', "[rejected]", ref, NULL,
258 "remote does not support deleting refs");
260 case REF_STATUS_UPTODATE:
261 print_ref_status('=', "[up to date]", ref,
262 ref->peer_ref, NULL);
264 case REF_STATUS_REJECT_NONFASTFORWARD:
265 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
268 case REF_STATUS_REMOTE_REJECT:
269 print_ref_status('!', "[remote rejected]", ref,
270 ref->deletion ? NULL : ref->peer_ref,
273 case REF_STATUS_EXPECTING_REPORT:
274 print_ref_status('!', "[remote failure]", ref,
275 ref->deletion ? NULL : ref->peer_ref,
276 "remote failed to report status");
279 print_ok_ref_status(ref);
286 static void print_push_status(const char *dest, struct ref *refs)
292 for (ref = refs; ref; ref = ref->next)
293 if (ref->status == REF_STATUS_UPTODATE)
294 n += print_one_push_status(ref, dest, n);
297 for (ref = refs; ref; ref = ref->next)
298 if (ref->status == REF_STATUS_OK)
299 n += print_one_push_status(ref, dest, n);
301 for (ref = refs; ref; ref = ref->next) {
302 if (ref->status != REF_STATUS_NONE &&
303 ref->status != REF_STATUS_UPTODATE &&
304 ref->status != REF_STATUS_OK)
305 n += print_one_push_status(ref, dest, n);
309 static int refs_pushed(struct ref *ref)
311 for (; ref; ref = ref->next) {
312 switch(ref->status) {
313 case REF_STATUS_NONE:
314 case REF_STATUS_UPTODATE:
323 static void print_helper_status(struct ref *ref)
325 struct strbuf buf = STRBUF_INIT;
327 for (; ref; ref = ref->next) {
328 const char *msg = NULL;
331 switch(ref->status) {
332 case REF_STATUS_NONE:
341 case REF_STATUS_UPTODATE:
346 case REF_STATUS_REJECT_NONFASTFORWARD:
348 msg = "non-fast forward";
351 case REF_STATUS_REJECT_NODELETE:
352 case REF_STATUS_REMOTE_REJECT:
356 case REF_STATUS_EXPECTING_REPORT:
362 strbuf_addf(&buf, "%s %s", res, ref->name);
363 if (ref->remote_status)
364 msg = ref->remote_status;
366 strbuf_addch(&buf, ' ');
367 quote_two_c_style(&buf, "", msg, 0);
369 strbuf_addch(&buf, '\n');
371 safe_write(1, buf.buf, buf.len);
373 strbuf_release(&buf);
376 static int sideband_demux(int in, int out, void *data)
382 int ret = recv_sideband("send-pack", fd[0], out);
387 int send_pack(struct send_pack_args *args,
388 int fd[], struct child_process *conn,
389 struct ref *remote_refs,
390 struct extra_have_objects *extra_have)
394 struct strbuf req_buf = STRBUF_INIT;
397 int allow_deleting_refs = 0;
398 int status_report = 0;
399 int use_sideband = 0;
400 unsigned cmds_sent = 0;
404 /* Does the other end support the reporting? */
405 if (server_supports("report-status"))
407 if (server_supports("delete-refs"))
408 allow_deleting_refs = 1;
409 if (server_supports("ofs-delta"))
410 args->use_ofs_delta = 1;
411 if (server_supports("side-band-64k"))
415 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
416 "Perhaps you should specify a branch such as 'master'.\n");
421 * Finally, tell the other end!
424 for (ref = remote_refs; ref; ref = ref->next) {
427 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
428 else if (!args->send_mirror)
431 ref->deletion = is_null_sha1(ref->new_sha1);
432 if (ref->deletion && !allow_deleting_refs) {
433 ref->status = REF_STATUS_REJECT_NODELETE;
436 if (!ref->deletion &&
437 !hashcmp(ref->old_sha1, ref->new_sha1)) {
438 ref->status = REF_STATUS_UPTODATE;
442 /* This part determines what can overwrite what.
445 * (0) you can always use --force or +A:B notation to
446 * selectively force individual ref pairs.
448 * (1) if the old thing does not exist, it is OK.
450 * (2) if you do not have the old thing, you are not allowed
451 * to overwrite it; you would not know what you are losing
454 * (3) if both new and old are commit-ish, and new is a
455 * descendant of old, it is OK.
457 * (4) regardless of all of the above, removing :B is
461 ref->nonfastforward =
463 !is_null_sha1(ref->old_sha1) &&
464 (!has_sha1_file(ref->old_sha1)
465 || !ref_newer(ref->new_sha1, ref->old_sha1));
467 if (ref->nonfastforward && !ref->force && !args->force_update) {
468 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
476 ref->status = REF_STATUS_OK;
478 char *old_hex = sha1_to_hex(ref->old_sha1);
479 char *new_hex = sha1_to_hex(ref->new_sha1);
481 if (!cmds_sent && (status_report || use_sideband)) {
482 packet_buf_write(&req_buf, "%s %s %s%c%s%s",
483 old_hex, new_hex, ref->name, 0,
484 status_report ? " report-status" : "",
485 use_sideband ? " side-band-64k" : "");
488 packet_buf_write(&req_buf, "%s %s %s",
489 old_hex, new_hex, ref->name);
490 ref->status = status_report ?
491 REF_STATUS_EXPECTING_REPORT :
497 if (args->stateless_rpc) {
498 if (!args->dry_run && cmds_sent) {
499 packet_buf_flush(&req_buf);
500 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
503 safe_write(out, req_buf.buf, req_buf.len);
506 strbuf_release(&req_buf);
508 if (use_sideband && cmds_sent) {
509 memset(&demux, 0, sizeof(demux));
510 demux.proc = sideband_demux;
513 if (start_async(&demux))
514 die("receive-pack: unable to fork off sideband demultiplexer");
518 if (new_refs && cmds_sent) {
519 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
520 for (ref = remote_refs; ref; ref = ref->next)
521 ref->status = REF_STATUS_NONE;
523 finish_async(&demux);
527 if (args->stateless_rpc && cmds_sent)
530 if (status_report && cmds_sent)
531 ret = receive_status(in, remote_refs);
534 if (args->stateless_rpc)
537 if (use_sideband && cmds_sent) {
538 if (finish_async(&demux)) {
539 error("error in sideband demultiplexer");
547 for (ref = remote_refs; ref; ref = ref->next) {
548 switch (ref->status) {
549 case REF_STATUS_NONE:
550 case REF_STATUS_UPTODATE:
560 static void verify_remote_names(int nr_heads, const char **heads)
564 for (i = 0; i < nr_heads; i++) {
565 const char *local = heads[i];
566 const char *remote = strrchr(heads[i], ':');
571 /* A matching refspec is okay. */
572 if (remote == local && remote[1] == '\0')
575 remote = remote ? (remote + 1) : local;
576 switch (check_ref_format(remote)) {
578 case CHECK_REF_FORMAT_ONELEVEL:
579 /* ok but a single level -- that is fine for
582 case CHECK_REF_FORMAT_WILDCARD:
583 /* ok but ends with a pattern-match character */
586 die("remote part of refspec is not a valid name in %s",
591 int cmd_send_pack(int argc, const char **argv, const char *prefix)
593 int i, nr_refspecs = 0;
594 const char **refspecs = NULL;
595 const char *remote_name = NULL;
596 struct remote *remote = NULL;
597 const char *dest = NULL;
599 struct child_process *conn;
600 struct extra_have_objects extra_have;
601 struct ref *remote_refs, *local_refs;
603 int helper_status = 0;
605 const char *receivepack = "git-receive-pack";
609 for (i = 1; i < argc; i++, argv++) {
610 const char *arg = *argv;
613 if (!prefixcmp(arg, "--receive-pack=")) {
614 receivepack = arg + 15;
617 if (!prefixcmp(arg, "--exec=")) {
618 receivepack = arg + 7;
621 if (!prefixcmp(arg, "--remote=")) {
622 remote_name = arg + 9;
625 if (!strcmp(arg, "--all")) {
629 if (!strcmp(arg, "--dry-run")) {
633 if (!strcmp(arg, "--mirror")) {
634 args.send_mirror = 1;
637 if (!strcmp(arg, "--force")) {
638 args.force_update = 1;
641 if (!strcmp(arg, "--verbose")) {
645 if (!strcmp(arg, "--thin")) {
646 args.use_thin_pack = 1;
649 if (!strcmp(arg, "--stateless-rpc")) {
650 args.stateless_rpc = 1;
653 if (!strcmp(arg, "--helper-status")) {
657 usage(send_pack_usage);
663 refspecs = (const char **) argv;
664 nr_refspecs = argc - i;
668 usage(send_pack_usage);
670 * --all and --mirror are incompatible; neither makes sense
673 if ((refspecs && (send_all || args.send_mirror)) ||
674 (send_all && args.send_mirror))
675 usage(send_pack_usage);
678 remote = remote_get(remote_name);
679 if (!remote_has_url(remote, dest)) {
680 die("Destination %s is not a uri for %s",
685 if (args.stateless_rpc) {
690 conn = git_connect(fd, dest, receivepack,
691 args.verbose ? CONNECT_VERBOSE : 0);
694 memset(&extra_have, 0, sizeof(extra_have));
696 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
699 verify_remote_names(nr_refspecs, refspecs);
701 local_refs = get_local_heads();
703 flags = MATCH_REFS_NONE;
706 flags |= MATCH_REFS_ALL;
707 if (args.send_mirror)
708 flags |= MATCH_REFS_MIRROR;
711 if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
714 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
717 print_helper_status(remote_refs);
722 ret |= finish_connect(conn);
725 print_push_status(dest, remote_refs);
727 if (!args.dry_run && remote) {
729 for (ref = remote_refs; ref; ref = ref->next)
730 update_tracking_ref(remote, ref);
733 if (!ret && !refs_pushed(remote_refs))
734 fprintf(stderr, "Everything up-to-date\n");