git-svn: respect i18n.commitencoding config
[git.git] / builtin-send-pack.c
blob298bd719240ba503a3128486883d33df011765b1
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;
145 /* we already know it starts with refs/ to get here */
146 if (check_ref_format(refname + 5))
147 return 0;
149 len = strlen(refname) + 1;
150 ref = xcalloc(1, sizeof(*ref) + len);
151 hashcpy(ref->new_sha1, sha1);
152 memcpy(ref->name, refname, len);
153 *local_tail = ref;
154 local_tail = &ref->next;
155 return 0;
158 static void get_local_heads(void)
160 local_tail = &local_refs;
161 for_each_ref(one_local_ref, NULL);
164 static int receive_status(int in, struct ref *refs)
166 struct ref *hint;
167 char line[1000];
168 int ret = 0;
169 int len = packet_read_line(in, line, sizeof(line));
170 if (len < 10 || memcmp(line, "unpack ", 7))
171 return error("did not receive remote status");
172 if (memcmp(line, "unpack ok\n", 10)) {
173 char *p = line + strlen(line) - 1;
174 if (*p == '\n')
175 *p = '\0';
176 error("unpack failed: %s", line + 7);
177 ret = -1;
179 hint = NULL;
180 while (1) {
181 char *refname;
182 char *msg;
183 len = packet_read_line(in, line, sizeof(line));
184 if (!len)
185 break;
186 if (len < 3 ||
187 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
188 fprintf(stderr, "protocol error: %s\n", line);
189 ret = -1;
190 break;
193 line[strlen(line)-1] = '\0';
194 refname = line + 3;
195 msg = strchr(refname, ' ');
196 if (msg)
197 *msg++ = '\0';
199 /* first try searching at our hint, falling back to all refs */
200 if (hint)
201 hint = find_ref_by_name(hint, refname);
202 if (!hint)
203 hint = find_ref_by_name(refs, refname);
204 if (!hint) {
205 warning("remote reported status on unknown ref: %s",
206 refname);
207 continue;
209 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
210 warning("remote reported status on unexpected ref: %s",
211 refname);
212 continue;
215 if (line[0] == 'o' && line[1] == 'k')
216 hint->status = REF_STATUS_OK;
217 else {
218 hint->status = REF_STATUS_REMOTE_REJECT;
219 ret = -1;
221 if (msg)
222 hint->remote_status = xstrdup(msg);
223 /* start our next search from the next ref */
224 hint = hint->next;
226 return ret;
229 static void update_tracking_ref(struct remote *remote, struct ref *ref)
231 struct refspec rs;
233 if (ref->status != REF_STATUS_OK)
234 return;
236 rs.src = ref->name;
237 rs.dst = NULL;
239 if (!remote_find_tracking(remote, &rs)) {
240 if (args.verbose)
241 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
242 if (ref->deletion) {
243 delete_ref(rs.dst, NULL, 0);
244 } else
245 update_ref("update by push", rs.dst,
246 ref->new_sha1, NULL, 0, 0);
247 free(rs.dst);
251 static const char *prettify_ref(const struct ref *ref)
253 const char *name = ref->name;
254 return name + (
255 !prefixcmp(name, "refs/heads/") ? 11 :
256 !prefixcmp(name, "refs/tags/") ? 10 :
257 !prefixcmp(name, "refs/remotes/") ? 13 :
261 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
263 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
265 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
266 if (from)
267 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
268 else
269 fputs(prettify_ref(to), stderr);
270 if (msg) {
271 fputs(" (", stderr);
272 fputs(msg, stderr);
273 fputc(')', stderr);
275 fputc('\n', stderr);
278 static const char *status_abbrev(unsigned char sha1[20])
280 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
283 static void print_ok_ref_status(struct ref *ref)
285 if (ref->deletion)
286 print_ref_status('-', "[deleted]", ref, NULL, NULL);
287 else if (is_null_sha1(ref->old_sha1))
288 print_ref_status('*',
289 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
290 "[new branch]"),
291 ref, ref->peer_ref, NULL);
292 else {
293 char quickref[84];
294 char type;
295 const char *msg;
297 strcpy(quickref, status_abbrev(ref->old_sha1));
298 if (ref->nonfastforward) {
299 strcat(quickref, "...");
300 type = '+';
301 msg = "forced update";
302 } else {
303 strcat(quickref, "..");
304 type = ' ';
305 msg = NULL;
307 strcat(quickref, status_abbrev(ref->new_sha1));
309 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
313 static int print_one_push_status(struct ref *ref, const char *dest, int count)
315 if (!count)
316 fprintf(stderr, "To %s\n", dest);
318 switch(ref->status) {
319 case REF_STATUS_NONE:
320 print_ref_status('X', "[no match]", ref, NULL, NULL);
321 break;
322 case REF_STATUS_REJECT_NODELETE:
323 print_ref_status('!', "[rejected]", ref, NULL,
324 "remote does not support deleting refs");
325 break;
326 case REF_STATUS_UPTODATE:
327 print_ref_status('=', "[up to date]", ref,
328 ref->peer_ref, NULL);
329 break;
330 case REF_STATUS_REJECT_NONFASTFORWARD:
331 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
332 "non-fast forward");
333 break;
334 case REF_STATUS_REMOTE_REJECT:
335 print_ref_status('!', "[remote rejected]", ref,
336 ref->deletion ? NULL : ref->peer_ref,
337 ref->remote_status);
338 break;
339 case REF_STATUS_EXPECTING_REPORT:
340 print_ref_status('!', "[remote failure]", ref,
341 ref->deletion ? NULL : ref->peer_ref,
342 "remote failed to report status");
343 break;
344 case REF_STATUS_OK:
345 print_ok_ref_status(ref);
346 break;
349 return 1;
352 static void print_push_status(const char *dest, struct ref *refs)
354 struct ref *ref;
355 int n = 0;
357 if (args.verbose) {
358 for (ref = refs; ref; ref = ref->next)
359 if (ref->status == REF_STATUS_UPTODATE)
360 n += print_one_push_status(ref, dest, n);
363 for (ref = refs; ref; ref = ref->next)
364 if (ref->status == REF_STATUS_OK)
365 n += print_one_push_status(ref, dest, n);
367 for (ref = refs; ref; ref = ref->next) {
368 if (ref->status != REF_STATUS_NONE &&
369 ref->status != REF_STATUS_UPTODATE &&
370 ref->status != REF_STATUS_OK)
371 n += print_one_push_status(ref, dest, n);
375 static int refs_pushed(struct ref *ref)
377 for (; ref; ref = ref->next) {
378 switch(ref->status) {
379 case REF_STATUS_NONE:
380 case REF_STATUS_UPTODATE:
381 break;
382 default:
383 return 1;
386 return 0;
389 static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
391 struct ref *ref;
392 int new_refs;
393 int ask_for_status_report = 0;
394 int allow_deleting_refs = 0;
395 int expect_status_report = 0;
396 int flags = MATCH_REFS_NONE;
397 int ret;
398 struct extra_have_objects extra_have;
400 memset(&extra_have, 0, sizeof(extra_have));
401 if (args.send_all)
402 flags |= MATCH_REFS_ALL;
403 if (args.send_mirror)
404 flags |= MATCH_REFS_MIRROR;
406 /* No funny business with the matcher */
407 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL,
408 &extra_have);
409 get_local_heads();
411 /* Does the other end support the reporting? */
412 if (server_supports("report-status"))
413 ask_for_status_report = 1;
414 if (server_supports("delete-refs"))
415 allow_deleting_refs = 1;
417 /* match them up */
418 if (!remote_tail)
419 remote_tail = &remote_refs;
420 if (match_refs(local_refs, remote_refs, &remote_tail,
421 nr_refspec, refspec, flags)) {
422 close(out);
423 return -1;
426 if (!remote_refs) {
427 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
428 "Perhaps you should specify a branch such as 'master'.\n");
429 close(out);
430 return 0;
434 * Finally, tell the other end!
436 new_refs = 0;
437 for (ref = remote_refs; ref; ref = ref->next) {
438 const unsigned char *new_sha1;
440 if (!ref->peer_ref) {
441 if (!args.send_mirror)
442 continue;
443 new_sha1 = null_sha1;
445 else
446 new_sha1 = ref->peer_ref->new_sha1;
449 ref->deletion = is_null_sha1(new_sha1);
450 if (ref->deletion && !allow_deleting_refs) {
451 ref->status = REF_STATUS_REJECT_NODELETE;
452 continue;
454 if (!ref->deletion &&
455 !hashcmp(ref->old_sha1, new_sha1)) {
456 ref->status = REF_STATUS_UPTODATE;
457 continue;
460 /* This part determines what can overwrite what.
461 * The rules are:
463 * (0) you can always use --force or +A:B notation to
464 * selectively force individual ref pairs.
466 * (1) if the old thing does not exist, it is OK.
468 * (2) if you do not have the old thing, you are not allowed
469 * to overwrite it; you would not know what you are losing
470 * otherwise.
472 * (3) if both new and old are commit-ish, and new is a
473 * descendant of old, it is OK.
475 * (4) regardless of all of the above, removing :B is
476 * always allowed.
479 ref->nonfastforward =
480 !ref->deletion &&
481 !is_null_sha1(ref->old_sha1) &&
482 (!has_sha1_file(ref->old_sha1)
483 || !ref_newer(new_sha1, ref->old_sha1));
485 if (ref->nonfastforward && !ref->force && !args.force_update) {
486 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
487 continue;
490 hashcpy(ref->new_sha1, new_sha1);
491 if (!ref->deletion)
492 new_refs++;
494 if (!args.dry_run) {
495 char *old_hex = sha1_to_hex(ref->old_sha1);
496 char *new_hex = sha1_to_hex(ref->new_sha1);
498 if (ask_for_status_report) {
499 packet_write(out, "%s %s %s%c%s",
500 old_hex, new_hex, ref->name, 0,
501 "report-status");
502 ask_for_status_report = 0;
503 expect_status_report = 1;
505 else
506 packet_write(out, "%s %s %s",
507 old_hex, new_hex, ref->name);
509 ref->status = expect_status_report ?
510 REF_STATUS_EXPECTING_REPORT :
511 REF_STATUS_OK;
514 packet_flush(out);
515 if (new_refs && !args.dry_run) {
516 if (pack_objects(out, remote_refs, &extra_have) < 0)
517 return -1;
519 else
520 close(out);
522 if (expect_status_report)
523 ret = receive_status(in, remote_refs);
524 else
525 ret = 0;
527 print_push_status(dest, remote_refs);
529 if (!args.dry_run && remote) {
530 for (ref = remote_refs; ref; ref = ref->next)
531 update_tracking_ref(remote, ref);
534 if (!refs_pushed(remote_refs))
535 fprintf(stderr, "Everything up-to-date\n");
536 if (ret < 0)
537 return ret;
538 for (ref = remote_refs; ref; ref = ref->next) {
539 switch (ref->status) {
540 case REF_STATUS_NONE:
541 case REF_STATUS_UPTODATE:
542 case REF_STATUS_OK:
543 break;
544 default:
545 return -1;
548 return 0;
551 static void verify_remote_names(int nr_heads, const char **heads)
553 int i;
555 for (i = 0; i < nr_heads; i++) {
556 const char *local = heads[i];
557 const char *remote = strrchr(heads[i], ':');
559 if (*local == '+')
560 local++;
562 /* A matching refspec is okay. */
563 if (remote == local && remote[1] == '\0')
564 continue;
566 remote = remote ? (remote + 1) : local;
567 switch (check_ref_format(remote)) {
568 case 0: /* ok */
569 case CHECK_REF_FORMAT_ONELEVEL:
570 /* ok but a single level -- that is fine for
571 * a match pattern.
573 case CHECK_REF_FORMAT_WILDCARD:
574 /* ok but ends with a pattern-match character */
575 continue;
577 die("remote part of refspec is not a valid name in %s",
578 heads[i]);
582 int cmd_send_pack(int argc, const char **argv, const char *prefix)
584 int i, nr_heads = 0;
585 const char **heads = NULL;
586 const char *remote_name = NULL;
587 struct remote *remote = NULL;
588 const char *dest = NULL;
590 argv++;
591 for (i = 1; i < argc; i++, argv++) {
592 const char *arg = *argv;
594 if (*arg == '-') {
595 if (!prefixcmp(arg, "--receive-pack=")) {
596 args.receivepack = arg + 15;
597 continue;
599 if (!prefixcmp(arg, "--exec=")) {
600 args.receivepack = arg + 7;
601 continue;
603 if (!prefixcmp(arg, "--remote=")) {
604 remote_name = arg + 9;
605 continue;
607 if (!strcmp(arg, "--all")) {
608 args.send_all = 1;
609 continue;
611 if (!strcmp(arg, "--dry-run")) {
612 args.dry_run = 1;
613 continue;
615 if (!strcmp(arg, "--mirror")) {
616 args.send_mirror = 1;
617 continue;
619 if (!strcmp(arg, "--force")) {
620 args.force_update = 1;
621 continue;
623 if (!strcmp(arg, "--verbose")) {
624 args.verbose = 1;
625 continue;
627 if (!strcmp(arg, "--thin")) {
628 args.use_thin_pack = 1;
629 continue;
631 usage(send_pack_usage);
633 if (!dest) {
634 dest = arg;
635 continue;
637 heads = (const char **) argv;
638 nr_heads = argc - i;
639 break;
641 if (!dest)
642 usage(send_pack_usage);
644 * --all and --mirror are incompatible; neither makes sense
645 * with any refspecs.
647 if ((heads && (args.send_all || args.send_mirror)) ||
648 (args.send_all && args.send_mirror))
649 usage(send_pack_usage);
651 if (remote_name) {
652 remote = remote_get(remote_name);
653 if (!remote_has_url(remote, dest)) {
654 die("Destination %s is not a uri for %s",
655 dest, remote_name);
659 return send_pack(&args, dest, remote, nr_heads, heads);
662 int send_pack(struct send_pack_args *my_args,
663 const char *dest, struct remote *remote,
664 int nr_heads, const char **heads)
666 int fd[2], ret;
667 struct child_process *conn;
669 memcpy(&args, my_args, sizeof(args));
671 verify_remote_names(nr_heads, heads);
673 conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
674 ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads);
675 close(fd[0]);
676 /* do_send_pack always closes fd[1] */
677 ret |= finish_connect(conn);
678 return !!ret;