Change current mailmap usage to do matching on both name and email of author/committer.
[alt-git.git] / builtin-send-pack.c
blobd65d01969252332eeee12b0419e4ba3a806952b1
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 int feed_object(const unsigned char *sha1, int fd, int negative)
20 char buf[42];
22 if (negative && !has_sha1_file(sha1))
23 return 1;
25 memcpy(buf + negative, sha1_to_hex(sha1), 40);
26 if (negative)
27 buf[0] = '^';
28 buf[40 + negative] = '\n';
29 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
33 * Make a pack stream and spit it out into file descriptor fd
35 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra)
38 * The child becomes pack-objects --revs; we feed
39 * the revision parameters to it via its stdin and
40 * let its stdout go back to the other end.
42 const char *argv[] = {
43 "pack-objects",
44 "--all-progress",
45 "--revs",
46 "--stdout",
47 NULL,
48 NULL,
50 struct child_process po;
51 int i;
53 if (args.use_thin_pack)
54 argv[4] = "--thin";
55 memset(&po, 0, sizeof(po));
56 po.argv = argv;
57 po.in = -1;
58 po.out = fd;
59 po.git_cmd = 1;
60 if (start_command(&po))
61 die("git pack-objects failed (%s)", strerror(errno));
64 * We feed the pack-objects we just spawned with revision
65 * parameters by writing to the pipe.
67 for (i = 0; i < extra->nr; i++)
68 if (!feed_object(extra->array[i], po.in, 1))
69 break;
71 while (refs) {
72 if (!is_null_sha1(refs->old_sha1) &&
73 !feed_object(refs->old_sha1, po.in, 1))
74 break;
75 if (!is_null_sha1(refs->new_sha1) &&
76 !feed_object(refs->new_sha1, po.in, 0))
77 break;
78 refs = refs->next;
81 close(po.in);
82 if (finish_command(&po))
83 return error("pack-objects died with strange error");
84 return 0;
87 static void unmark_and_free(struct commit_list *list, unsigned int mark)
89 while (list) {
90 struct commit_list *temp = list;
91 temp->item->object.flags &= ~mark;
92 list = temp->next;
93 free(temp);
97 static int ref_newer(const unsigned char *new_sha1,
98 const unsigned char *old_sha1)
100 struct object *o;
101 struct commit *old, *new;
102 struct commit_list *list, *used;
103 int found = 0;
105 /* Both new and old must be commit-ish and new is descendant of
106 * old. Otherwise we require --force.
108 o = deref_tag(parse_object(old_sha1), NULL, 0);
109 if (!o || o->type != OBJ_COMMIT)
110 return 0;
111 old = (struct commit *) o;
113 o = deref_tag(parse_object(new_sha1), NULL, 0);
114 if (!o || o->type != OBJ_COMMIT)
115 return 0;
116 new = (struct commit *) o;
118 if (parse_commit(new) < 0)
119 return 0;
121 used = list = NULL;
122 commit_list_insert(new, &list);
123 while (list) {
124 new = pop_most_recent_commit(&list, 1);
125 commit_list_insert(new, &used);
126 if (new == old) {
127 found = 1;
128 break;
131 unmark_and_free(list, 1);
132 unmark_and_free(used, 1);
133 return found;
136 static struct ref *local_refs, **local_tail;
137 static struct ref *remote_refs, **remote_tail;
139 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
141 struct ref *ref;
142 int len;
144 /* we already know it starts with refs/ to get here */
145 if (check_ref_format(refname + 5))
146 return 0;
148 len = strlen(refname) + 1;
149 ref = xcalloc(1, sizeof(*ref) + len);
150 hashcpy(ref->new_sha1, sha1);
151 memcpy(ref->name, refname, len);
152 *local_tail = ref;
153 local_tail = &ref->next;
154 return 0;
157 static void get_local_heads(void)
159 local_tail = &local_refs;
160 for_each_ref(one_local_ref, NULL);
163 static int receive_status(int in, struct ref *refs)
165 struct ref *hint;
166 char line[1000];
167 int ret = 0;
168 int len = packet_read_line(in, line, sizeof(line));
169 if (len < 10 || memcmp(line, "unpack ", 7))
170 return error("did not receive remote status");
171 if (memcmp(line, "unpack ok\n", 10)) {
172 char *p = line + strlen(line) - 1;
173 if (*p == '\n')
174 *p = '\0';
175 error("unpack failed: %s", line + 7);
176 ret = -1;
178 hint = NULL;
179 while (1) {
180 char *refname;
181 char *msg;
182 len = packet_read_line(in, line, sizeof(line));
183 if (!len)
184 break;
185 if (len < 3 ||
186 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
187 fprintf(stderr, "protocol error: %s\n", line);
188 ret = -1;
189 break;
192 line[strlen(line)-1] = '\0';
193 refname = line + 3;
194 msg = strchr(refname, ' ');
195 if (msg)
196 *msg++ = '\0';
198 /* first try searching at our hint, falling back to all refs */
199 if (hint)
200 hint = find_ref_by_name(hint, refname);
201 if (!hint)
202 hint = find_ref_by_name(refs, refname);
203 if (!hint) {
204 warning("remote reported status on unknown ref: %s",
205 refname);
206 continue;
208 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
209 warning("remote reported status on unexpected ref: %s",
210 refname);
211 continue;
214 if (line[0] == 'o' && line[1] == 'k')
215 hint->status = REF_STATUS_OK;
216 else {
217 hint->status = REF_STATUS_REMOTE_REJECT;
218 ret = -1;
220 if (msg)
221 hint->remote_status = xstrdup(msg);
222 /* start our next search from the next ref */
223 hint = hint->next;
225 return ret;
228 static void update_tracking_ref(struct remote *remote, struct ref *ref)
230 struct refspec rs;
232 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
233 return;
235 rs.src = ref->name;
236 rs.dst = NULL;
238 if (!remote_find_tracking(remote, &rs)) {
239 if (args.verbose)
240 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
241 if (ref->deletion) {
242 delete_ref(rs.dst, NULL, 0);
243 } else
244 update_ref("update by push", rs.dst,
245 ref->new_sha1, NULL, 0, 0);
246 free(rs.dst);
250 static const char *prettify_ref(const struct ref *ref)
252 const char *name = ref->name;
253 return name + (
254 !prefixcmp(name, "refs/heads/") ? 11 :
255 !prefixcmp(name, "refs/tags/") ? 10 :
256 !prefixcmp(name, "refs/remotes/") ? 13 :
260 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
262 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
264 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
265 if (from)
266 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
267 else
268 fputs(prettify_ref(to), stderr);
269 if (msg) {
270 fputs(" (", stderr);
271 fputs(msg, stderr);
272 fputc(')', stderr);
274 fputc('\n', stderr);
277 static const char *status_abbrev(unsigned char sha1[20])
279 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
282 static void print_ok_ref_status(struct ref *ref)
284 if (ref->deletion)
285 print_ref_status('-', "[deleted]", ref, NULL, NULL);
286 else if (is_null_sha1(ref->old_sha1))
287 print_ref_status('*',
288 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
289 "[new branch]"),
290 ref, ref->peer_ref, NULL);
291 else {
292 char quickref[84];
293 char type;
294 const char *msg;
296 strcpy(quickref, status_abbrev(ref->old_sha1));
297 if (ref->nonfastforward) {
298 strcat(quickref, "...");
299 type = '+';
300 msg = "forced update";
301 } else {
302 strcat(quickref, "..");
303 type = ' ';
304 msg = NULL;
306 strcat(quickref, status_abbrev(ref->new_sha1));
308 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
312 static int print_one_push_status(struct ref *ref, const char *dest, int count)
314 if (!count)
315 fprintf(stderr, "To %s\n", dest);
317 switch(ref->status) {
318 case REF_STATUS_NONE:
319 print_ref_status('X', "[no match]", ref, NULL, NULL);
320 break;
321 case REF_STATUS_REJECT_NODELETE:
322 print_ref_status('!', "[rejected]", ref, NULL,
323 "remote does not support deleting refs");
324 break;
325 case REF_STATUS_UPTODATE:
326 print_ref_status('=', "[up to date]", ref,
327 ref->peer_ref, NULL);
328 break;
329 case REF_STATUS_REJECT_NONFASTFORWARD:
330 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
331 "non-fast forward");
332 break;
333 case REF_STATUS_REMOTE_REJECT:
334 print_ref_status('!', "[remote rejected]", ref,
335 ref->deletion ? NULL : ref->peer_ref,
336 ref->remote_status);
337 break;
338 case REF_STATUS_EXPECTING_REPORT:
339 print_ref_status('!', "[remote failure]", ref,
340 ref->deletion ? NULL : ref->peer_ref,
341 "remote failed to report status");
342 break;
343 case REF_STATUS_OK:
344 print_ok_ref_status(ref);
345 break;
348 return 1;
351 static void print_push_status(const char *dest, struct ref *refs)
353 struct ref *ref;
354 int n = 0;
356 if (args.verbose) {
357 for (ref = refs; ref; ref = ref->next)
358 if (ref->status == REF_STATUS_UPTODATE)
359 n += print_one_push_status(ref, dest, n);
362 for (ref = refs; ref; ref = ref->next)
363 if (ref->status == REF_STATUS_OK)
364 n += print_one_push_status(ref, dest, n);
366 for (ref = refs; ref; ref = ref->next) {
367 if (ref->status != REF_STATUS_NONE &&
368 ref->status != REF_STATUS_UPTODATE &&
369 ref->status != REF_STATUS_OK)
370 n += print_one_push_status(ref, dest, n);
374 static int refs_pushed(struct ref *ref)
376 for (; ref; ref = ref->next) {
377 switch(ref->status) {
378 case REF_STATUS_NONE:
379 case REF_STATUS_UPTODATE:
380 break;
381 default:
382 return 1;
385 return 0;
388 static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
390 struct ref *ref;
391 int new_refs;
392 int ask_for_status_report = 0;
393 int allow_deleting_refs = 0;
394 int expect_status_report = 0;
395 int flags = MATCH_REFS_NONE;
396 int ret;
397 struct extra_have_objects extra_have;
399 memset(&extra_have, 0, sizeof(extra_have));
400 if (args.send_all)
401 flags |= MATCH_REFS_ALL;
402 if (args.send_mirror)
403 flags |= MATCH_REFS_MIRROR;
405 /* No funny business with the matcher */
406 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL,
407 &extra_have);
408 get_local_heads();
410 /* Does the other end support the reporting? */
411 if (server_supports("report-status"))
412 ask_for_status_report = 1;
413 if (server_supports("delete-refs"))
414 allow_deleting_refs = 1;
416 /* match them up */
417 if (!remote_tail)
418 remote_tail = &remote_refs;
419 if (match_refs(local_refs, remote_refs, &remote_tail,
420 nr_refspec, refspec, flags)) {
421 close(out);
422 return -1;
425 if (!remote_refs) {
426 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
427 "Perhaps you should specify a branch such as 'master'.\n");
428 close(out);
429 return 0;
433 * Finally, tell the other end!
435 new_refs = 0;
436 for (ref = remote_refs; ref; ref = ref->next) {
438 if (ref->peer_ref)
439 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
440 else if (!args.send_mirror)
441 continue;
443 ref->deletion = is_null_sha1(ref->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, ref->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(ref->new_sha1, ref->old_sha1));
479 if (ref->nonfastforward && !ref->force && !args.force_update) {
480 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
481 continue;
484 if (!ref->deletion)
485 new_refs++;
487 if (!args.dry_run) {
488 char *old_hex = sha1_to_hex(ref->old_sha1);
489 char *new_hex = sha1_to_hex(ref->new_sha1);
491 if (ask_for_status_report) {
492 packet_write(out, "%s %s %s%c%s",
493 old_hex, new_hex, ref->name, 0,
494 "report-status");
495 ask_for_status_report = 0;
496 expect_status_report = 1;
498 else
499 packet_write(out, "%s %s %s",
500 old_hex, new_hex, ref->name);
502 ref->status = expect_status_report ?
503 REF_STATUS_EXPECTING_REPORT :
504 REF_STATUS_OK;
507 packet_flush(out);
508 if (new_refs && !args.dry_run) {
509 if (pack_objects(out, remote_refs, &extra_have) < 0)
510 return -1;
512 else
513 close(out);
515 if (expect_status_report)
516 ret = receive_status(in, remote_refs);
517 else
518 ret = 0;
520 print_push_status(dest, remote_refs);
522 if (!args.dry_run && remote) {
523 for (ref = remote_refs; ref; ref = ref->next)
524 update_tracking_ref(remote, ref);
527 if (!refs_pushed(remote_refs))
528 fprintf(stderr, "Everything up-to-date\n");
529 if (ret < 0)
530 return ret;
531 for (ref = remote_refs; ref; ref = ref->next) {
532 switch (ref->status) {
533 case REF_STATUS_NONE:
534 case REF_STATUS_UPTODATE:
535 case REF_STATUS_OK:
536 break;
537 default:
538 return -1;
541 return 0;
544 static void verify_remote_names(int nr_heads, const char **heads)
546 int i;
548 for (i = 0; i < nr_heads; i++) {
549 const char *local = heads[i];
550 const char *remote = strrchr(heads[i], ':');
552 if (*local == '+')
553 local++;
555 /* A matching refspec is okay. */
556 if (remote == local && remote[1] == '\0')
557 continue;
559 remote = remote ? (remote + 1) : local;
560 switch (check_ref_format(remote)) {
561 case 0: /* ok */
562 case CHECK_REF_FORMAT_ONELEVEL:
563 /* ok but a single level -- that is fine for
564 * a match pattern.
566 case CHECK_REF_FORMAT_WILDCARD:
567 /* ok but ends with a pattern-match character */
568 continue;
570 die("remote part of refspec is not a valid name in %s",
571 heads[i]);
575 int cmd_send_pack(int argc, const char **argv, const char *prefix)
577 int i, nr_heads = 0;
578 const char **heads = NULL;
579 const char *remote_name = NULL;
580 struct remote *remote = NULL;
581 const char *dest = NULL;
583 argv++;
584 for (i = 1; i < argc; i++, argv++) {
585 const char *arg = *argv;
587 if (*arg == '-') {
588 if (!prefixcmp(arg, "--receive-pack=")) {
589 args.receivepack = arg + 15;
590 continue;
592 if (!prefixcmp(arg, "--exec=")) {
593 args.receivepack = arg + 7;
594 continue;
596 if (!prefixcmp(arg, "--remote=")) {
597 remote_name = arg + 9;
598 continue;
600 if (!strcmp(arg, "--all")) {
601 args.send_all = 1;
602 continue;
604 if (!strcmp(arg, "--dry-run")) {
605 args.dry_run = 1;
606 continue;
608 if (!strcmp(arg, "--mirror")) {
609 args.send_mirror = 1;
610 continue;
612 if (!strcmp(arg, "--force")) {
613 args.force_update = 1;
614 continue;
616 if (!strcmp(arg, "--verbose")) {
617 args.verbose = 1;
618 continue;
620 if (!strcmp(arg, "--thin")) {
621 args.use_thin_pack = 1;
622 continue;
624 usage(send_pack_usage);
626 if (!dest) {
627 dest = arg;
628 continue;
630 heads = (const char **) argv;
631 nr_heads = argc - i;
632 break;
634 if (!dest)
635 usage(send_pack_usage);
637 * --all and --mirror are incompatible; neither makes sense
638 * with any refspecs.
640 if ((heads && (args.send_all || args.send_mirror)) ||
641 (args.send_all && args.send_mirror))
642 usage(send_pack_usage);
644 if (remote_name) {
645 remote = remote_get(remote_name);
646 if (!remote_has_url(remote, dest)) {
647 die("Destination %s is not a uri for %s",
648 dest, remote_name);
652 return send_pack(&args, dest, remote, nr_heads, heads);
655 int send_pack(struct send_pack_args *my_args,
656 const char *dest, struct remote *remote,
657 int nr_heads, const char **heads)
659 int fd[2], ret;
660 struct child_process *conn;
662 memcpy(&args, my_args, sizeof(args));
664 verify_remote_names(nr_heads, heads);
666 conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
667 ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads);
668 close(fd[0]);
669 /* do_send_pack always closes fd[1] */
670 ret |= finish_connect(conn);
671 return !!ret;