for-each-ref: fix segfault in copy_email
[git/dscho.git] / builtin-send-pack.c
blobd5a1c48d0e2abab16c646ce86ac9670b95dddc07
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "run-command.h"
6 #include "remote.h"
7 #include "send-pack.h"
9 static const char send_pack_usage[] =
10 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
11 " --all and explicit <ref> specification are mutually exclusive.";
13 static struct send_pack_args args;
15 static int feed_object(const unsigned char *sha1, int fd, int negative)
17 char buf[42];
19 if (negative && !has_sha1_file(sha1))
20 return 1;
22 memcpy(buf + negative, sha1_to_hex(sha1), 40);
23 if (negative)
24 buf[0] = '^';
25 buf[40 + negative] = '\n';
26 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
30 * Make a pack stream and spit it out into file descriptor fd
32 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
35 * The child becomes pack-objects --revs; we feed
36 * the revision parameters to it via its stdin and
37 * let its stdout go back to the other end.
39 const char *argv[] = {
40 "pack-objects",
41 "--all-progress",
42 "--revs",
43 "--stdout",
44 NULL,
45 NULL,
47 struct child_process po;
48 int i;
50 if (args->use_thin_pack)
51 argv[4] = "--thin";
52 memset(&po, 0, sizeof(po));
53 po.argv = argv;
54 po.in = -1;
55 po.out = fd;
56 po.git_cmd = 1;
57 if (start_command(&po))
58 die("git pack-objects failed (%s)", strerror(errno));
61 * We feed the pack-objects we just spawned with revision
62 * parameters by writing to the pipe.
64 for (i = 0; i < extra->nr; i++)
65 if (!feed_object(extra->array[i], po.in, 1))
66 break;
68 while (refs) {
69 if (!is_null_sha1(refs->old_sha1) &&
70 !feed_object(refs->old_sha1, po.in, 1))
71 break;
72 if (!is_null_sha1(refs->new_sha1) &&
73 !feed_object(refs->new_sha1, po.in, 0))
74 break;
75 refs = refs->next;
78 close(po.in);
79 if (finish_command(&po))
80 return error("pack-objects died with strange error");
81 return 0;
84 static int receive_status(int in, struct ref *refs)
86 struct ref *hint;
87 char line[1000];
88 int ret = 0;
89 int len = packet_read_line(in, line, sizeof(line));
90 if (len < 10 || memcmp(line, "unpack ", 7))
91 return error("did not receive remote status");
92 if (memcmp(line, "unpack ok\n", 10)) {
93 char *p = line + strlen(line) - 1;
94 if (*p == '\n')
95 *p = '\0';
96 error("unpack failed: %s", line + 7);
97 ret = -1;
99 hint = NULL;
100 while (1) {
101 char *refname;
102 char *msg;
103 len = packet_read_line(in, line, sizeof(line));
104 if (!len)
105 break;
106 if (len < 3 ||
107 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
108 fprintf(stderr, "protocol error: %s\n", line);
109 ret = -1;
110 break;
113 line[strlen(line)-1] = '\0';
114 refname = line + 3;
115 msg = strchr(refname, ' ');
116 if (msg)
117 *msg++ = '\0';
119 /* first try searching at our hint, falling back to all refs */
120 if (hint)
121 hint = find_ref_by_name(hint, refname);
122 if (!hint)
123 hint = find_ref_by_name(refs, refname);
124 if (!hint) {
125 warning("remote reported status on unknown ref: %s",
126 refname);
127 continue;
129 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
130 warning("remote reported status on unexpected ref: %s",
131 refname);
132 continue;
135 if (line[0] == 'o' && line[1] == 'k')
136 hint->status = REF_STATUS_OK;
137 else {
138 hint->status = REF_STATUS_REMOTE_REJECT;
139 ret = -1;
141 if (msg)
142 hint->remote_status = xstrdup(msg);
143 /* start our next search from the next ref */
144 hint = hint->next;
146 return ret;
149 static void update_tracking_ref(struct remote *remote, struct ref *ref)
151 struct refspec rs;
153 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
154 return;
156 rs.src = ref->name;
157 rs.dst = NULL;
159 if (!remote_find_tracking(remote, &rs)) {
160 if (args.verbose)
161 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
162 if (ref->deletion) {
163 delete_ref(rs.dst, NULL, 0);
164 } else
165 update_ref("update by push", rs.dst,
166 ref->new_sha1, NULL, 0, 0);
167 free(rs.dst);
171 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
173 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
175 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
176 if (from)
177 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
178 else
179 fputs(prettify_ref(to), stderr);
180 if (msg) {
181 fputs(" (", stderr);
182 fputs(msg, stderr);
183 fputc(')', stderr);
185 fputc('\n', stderr);
188 static const char *status_abbrev(unsigned char sha1[20])
190 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
193 static void print_ok_ref_status(struct ref *ref)
195 if (ref->deletion)
196 print_ref_status('-', "[deleted]", ref, NULL, NULL);
197 else if (is_null_sha1(ref->old_sha1))
198 print_ref_status('*',
199 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
200 "[new branch]"),
201 ref, ref->peer_ref, NULL);
202 else {
203 char quickref[84];
204 char type;
205 const char *msg;
207 strcpy(quickref, status_abbrev(ref->old_sha1));
208 if (ref->nonfastforward) {
209 strcat(quickref, "...");
210 type = '+';
211 msg = "forced update";
212 } else {
213 strcat(quickref, "..");
214 type = ' ';
215 msg = NULL;
217 strcat(quickref, status_abbrev(ref->new_sha1));
219 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
223 static int print_one_push_status(struct ref *ref, const char *dest, int count)
225 if (!count)
226 fprintf(stderr, "To %s\n", dest);
228 switch(ref->status) {
229 case REF_STATUS_NONE:
230 print_ref_status('X', "[no match]", ref, NULL, NULL);
231 break;
232 case REF_STATUS_REJECT_NODELETE:
233 print_ref_status('!', "[rejected]", ref, NULL,
234 "remote does not support deleting refs");
235 break;
236 case REF_STATUS_UPTODATE:
237 print_ref_status('=', "[up to date]", ref,
238 ref->peer_ref, NULL);
239 break;
240 case REF_STATUS_REJECT_NONFASTFORWARD:
241 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
242 "non-fast forward");
243 break;
244 case REF_STATUS_REMOTE_REJECT:
245 print_ref_status('!', "[remote rejected]", ref,
246 ref->deletion ? NULL : ref->peer_ref,
247 ref->remote_status);
248 break;
249 case REF_STATUS_EXPECTING_REPORT:
250 print_ref_status('!', "[remote failure]", ref,
251 ref->deletion ? NULL : ref->peer_ref,
252 "remote failed to report status");
253 break;
254 case REF_STATUS_OK:
255 print_ok_ref_status(ref);
256 break;
259 return 1;
262 static void print_push_status(const char *dest, struct ref *refs)
264 struct ref *ref;
265 int n = 0;
267 if (args.verbose) {
268 for (ref = refs; ref; ref = ref->next)
269 if (ref->status == REF_STATUS_UPTODATE)
270 n += print_one_push_status(ref, dest, n);
273 for (ref = refs; ref; ref = ref->next)
274 if (ref->status == REF_STATUS_OK)
275 n += print_one_push_status(ref, dest, n);
277 for (ref = refs; ref; ref = ref->next) {
278 if (ref->status != REF_STATUS_NONE &&
279 ref->status != REF_STATUS_UPTODATE &&
280 ref->status != REF_STATUS_OK)
281 n += print_one_push_status(ref, dest, n);
285 static int refs_pushed(struct ref *ref)
287 for (; ref; ref = ref->next) {
288 switch(ref->status) {
289 case REF_STATUS_NONE:
290 case REF_STATUS_UPTODATE:
291 break;
292 default:
293 return 1;
296 return 0;
299 int send_pack(struct send_pack_args *args,
300 int fd[], struct child_process *conn,
301 struct ref *remote_refs,
302 struct extra_have_objects *extra_have)
304 int in = fd[0];
305 int out = fd[1];
306 struct ref *ref;
307 int new_refs;
308 int ask_for_status_report = 0;
309 int allow_deleting_refs = 0;
310 int expect_status_report = 0;
311 int ret;
313 /* Does the other end support the reporting? */
314 if (server_supports("report-status"))
315 ask_for_status_report = 1;
316 if (server_supports("delete-refs"))
317 allow_deleting_refs = 1;
319 if (!remote_refs) {
320 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
321 "Perhaps you should specify a branch such as 'master'.\n");
322 return 0;
326 * Finally, tell the other end!
328 new_refs = 0;
329 for (ref = remote_refs; ref; ref = ref->next) {
331 if (ref->peer_ref)
332 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
333 else if (!args->send_mirror)
334 continue;
336 ref->deletion = is_null_sha1(ref->new_sha1);
337 if (ref->deletion && !allow_deleting_refs) {
338 ref->status = REF_STATUS_REJECT_NODELETE;
339 continue;
341 if (!ref->deletion &&
342 !hashcmp(ref->old_sha1, ref->new_sha1)) {
343 ref->status = REF_STATUS_UPTODATE;
344 continue;
347 /* This part determines what can overwrite what.
348 * The rules are:
350 * (0) you can always use --force or +A:B notation to
351 * selectively force individual ref pairs.
353 * (1) if the old thing does not exist, it is OK.
355 * (2) if you do not have the old thing, you are not allowed
356 * to overwrite it; you would not know what you are losing
357 * otherwise.
359 * (3) if both new and old are commit-ish, and new is a
360 * descendant of old, it is OK.
362 * (4) regardless of all of the above, removing :B is
363 * always allowed.
366 ref->nonfastforward =
367 !ref->deletion &&
368 !is_null_sha1(ref->old_sha1) &&
369 (!has_sha1_file(ref->old_sha1)
370 || !ref_newer(ref->new_sha1, ref->old_sha1));
372 if (ref->nonfastforward && !ref->force && !args->force_update) {
373 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
374 continue;
377 if (!ref->deletion)
378 new_refs++;
380 if (!args->dry_run) {
381 char *old_hex = sha1_to_hex(ref->old_sha1);
382 char *new_hex = sha1_to_hex(ref->new_sha1);
384 if (ask_for_status_report) {
385 packet_write(out, "%s %s %s%c%s",
386 old_hex, new_hex, ref->name, 0,
387 "report-status");
388 ask_for_status_report = 0;
389 expect_status_report = 1;
391 else
392 packet_write(out, "%s %s %s",
393 old_hex, new_hex, ref->name);
395 ref->status = expect_status_report ?
396 REF_STATUS_EXPECTING_REPORT :
397 REF_STATUS_OK;
400 packet_flush(out);
401 if (new_refs && !args->dry_run) {
402 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
403 for (ref = remote_refs; ref; ref = ref->next)
404 ref->status = REF_STATUS_NONE;
405 return -1;
409 if (expect_status_report)
410 ret = receive_status(in, remote_refs);
411 else
412 ret = 0;
414 if (ret < 0)
415 return ret;
416 for (ref = remote_refs; ref; ref = ref->next) {
417 switch (ref->status) {
418 case REF_STATUS_NONE:
419 case REF_STATUS_UPTODATE:
420 case REF_STATUS_OK:
421 break;
422 default:
423 return -1;
426 return 0;
429 static void verify_remote_names(int nr_heads, const char **heads)
431 int i;
433 for (i = 0; i < nr_heads; i++) {
434 const char *local = heads[i];
435 const char *remote = strrchr(heads[i], ':');
437 if (*local == '+')
438 local++;
440 /* A matching refspec is okay. */
441 if (remote == local && remote[1] == '\0')
442 continue;
444 remote = remote ? (remote + 1) : local;
445 switch (check_ref_format(remote)) {
446 case 0: /* ok */
447 case CHECK_REF_FORMAT_ONELEVEL:
448 /* ok but a single level -- that is fine for
449 * a match pattern.
451 case CHECK_REF_FORMAT_WILDCARD:
452 /* ok but ends with a pattern-match character */
453 continue;
455 die("remote part of refspec is not a valid name in %s",
456 heads[i]);
460 int cmd_send_pack(int argc, const char **argv, const char *prefix)
462 int i, nr_refspecs = 0;
463 const char **refspecs = NULL;
464 const char *remote_name = NULL;
465 struct remote *remote = NULL;
466 const char *dest = NULL;
467 int fd[2];
468 struct child_process *conn;
469 struct extra_have_objects extra_have;
470 struct ref *remote_refs, **remote_tail, *local_refs;
471 int ret;
472 int send_all = 0;
473 const char *receivepack = "git-receive-pack";
474 int flags;
476 argv++;
477 for (i = 1; i < argc; i++, argv++) {
478 const char *arg = *argv;
480 if (*arg == '-') {
481 if (!prefixcmp(arg, "--receive-pack=")) {
482 receivepack = arg + 15;
483 continue;
485 if (!prefixcmp(arg, "--exec=")) {
486 receivepack = arg + 7;
487 continue;
489 if (!prefixcmp(arg, "--remote=")) {
490 remote_name = arg + 9;
491 continue;
493 if (!strcmp(arg, "--all")) {
494 send_all = 1;
495 continue;
497 if (!strcmp(arg, "--dry-run")) {
498 args.dry_run = 1;
499 continue;
501 if (!strcmp(arg, "--mirror")) {
502 args.send_mirror = 1;
503 continue;
505 if (!strcmp(arg, "--force")) {
506 args.force_update = 1;
507 continue;
509 if (!strcmp(arg, "--verbose")) {
510 args.verbose = 1;
511 continue;
513 if (!strcmp(arg, "--thin")) {
514 args.use_thin_pack = 1;
515 continue;
517 usage(send_pack_usage);
519 if (!dest) {
520 dest = arg;
521 continue;
523 refspecs = (const char **) argv;
524 nr_refspecs = argc - i;
525 break;
527 if (!dest)
528 usage(send_pack_usage);
530 * --all and --mirror are incompatible; neither makes sense
531 * with any refspecs.
533 if ((refspecs && (send_all || args.send_mirror)) ||
534 (send_all && args.send_mirror))
535 usage(send_pack_usage);
537 if (remote_name) {
538 remote = remote_get(remote_name);
539 if (!remote_has_url(remote, dest)) {
540 die("Destination %s is not a uri for %s",
541 dest, remote_name);
545 conn = git_connect(fd, dest, receivepack, args.verbose ? CONNECT_VERBOSE : 0);
547 memset(&extra_have, 0, sizeof(extra_have));
549 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
550 &extra_have);
552 verify_remote_names(nr_refspecs, refspecs);
554 local_refs = get_local_heads();
556 flags = MATCH_REFS_NONE;
558 if (send_all)
559 flags |= MATCH_REFS_ALL;
560 if (args.send_mirror)
561 flags |= MATCH_REFS_MIRROR;
563 /* match them up */
564 remote_tail = &remote_refs;
565 while (*remote_tail)
566 remote_tail = &((*remote_tail)->next);
567 if (match_refs(local_refs, remote_refs, &remote_tail,
568 nr_refspecs, refspecs, flags)) {
569 return -1;
572 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
574 close(fd[1]);
575 close(fd[0]);
577 ret |= finish_connect(conn);
579 print_push_status(dest, remote_refs);
581 if (!args.dry_run && remote) {
582 struct ref *ref;
583 for (ref = remote_refs; ref; ref = ref->next)
584 update_tracking_ref(remote, ref);
587 if (!ret && !refs_pushed(remote_refs))
588 fprintf(stderr, "Everything up-to-date\n");
590 return ret;