builtin-remote: Make "remote show" display all urls
[git/mingw/4msysgit.git] / builtin-send-pack.c
blob473a3de40c4cc9aab5c1355d0602646234e7bc1c
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,
48 struct child_process po;
49 int i;
51 i = 4;
52 if (args->use_thin_pack)
53 argv[i++] = "--thin";
54 if (args->use_ofs_delta)
55 argv[i++] = "--delta-base-offset";
56 memset(&po, 0, sizeof(po));
57 po.argv = argv;
58 po.in = -1;
59 po.out = fd;
60 po.git_cmd = 1;
61 if (start_command(&po))
62 die("git pack-objects failed (%s)", strerror(errno));
65 * We feed the pack-objects we just spawned with revision
66 * parameters by writing to the pipe.
68 for (i = 0; i < extra->nr; i++)
69 if (!feed_object(extra->array[i], po.in, 1))
70 break;
72 while (refs) {
73 if (!is_null_sha1(refs->old_sha1) &&
74 !feed_object(refs->old_sha1, po.in, 1))
75 break;
76 if (!is_null_sha1(refs->new_sha1) &&
77 !feed_object(refs->new_sha1, po.in, 0))
78 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 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 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
177 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
179 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
180 if (from)
181 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
182 else
183 fputs(prettify_ref(to), stderr);
184 if (msg) {
185 fputs(" (", stderr);
186 fputs(msg, stderr);
187 fputc(')', stderr);
189 fputc('\n', stderr);
192 static const char *status_abbrev(unsigned char sha1[20])
194 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
197 static void print_ok_ref_status(struct ref *ref)
199 if (ref->deletion)
200 print_ref_status('-', "[deleted]", ref, NULL, NULL);
201 else if (is_null_sha1(ref->old_sha1))
202 print_ref_status('*',
203 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
204 "[new branch]"),
205 ref, ref->peer_ref, NULL);
206 else {
207 char quickref[84];
208 char type;
209 const char *msg;
211 strcpy(quickref, status_abbrev(ref->old_sha1));
212 if (ref->nonfastforward) {
213 strcat(quickref, "...");
214 type = '+';
215 msg = "forced update";
216 } else {
217 strcat(quickref, "..");
218 type = ' ';
219 msg = NULL;
221 strcat(quickref, status_abbrev(ref->new_sha1));
223 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
227 static int print_one_push_status(struct ref *ref, const char *dest, int count)
229 if (!count)
230 fprintf(stderr, "To %s\n", dest);
232 switch(ref->status) {
233 case REF_STATUS_NONE:
234 print_ref_status('X', "[no match]", ref, NULL, NULL);
235 break;
236 case REF_STATUS_REJECT_NODELETE:
237 print_ref_status('!', "[rejected]", ref, NULL,
238 "remote does not support deleting refs");
239 break;
240 case REF_STATUS_UPTODATE:
241 print_ref_status('=', "[up to date]", ref,
242 ref->peer_ref, NULL);
243 break;
244 case REF_STATUS_REJECT_NONFASTFORWARD:
245 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
246 "non-fast forward");
247 break;
248 case REF_STATUS_REMOTE_REJECT:
249 print_ref_status('!', "[remote rejected]", ref,
250 ref->deletion ? NULL : ref->peer_ref,
251 ref->remote_status);
252 break;
253 case REF_STATUS_EXPECTING_REPORT:
254 print_ref_status('!', "[remote failure]", ref,
255 ref->deletion ? NULL : ref->peer_ref,
256 "remote failed to report status");
257 break;
258 case REF_STATUS_OK:
259 print_ok_ref_status(ref);
260 break;
263 return 1;
266 static void print_push_status(const char *dest, struct ref *refs)
268 struct ref *ref;
269 int n = 0;
271 if (args.verbose) {
272 for (ref = refs; ref; ref = ref->next)
273 if (ref->status == REF_STATUS_UPTODATE)
274 n += print_one_push_status(ref, dest, n);
277 for (ref = refs; ref; ref = ref->next)
278 if (ref->status == REF_STATUS_OK)
279 n += print_one_push_status(ref, dest, n);
281 for (ref = refs; ref; ref = ref->next) {
282 if (ref->status != REF_STATUS_NONE &&
283 ref->status != REF_STATUS_UPTODATE &&
284 ref->status != REF_STATUS_OK)
285 n += print_one_push_status(ref, dest, n);
289 static int refs_pushed(struct ref *ref)
291 for (; ref; ref = ref->next) {
292 switch(ref->status) {
293 case REF_STATUS_NONE:
294 case REF_STATUS_UPTODATE:
295 break;
296 default:
297 return 1;
300 return 0;
303 int send_pack(struct send_pack_args *args,
304 int fd[], struct child_process *conn,
305 struct ref *remote_refs,
306 struct extra_have_objects *extra_have)
308 int in = fd[0];
309 int out = fd[1];
310 struct ref *ref;
311 int new_refs;
312 int ask_for_status_report = 0;
313 int allow_deleting_refs = 0;
314 int expect_status_report = 0;
315 int ret;
317 /* Does the other end support the reporting? */
318 if (server_supports("report-status"))
319 ask_for_status_report = 1;
320 if (server_supports("delete-refs"))
321 allow_deleting_refs = 1;
322 if (server_supports("ofs-delta"))
323 args->use_ofs_delta = 1;
325 if (!remote_refs) {
326 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
327 "Perhaps you should specify a branch such as 'master'.\n");
328 return 0;
332 * Finally, tell the other end!
334 new_refs = 0;
335 for (ref = remote_refs; ref; ref = ref->next) {
337 if (ref->peer_ref)
338 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
339 else if (!args->send_mirror)
340 continue;
342 ref->deletion = is_null_sha1(ref->new_sha1);
343 if (ref->deletion && !allow_deleting_refs) {
344 ref->status = REF_STATUS_REJECT_NODELETE;
345 continue;
347 if (!ref->deletion &&
348 !hashcmp(ref->old_sha1, ref->new_sha1)) {
349 ref->status = REF_STATUS_UPTODATE;
350 continue;
353 /* This part determines what can overwrite what.
354 * The rules are:
356 * (0) you can always use --force or +A:B notation to
357 * selectively force individual ref pairs.
359 * (1) if the old thing does not exist, it is OK.
361 * (2) if you do not have the old thing, you are not allowed
362 * to overwrite it; you would not know what you are losing
363 * otherwise.
365 * (3) if both new and old are commit-ish, and new is a
366 * descendant of old, it is OK.
368 * (4) regardless of all of the above, removing :B is
369 * always allowed.
372 ref->nonfastforward =
373 !ref->deletion &&
374 !is_null_sha1(ref->old_sha1) &&
375 (!has_sha1_file(ref->old_sha1)
376 || !ref_newer(ref->new_sha1, ref->old_sha1));
378 if (ref->nonfastforward && !ref->force && !args->force_update) {
379 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
380 continue;
383 if (!ref->deletion)
384 new_refs++;
386 if (!args->dry_run) {
387 char *old_hex = sha1_to_hex(ref->old_sha1);
388 char *new_hex = sha1_to_hex(ref->new_sha1);
390 if (ask_for_status_report) {
391 packet_write(out, "%s %s %s%c%s",
392 old_hex, new_hex, ref->name, 0,
393 "report-status");
394 ask_for_status_report = 0;
395 expect_status_report = 1;
397 else
398 packet_write(out, "%s %s %s",
399 old_hex, new_hex, ref->name);
401 ref->status = expect_status_report ?
402 REF_STATUS_EXPECTING_REPORT :
403 REF_STATUS_OK;
406 packet_flush(out);
407 if (new_refs && !args->dry_run) {
408 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
409 for (ref = remote_refs; ref; ref = ref->next)
410 ref->status = REF_STATUS_NONE;
411 return -1;
415 if (expect_status_report)
416 ret = receive_status(in, remote_refs);
417 else
418 ret = 0;
420 if (ret < 0)
421 return ret;
422 for (ref = remote_refs; ref; ref = ref->next) {
423 switch (ref->status) {
424 case REF_STATUS_NONE:
425 case REF_STATUS_UPTODATE:
426 case REF_STATUS_OK:
427 break;
428 default:
429 return -1;
432 return 0;
435 static void verify_remote_names(int nr_heads, const char **heads)
437 int i;
439 for (i = 0; i < nr_heads; i++) {
440 const char *local = heads[i];
441 const char *remote = strrchr(heads[i], ':');
443 if (*local == '+')
444 local++;
446 /* A matching refspec is okay. */
447 if (remote == local && remote[1] == '\0')
448 continue;
450 remote = remote ? (remote + 1) : local;
451 switch (check_ref_format(remote)) {
452 case 0: /* ok */
453 case CHECK_REF_FORMAT_ONELEVEL:
454 /* ok but a single level -- that is fine for
455 * a match pattern.
457 case CHECK_REF_FORMAT_WILDCARD:
458 /* ok but ends with a pattern-match character */
459 continue;
461 die("remote part of refspec is not a valid name in %s",
462 heads[i]);
466 int cmd_send_pack(int argc, const char **argv, const char *prefix)
468 int i, nr_refspecs = 0;
469 const char **refspecs = NULL;
470 const char *remote_name = NULL;
471 struct remote *remote = NULL;
472 const char *dest = NULL;
473 int fd[2];
474 struct child_process *conn;
475 struct extra_have_objects extra_have;
476 struct ref *remote_refs, **remote_tail, *local_refs;
477 int ret;
478 int send_all = 0;
479 const char *receivepack = "git-receive-pack";
480 int flags;
482 argv++;
483 for (i = 1; i < argc; i++, argv++) {
484 const char *arg = *argv;
486 if (*arg == '-') {
487 if (!prefixcmp(arg, "--receive-pack=")) {
488 receivepack = arg + 15;
489 continue;
491 if (!prefixcmp(arg, "--exec=")) {
492 receivepack = arg + 7;
493 continue;
495 if (!prefixcmp(arg, "--remote=")) {
496 remote_name = arg + 9;
497 continue;
499 if (!strcmp(arg, "--all")) {
500 send_all = 1;
501 continue;
503 if (!strcmp(arg, "--dry-run")) {
504 args.dry_run = 1;
505 continue;
507 if (!strcmp(arg, "--mirror")) {
508 args.send_mirror = 1;
509 continue;
511 if (!strcmp(arg, "--force")) {
512 args.force_update = 1;
513 continue;
515 if (!strcmp(arg, "--verbose")) {
516 args.verbose = 1;
517 continue;
519 if (!strcmp(arg, "--thin")) {
520 args.use_thin_pack = 1;
521 continue;
523 usage(send_pack_usage);
525 if (!dest) {
526 dest = arg;
527 continue;
529 refspecs = (const char **) argv;
530 nr_refspecs = argc - i;
531 break;
533 if (!dest)
534 usage(send_pack_usage);
536 * --all and --mirror are incompatible; neither makes sense
537 * with any refspecs.
539 if ((refspecs && (send_all || args.send_mirror)) ||
540 (send_all && args.send_mirror))
541 usage(send_pack_usage);
543 if (remote_name) {
544 remote = remote_get(remote_name);
545 if (!remote_has_url(remote, dest)) {
546 die("Destination %s is not a uri for %s",
547 dest, remote_name);
551 conn = git_connect(fd, dest, receivepack, args.verbose ? CONNECT_VERBOSE : 0);
553 memset(&extra_have, 0, sizeof(extra_have));
555 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
556 &extra_have);
558 verify_remote_names(nr_refspecs, refspecs);
560 local_refs = get_local_heads();
562 flags = MATCH_REFS_NONE;
564 if (send_all)
565 flags |= MATCH_REFS_ALL;
566 if (args.send_mirror)
567 flags |= MATCH_REFS_MIRROR;
569 /* match them up */
570 remote_tail = &remote_refs;
571 while (*remote_tail)
572 remote_tail = &((*remote_tail)->next);
573 if (match_refs(local_refs, remote_refs, &remote_tail,
574 nr_refspecs, refspecs, flags)) {
575 return -1;
578 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
580 close(fd[1]);
581 close(fd[0]);
583 ret |= finish_connect(conn);
585 print_push_status(dest, remote_refs);
587 if (!args.dry_run && remote) {
588 struct ref *ref;
589 for (ref = remote_refs; ref; ref = ref->next)
590 update_tracking_ref(remote, ref);
593 if (!ret && !refs_pushed(remote_refs))
594 fprintf(stderr, "Everything up-to-date\n");
596 return ret;