t5516: Use test_cmp when appropriate
[git/mingw.git] / builtin-send-pack.c
blob078bc3e8ec2c19f7fc5e8688cccd3ac4679c7a51
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"
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)
19 char buf[42];
21 if (negative && !has_sha1_file(sha1))
22 return 1;
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");
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[] = {
42 "pack-objects",
43 "--all-progress-implied",
44 "--revs",
45 "--stdout",
46 NULL,
47 NULL,
48 NULL,
49 NULL,
51 struct child_process po;
52 int i;
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");
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))
75 break;
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;
87 close(po.in);
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);
97 free(buf);
98 close(po.out);
99 po.out = -1;
102 if (finish_command(&po))
103 return error("pack-objects died with strange error");
104 return 0;
107 static int receive_status(int in, struct ref *refs)
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;
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;
136 line[strlen(line)-1] = '\0';
137 refname = line + 3;
138 msg = strchr(refname, ' ');
139 if (msg)
140 *msg++ = '\0';
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;
152 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
153 warning("remote reported status on unexpected ref: %s",
154 refname);
155 continue;
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;
164 if (msg)
165 hint->remote_status = xstrdup(msg);
166 /* start our next search from the next ref */
167 hint = hint->next;
169 return ret;
172 static void update_tracking_ref(struct remote *remote, struct ref *ref)
174 struct refspec rs;
176 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
177 return;
179 rs.src = ref->name;
180 rs.dst = NULL;
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);
194 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
196 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
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);
208 fputc('\n', stderr);
211 static const char *status_abbrev(unsigned char sha1[20])
213 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
216 static void print_ok_ref_status(struct ref *ref)
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;
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;
240 strcat(quickref, status_abbrev(ref->new_sha1));
242 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
246 static int print_one_push_status(struct ref *ref, const char *dest, int count)
248 if (!count)
249 fprintf(stderr, "To %s\n", dest);
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;
282 return 1;
285 static void print_push_status(const char *dest, struct ref *refs)
287 struct ref *ref;
288 int n = 0;
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);
296 for (ref = refs; ref; ref = ref->next)
297 if (ref->status == REF_STATUS_OK)
298 n += print_one_push_status(ref, dest, n);
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);
308 static int refs_pushed(struct ref *ref)
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;
319 return 0;
322 static void print_helper_status(struct ref *ref)
324 struct strbuf buf = STRBUF_INIT;
326 for (; ref; ref = ref->next) {
327 const char *msg = NULL;
328 const char *res;
330 switch(ref->status) {
331 case REF_STATUS_NONE:
332 res = "error";
333 msg = "no match";
334 break;
336 case REF_STATUS_OK:
337 res = "ok";
338 break;
340 case REF_STATUS_UPTODATE:
341 res = "ok";
342 msg = "up to date";
343 break;
345 case REF_STATUS_REJECT_NONFASTFORWARD:
346 res = "error";
347 msg = "non-fast forward";
348 break;
350 case REF_STATUS_REJECT_NODELETE:
351 case REF_STATUS_REMOTE_REJECT:
352 res = "error";
353 break;
355 case REF_STATUS_EXPECTING_REPORT:
356 default:
357 continue;
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);
368 strbuf_addch(&buf, '\n');
370 safe_write(1, buf.buf, buf.len);
372 strbuf_release(&buf);
375 int send_pack(struct send_pack_args *args,
376 int fd[], struct child_process *conn,
377 struct ref *remote_refs,
378 struct extra_have_objects *extra_have)
380 int in = fd[0];
381 int out = fd[1];
382 struct strbuf req_buf = STRBUF_INIT;
383 struct ref *ref;
384 int new_refs;
385 int ask_for_status_report = 0;
386 int allow_deleting_refs = 0;
387 int expect_status_report = 0;
388 int ret;
390 /* Does the other end support the reporting? */
391 if (server_supports("report-status"))
392 ask_for_status_report = 1;
393 if (server_supports("delete-refs"))
394 allow_deleting_refs = 1;
395 if (server_supports("ofs-delta"))
396 args->use_ofs_delta = 1;
398 if (!remote_refs) {
399 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
400 "Perhaps you should specify a branch such as 'master'.\n");
401 return 0;
405 * Finally, tell the other end!
407 new_refs = 0;
408 for (ref = remote_refs; ref; ref = ref->next) {
409 if (!ref->peer_ref && !args->send_mirror)
410 continue;
412 /* Check for statuses set by set_ref_status_for_push() */
413 switch (ref->status) {
414 case REF_STATUS_REJECT_NONFASTFORWARD:
415 case REF_STATUS_UPTODATE:
416 continue;
417 default:
418 ; /* do nothing */
421 if (ref->deletion && !allow_deleting_refs) {
422 ref->status = REF_STATUS_REJECT_NODELETE;
423 continue;
426 if (!ref->deletion)
427 new_refs++;
429 if (!args->dry_run) {
430 char *old_hex = sha1_to_hex(ref->old_sha1);
431 char *new_hex = sha1_to_hex(ref->new_sha1);
433 if (ask_for_status_report) {
434 packet_buf_write(&req_buf, "%s %s %s%c%s",
435 old_hex, new_hex, ref->name, 0,
436 "report-status");
437 ask_for_status_report = 0;
438 expect_status_report = 1;
440 else
441 packet_buf_write(&req_buf, "%s %s %s",
442 old_hex, new_hex, ref->name);
444 ref->status = expect_status_report ?
445 REF_STATUS_EXPECTING_REPORT :
446 REF_STATUS_OK;
449 if (args->stateless_rpc) {
450 if (!args->dry_run) {
451 packet_buf_flush(&req_buf);
452 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
454 } else {
455 safe_write(out, req_buf.buf, req_buf.len);
456 packet_flush(out);
458 strbuf_release(&req_buf);
460 if (new_refs && !args->dry_run) {
461 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
462 for (ref = remote_refs; ref; ref = ref->next)
463 ref->status = REF_STATUS_NONE;
464 return -1;
467 if (args->stateless_rpc && !args->dry_run)
468 packet_flush(out);
470 if (expect_status_report)
471 ret = receive_status(in, remote_refs);
472 else
473 ret = 0;
474 if (args->stateless_rpc)
475 packet_flush(out);
477 if (ret < 0)
478 return ret;
480 if (args->porcelain)
481 return 0;
483 for (ref = remote_refs; ref; ref = ref->next) {
484 switch (ref->status) {
485 case REF_STATUS_NONE:
486 case REF_STATUS_UPTODATE:
487 case REF_STATUS_OK:
488 break;
489 default:
490 return -1;
493 return 0;
496 static void verify_remote_names(int nr_heads, const char **heads)
498 int i;
500 for (i = 0; i < nr_heads; i++) {
501 const char *local = heads[i];
502 const char *remote = strrchr(heads[i], ':');
504 if (*local == '+')
505 local++;
507 /* A matching refspec is okay. */
508 if (remote == local && remote[1] == '\0')
509 continue;
511 remote = remote ? (remote + 1) : local;
512 switch (check_ref_format(remote)) {
513 case 0: /* ok */
514 case CHECK_REF_FORMAT_ONELEVEL:
515 /* ok but a single level -- that is fine for
516 * a match pattern.
518 case CHECK_REF_FORMAT_WILDCARD:
519 /* ok but ends with a pattern-match character */
520 continue;
522 die("remote part of refspec is not a valid name in %s",
523 heads[i]);
527 int cmd_send_pack(int argc, const char **argv, const char *prefix)
529 int i, nr_refspecs = 0;
530 const char **refspecs = NULL;
531 const char *remote_name = NULL;
532 struct remote *remote = NULL;
533 const char *dest = NULL;
534 int fd[2];
535 struct child_process *conn;
536 struct extra_have_objects extra_have;
537 struct ref *remote_refs, *local_refs;
538 int ret;
539 int helper_status = 0;
540 int send_all = 0;
541 const char *receivepack = "git-receive-pack";
542 int flags;
544 argv++;
545 for (i = 1; i < argc; i++, argv++) {
546 const char *arg = *argv;
548 if (*arg == '-') {
549 if (!prefixcmp(arg, "--receive-pack=")) {
550 receivepack = arg + 15;
551 continue;
553 if (!prefixcmp(arg, "--exec=")) {
554 receivepack = arg + 7;
555 continue;
557 if (!prefixcmp(arg, "--remote=")) {
558 remote_name = arg + 9;
559 continue;
561 if (!strcmp(arg, "--all")) {
562 send_all = 1;
563 continue;
565 if (!strcmp(arg, "--dry-run")) {
566 args.dry_run = 1;
567 continue;
569 if (!strcmp(arg, "--mirror")) {
570 args.send_mirror = 1;
571 continue;
573 if (!strcmp(arg, "--force")) {
574 args.force_update = 1;
575 continue;
577 if (!strcmp(arg, "--verbose")) {
578 args.verbose = 1;
579 continue;
581 if (!strcmp(arg, "--thin")) {
582 args.use_thin_pack = 1;
583 continue;
585 if (!strcmp(arg, "--stateless-rpc")) {
586 args.stateless_rpc = 1;
587 continue;
589 if (!strcmp(arg, "--helper-status")) {
590 helper_status = 1;
591 continue;
593 usage(send_pack_usage);
595 if (!dest) {
596 dest = arg;
597 continue;
599 refspecs = (const char **) argv;
600 nr_refspecs = argc - i;
601 break;
603 if (!dest)
604 usage(send_pack_usage);
606 * --all and --mirror are incompatible; neither makes sense
607 * with any refspecs.
609 if ((refspecs && (send_all || args.send_mirror)) ||
610 (send_all && args.send_mirror))
611 usage(send_pack_usage);
613 if (remote_name) {
614 remote = remote_get(remote_name);
615 if (!remote_has_url(remote, dest)) {
616 die("Destination %s is not a uri for %s",
617 dest, remote_name);
621 if (args.stateless_rpc) {
622 conn = NULL;
623 fd[0] = 0;
624 fd[1] = 1;
625 } else {
626 conn = git_connect(fd, dest, receivepack,
627 args.verbose ? CONNECT_VERBOSE : 0);
630 memset(&extra_have, 0, sizeof(extra_have));
632 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
633 &extra_have);
635 verify_remote_names(nr_refspecs, refspecs);
637 local_refs = get_local_heads();
639 flags = MATCH_REFS_NONE;
641 if (send_all)
642 flags |= MATCH_REFS_ALL;
643 if (args.send_mirror)
644 flags |= MATCH_REFS_MIRROR;
646 /* match them up */
647 if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
648 return -1;
650 set_ref_status_for_push(remote_refs, args.send_mirror,
651 args.force_update);
653 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
655 if (helper_status)
656 print_helper_status(remote_refs);
658 close(fd[1]);
659 close(fd[0]);
661 ret |= finish_connect(conn);
663 if (!helper_status)
664 print_push_status(dest, remote_refs);
666 if (!args.dry_run && remote) {
667 struct ref *ref;
668 for (ref = remote_refs; ref; ref = ref->next)
669 update_tracking_ref(remote, ref);
672 if (!ret && !refs_pushed(remote_refs))
673 fprintf(stderr, "Everything up-to-date\n");
675 return ret;