test-lib: Work around missing sum on Windows
[git/dscho.git] / builtin-send-pack.c
blob9072905f10cbbb0f8297bc1b1c7dfb12613c80a7
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 = {
14 /* .receivepack = */ "git-receive-pack",
17 static int feed_object(const unsigned char *sha1, int fd, int negative)
19 char buf[42];
21 if (negative && !has_sha1_file(sha1))
22 return 1;
24 memcpy(buf + negative, sha1_to_hex(sha1), 40);
25 if (negative)
26 buf[0] = '^';
27 buf[40 + negative] = '\n';
28 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
32 * Make a pack stream and spit it out into file descriptor fd
34 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra)
37 * The child becomes pack-objects --revs; we feed
38 * the revision parameters to it via its stdin and
39 * let its stdout go back to the other end.
41 const char *argv[] = {
42 "pack-objects",
43 "--all-progress",
44 "--revs",
45 "--stdout",
46 NULL,
47 NULL,
49 struct child_process po;
50 int i;
52 if (args.use_thin_pack)
53 argv[4] = "--thin";
54 memset(&po, 0, sizeof(po));
55 po.argv = argv;
56 po.in = -1;
57 po.out = fd;
58 po.git_cmd = 1;
59 if (start_command(&po))
60 die("git pack-objects failed (%s)", strerror(errno));
63 * We feed the pack-objects we just spawned with revision
64 * parameters by writing to the pipe.
66 for (i = 0; i < extra->nr; i++)
67 if (!feed_object(extra->array[i], po.in, 1))
68 break;
70 while (refs) {
71 if (!is_null_sha1(refs->old_sha1) &&
72 !feed_object(refs->old_sha1, po.in, 1))
73 break;
74 if (!is_null_sha1(refs->new_sha1) &&
75 !feed_object(refs->new_sha1, po.in, 0))
76 break;
77 refs = refs->next;
80 close(po.in);
81 if (finish_command(&po))
82 return error("pack-objects died with strange error");
83 return 0;
86 static struct ref *remote_refs, **remote_tail;
88 static int receive_status(int in, struct ref *refs)
90 struct ref *hint;
91 char line[1000];
92 int ret = 0;
93 int len = packet_read_line(in, line, sizeof(line));
94 if (len < 10 || memcmp(line, "unpack ", 7))
95 return error("did not receive remote status");
96 if (memcmp(line, "unpack ok\n", 10)) {
97 char *p = line + strlen(line) - 1;
98 if (*p == '\n')
99 *p = '\0';
100 error("unpack failed: %s", line + 7);
101 ret = -1;
103 hint = NULL;
104 while (1) {
105 char *refname;
106 char *msg;
107 len = packet_read_line(in, line, sizeof(line));
108 if (!len)
109 break;
110 if (len < 3 ||
111 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
112 fprintf(stderr, "protocol error: %s\n", line);
113 ret = -1;
114 break;
117 line[strlen(line)-1] = '\0';
118 refname = line + 3;
119 msg = strchr(refname, ' ');
120 if (msg)
121 *msg++ = '\0';
123 /* first try searching at our hint, falling back to all refs */
124 if (hint)
125 hint = find_ref_by_name(hint, refname);
126 if (!hint)
127 hint = find_ref_by_name(refs, refname);
128 if (!hint) {
129 warning("remote reported status on unknown ref: %s",
130 refname);
131 continue;
133 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
134 warning("remote reported status on unexpected ref: %s",
135 refname);
136 continue;
139 if (line[0] == 'o' && line[1] == 'k')
140 hint->status = REF_STATUS_OK;
141 else {
142 hint->status = REF_STATUS_REMOTE_REJECT;
143 ret = -1;
145 if (msg)
146 hint->remote_status = xstrdup(msg);
147 /* start our next search from the next ref */
148 hint = hint->next;
150 return ret;
153 static void update_tracking_ref(struct remote *remote, struct ref *ref)
155 struct refspec rs;
157 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
158 return;
160 rs.src = ref->name;
161 rs.dst = NULL;
163 if (!remote_find_tracking(remote, &rs)) {
164 if (args.verbose)
165 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
166 if (ref->deletion) {
167 delete_ref(rs.dst, NULL, 0);
168 } else
169 update_ref("update by push", rs.dst,
170 ref->new_sha1, NULL, 0, 0);
171 free(rs.dst);
175 static const char *prettify_ref(const struct ref *ref)
177 const char *name = ref->name;
178 return name + (
179 !prefixcmp(name, "refs/heads/") ? 11 :
180 !prefixcmp(name, "refs/tags/") ? 10 :
181 !prefixcmp(name, "refs/remotes/") ? 13 :
185 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
187 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
189 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
190 if (from)
191 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
192 else
193 fputs(prettify_ref(to), stderr);
194 if (msg) {
195 fputs(" (", stderr);
196 fputs(msg, stderr);
197 fputc(')', stderr);
199 fputc('\n', stderr);
202 static const char *status_abbrev(unsigned char sha1[20])
204 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
207 static void print_ok_ref_status(struct ref *ref)
209 if (ref->deletion)
210 print_ref_status('-', "[deleted]", ref, NULL, NULL);
211 else if (is_null_sha1(ref->old_sha1))
212 print_ref_status('*',
213 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
214 "[new branch]"),
215 ref, ref->peer_ref, NULL);
216 else {
217 char quickref[84];
218 char type;
219 const char *msg;
221 strcpy(quickref, status_abbrev(ref->old_sha1));
222 if (ref->nonfastforward) {
223 strcat(quickref, "...");
224 type = '+';
225 msg = "forced update";
226 } else {
227 strcat(quickref, "..");
228 type = ' ';
229 msg = NULL;
231 strcat(quickref, status_abbrev(ref->new_sha1));
233 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
237 static int print_one_push_status(struct ref *ref, const char *dest, int count)
239 if (!count)
240 fprintf(stderr, "To %s\n", dest);
242 switch(ref->status) {
243 case REF_STATUS_NONE:
244 print_ref_status('X', "[no match]", ref, NULL, NULL);
245 break;
246 case REF_STATUS_REJECT_NODELETE:
247 print_ref_status('!', "[rejected]", ref, NULL,
248 "remote does not support deleting refs");
249 break;
250 case REF_STATUS_UPTODATE:
251 print_ref_status('=', "[up to date]", ref,
252 ref->peer_ref, NULL);
253 break;
254 case REF_STATUS_REJECT_NONFASTFORWARD:
255 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
256 "non-fast forward");
257 break;
258 case REF_STATUS_REMOTE_REJECT:
259 print_ref_status('!', "[remote rejected]", ref,
260 ref->deletion ? NULL : ref->peer_ref,
261 ref->remote_status);
262 break;
263 case REF_STATUS_EXPECTING_REPORT:
264 print_ref_status('!', "[remote failure]", ref,
265 ref->deletion ? NULL : ref->peer_ref,
266 "remote failed to report status");
267 break;
268 case REF_STATUS_OK:
269 print_ok_ref_status(ref);
270 break;
273 return 1;
276 static void print_push_status(const char *dest, struct ref *refs)
278 struct ref *ref;
279 int n = 0;
281 if (args.verbose) {
282 for (ref = refs; ref; ref = ref->next)
283 if (ref->status == REF_STATUS_UPTODATE)
284 n += print_one_push_status(ref, dest, n);
287 for (ref = refs; ref; ref = ref->next)
288 if (ref->status == REF_STATUS_OK)
289 n += print_one_push_status(ref, dest, n);
291 for (ref = refs; ref; ref = ref->next) {
292 if (ref->status != REF_STATUS_NONE &&
293 ref->status != REF_STATUS_UPTODATE &&
294 ref->status != REF_STATUS_OK)
295 n += print_one_push_status(ref, dest, n);
299 static int refs_pushed(struct ref *ref)
301 for (; ref; ref = ref->next) {
302 switch(ref->status) {
303 case REF_STATUS_NONE:
304 case REF_STATUS_UPTODATE:
305 break;
306 default:
307 return 1;
310 return 0;
313 static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
315 struct ref *ref, *local_refs;
316 int new_refs;
317 int ask_for_status_report = 0;
318 int allow_deleting_refs = 0;
319 int expect_status_report = 0;
320 int flags = MATCH_REFS_NONE;
321 int ret;
322 struct extra_have_objects extra_have;
324 memset(&extra_have, 0, sizeof(extra_have));
325 if (args.send_all)
326 flags |= MATCH_REFS_ALL;
327 if (args.send_mirror)
328 flags |= MATCH_REFS_MIRROR;
330 /* No funny business with the matcher */
331 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL,
332 &extra_have);
333 local_refs = get_local_heads();
335 /* Does the other end support the reporting? */
336 if (server_supports("report-status"))
337 ask_for_status_report = 1;
338 if (server_supports("delete-refs"))
339 allow_deleting_refs = 1;
341 /* match them up */
342 if (!remote_tail)
343 remote_tail = &remote_refs;
344 if (match_refs(local_refs, remote_refs, &remote_tail,
345 nr_refspec, refspec, flags)) {
346 close(out);
347 return -1;
350 if (!remote_refs) {
351 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
352 "Perhaps you should specify a branch such as 'master'.\n");
353 close(out);
354 return 0;
358 * Finally, tell the other end!
360 new_refs = 0;
361 for (ref = remote_refs; ref; ref = ref->next) {
363 if (ref->peer_ref)
364 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
365 else if (!args.send_mirror)
366 continue;
368 ref->deletion = is_null_sha1(ref->new_sha1);
369 if (ref->deletion && !allow_deleting_refs) {
370 ref->status = REF_STATUS_REJECT_NODELETE;
371 continue;
373 if (!ref->deletion &&
374 !hashcmp(ref->old_sha1, ref->new_sha1)) {
375 ref->status = REF_STATUS_UPTODATE;
376 continue;
379 /* This part determines what can overwrite what.
380 * The rules are:
382 * (0) you can always use --force or +A:B notation to
383 * selectively force individual ref pairs.
385 * (1) if the old thing does not exist, it is OK.
387 * (2) if you do not have the old thing, you are not allowed
388 * to overwrite it; you would not know what you are losing
389 * otherwise.
391 * (3) if both new and old are commit-ish, and new is a
392 * descendant of old, it is OK.
394 * (4) regardless of all of the above, removing :B is
395 * always allowed.
398 ref->nonfastforward =
399 !ref->deletion &&
400 !is_null_sha1(ref->old_sha1) &&
401 (!has_sha1_file(ref->old_sha1)
402 || !ref_newer(ref->new_sha1, ref->old_sha1));
404 if (ref->nonfastforward && !ref->force && !args.force_update) {
405 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
406 continue;
409 if (!ref->deletion)
410 new_refs++;
412 if (!args.dry_run) {
413 char *old_hex = sha1_to_hex(ref->old_sha1);
414 char *new_hex = sha1_to_hex(ref->new_sha1);
416 if (ask_for_status_report) {
417 packet_write(out, "%s %s %s%c%s",
418 old_hex, new_hex, ref->name, 0,
419 "report-status");
420 ask_for_status_report = 0;
421 expect_status_report = 1;
423 else
424 packet_write(out, "%s %s %s",
425 old_hex, new_hex, ref->name);
427 ref->status = expect_status_report ?
428 REF_STATUS_EXPECTING_REPORT :
429 REF_STATUS_OK;
432 packet_flush(out);
433 if (new_refs && !args.dry_run) {
434 if (pack_objects(out, remote_refs, &extra_have) < 0)
435 return -1;
437 else
438 close(out);
440 if (expect_status_report)
441 ret = receive_status(in, remote_refs);
442 else
443 ret = 0;
445 print_push_status(dest, remote_refs);
447 if (!args.dry_run && remote) {
448 for (ref = remote_refs; ref; ref = ref->next)
449 update_tracking_ref(remote, ref);
452 if (!refs_pushed(remote_refs))
453 fprintf(stderr, "Everything up-to-date\n");
454 if (ret < 0)
455 return ret;
456 for (ref = remote_refs; ref; ref = ref->next) {
457 switch (ref->status) {
458 case REF_STATUS_NONE:
459 case REF_STATUS_UPTODATE:
460 case REF_STATUS_OK:
461 break;
462 default:
463 return -1;
466 return 0;
469 static void verify_remote_names(int nr_heads, const char **heads)
471 int i;
473 for (i = 0; i < nr_heads; i++) {
474 const char *local = heads[i];
475 const char *remote = strrchr(heads[i], ':');
477 if (*local == '+')
478 local++;
480 /* A matching refspec is okay. */
481 if (remote == local && remote[1] == '\0')
482 continue;
484 remote = remote ? (remote + 1) : local;
485 switch (check_ref_format(remote)) {
486 case 0: /* ok */
487 case CHECK_REF_FORMAT_ONELEVEL:
488 /* ok but a single level -- that is fine for
489 * a match pattern.
491 case CHECK_REF_FORMAT_WILDCARD:
492 /* ok but ends with a pattern-match character */
493 continue;
495 die("remote part of refspec is not a valid name in %s",
496 heads[i]);
500 int cmd_send_pack(int argc, const char **argv, const char *prefix)
502 int i, nr_heads = 0;
503 const char **heads = NULL;
504 const char *remote_name = NULL;
505 struct remote *remote = NULL;
506 const char *dest = NULL;
508 argv++;
509 for (i = 1; i < argc; i++, argv++) {
510 const char *arg = *argv;
512 if (*arg == '-') {
513 if (!prefixcmp(arg, "--receive-pack=")) {
514 args.receivepack = arg + 15;
515 continue;
517 if (!prefixcmp(arg, "--exec=")) {
518 args.receivepack = arg + 7;
519 continue;
521 if (!prefixcmp(arg, "--remote=")) {
522 remote_name = arg + 9;
523 continue;
525 if (!strcmp(arg, "--all")) {
526 args.send_all = 1;
527 continue;
529 if (!strcmp(arg, "--dry-run")) {
530 args.dry_run = 1;
531 continue;
533 if (!strcmp(arg, "--mirror")) {
534 args.send_mirror = 1;
535 continue;
537 if (!strcmp(arg, "--force")) {
538 args.force_update = 1;
539 continue;
541 if (!strcmp(arg, "--verbose")) {
542 args.verbose = 1;
543 continue;
545 if (!strcmp(arg, "--thin")) {
546 args.use_thin_pack = 1;
547 continue;
549 usage(send_pack_usage);
551 if (!dest) {
552 dest = arg;
553 continue;
555 heads = (const char **) argv;
556 nr_heads = argc - i;
557 break;
559 if (!dest)
560 usage(send_pack_usage);
562 * --all and --mirror are incompatible; neither makes sense
563 * with any refspecs.
565 if ((heads && (args.send_all || args.send_mirror)) ||
566 (args.send_all && args.send_mirror))
567 usage(send_pack_usage);
569 if (remote_name) {
570 remote = remote_get(remote_name);
571 if (!remote_has_url(remote, dest)) {
572 die("Destination %s is not a uri for %s",
573 dest, remote_name);
577 return send_pack(&args, dest, remote, nr_heads, heads);
580 int send_pack(struct send_pack_args *my_args,
581 const char *dest, struct remote *remote,
582 int nr_heads, const char **heads)
584 int fd[2], ret;
585 struct child_process *conn;
587 memcpy(&args, my_args, sizeof(args));
589 verify_remote_names(nr_heads, heads);
591 conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
592 ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads);
593 close(fd[0]);
594 /* do_send_pack always closes fd[1] */
595 ret |= finish_connect(conn);
596 return !!ret;