5 #include "run-command.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)
19 if (negative && !has_sha1_file(sha1))
22 memcpy(buf + negative, sha1_to_hex(sha1), 40);
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[] = {
48 struct child_process po;
52 if (args->use_thin_pack)
54 if (args->use_ofs_delta)
55 argv[i++] = "--delta-base-offset";
56 memset(&po, 0, sizeof(po));
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))
73 if (!is_null_sha1(refs->old_sha1) &&
74 !feed_object(refs->old_sha1, po.in, 1))
76 if (!is_null_sha1(refs->new_sha1) &&
77 !feed_object(refs->new_sha1, po.in, 0))
83 if (finish_command(&po))
84 return error("pack-objects died with strange error");
88 static int receive_status(int in, struct ref *refs)
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;
100 error("unpack failed: %s", line + 7);
107 len = packet_read_line(in, line, sizeof(line));
111 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
112 fprintf(stderr, "protocol error: %s\n", line);
117 line[strlen(line)-1] = '\0';
119 msg = strchr(refname, ' ');
123 /* first try searching at our hint, falling back to all refs */
125 hint = find_ref_by_name(hint, refname);
127 hint = find_ref_by_name(refs, refname);
129 warning("remote reported status on unknown ref: %s",
133 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
134 warning("remote reported status on unexpected ref: %s",
139 if (line[0] == 'o' && line[1] == 'k')
140 hint->status = REF_STATUS_OK;
142 hint->status = REF_STATUS_REMOTE_REJECT;
146 hint->remote_status = xstrdup(msg);
147 /* start our next search from the next ref */
153 static void update_tracking_ref(struct remote *remote, struct ref *ref)
157 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
163 if (!remote_find_tracking(remote, &rs)) {
165 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
167 delete_ref(rs.dst, NULL, 0);
169 update_ref("update by push", rs.dst,
170 ref->new_sha1, NULL, 0, 0);
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);
181 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
183 fputs(prettify_refname(to->name), 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)
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]" :
205 ref, ref->peer_ref, NULL);
211 strcpy(quickref, status_abbrev(ref->old_sha1));
212 if (ref->nonfastforward) {
213 strcat(quickref, "...");
215 msg = "forced update";
217 strcat(quickref, "..");
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)
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);
236 case REF_STATUS_REJECT_NODELETE:
237 print_ref_status('!', "[rejected]", ref, NULL,
238 "remote does not support deleting refs");
240 case REF_STATUS_UPTODATE:
241 print_ref_status('=', "[up to date]", ref,
242 ref->peer_ref, NULL);
244 case REF_STATUS_REJECT_NONFASTFORWARD:
245 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
248 case REF_STATUS_REMOTE_REJECT:
249 print_ref_status('!', "[remote rejected]", ref,
250 ref->deletion ? NULL : ref->peer_ref,
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");
259 print_ok_ref_status(ref);
266 static void print_push_status(const char *dest, struct ref *refs)
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:
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)
312 int ask_for_status_report = 0;
313 int allow_deleting_refs = 0;
314 int expect_status_report = 0;
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;
326 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
327 "Perhaps you should specify a branch such as 'master'.\n");
332 * Finally, tell the other end!
335 for (ref = remote_refs; ref; ref = ref->next) {
338 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
339 else if (!args->send_mirror)
342 ref->deletion = is_null_sha1(ref->new_sha1);
343 if (ref->deletion && !allow_deleting_refs) {
344 ref->status = REF_STATUS_REJECT_NODELETE;
347 if (!ref->deletion &&
348 !hashcmp(ref->old_sha1, ref->new_sha1)) {
349 ref->status = REF_STATUS_UPTODATE;
353 /* This part determines what can overwrite what.
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
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
372 ref->nonfastforward =
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;
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,
394 ask_for_status_report = 0;
395 expect_status_report = 1;
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 :
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;
415 if (expect_status_report)
416 ret = receive_status(in, remote_refs);
422 for (ref = remote_refs; ref; ref = ref->next) {
423 switch (ref->status) {
424 case REF_STATUS_NONE:
425 case REF_STATUS_UPTODATE:
435 static void verify_remote_names(int nr_heads, const char **heads)
439 for (i = 0; i < nr_heads; i++) {
440 const char *local = heads[i];
441 const char *remote = strrchr(heads[i], ':');
446 /* A matching refspec is okay. */
447 if (remote == local && remote[1] == '\0')
450 remote = remote ? (remote + 1) : local;
451 switch (check_ref_format(remote)) {
453 case CHECK_REF_FORMAT_ONELEVEL:
454 /* ok but a single level -- that is fine for
457 case CHECK_REF_FORMAT_WILDCARD:
458 /* ok but ends with a pattern-match character */
461 die("remote part of refspec is not a valid name in %s",
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;
474 struct child_process *conn;
475 struct extra_have_objects extra_have;
476 struct ref *remote_refs, **remote_tail, *local_refs;
479 const char *receivepack = "git-receive-pack";
483 for (i = 1; i < argc; i++, argv++) {
484 const char *arg = *argv;
487 if (!prefixcmp(arg, "--receive-pack=")) {
488 receivepack = arg + 15;
491 if (!prefixcmp(arg, "--exec=")) {
492 receivepack = arg + 7;
495 if (!prefixcmp(arg, "--remote=")) {
496 remote_name = arg + 9;
499 if (!strcmp(arg, "--all")) {
503 if (!strcmp(arg, "--dry-run")) {
507 if (!strcmp(arg, "--mirror")) {
508 args.send_mirror = 1;
511 if (!strcmp(arg, "--force")) {
512 args.force_update = 1;
515 if (!strcmp(arg, "--verbose")) {
519 if (!strcmp(arg, "--thin")) {
520 args.use_thin_pack = 1;
523 usage(send_pack_usage);
529 refspecs = (const char **) argv;
530 nr_refspecs = argc - i;
534 usage(send_pack_usage);
536 * --all and --mirror are incompatible; neither makes sense
539 if ((refspecs && (send_all || args.send_mirror)) ||
540 (send_all && args.send_mirror))
541 usage(send_pack_usage);
544 remote = remote_get(remote_name);
545 if (!remote_has_url(remote, dest)) {
546 die("Destination %s is not a uri for %s",
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,
558 verify_remote_names(nr_refspecs, refspecs);
560 local_refs = get_local_heads();
562 flags = MATCH_REFS_NONE;
565 flags |= MATCH_REFS_ALL;
566 if (args.send_mirror)
567 flags |= MATCH_REFS_MIRROR;
570 remote_tail = &remote_refs;
572 remote_tail = &((*remote_tail)->next);
573 if (match_refs(local_refs, remote_refs, &remote_tail,
574 nr_refspecs, refspecs, flags)) {
578 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
583 ret |= finish_connect(conn);
585 print_push_status(dest, remote_refs);
587 if (!args.dry_run && remote) {
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");