Merge commit 'junio/next' into next
[git/platforms/storm.git] / builtin-send-pack.c
blob8d1e7be0a8369c17712c14bea10a65565e7a95b5
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "run-command.h"
7 #include "remote.h"
8 #include "send-pack.h"
10 static const char send_pack_usage[] =
11 "git-send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
12 " --all and explicit <ref> specification are mutually exclusive.";
14 static struct send_pack_args args = {
15 /* .receivepack = */ "git-receive-pack",
18 static struct additional_base {
19 unsigned char tip[20];
20 unsigned char last[20];
21 struct commit *commit;
22 } *unknown;
23 static int unknown_nr;
24 static int unknown_alloc;
27 * Make a pack stream and spit it out into file descriptor fd
29 static int pack_objects(int fd, struct ref *refs)
32 * The child becomes pack-objects --revs; we feed
33 * the revision parameters to it via its stdin and
34 * let its stdout go back to the other end.
36 const char *argv[] = {
37 "pack-objects",
38 "--all-progress",
39 "--revs",
40 "--stdout",
41 NULL,
42 NULL,
44 struct child_process po;
46 if (args.use_thin_pack)
47 argv[4] = "--thin";
48 memset(&po, 0, sizeof(po));
49 po.argv = argv;
50 po.in = -1;
51 po.out = fd;
52 po.git_cmd = 1;
53 if (start_command(&po))
54 die("git-pack-objects failed (%s)", strerror(errno));
57 * We feed the pack-objects we just spawned with revision
58 * parameters by writing to the pipe.
60 while (refs) {
61 char buf[42];
63 if (!is_null_sha1(refs->old_sha1) &&
64 has_sha1_file(refs->old_sha1)) {
65 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
66 buf[0] = '^';
67 buf[41] = '\n';
68 if (!write_or_whine(po.in, buf, 42,
69 "send-pack: send refs"))
70 break;
72 if (!is_null_sha1(refs->new_sha1)) {
73 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
74 buf[40] = '\n';
75 if (!write_or_whine(po.in, buf, 41,
76 "send-pack: send refs"))
77 break;
79 refs = refs->next;
82 if (unknown) {
83 int i;
84 char buf[42];
86 for (i = 0; i < unknown_nr; i++) {
87 struct commit *c = unknown[i].commit;
88 if (!c)
89 continue;
90 memcpy(buf + 1, sha1_to_hex(c->object.sha1), 40);
91 buf[0] = '^';
92 buf[41] = '\n';
93 if (!write_or_whine(po.in, buf, 42,
94 "send-pack: send additional base"))
95 break;
99 close(po.in);
100 if (finish_command(&po))
101 return error("pack-objects died with strange error");
102 return 0;
105 static void unmark_and_free(struct commit_list *list, unsigned int mark)
107 while (list) {
108 struct commit_list *temp = list;
109 temp->item->object.flags &= ~mark;
110 list = temp->next;
111 free(temp);
115 static int ref_newer(const unsigned char *new_sha1,
116 const unsigned char *old_sha1)
118 struct object *o;
119 struct commit *old, *new;
120 struct commit_list *list, *used;
121 int found = 0;
123 /* Both new and old must be commit-ish and new is descendant of
124 * old. Otherwise we require --force.
126 o = deref_tag(parse_object(old_sha1), NULL, 0);
127 if (!o || o->type != OBJ_COMMIT)
128 return 0;
129 old = (struct commit *) o;
131 o = deref_tag(parse_object(new_sha1), NULL, 0);
132 if (!o || o->type != OBJ_COMMIT)
133 return 0;
134 new = (struct commit *) o;
136 if (parse_commit(new) < 0)
137 return 0;
139 used = list = NULL;
140 commit_list_insert(new, &list);
141 while (list) {
142 new = pop_most_recent_commit(&list, 1);
143 commit_list_insert(new, &used);
144 if (new == old) {
145 found = 1;
146 break;
149 unmark_and_free(list, 1);
150 unmark_and_free(used, 1);
151 return found;
154 static struct ref *local_refs, **local_tail;
155 static struct ref *remote_refs, **remote_tail;
157 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
159 struct ref *ref;
160 int len = strlen(refname) + 1;
161 ref = xcalloc(1, sizeof(*ref) + len);
162 hashcpy(ref->new_sha1, sha1);
163 memcpy(ref->name, refname, len);
164 *local_tail = ref;
165 local_tail = &ref->next;
166 return 0;
169 static void get_local_heads(void)
171 local_tail = &local_refs;
172 for_each_ref(one_local_ref, NULL);
175 static int receive_status(int in, struct ref *refs)
177 struct ref *hint;
178 char line[1000];
179 int ret = 0;
180 int len = packet_read_line(in, line, sizeof(line));
181 if (len < 10 || memcmp(line, "unpack ", 7))
182 return error("did not receive remote status");
183 if (memcmp(line, "unpack ok\n", 10)) {
184 char *p = line + strlen(line) - 1;
185 if (*p == '\n')
186 *p = '\0';
187 error("unpack failed: %s", line + 7);
188 ret = -1;
190 hint = NULL;
191 while (1) {
192 char *refname;
193 char *msg;
194 len = packet_read_line(in, line, sizeof(line));
195 if (!len)
196 break;
197 if (len < 3 ||
198 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
199 fprintf(stderr, "protocol error: %s\n", line);
200 ret = -1;
201 break;
204 line[strlen(line)-1] = '\0';
205 refname = line + 3;
206 msg = strchr(refname, ' ');
207 if (msg)
208 *msg++ = '\0';
210 /* first try searching at our hint, falling back to all refs */
211 if (hint)
212 hint = find_ref_by_name(hint, refname);
213 if (!hint)
214 hint = find_ref_by_name(refs, refname);
215 if (!hint) {
216 warning("remote reported status on unknown ref: %s",
217 refname);
218 continue;
220 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
221 warning("remote reported status on unexpected ref: %s",
222 refname);
223 continue;
226 if (line[0] == 'o' && line[1] == 'k')
227 hint->status = REF_STATUS_OK;
228 else {
229 hint->status = REF_STATUS_REMOTE_REJECT;
230 ret = -1;
232 if (msg)
233 hint->remote_status = xstrdup(msg);
234 /* start our next search from the next ref */
235 hint = hint->next;
237 return ret;
240 static void update_tracking_ref(struct remote *remote, struct ref *ref)
242 struct refspec rs;
244 if (ref->status != REF_STATUS_OK)
245 return;
247 rs.src = ref->name;
248 rs.dst = NULL;
250 if (!remote_find_tracking(remote, &rs)) {
251 if (args.verbose)
252 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
253 if (ref->deletion) {
254 if (delete_ref(rs.dst, NULL))
255 error("Failed to delete");
256 } else
257 update_ref("update by push", rs.dst,
258 ref->new_sha1, NULL, 0, 0);
259 free(rs.dst);
263 static const char *prettify_ref(const struct ref *ref)
265 const char *name = ref->name;
266 return name + (
267 !prefixcmp(name, "refs/heads/") ? 11 :
268 !prefixcmp(name, "refs/tags/") ? 10 :
269 !prefixcmp(name, "refs/remotes/") ? 13 :
273 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
275 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
277 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
278 if (from)
279 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
280 else
281 fputs(prettify_ref(to), stderr);
282 if (msg) {
283 fputs(" (", stderr);
284 fputs(msg, stderr);
285 fputc(')', stderr);
287 fputc('\n', stderr);
290 static const char *status_abbrev(unsigned char sha1[20])
292 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
295 static void print_ok_ref_status(struct ref *ref)
297 if (ref->deletion)
298 print_ref_status('-', "[deleted]", ref, NULL, NULL);
299 else if (is_null_sha1(ref->old_sha1))
300 print_ref_status('*',
301 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
302 "[new branch]"),
303 ref, ref->peer_ref, NULL);
304 else {
305 char quickref[84];
306 char type;
307 const char *msg;
309 strcpy(quickref, status_abbrev(ref->old_sha1));
310 if (ref->nonfastforward) {
311 strcat(quickref, "...");
312 type = '+';
313 msg = "forced update";
314 } else {
315 strcat(quickref, "..");
316 type = ' ';
317 msg = NULL;
319 strcat(quickref, status_abbrev(ref->new_sha1));
321 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
325 static int print_one_push_status(struct ref *ref, const char *dest, int count)
327 if (!count)
328 fprintf(stderr, "To %s\n", dest);
330 switch(ref->status) {
331 case REF_STATUS_NONE:
332 print_ref_status('X', "[no match]", ref, NULL, NULL);
333 break;
334 case REF_STATUS_REJECT_NODELETE:
335 print_ref_status('!', "[rejected]", ref, NULL,
336 "remote does not support deleting refs");
337 break;
338 case REF_STATUS_UPTODATE:
339 print_ref_status('=', "[up to date]", ref,
340 ref->peer_ref, NULL);
341 break;
342 case REF_STATUS_REJECT_NONFASTFORWARD:
343 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
344 "non-fast forward");
345 break;
346 case REF_STATUS_REMOTE_REJECT:
347 print_ref_status('!', "[remote rejected]", ref,
348 ref->deletion ? NULL : ref->peer_ref,
349 ref->remote_status);
350 break;
351 case REF_STATUS_EXPECTING_REPORT:
352 print_ref_status('!', "[remote failure]", ref,
353 ref->deletion ? NULL : ref->peer_ref,
354 "remote failed to report status");
355 break;
356 case REF_STATUS_OK:
357 print_ok_ref_status(ref);
358 break;
361 return 1;
364 static void print_push_status(const char *dest, struct ref *refs)
366 struct ref *ref;
367 int n = 0;
369 if (args.verbose) {
370 for (ref = refs; ref; ref = ref->next)
371 if (ref->status == REF_STATUS_UPTODATE)
372 n += print_one_push_status(ref, dest, n);
375 for (ref = refs; ref; ref = ref->next)
376 if (ref->status == REF_STATUS_OK)
377 n += print_one_push_status(ref, dest, n);
379 for (ref = refs; ref; ref = ref->next) {
380 if (ref->status != REF_STATUS_NONE &&
381 ref->status != REF_STATUS_UPTODATE &&
382 ref->status != REF_STATUS_OK)
383 n += print_one_push_status(ref, dest, n);
387 static int refs_pushed(struct ref *ref)
389 for (; ref; ref = ref->next) {
390 switch(ref->status) {
391 case REF_STATUS_NONE:
392 case REF_STATUS_UPTODATE:
393 break;
394 default:
395 return 1;
398 return 0;
401 static void ask_for_more(int in, int out, struct ref *remote_refs)
403 struct ref *refs;
404 int i;
406 for (refs = remote_refs; refs; refs = refs->next) {
407 struct commit *commit;
409 if (is_null_sha1(refs->old_sha1))
410 continue;
411 commit = lookup_commit_reference_gently(refs->old_sha1, 1);
412 if (!commit) {
413 ALLOC_GROW(unknown, unknown_nr+1, unknown_alloc);
414 hashcpy(unknown[unknown_nr].last, refs->old_sha1);
415 unknown[unknown_nr].commit = NULL;
416 unknown_nr++;
419 if (!unknown_nr) {
420 packet_flush(out);
421 return;
424 for (i = 0; i < 5; i++) {
425 int j, sent = 0;
426 for (j = 0; j < unknown_nr; j++) {
427 if (unknown[j].commit)
428 continue;
429 hashcpy(unknown[j].tip, unknown[j].last);
430 packet_write(out, "tellme-more %s\n",
431 sha1_to_hex(unknown[j].tip));
432 sent++;
434 packet_flush(out);
435 if (!sent)
436 return;
437 sent = 0;
438 while (1) {
439 char line[1000];
440 unsigned char tip[20];
441 unsigned char last[20];
442 int len;
443 struct commit *commit;
445 len = packet_read_line(in, line, sizeof(line));
446 if (!len)
447 break;
448 sent++;
449 if (line[len-1] == '\n')
450 line[--len] = '\0';
451 if (len < 81 ||
452 line[40] != ' ' ||
453 get_sha1_hex(line, last) ||
454 get_sha1_hex(line + 41, tip))
455 die("protocol error: got '%s'", line);
456 for (j = 0; j < unknown_nr; j++) {
457 if (unknown[j].commit ||
458 hashcmp(unknown[j].tip, tip))
459 continue;
460 hashcpy(unknown[j].last, last);
461 commit = lookup_commit_reference_gently(last, 1);
462 if (!commit)
463 continue;
464 unknown[j].commit = commit;
467 if (!sent)
468 /* they do not have any more to tell us */
469 break;
471 packet_flush(out);
474 static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
476 struct ref *ref;
477 int new_refs;
478 int ask_for_status_report = 0;
479 int allow_deleting_refs = 0;
480 int expect_status_report = 0;
481 int can_ask_for_more = 0;
482 int flags = MATCH_REFS_NONE;
483 int ret;
485 if (args.send_all)
486 flags |= MATCH_REFS_ALL;
487 if (args.send_mirror)
488 flags |= MATCH_REFS_MIRROR;
490 /* No funny business with the matcher */
491 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
492 get_local_heads();
494 /* Does the other end support the reporting? */
495 if (server_supports("report-status"))
496 ask_for_status_report = 1;
497 if (server_supports("delete-refs"))
498 allow_deleting_refs = 1;
499 if (server_supports("tellme-more"))
500 can_ask_for_more = 1;
502 /* match them up */
503 if (!remote_tail)
504 remote_tail = &remote_refs;
505 if (match_refs(local_refs, remote_refs, &remote_tail,
506 nr_refspec, refspec, flags)) {
507 close(out);
508 return -1;
511 if (!remote_refs) {
512 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
513 "Perhaps you should specify a branch such as 'master'.\n");
514 close(out);
515 return 0;
519 * Finally, tell the other end!
521 new_refs = 0;
522 for (ref = remote_refs; ref; ref = ref->next) {
523 const unsigned char *new_sha1;
525 if (!ref->peer_ref) {
526 if (!args.send_mirror)
527 continue;
528 new_sha1 = null_sha1;
530 else
531 new_sha1 = ref->peer_ref->new_sha1;
534 ref->deletion = is_null_sha1(new_sha1);
535 if (ref->deletion && !allow_deleting_refs) {
536 ref->status = REF_STATUS_REJECT_NODELETE;
537 continue;
539 if (!ref->deletion &&
540 !hashcmp(ref->old_sha1, new_sha1)) {
541 ref->status = REF_STATUS_UPTODATE;
542 continue;
545 /* This part determines what can overwrite what.
546 * The rules are:
548 * (0) you can always use --force or +A:B notation to
549 * selectively force individual ref pairs.
551 * (1) if the old thing does not exist, it is OK.
553 * (2) if you do not have the old thing, you are not allowed
554 * to overwrite it; you would not know what you are losing
555 * otherwise.
557 * (3) if both new and old are commit-ish, and new is a
558 * descendant of old, it is OK.
560 * (4) regardless of all of the above, removing :B is
561 * always allowed.
564 ref->nonfastforward =
565 !ref->deletion &&
566 !is_null_sha1(ref->old_sha1) &&
567 (!has_sha1_file(ref->old_sha1)
568 || !ref_newer(new_sha1, ref->old_sha1));
570 if (ref->nonfastforward && !ref->force && !args.force_update) {
571 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
572 continue;
575 hashcpy(ref->new_sha1, new_sha1);
576 if (!ref->deletion)
577 new_refs++;
579 if (!args.dry_run) {
580 char *old_hex = sha1_to_hex(ref->old_sha1);
581 char *new_hex = sha1_to_hex(ref->new_sha1);
583 if (ask_for_status_report) {
584 packet_write(out, "%s %s %s%c%s",
585 old_hex, new_hex, ref->name, 0,
586 "report-status");
587 ask_for_status_report = 0;
588 expect_status_report = 1;
590 else
591 packet_write(out, "%s %s %s",
592 old_hex, new_hex, ref->name);
594 ref->status = expect_status_report ?
595 REF_STATUS_EXPECTING_REPORT :
596 REF_STATUS_OK;
599 if (can_ask_for_more && new_refs)
600 ask_for_more(in, out, remote_refs);
601 else
602 packet_flush(out);
604 if (new_refs && !args.dry_run) {
605 if (pack_objects(out, remote_refs) < 0)
606 return -1;
608 else
609 close(out);
611 if (expect_status_report)
612 ret = receive_status(in, remote_refs);
613 else
614 ret = 0;
616 print_push_status(dest, remote_refs);
618 if (!args.dry_run && remote) {
619 for (ref = remote_refs; ref; ref = ref->next)
620 update_tracking_ref(remote, ref);
623 if (!refs_pushed(remote_refs))
624 fprintf(stderr, "Everything up-to-date\n");
625 if (ret < 0)
626 return ret;
627 for (ref = remote_refs; ref; ref = ref->next) {
628 switch (ref->status) {
629 case REF_STATUS_NONE:
630 case REF_STATUS_UPTODATE:
631 case REF_STATUS_OK:
632 break;
633 default:
634 return -1;
637 return 0;
640 static void verify_remote_names(int nr_heads, const char **heads)
642 int i;
644 for (i = 0; i < nr_heads; i++) {
645 const char *local = heads[i];
646 const char *remote = strrchr(heads[i], ':');
648 if (*local == '+')
649 local++;
651 /* A matching refspec is okay. */
652 if (remote == local && remote[1] == '\0')
653 continue;
655 remote = remote ? (remote + 1) : local;
656 switch (check_ref_format(remote)) {
657 case 0: /* ok */
658 case CHECK_REF_FORMAT_ONELEVEL:
659 /* ok but a single level -- that is fine for
660 * a match pattern.
662 case CHECK_REF_FORMAT_WILDCARD:
663 /* ok but ends with a pattern-match character */
664 continue;
666 die("remote part of refspec is not a valid name in %s",
667 heads[i]);
671 int cmd_send_pack(int argc, const char **argv, const char *prefix)
673 int i, nr_heads = 0;
674 const char **heads = NULL;
675 const char *remote_name = NULL;
676 struct remote *remote = NULL;
677 const char *dest = NULL;
679 argv++;
680 for (i = 1; i < argc; i++, argv++) {
681 const char *arg = *argv;
683 if (*arg == '-') {
684 if (!prefixcmp(arg, "--receive-pack=")) {
685 args.receivepack = arg + 15;
686 continue;
688 if (!prefixcmp(arg, "--exec=")) {
689 args.receivepack = arg + 7;
690 continue;
692 if (!prefixcmp(arg, "--remote=")) {
693 remote_name = arg + 9;
694 continue;
696 if (!strcmp(arg, "--all")) {
697 args.send_all = 1;
698 continue;
700 if (!strcmp(arg, "--dry-run")) {
701 args.dry_run = 1;
702 continue;
704 if (!strcmp(arg, "--mirror")) {
705 args.send_mirror = 1;
706 continue;
708 if (!strcmp(arg, "--force")) {
709 args.force_update = 1;
710 continue;
712 if (!strcmp(arg, "--verbose")) {
713 args.verbose = 1;
714 continue;
716 if (!strcmp(arg, "--thin")) {
717 args.use_thin_pack = 1;
718 continue;
720 usage(send_pack_usage);
722 if (!dest) {
723 dest = arg;
724 continue;
726 heads = (const char **) argv;
727 nr_heads = argc - i;
728 break;
730 if (!dest)
731 usage(send_pack_usage);
733 * --all and --mirror are incompatible; neither makes sense
734 * with any refspecs.
736 if ((heads && (args.send_all || args.send_mirror)) ||
737 (args.send_all && args.send_mirror))
738 usage(send_pack_usage);
740 if (remote_name) {
741 remote = remote_get(remote_name);
742 if (!remote_has_url(remote, dest)) {
743 die("Destination %s is not a uri for %s",
744 dest, remote_name);
748 return send_pack(&args, dest, remote, nr_heads, heads);
751 int send_pack(struct send_pack_args *my_args,
752 const char *dest, struct remote *remote,
753 int nr_heads, const char **heads)
755 int fd[2], ret;
756 struct child_process *conn;
758 memcpy(&args, my_args, sizeof(args));
760 verify_remote_names(nr_heads, heads);
762 conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
763 ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads);
764 close(fd[0]);
765 /* do_send_pack always closes fd[1] */
766 ret |= finish_connect(conn);
767 return !!ret;