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 = {
16 static int feed_object(const unsigned char *sha1, int fd, int negative)
20 if (negative && !has_sha1_file(sha1))
23 memcpy(buf + negative, sha1_to_hex(sha1), 40);
26 buf[40 + negative] = '\n';
27 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
31 * Make a pack stream and spit it out into file descriptor fd
33 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
36 * The child becomes pack-objects --revs; we feed
37 * the revision parameters to it via its stdin and
38 * let its stdout go back to the other end.
40 const char *argv[] = {
48 struct child_process po;
51 if (args->use_thin_pack)
53 memset(&po, 0, sizeof(po));
58 if (start_command(&po))
59 die("git pack-objects failed (%s)", strerror(errno));
62 * We feed the pack-objects we just spawned with revision
63 * parameters by writing to the pipe.
65 for (i = 0; i < extra->nr; i++)
66 if (!feed_object(extra->array[i], po.in, 1))
70 if (!is_null_sha1(refs->old_sha1) &&
71 !feed_object(refs->old_sha1, po.in, 1))
73 if (!is_null_sha1(refs->new_sha1) &&
74 !feed_object(refs->new_sha1, po.in, 0))
80 if (finish_command(&po))
81 return error("pack-objects died with strange error");
85 static int receive_status(int in, struct ref *refs)
90 int len = packet_read_line(in, line, sizeof(line));
91 if (len < 10 || memcmp(line, "unpack ", 7))
92 return error("did not receive remote status");
93 if (memcmp(line, "unpack ok\n", 10)) {
94 char *p = line + strlen(line) - 1;
97 error("unpack failed: %s", line + 7);
104 len = packet_read_line(in, line, sizeof(line));
108 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
109 fprintf(stderr, "protocol error: %s\n", line);
114 line[strlen(line)-1] = '\0';
116 msg = strchr(refname, ' ');
120 /* first try searching at our hint, falling back to all refs */
122 hint = find_ref_by_name(hint, refname);
124 hint = find_ref_by_name(refs, refname);
126 warning("remote reported status on unknown ref: %s",
130 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
131 warning("remote reported status on unexpected ref: %s",
136 if (line[0] == 'o' && line[1] == 'k')
137 hint->status = REF_STATUS_OK;
139 hint->status = REF_STATUS_REMOTE_REJECT;
143 hint->remote_status = xstrdup(msg);
144 /* start our next search from the next ref */
150 static void update_tracking_ref(struct remote *remote, struct ref *ref)
154 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
160 if (!remote_find_tracking(remote, &rs)) {
162 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
164 delete_ref(rs.dst, NULL, 0);
166 update_ref("update by push", rs.dst,
167 ref->new_sha1, NULL, 0, 0);
172 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
174 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
176 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
178 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
180 fputs(prettify_ref(to), stderr);
189 static const char *status_abbrev(unsigned char sha1[20])
191 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
194 static void print_ok_ref_status(struct ref *ref)
197 print_ref_status('-', "[deleted]", ref, NULL, NULL);
198 else if (is_null_sha1(ref->old_sha1))
199 print_ref_status('*',
200 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
202 ref, ref->peer_ref, NULL);
208 strcpy(quickref, status_abbrev(ref->old_sha1));
209 if (ref->nonfastforward) {
210 strcat(quickref, "...");
212 msg = "forced update";
214 strcat(quickref, "..");
218 strcat(quickref, status_abbrev(ref->new_sha1));
220 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
224 static int print_one_push_status(struct ref *ref, const char *dest, int count)
227 fprintf(stderr, "To %s\n", dest);
229 switch(ref->status) {
230 case REF_STATUS_NONE:
231 print_ref_status('X', "[no match]", ref, NULL, NULL);
233 case REF_STATUS_REJECT_NODELETE:
234 print_ref_status('!', "[rejected]", ref, NULL,
235 "remote does not support deleting refs");
237 case REF_STATUS_UPTODATE:
238 print_ref_status('=', "[up to date]", ref,
239 ref->peer_ref, NULL);
241 case REF_STATUS_REJECT_NONFASTFORWARD:
242 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
245 case REF_STATUS_REMOTE_REJECT:
246 print_ref_status('!', "[remote rejected]", ref,
247 ref->deletion ? NULL : ref->peer_ref,
250 case REF_STATUS_EXPECTING_REPORT:
251 print_ref_status('!', "[remote failure]", ref,
252 ref->deletion ? NULL : ref->peer_ref,
253 "remote failed to report status");
256 print_ok_ref_status(ref);
263 static void print_push_status(const char *dest, struct ref *refs)
269 for (ref = refs; ref; ref = ref->next)
270 if (ref->status == REF_STATUS_UPTODATE)
271 n += print_one_push_status(ref, dest, n);
274 for (ref = refs; ref; ref = ref->next)
275 if (ref->status == REF_STATUS_OK)
276 n += print_one_push_status(ref, dest, n);
278 for (ref = refs; ref; ref = ref->next) {
279 if (ref->status != REF_STATUS_NONE &&
280 ref->status != REF_STATUS_UPTODATE &&
281 ref->status != REF_STATUS_OK)
282 n += print_one_push_status(ref, dest, n);
286 static int refs_pushed(struct ref *ref)
288 for (; ref; ref = ref->next) {
289 switch(ref->status) {
290 case REF_STATUS_NONE:
291 case REF_STATUS_UPTODATE:
300 int send_pack(struct send_pack_args *args,
301 int fd[], struct child_process *conn,
302 struct ref *remote_refs,
303 struct extra_have_objects *extra_have)
309 int ask_for_status_report = 0;
310 int allow_deleting_refs = 0;
311 int expect_status_report = 0;
314 /* Does the other end support the reporting? */
315 if (server_supports("report-status"))
316 ask_for_status_report = 1;
317 if (server_supports("delete-refs"))
318 allow_deleting_refs = 1;
321 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
322 "Perhaps you should specify a branch such as 'master'.\n");
327 * Finally, tell the other end!
330 for (ref = remote_refs; ref; ref = ref->next) {
333 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
334 else if (!args->send_mirror)
337 ref->deletion = is_null_sha1(ref->new_sha1);
338 if (ref->deletion && !allow_deleting_refs) {
339 ref->status = REF_STATUS_REJECT_NODELETE;
342 if (!ref->deletion &&
343 !hashcmp(ref->old_sha1, ref->new_sha1)) {
344 ref->status = REF_STATUS_UPTODATE;
348 /* This part determines what can overwrite what.
351 * (0) you can always use --force or +A:B notation to
352 * selectively force individual ref pairs.
354 * (1) if the old thing does not exist, it is OK.
356 * (2) if you do not have the old thing, you are not allowed
357 * to overwrite it; you would not know what you are losing
360 * (3) if both new and old are commit-ish, and new is a
361 * descendant of old, it is OK.
363 * (4) regardless of all of the above, removing :B is
367 ref->nonfastforward =
369 !is_null_sha1(ref->old_sha1) &&
370 (!has_sha1_file(ref->old_sha1)
371 || !ref_newer(ref->new_sha1, ref->old_sha1));
373 if (ref->nonfastforward && !ref->force && !args->force_update) {
374 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
381 if (!args->dry_run) {
382 char *old_hex = sha1_to_hex(ref->old_sha1);
383 char *new_hex = sha1_to_hex(ref->new_sha1);
385 if (ask_for_status_report) {
386 packet_write(out, "%s %s %s%c%s",
387 old_hex, new_hex, ref->name, 0,
389 ask_for_status_report = 0;
390 expect_status_report = 1;
393 packet_write(out, "%s %s %s",
394 old_hex, new_hex, ref->name);
396 ref->status = expect_status_report ?
397 REF_STATUS_EXPECTING_REPORT :
402 if (new_refs && !args->dry_run) {
403 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
404 for (ref = remote_refs; ref; ref = ref->next)
405 ref->status = REF_STATUS_NONE;
410 if (expect_status_report)
411 ret = receive_status(in, remote_refs);
417 for (ref = remote_refs; ref; ref = ref->next) {
418 switch (ref->status) {
419 case REF_STATUS_NONE:
420 case REF_STATUS_UPTODATE:
430 static void verify_remote_names(int nr_heads, const char **heads)
434 for (i = 0; i < nr_heads; i++) {
435 const char *local = heads[i];
436 const char *remote = strrchr(heads[i], ':');
441 /* A matching refspec is okay. */
442 if (remote == local && remote[1] == '\0')
445 remote = remote ? (remote + 1) : local;
446 switch (check_ref_format(remote)) {
448 case CHECK_REF_FORMAT_ONELEVEL:
449 /* ok but a single level -- that is fine for
452 case CHECK_REF_FORMAT_WILDCARD:
453 /* ok but ends with a pattern-match character */
456 die("remote part of refspec is not a valid name in %s",
461 int cmd_send_pack(int argc, const char **argv, const char *prefix)
463 int i, nr_refspecs = 0;
464 const char **refspecs = NULL;
465 const char *remote_name = NULL;
466 struct remote *remote = NULL;
467 const char *dest = NULL;
469 struct child_process *conn;
470 struct extra_have_objects extra_have;
471 struct ref *remote_refs, **remote_tail, *local_refs;
474 const char *receivepack = "git-receive-pack";
478 for (i = 1; i < argc; i++, argv++) {
479 const char *arg = *argv;
482 if (!prefixcmp(arg, "--receive-pack=")) {
483 receivepack = arg + 15;
486 if (!prefixcmp(arg, "--exec=")) {
487 receivepack = arg + 7;
490 if (!prefixcmp(arg, "--remote=")) {
491 remote_name = arg + 9;
494 if (!strcmp(arg, "--all")) {
498 if (!strcmp(arg, "--dry-run")) {
502 if (!strcmp(arg, "--mirror")) {
503 args.send_mirror = 1;
506 if (!strcmp(arg, "--force")) {
507 args.force_update = 1;
510 if (!strcmp(arg, "--verbose")) {
514 if (!strcmp(arg, "--thin")) {
515 args.use_thin_pack = 1;
518 usage(send_pack_usage);
524 refspecs = (const char **) argv;
525 nr_refspecs = argc - i;
529 usage(send_pack_usage);
531 * --all and --mirror are incompatible; neither makes sense
534 if ((refspecs && (send_all || args.send_mirror)) ||
535 (send_all && args.send_mirror))
536 usage(send_pack_usage);
539 remote = remote_get(remote_name);
540 if (!remote_has_url(remote, dest)) {
541 die("Destination %s is not a uri for %s",
546 conn = git_connect(fd, dest, receivepack, args.verbose ? CONNECT_VERBOSE : 0);
548 memset(&extra_have, 0, sizeof(extra_have));
550 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
553 verify_remote_names(nr_refspecs, refspecs);
555 local_refs = get_local_heads();
557 flags = MATCH_REFS_NONE;
560 flags |= MATCH_REFS_ALL;
561 if (args.send_mirror)
562 flags |= MATCH_REFS_MIRROR;
565 remote_tail = &remote_refs;
567 remote_tail = &((*remote_tail)->next);
568 if (match_refs(local_refs, remote_refs, &remote_tail,
569 nr_refspecs, refspecs, flags)) {
573 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
578 ret |= finish_connect(conn);
580 print_push_status(dest, remote_refs);
582 if (!args.dry_run && remote) {
584 for (ref = remote_refs; ref; ref = ref->next)
585 update_tracking_ref(remote, ref);
588 if (!ret && !refs_pushed(remote_refs))
589 fprintf(stderr, "Everything up-to-date\n");