Merge commit 'refs/top-bases/girocco' into girocco
[git/repo.git] / builtin-send-pack.c
blob910db92b62eb6dd91a4002b2643fef4a76ec8f83
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",
19 * Make a pack stream and spit it out into file descriptor fd
21 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra)
24 * The child becomes pack-objects --revs; we feed
25 * the revision parameters to it via its stdin and
26 * let its stdout go back to the other end.
28 const char *argv[] = {
29 "pack-objects",
30 "--all-progress",
31 "--revs",
32 "--stdout",
33 NULL,
34 NULL,
36 struct child_process po;
37 int i;
38 char buf[42];
40 if (args.use_thin_pack)
41 argv[4] = "--thin";
42 memset(&po, 0, sizeof(po));
43 po.argv = argv;
44 po.in = -1;
45 po.out = fd;
46 po.git_cmd = 1;
47 if (start_command(&po))
48 die("git pack-objects failed (%s)", strerror(errno));
51 * We feed the pack-objects we just spawned with revision
52 * parameters by writing to the pipe.
54 for (i = 0; i < extra->nr; i++) {
55 memcpy(buf + 1, sha1_to_hex(&extra->array[i][0]), 40);
56 buf[0] = '^';
57 buf[41] = '\n';
58 if (!write_or_whine(po.in, buf, 42, "send-pack: send refs"))
59 break;
62 while (refs) {
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 close(po.in);
83 if (finish_command(&po))
84 return error("pack-objects died with strange error");
85 return 0;
88 static void unmark_and_free(struct commit_list *list, unsigned int mark)
90 while (list) {
91 struct commit_list *temp = list;
92 temp->item->object.flags &= ~mark;
93 list = temp->next;
94 free(temp);
98 static int ref_newer(const unsigned char *new_sha1,
99 const unsigned char *old_sha1)
101 struct object *o;
102 struct commit *old, *new;
103 struct commit_list *list, *used;
104 int found = 0;
106 /* Both new and old must be commit-ish and new is descendant of
107 * old. Otherwise we require --force.
109 o = deref_tag(parse_object(old_sha1), NULL, 0);
110 if (!o || o->type != OBJ_COMMIT)
111 return 0;
112 old = (struct commit *) o;
114 o = deref_tag(parse_object(new_sha1), NULL, 0);
115 if (!o || o->type != OBJ_COMMIT)
116 return 0;
117 new = (struct commit *) o;
119 if (parse_commit(new) < 0)
120 return 0;
122 used = list = NULL;
123 commit_list_insert(new, &list);
124 while (list) {
125 new = pop_most_recent_commit(&list, 1);
126 commit_list_insert(new, &used);
127 if (new == old) {
128 found = 1;
129 break;
132 unmark_and_free(list, 1);
133 unmark_and_free(used, 1);
134 return found;
137 static struct ref *local_refs, **local_tail;
138 static struct ref *remote_refs, **remote_tail;
140 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
142 struct ref *ref;
143 int len = strlen(refname) + 1;
144 ref = xcalloc(1, sizeof(*ref) + len);
145 hashcpy(ref->new_sha1, sha1);
146 memcpy(ref->name, refname, len);
147 *local_tail = ref;
148 local_tail = &ref->next;
149 return 0;
152 static void get_local_heads(void)
154 local_tail = &local_refs;
155 for_each_ref(one_local_ref, NULL);
158 static int receive_status(int in, struct ref *refs)
160 struct ref *hint;
161 char line[1000];
162 int ret = 0;
163 int len = packet_read_line(in, line, sizeof(line));
164 if (len < 10 || memcmp(line, "unpack ", 7))
165 return error("did not receive remote status");
166 if (memcmp(line, "unpack ok\n", 10)) {
167 char *p = line + strlen(line) - 1;
168 if (*p == '\n')
169 *p = '\0';
170 error("unpack failed: %s", line + 7);
171 ret = -1;
173 hint = NULL;
174 while (1) {
175 char *refname;
176 char *msg;
177 len = packet_read_line(in, line, sizeof(line));
178 if (!len)
179 break;
180 if (len < 3 ||
181 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
182 fprintf(stderr, "protocol error: %s\n", line);
183 ret = -1;
184 break;
187 line[strlen(line)-1] = '\0';
188 refname = line + 3;
189 msg = strchr(refname, ' ');
190 if (msg)
191 *msg++ = '\0';
193 /* first try searching at our hint, falling back to all refs */
194 if (hint)
195 hint = find_ref_by_name(hint, refname);
196 if (!hint)
197 hint = find_ref_by_name(refs, refname);
198 if (!hint) {
199 warning("remote reported status on unknown ref: %s",
200 refname);
201 continue;
203 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
204 warning("remote reported status on unexpected ref: %s",
205 refname);
206 continue;
209 if (line[0] == 'o' && line[1] == 'k')
210 hint->status = REF_STATUS_OK;
211 else {
212 hint->status = REF_STATUS_REMOTE_REJECT;
213 ret = -1;
215 if (msg)
216 hint->remote_status = xstrdup(msg);
217 /* start our next search from the next ref */
218 hint = hint->next;
220 return ret;
223 static void update_tracking_ref(struct remote *remote, struct ref *ref)
225 struct refspec rs;
227 if (ref->status != REF_STATUS_OK)
228 return;
230 rs.src = ref->name;
231 rs.dst = NULL;
233 if (!remote_find_tracking(remote, &rs)) {
234 if (args.verbose)
235 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
236 if (ref->deletion) {
237 delete_ref(rs.dst, NULL);
238 } else
239 update_ref("update by push", rs.dst,
240 ref->new_sha1, NULL, 0, 0);
241 free(rs.dst);
245 static const char *prettify_ref(const struct ref *ref)
247 const char *name = ref->name;
248 return name + (
249 !prefixcmp(name, "refs/heads/") ? 11 :
250 !prefixcmp(name, "refs/tags/") ? 10 :
251 !prefixcmp(name, "refs/remotes/") ? 13 :
255 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
257 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
259 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
260 if (from)
261 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
262 else
263 fputs(prettify_ref(to), stderr);
264 if (msg) {
265 fputs(" (", stderr);
266 fputs(msg, stderr);
267 fputc(')', stderr);
269 fputc('\n', stderr);
272 static const char *status_abbrev(unsigned char sha1[20])
274 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
277 static void print_ok_ref_status(struct ref *ref)
279 if (ref->deletion)
280 print_ref_status('-', "[deleted]", ref, NULL, NULL);
281 else if (is_null_sha1(ref->old_sha1))
282 print_ref_status('*',
283 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
284 "[new branch]"),
285 ref, ref->peer_ref, NULL);
286 else {
287 char quickref[84];
288 char type;
289 const char *msg;
291 strcpy(quickref, status_abbrev(ref->old_sha1));
292 if (ref->nonfastforward) {
293 strcat(quickref, "...");
294 type = '+';
295 msg = "forced update";
296 } else {
297 strcat(quickref, "..");
298 type = ' ';
299 msg = NULL;
301 strcat(quickref, status_abbrev(ref->new_sha1));
303 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
307 static int print_one_push_status(struct ref *ref, const char *dest, int count)
309 if (!count)
310 fprintf(stderr, "To %s\n", dest);
312 switch(ref->status) {
313 case REF_STATUS_NONE:
314 print_ref_status('X', "[no match]", ref, NULL, NULL);
315 break;
316 case REF_STATUS_REJECT_NODELETE:
317 print_ref_status('!', "[rejected]", ref, NULL,
318 "remote does not support deleting refs");
319 break;
320 case REF_STATUS_UPTODATE:
321 print_ref_status('=', "[up to date]", ref,
322 ref->peer_ref, NULL);
323 break;
324 case REF_STATUS_REJECT_NONFASTFORWARD:
325 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
326 "non-fast forward");
327 break;
328 case REF_STATUS_REMOTE_REJECT:
329 print_ref_status('!', "[remote rejected]", ref,
330 ref->deletion ? NULL : ref->peer_ref,
331 ref->remote_status);
332 break;
333 case REF_STATUS_EXPECTING_REPORT:
334 print_ref_status('!', "[remote failure]", ref,
335 ref->deletion ? NULL : ref->peer_ref,
336 "remote failed to report status");
337 break;
338 case REF_STATUS_OK:
339 print_ok_ref_status(ref);
340 break;
343 return 1;
346 static void print_push_status(const char *dest, struct ref *refs)
348 struct ref *ref;
349 int n = 0;
351 if (args.verbose) {
352 for (ref = refs; ref; ref = ref->next)
353 if (ref->status == REF_STATUS_UPTODATE)
354 n += print_one_push_status(ref, dest, n);
357 for (ref = refs; ref; ref = ref->next)
358 if (ref->status == REF_STATUS_OK)
359 n += print_one_push_status(ref, dest, n);
361 for (ref = refs; ref; ref = ref->next) {
362 if (ref->status != REF_STATUS_NONE &&
363 ref->status != REF_STATUS_UPTODATE &&
364 ref->status != REF_STATUS_OK)
365 n += print_one_push_status(ref, dest, n);
369 static int refs_pushed(struct ref *ref)
371 for (; ref; ref = ref->next) {
372 switch(ref->status) {
373 case REF_STATUS_NONE:
374 case REF_STATUS_UPTODATE:
375 break;
376 default:
377 return 1;
380 return 0;
383 static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
385 struct ref *ref;
386 int new_refs;
387 int ask_for_status_report = 0;
388 int allow_deleting_refs = 0;
389 int expect_status_report = 0;
390 int flags = MATCH_REFS_NONE;
391 int ret;
392 struct extra_have_objects extra_have;
394 memset(&extra_have, 0, sizeof(extra_have));
395 if (args.send_all)
396 flags |= MATCH_REFS_ALL;
397 if (args.send_mirror)
398 flags |= MATCH_REFS_MIRROR;
400 /* No funny business with the matcher */
401 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL,
402 &extra_have);
403 get_local_heads();
405 /* Does the other end support the reporting? */
406 if (server_supports("report-status"))
407 ask_for_status_report = 1;
408 if (server_supports("delete-refs"))
409 allow_deleting_refs = 1;
411 /* match them up */
412 if (!remote_tail)
413 remote_tail = &remote_refs;
414 if (match_refs(local_refs, remote_refs, &remote_tail,
415 nr_refspec, refspec, flags)) {
416 close(out);
417 return -1;
420 if (!remote_refs) {
421 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
422 "Perhaps you should specify a branch such as 'master'.\n");
423 close(out);
424 return 0;
428 * Finally, tell the other end!
430 new_refs = 0;
431 for (ref = remote_refs; ref; ref = ref->next) {
432 const unsigned char *new_sha1;
434 if (!ref->peer_ref) {
435 if (!args.send_mirror)
436 continue;
437 new_sha1 = null_sha1;
439 else
440 new_sha1 = ref->peer_ref->new_sha1;
443 ref->deletion = is_null_sha1(new_sha1);
444 if (ref->deletion && !allow_deleting_refs) {
445 ref->status = REF_STATUS_REJECT_NODELETE;
446 continue;
448 if (!ref->deletion &&
449 !hashcmp(ref->old_sha1, new_sha1)) {
450 ref->status = REF_STATUS_UPTODATE;
451 continue;
454 /* This part determines what can overwrite what.
455 * The rules are:
457 * (0) you can always use --force or +A:B notation to
458 * selectively force individual ref pairs.
460 * (1) if the old thing does not exist, it is OK.
462 * (2) if you do not have the old thing, you are not allowed
463 * to overwrite it; you would not know what you are losing
464 * otherwise.
466 * (3) if both new and old are commit-ish, and new is a
467 * descendant of old, it is OK.
469 * (4) regardless of all of the above, removing :B is
470 * always allowed.
473 ref->nonfastforward =
474 !ref->deletion &&
475 !is_null_sha1(ref->old_sha1) &&
476 (!has_sha1_file(ref->old_sha1)
477 || !ref_newer(new_sha1, ref->old_sha1));
479 if (ref->nonfastforward && !ref->force && !args.force_update) {
480 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
481 continue;
484 hashcpy(ref->new_sha1, new_sha1);
485 if (!ref->deletion)
486 new_refs++;
488 if (!args.dry_run) {
489 char *old_hex = sha1_to_hex(ref->old_sha1);
490 char *new_hex = sha1_to_hex(ref->new_sha1);
492 if (ask_for_status_report) {
493 packet_write(out, "%s %s %s%c%s",
494 old_hex, new_hex, ref->name, 0,
495 "report-status");
496 ask_for_status_report = 0;
497 expect_status_report = 1;
499 else
500 packet_write(out, "%s %s %s",
501 old_hex, new_hex, ref->name);
503 ref->status = expect_status_report ?
504 REF_STATUS_EXPECTING_REPORT :
505 REF_STATUS_OK;
508 packet_flush(out);
509 if (new_refs && !args.dry_run) {
510 if (pack_objects(out, remote_refs, &extra_have) < 0)
511 return -1;
513 else
514 close(out);
516 if (expect_status_report)
517 ret = receive_status(in, remote_refs);
518 else
519 ret = 0;
521 print_push_status(dest, remote_refs);
523 if (!args.dry_run && remote) {
524 for (ref = remote_refs; ref; ref = ref->next)
525 update_tracking_ref(remote, ref);
528 if (!refs_pushed(remote_refs))
529 fprintf(stderr, "Everything up-to-date\n");
530 if (ret < 0)
531 return ret;
532 for (ref = remote_refs; ref; ref = ref->next) {
533 switch (ref->status) {
534 case REF_STATUS_NONE:
535 case REF_STATUS_UPTODATE:
536 case REF_STATUS_OK:
537 break;
538 default:
539 return -1;
542 return 0;
545 static void verify_remote_names(int nr_heads, const char **heads)
547 int i;
549 for (i = 0; i < nr_heads; i++) {
550 const char *local = heads[i];
551 const char *remote = strrchr(heads[i], ':');
553 if (*local == '+')
554 local++;
556 /* A matching refspec is okay. */
557 if (remote == local && remote[1] == '\0')
558 continue;
560 remote = remote ? (remote + 1) : local;
561 switch (check_ref_format(remote)) {
562 case 0: /* ok */
563 case CHECK_REF_FORMAT_ONELEVEL:
564 /* ok but a single level -- that is fine for
565 * a match pattern.
567 case CHECK_REF_FORMAT_WILDCARD:
568 /* ok but ends with a pattern-match character */
569 continue;
571 die("remote part of refspec is not a valid name in %s",
572 heads[i]);
576 int cmd_send_pack(int argc, const char **argv, const char *prefix)
578 int i, nr_heads = 0;
579 const char **heads = NULL;
580 const char *remote_name = NULL;
581 struct remote *remote = NULL;
582 const char *dest = NULL;
584 argv++;
585 for (i = 1; i < argc; i++, argv++) {
586 const char *arg = *argv;
588 if (*arg == '-') {
589 if (!prefixcmp(arg, "--receive-pack=")) {
590 args.receivepack = arg + 15;
591 continue;
593 if (!prefixcmp(arg, "--exec=")) {
594 args.receivepack = arg + 7;
595 continue;
597 if (!prefixcmp(arg, "--remote=")) {
598 remote_name = arg + 9;
599 continue;
601 if (!strcmp(arg, "--all")) {
602 args.send_all = 1;
603 continue;
605 if (!strcmp(arg, "--dry-run")) {
606 args.dry_run = 1;
607 continue;
609 if (!strcmp(arg, "--mirror")) {
610 args.send_mirror = 1;
611 continue;
613 if (!strcmp(arg, "--force")) {
614 args.force_update = 1;
615 continue;
617 if (!strcmp(arg, "--verbose")) {
618 args.verbose = 1;
619 continue;
621 if (!strcmp(arg, "--thin")) {
622 args.use_thin_pack = 1;
623 continue;
625 usage(send_pack_usage);
627 if (!dest) {
628 dest = arg;
629 continue;
631 heads = (const char **) argv;
632 nr_heads = argc - i;
633 break;
635 if (!dest)
636 usage(send_pack_usage);
638 * --all and --mirror are incompatible; neither makes sense
639 * with any refspecs.
641 if ((heads && (args.send_all || args.send_mirror)) ||
642 (args.send_all && args.send_mirror))
643 usage(send_pack_usage);
645 if (remote_name) {
646 remote = remote_get(remote_name);
647 if (!remote_has_url(remote, dest)) {
648 die("Destination %s is not a uri for %s",
649 dest, remote_name);
653 return send_pack(&args, dest, remote, nr_heads, heads);
656 int send_pack(struct send_pack_args *my_args,
657 const char *dest, struct remote *remote,
658 int nr_heads, const char **heads)
660 int fd[2], ret;
661 struct child_process *conn;
663 memcpy(&args, my_args, sizeof(args));
665 verify_remote_names(nr_heads, heads);
667 conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
668 ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads);
669 close(fd[0]);
670 /* do_send_pack always closes fd[1] */
671 ret |= finish_connect(conn);
672 return !!ret;