builtin-mailinfo.c: check error status from rewind and ftruncate
[git/spearce.git] / builtin-send-pack.c
blob37e528e28364fbde5a5ba31316aa7bf66d43586b
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,
46 NULL,
47 NULL,
49 struct child_process po;
50 int i;
52 i = 4;
53 if (args->use_thin_pack)
54 argv[i++] = "--thin";
55 if (args->use_ofs_delta)
56 argv[i++] = "--delta-base-offset";
57 if (args->quiet)
58 argv[i++] = "-q";
59 memset(&po, 0, sizeof(po));
60 po.argv = argv;
61 po.in = -1;
62 po.out = fd;
63 po.git_cmd = 1;
64 if (start_command(&po))
65 die_errno("git pack-objects failed");
68 * We feed the pack-objects we just spawned with revision
69 * parameters by writing to the pipe.
71 for (i = 0; i < extra->nr; i++)
72 if (!feed_object(extra->array[i], po.in, 1))
73 break;
75 while (refs) {
76 if (!is_null_sha1(refs->old_sha1) &&
77 !feed_object(refs->old_sha1, po.in, 1))
78 break;
79 if (!is_null_sha1(refs->new_sha1) &&
80 !feed_object(refs->new_sha1, po.in, 0))
81 break;
82 refs = refs->next;
85 close(po.in);
86 if (finish_command(&po))
87 return error("pack-objects died with strange error");
88 return 0;
91 static int receive_status(int in, struct ref *refs)
93 struct ref *hint;
94 char line[1000];
95 int ret = 0;
96 int len = packet_read_line(in, line, sizeof(line));
97 if (len < 10 || memcmp(line, "unpack ", 7))
98 return error("did not receive remote status");
99 if (memcmp(line, "unpack ok\n", 10)) {
100 char *p = line + strlen(line) - 1;
101 if (*p == '\n')
102 *p = '\0';
103 error("unpack failed: %s", line + 7);
104 ret = -1;
106 hint = NULL;
107 while (1) {
108 char *refname;
109 char *msg;
110 len = packet_read_line(in, line, sizeof(line));
111 if (!len)
112 break;
113 if (len < 3 ||
114 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
115 fprintf(stderr, "protocol error: %s\n", line);
116 ret = -1;
117 break;
120 line[strlen(line)-1] = '\0';
121 refname = line + 3;
122 msg = strchr(refname, ' ');
123 if (msg)
124 *msg++ = '\0';
126 /* first try searching at our hint, falling back to all refs */
127 if (hint)
128 hint = find_ref_by_name(hint, refname);
129 if (!hint)
130 hint = find_ref_by_name(refs, refname);
131 if (!hint) {
132 warning("remote reported status on unknown ref: %s",
133 refname);
134 continue;
136 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
137 warning("remote reported status on unexpected ref: %s",
138 refname);
139 continue;
142 if (line[0] == 'o' && line[1] == 'k')
143 hint->status = REF_STATUS_OK;
144 else {
145 hint->status = REF_STATUS_REMOTE_REJECT;
146 ret = -1;
148 if (msg)
149 hint->remote_status = xstrdup(msg);
150 /* start our next search from the next ref */
151 hint = hint->next;
153 return ret;
156 static void update_tracking_ref(struct remote *remote, struct ref *ref)
158 struct refspec rs;
160 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
161 return;
163 rs.src = ref->name;
164 rs.dst = NULL;
166 if (!remote_find_tracking(remote, &rs)) {
167 if (args.verbose)
168 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
169 if (ref->deletion) {
170 delete_ref(rs.dst, NULL, 0);
171 } else
172 update_ref("update by push", rs.dst,
173 ref->new_sha1, NULL, 0, 0);
174 free(rs.dst);
178 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
180 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
182 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
183 if (from)
184 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
185 else
186 fputs(prettify_refname(to->name), stderr);
187 if (msg) {
188 fputs(" (", stderr);
189 fputs(msg, stderr);
190 fputc(')', stderr);
192 fputc('\n', stderr);
195 static const char *status_abbrev(unsigned char sha1[20])
197 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
200 static void print_ok_ref_status(struct ref *ref)
202 if (ref->deletion)
203 print_ref_status('-', "[deleted]", ref, NULL, NULL);
204 else if (is_null_sha1(ref->old_sha1))
205 print_ref_status('*',
206 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
207 "[new branch]"),
208 ref, ref->peer_ref, NULL);
209 else {
210 char quickref[84];
211 char type;
212 const char *msg;
214 strcpy(quickref, status_abbrev(ref->old_sha1));
215 if (ref->nonfastforward) {
216 strcat(quickref, "...");
217 type = '+';
218 msg = "forced update";
219 } else {
220 strcat(quickref, "..");
221 type = ' ';
222 msg = NULL;
224 strcat(quickref, status_abbrev(ref->new_sha1));
226 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
230 static int print_one_push_status(struct ref *ref, const char *dest, int count)
232 if (!count)
233 fprintf(stderr, "To %s\n", dest);
235 switch(ref->status) {
236 case REF_STATUS_NONE:
237 print_ref_status('X', "[no match]", ref, NULL, NULL);
238 break;
239 case REF_STATUS_REJECT_NODELETE:
240 print_ref_status('!', "[rejected]", ref, NULL,
241 "remote does not support deleting refs");
242 break;
243 case REF_STATUS_UPTODATE:
244 print_ref_status('=', "[up to date]", ref,
245 ref->peer_ref, NULL);
246 break;
247 case REF_STATUS_REJECT_NONFASTFORWARD:
248 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
249 "non-fast forward");
250 break;
251 case REF_STATUS_REMOTE_REJECT:
252 print_ref_status('!', "[remote rejected]", ref,
253 ref->deletion ? NULL : ref->peer_ref,
254 ref->remote_status);
255 break;
256 case REF_STATUS_EXPECTING_REPORT:
257 print_ref_status('!', "[remote failure]", ref,
258 ref->deletion ? NULL : ref->peer_ref,
259 "remote failed to report status");
260 break;
261 case REF_STATUS_OK:
262 print_ok_ref_status(ref);
263 break;
266 return 1;
269 static void print_push_status(const char *dest, struct ref *refs)
271 struct ref *ref;
272 int n = 0;
274 if (args.verbose) {
275 for (ref = refs; ref; ref = ref->next)
276 if (ref->status == REF_STATUS_UPTODATE)
277 n += print_one_push_status(ref, dest, n);
280 for (ref = refs; ref; ref = ref->next)
281 if (ref->status == REF_STATUS_OK)
282 n += print_one_push_status(ref, dest, n);
284 for (ref = refs; ref; ref = ref->next) {
285 if (ref->status != REF_STATUS_NONE &&
286 ref->status != REF_STATUS_UPTODATE &&
287 ref->status != REF_STATUS_OK)
288 n += print_one_push_status(ref, dest, n);
292 static int refs_pushed(struct ref *ref)
294 for (; ref; ref = ref->next) {
295 switch(ref->status) {
296 case REF_STATUS_NONE:
297 case REF_STATUS_UPTODATE:
298 break;
299 default:
300 return 1;
303 return 0;
306 int send_pack(struct send_pack_args *args,
307 int fd[], struct child_process *conn,
308 struct ref *remote_refs,
309 struct extra_have_objects *extra_have)
311 int in = fd[0];
312 int out = fd[1];
313 struct ref *ref;
314 int new_refs;
315 int ask_for_status_report = 0;
316 int allow_deleting_refs = 0;
317 int expect_status_report = 0;
318 int ret;
320 /* Does the other end support the reporting? */
321 if (server_supports("report-status"))
322 ask_for_status_report = 1;
323 if (server_supports("delete-refs"))
324 allow_deleting_refs = 1;
325 if (server_supports("ofs-delta"))
326 args->use_ofs_delta = 1;
328 if (!remote_refs) {
329 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
330 "Perhaps you should specify a branch such as 'master'.\n");
331 return 0;
335 * Finally, tell the other end!
337 new_refs = 0;
338 for (ref = remote_refs; ref; ref = ref->next) {
340 if (ref->peer_ref)
341 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
342 else if (!args->send_mirror)
343 continue;
345 ref->deletion = is_null_sha1(ref->new_sha1);
346 if (ref->deletion && !allow_deleting_refs) {
347 ref->status = REF_STATUS_REJECT_NODELETE;
348 continue;
350 if (!ref->deletion &&
351 !hashcmp(ref->old_sha1, ref->new_sha1)) {
352 ref->status = REF_STATUS_UPTODATE;
353 continue;
356 /* This part determines what can overwrite what.
357 * The rules are:
359 * (0) you can always use --force or +A:B notation to
360 * selectively force individual ref pairs.
362 * (1) if the old thing does not exist, it is OK.
364 * (2) if you do not have the old thing, you are not allowed
365 * to overwrite it; you would not know what you are losing
366 * otherwise.
368 * (3) if both new and old are commit-ish, and new is a
369 * descendant of old, it is OK.
371 * (4) regardless of all of the above, removing :B is
372 * always allowed.
375 ref->nonfastforward =
376 !ref->deletion &&
377 !is_null_sha1(ref->old_sha1) &&
378 (!has_sha1_file(ref->old_sha1)
379 || !ref_newer(ref->new_sha1, ref->old_sha1));
381 if (ref->nonfastforward && !ref->force && !args->force_update) {
382 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
383 continue;
386 if (!ref->deletion)
387 new_refs++;
389 if (!args->dry_run) {
390 char *old_hex = sha1_to_hex(ref->old_sha1);
391 char *new_hex = sha1_to_hex(ref->new_sha1);
393 if (ask_for_status_report) {
394 packet_write(out, "%s %s %s%c%s",
395 old_hex, new_hex, ref->name, 0,
396 "report-status");
397 ask_for_status_report = 0;
398 expect_status_report = 1;
400 else
401 packet_write(out, "%s %s %s",
402 old_hex, new_hex, ref->name);
404 ref->status = expect_status_report ?
405 REF_STATUS_EXPECTING_REPORT :
406 REF_STATUS_OK;
409 packet_flush(out);
410 if (new_refs && !args->dry_run) {
411 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
412 for (ref = remote_refs; ref; ref = ref->next)
413 ref->status = REF_STATUS_NONE;
414 return -1;
418 if (expect_status_report)
419 ret = receive_status(in, remote_refs);
420 else
421 ret = 0;
423 if (ret < 0)
424 return ret;
425 for (ref = remote_refs; ref; ref = ref->next) {
426 switch (ref->status) {
427 case REF_STATUS_NONE:
428 case REF_STATUS_UPTODATE:
429 case REF_STATUS_OK:
430 break;
431 default:
432 return -1;
435 return 0;
438 static void verify_remote_names(int nr_heads, const char **heads)
440 int i;
442 for (i = 0; i < nr_heads; i++) {
443 const char *local = heads[i];
444 const char *remote = strrchr(heads[i], ':');
446 if (*local == '+')
447 local++;
449 /* A matching refspec is okay. */
450 if (remote == local && remote[1] == '\0')
451 continue;
453 remote = remote ? (remote + 1) : local;
454 switch (check_ref_format(remote)) {
455 case 0: /* ok */
456 case CHECK_REF_FORMAT_ONELEVEL:
457 /* ok but a single level -- that is fine for
458 * a match pattern.
460 case CHECK_REF_FORMAT_WILDCARD:
461 /* ok but ends with a pattern-match character */
462 continue;
464 die("remote part of refspec is not a valid name in %s",
465 heads[i]);
469 int cmd_send_pack(int argc, const char **argv, const char *prefix)
471 int i, nr_refspecs = 0;
472 const char **refspecs = NULL;
473 const char *remote_name = NULL;
474 struct remote *remote = NULL;
475 const char *dest = NULL;
476 int fd[2];
477 struct child_process *conn;
478 struct extra_have_objects extra_have;
479 struct ref *remote_refs, *local_refs;
480 int ret;
481 int send_all = 0;
482 const char *receivepack = "git-receive-pack";
483 int flags;
485 argv++;
486 for (i = 1; i < argc; i++, argv++) {
487 const char *arg = *argv;
489 if (*arg == '-') {
490 if (!prefixcmp(arg, "--receive-pack=")) {
491 receivepack = arg + 15;
492 continue;
494 if (!prefixcmp(arg, "--exec=")) {
495 receivepack = arg + 7;
496 continue;
498 if (!prefixcmp(arg, "--remote=")) {
499 remote_name = arg + 9;
500 continue;
502 if (!strcmp(arg, "--all")) {
503 send_all = 1;
504 continue;
506 if (!strcmp(arg, "--dry-run")) {
507 args.dry_run = 1;
508 continue;
510 if (!strcmp(arg, "--mirror")) {
511 args.send_mirror = 1;
512 continue;
514 if (!strcmp(arg, "--force")) {
515 args.force_update = 1;
516 continue;
518 if (!strcmp(arg, "--verbose")) {
519 args.verbose = 1;
520 continue;
522 if (!strcmp(arg, "--thin")) {
523 args.use_thin_pack = 1;
524 continue;
526 usage(send_pack_usage);
528 if (!dest) {
529 dest = arg;
530 continue;
532 refspecs = (const char **) argv;
533 nr_refspecs = argc - i;
534 break;
536 if (!dest)
537 usage(send_pack_usage);
539 * --all and --mirror are incompatible; neither makes sense
540 * with any refspecs.
542 if ((refspecs && (send_all || args.send_mirror)) ||
543 (send_all && args.send_mirror))
544 usage(send_pack_usage);
546 if (remote_name) {
547 remote = remote_get(remote_name);
548 if (!remote_has_url(remote, dest)) {
549 die("Destination %s is not a uri for %s",
550 dest, remote_name);
554 conn = git_connect(fd, dest, receivepack, args.verbose ? CONNECT_VERBOSE : 0);
556 memset(&extra_have, 0, sizeof(extra_have));
558 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
559 &extra_have);
561 verify_remote_names(nr_refspecs, refspecs);
563 local_refs = get_local_heads();
565 flags = MATCH_REFS_NONE;
567 if (send_all)
568 flags |= MATCH_REFS_ALL;
569 if (args.send_mirror)
570 flags |= MATCH_REFS_MIRROR;
572 /* match them up */
573 if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
574 return -1;
576 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
578 close(fd[1]);
579 close(fd[0]);
581 ret |= finish_connect(conn);
583 print_push_status(dest, remote_refs);
585 if (!args.dry_run && remote) {
586 struct ref *ref;
587 for (ref = remote_refs; ref; ref = ref->next)
588 update_tracking_ref(remote, ref);
591 if (!ret && !refs_pushed(remote_refs))
592 fprintf(stderr, "Everything up-to-date\n");
594 return ret;