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 = {
14 /* .receivepack = */ "git-receive-pack",
17 static int feed_object(const unsigned char *sha1, int fd, int negative)
21 if (negative && !has_sha1_file(sha1))
24 memcpy(buf + negative, sha1_to_hex(sha1), 40);
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[] = {
49 struct child_process po;
52 if (args.use_thin_pack)
54 memset(&po, 0, sizeof(po));
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))
71 if (!is_null_sha1(refs->old_sha1) &&
72 !feed_object(refs->old_sha1, po.in, 1))
74 if (!is_null_sha1(refs->new_sha1) &&
75 !feed_object(refs->new_sha1, po.in, 0))
81 if (finish_command(&po))
82 return error("pack-objects died with strange error");
86 static struct ref *remote_refs, **remote_tail;
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 static const char *prettify_ref(const struct ref *ref)
177 const char *name = ref->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);
191 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
193 fputs(prettify_ref(to), 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)
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]" :
215 ref, ref->peer_ref, NULL);
221 strcpy(quickref, status_abbrev(ref->old_sha1));
222 if (ref->nonfastforward) {
223 strcat(quickref, "...");
225 msg = "forced update";
227 strcat(quickref, "..");
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)
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);
246 case REF_STATUS_REJECT_NODELETE:
247 print_ref_status('!', "[rejected]", ref, NULL,
248 "remote does not support deleting refs");
250 case REF_STATUS_UPTODATE:
251 print_ref_status('=', "[up to date]", ref,
252 ref->peer_ref, NULL);
254 case REF_STATUS_REJECT_NONFASTFORWARD:
255 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
258 case REF_STATUS_REMOTE_REJECT:
259 print_ref_status('!', "[remote rejected]", ref,
260 ref->deletion ? NULL : ref->peer_ref,
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");
269 print_ok_ref_status(ref);
276 static void print_push_status(const char *dest, struct ref *refs)
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:
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;
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;
322 struct extra_have_objects extra_have;
324 memset(&extra_have, 0, sizeof(extra_have));
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,
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;
343 remote_tail = &remote_refs;
344 if (match_refs(local_refs, remote_refs, &remote_tail,
345 nr_refspec, refspec, flags)) {
351 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
352 "Perhaps you should specify a branch such as 'master'.\n");
358 * Finally, tell the other end!
361 for (ref = remote_refs; ref; ref = ref->next) {
364 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
365 else if (!args.send_mirror)
368 ref->deletion = is_null_sha1(ref->new_sha1);
369 if (ref->deletion && !allow_deleting_refs) {
370 ref->status = REF_STATUS_REJECT_NODELETE;
373 if (!ref->deletion &&
374 !hashcmp(ref->old_sha1, ref->new_sha1)) {
375 ref->status = REF_STATUS_UPTODATE;
379 /* This part determines what can overwrite what.
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
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
398 ref->nonfastforward =
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;
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,
420 ask_for_status_report = 0;
421 expect_status_report = 1;
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 :
433 if (new_refs && !args.dry_run) {
434 if (pack_objects(out, remote_refs, &extra_have) < 0)
440 if (expect_status_report)
441 ret = receive_status(in, remote_refs);
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");
456 for (ref = remote_refs; ref; ref = ref->next) {
457 switch (ref->status) {
458 case REF_STATUS_NONE:
459 case REF_STATUS_UPTODATE:
469 static void verify_remote_names(int nr_heads, const char **heads)
473 for (i = 0; i < nr_heads; i++) {
474 const char *local = heads[i];
475 const char *remote = strrchr(heads[i], ':');
480 /* A matching refspec is okay. */
481 if (remote == local && remote[1] == '\0')
484 remote = remote ? (remote + 1) : local;
485 switch (check_ref_format(remote)) {
487 case CHECK_REF_FORMAT_ONELEVEL:
488 /* ok but a single level -- that is fine for
491 case CHECK_REF_FORMAT_WILDCARD:
492 /* ok but ends with a pattern-match character */
495 die("remote part of refspec is not a valid name in %s",
500 int cmd_send_pack(int argc, const char **argv, const char *prefix)
503 const char **heads = NULL;
504 const char *remote_name = NULL;
505 struct remote *remote = NULL;
506 const char *dest = NULL;
509 for (i = 1; i < argc; i++, argv++) {
510 const char *arg = *argv;
513 if (!prefixcmp(arg, "--receive-pack=")) {
514 args.receivepack = arg + 15;
517 if (!prefixcmp(arg, "--exec=")) {
518 args.receivepack = arg + 7;
521 if (!prefixcmp(arg, "--remote=")) {
522 remote_name = arg + 9;
525 if (!strcmp(arg, "--all")) {
529 if (!strcmp(arg, "--dry-run")) {
533 if (!strcmp(arg, "--mirror")) {
534 args.send_mirror = 1;
537 if (!strcmp(arg, "--force")) {
538 args.force_update = 1;
541 if (!strcmp(arg, "--verbose")) {
545 if (!strcmp(arg, "--thin")) {
546 args.use_thin_pack = 1;
549 usage(send_pack_usage);
555 heads = (const char **) argv;
560 usage(send_pack_usage);
562 * --all and --mirror are incompatible; neither makes sense
565 if ((heads && (args.send_all || args.send_mirror)) ||
566 (args.send_all && args.send_mirror))
567 usage(send_pack_usage);
570 remote = remote_get(remote_name);
571 if (!remote_has_url(remote, dest)) {
572 die("Destination %s is not a uri for %s",
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)
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);
594 /* do_send_pack always closes fd[1] */
595 ret |= finish_connect(conn);