6 #include "run-command.h"
11 static const char send_pack_usage
[] =
12 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
13 " --all and explicit <ref> specification are mutually exclusive.";
15 static struct send_pack_args args
;
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
, struct send_pack_args
*args
)
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
[] = {
43 "--all-progress-implied",
51 struct child_process po
;
55 if (args
->use_thin_pack
)
57 if (args
->use_ofs_delta
)
58 argv
[i
++] = "--delta-base-offset";
61 memset(&po
, 0, sizeof(po
));
64 po
.out
= args
->stateless_rpc
? -1 : fd
;
66 if (start_command(&po
))
67 die_errno("git pack-objects failed");
70 * We feed the pack-objects we just spawned with revision
71 * parameters by writing to the pipe.
73 for (i
= 0; i
< extra
->nr
; i
++)
74 if (!feed_object(extra
->array
[i
], po
.in
, 1))
78 if (!is_null_sha1(refs
->old_sha1
) &&
79 !feed_object(refs
->old_sha1
, po
.in
, 1))
81 if (!is_null_sha1(refs
->new_sha1
) &&
82 !feed_object(refs
->new_sha1
, po
.in
, 0))
89 if (args
->stateless_rpc
) {
90 char *buf
= xmalloc(LARGE_PACKET_MAX
);
92 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
95 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
102 if (finish_command(&po
))
103 return error("pack-objects died with strange error");
107 static int receive_status(int in
, struct ref
*refs
)
112 int len
= packet_read_line(in
, line
, sizeof(line
));
113 if (len
< 10 || memcmp(line
, "unpack ", 7))
114 return error("did not receive remote status");
115 if (memcmp(line
, "unpack ok\n", 10)) {
116 char *p
= line
+ strlen(line
) - 1;
119 error("unpack failed: %s", line
+ 7);
126 len
= packet_read_line(in
, line
, sizeof(line
));
130 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
131 fprintf(stderr
, "protocol error: %s\n", line
);
136 line
[strlen(line
)-1] = '\0';
138 msg
= strchr(refname
, ' ');
142 /* first try searching at our hint, falling back to all refs */
144 hint
= find_ref_by_name(hint
, refname
);
146 hint
= find_ref_by_name(refs
, refname
);
148 warning("remote reported status on unknown ref: %s",
152 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
153 warning("remote reported status on unexpected ref: %s",
158 if (line
[0] == 'o' && line
[1] == 'k')
159 hint
->status
= REF_STATUS_OK
;
161 hint
->status
= REF_STATUS_REMOTE_REJECT
;
165 hint
->remote_status
= xstrdup(msg
);
166 /* start our next search from the next ref */
172 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
)
176 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
182 if (!remote_find_tracking(remote
, &rs
)) {
184 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
186 delete_ref(rs
.dst
, NULL
, 0);
188 update_ref("update by push", rs
.dst
,
189 ref
->new_sha1
, NULL
, 0, 0);
194 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
196 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
)
198 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
200 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
202 fputs(prettify_refname(to
->name
), stderr
);
211 static const char *status_abbrev(unsigned char sha1
[20])
213 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
216 static void print_ok_ref_status(struct ref
*ref
)
219 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
);
220 else if (is_null_sha1(ref
->old_sha1
))
221 print_ref_status('*',
222 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
224 ref
, ref
->peer_ref
, NULL
);
230 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
231 if (ref
->nonfastforward
) {
232 strcat(quickref
, "...");
234 msg
= "forced update";
236 strcat(quickref
, "..");
240 strcat(quickref
, status_abbrev(ref
->new_sha1
));
242 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
);
246 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
)
249 fprintf(stderr
, "To %s\n", dest
);
251 switch(ref
->status
) {
252 case REF_STATUS_NONE
:
253 print_ref_status('X', "[no match]", ref
, NULL
, NULL
);
255 case REF_STATUS_REJECT_NODELETE
:
256 print_ref_status('!', "[rejected]", ref
, NULL
,
257 "remote does not support deleting refs");
259 case REF_STATUS_UPTODATE
:
260 print_ref_status('=', "[up to date]", ref
,
261 ref
->peer_ref
, NULL
);
263 case REF_STATUS_REJECT_NONFASTFORWARD
:
264 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
267 case REF_STATUS_REMOTE_REJECT
:
268 print_ref_status('!', "[remote rejected]", ref
,
269 ref
->deletion
? NULL
: ref
->peer_ref
,
272 case REF_STATUS_EXPECTING_REPORT
:
273 print_ref_status('!', "[remote failure]", ref
,
274 ref
->deletion
? NULL
: ref
->peer_ref
,
275 "remote failed to report status");
278 print_ok_ref_status(ref
);
285 static void print_push_status(const char *dest
, struct ref
*refs
)
291 for (ref
= refs
; ref
; ref
= ref
->next
)
292 if (ref
->status
== REF_STATUS_UPTODATE
)
293 n
+= print_one_push_status(ref
, dest
, n
);
296 for (ref
= refs
; ref
; ref
= ref
->next
)
297 if (ref
->status
== REF_STATUS_OK
)
298 n
+= print_one_push_status(ref
, dest
, n
);
300 for (ref
= refs
; ref
; ref
= ref
->next
) {
301 if (ref
->status
!= REF_STATUS_NONE
&&
302 ref
->status
!= REF_STATUS_UPTODATE
&&
303 ref
->status
!= REF_STATUS_OK
)
304 n
+= print_one_push_status(ref
, dest
, n
);
308 static int refs_pushed(struct ref
*ref
)
310 for (; ref
; ref
= ref
->next
) {
311 switch(ref
->status
) {
312 case REF_STATUS_NONE
:
313 case REF_STATUS_UPTODATE
:
322 static void print_helper_status(struct ref
*ref
)
324 struct strbuf buf
= STRBUF_INIT
;
326 for (; ref
; ref
= ref
->next
) {
327 const char *msg
= NULL
;
330 switch(ref
->status
) {
331 case REF_STATUS_NONE
:
340 case REF_STATUS_UPTODATE
:
345 case REF_STATUS_REJECT_NONFASTFORWARD
:
347 msg
= "non-fast forward";
350 case REF_STATUS_REJECT_NODELETE
:
351 case REF_STATUS_REMOTE_REJECT
:
355 case REF_STATUS_EXPECTING_REPORT
:
361 strbuf_addf(&buf
, "%s %s", res
, ref
->name
);
362 if (ref
->remote_status
)
363 msg
= ref
->remote_status
;
365 strbuf_addch(&buf
, ' ');
366 quote_two_c_style(&buf
, "", msg
, 0);
368 strbuf_addch(&buf
, '\n');
370 safe_write(1, buf
.buf
, buf
.len
);
372 strbuf_release(&buf
);
375 static int sideband_demux(int in
, int out
, void *data
)
378 int ret
= recv_sideband("send-pack", fd
[0], out
);
383 int send_pack(struct send_pack_args
*args
,
384 int fd
[], struct child_process
*conn
,
385 struct ref
*remote_refs
,
386 struct extra_have_objects
*extra_have
)
390 struct strbuf req_buf
= STRBUF_INIT
;
393 int allow_deleting_refs
= 0;
394 int status_report
= 0;
395 int use_sideband
= 0;
396 unsigned cmds_sent
= 0;
400 /* Does the other end support the reporting? */
401 if (server_supports("report-status"))
403 if (server_supports("delete-refs"))
404 allow_deleting_refs
= 1;
405 if (server_supports("ofs-delta"))
406 args
->use_ofs_delta
= 1;
407 if (server_supports("side-band-64k"))
411 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
412 "Perhaps you should specify a branch such as 'master'.\n");
417 * Finally, tell the other end!
420 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
423 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
424 else if (!args
->send_mirror
)
427 ref
->deletion
= is_null_sha1(ref
->new_sha1
);
428 if (ref
->deletion
&& !allow_deleting_refs
) {
429 ref
->status
= REF_STATUS_REJECT_NODELETE
;
432 if (!ref
->deletion
&&
433 !hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
434 ref
->status
= REF_STATUS_UPTODATE
;
438 /* This part determines what can overwrite what.
441 * (0) you can always use --force or +A:B notation to
442 * selectively force individual ref pairs.
444 * (1) if the old thing does not exist, it is OK.
446 * (2) if you do not have the old thing, you are not allowed
447 * to overwrite it; you would not know what you are losing
450 * (3) if both new and old are commit-ish, and new is a
451 * descendant of old, it is OK.
453 * (4) regardless of all of the above, removing :B is
457 ref
->nonfastforward
=
459 !is_null_sha1(ref
->old_sha1
) &&
460 (!has_sha1_file(ref
->old_sha1
)
461 || !ref_newer(ref
->new_sha1
, ref
->old_sha1
));
463 if (ref
->nonfastforward
&& !ref
->force
&& !args
->force_update
) {
464 ref
->status
= REF_STATUS_REJECT_NONFASTFORWARD
;
472 ref
->status
= REF_STATUS_OK
;
474 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
475 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
477 if (!cmds_sent
&& (status_report
|| use_sideband
)) {
478 packet_buf_write(&req_buf
, "%s %s %s%c%s%s",
479 old_hex
, new_hex
, ref
->name
, 0,
480 status_report
? " report-status" : "",
481 use_sideband
? " side-band-64k" : "");
484 packet_buf_write(&req_buf
, "%s %s %s",
485 old_hex
, new_hex
, ref
->name
);
486 ref
->status
= status_report
?
487 REF_STATUS_EXPECTING_REPORT
:
493 if (args
->stateless_rpc
) {
494 if (!args
->dry_run
&& cmds_sent
) {
495 packet_buf_flush(&req_buf
);
496 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
499 safe_write(out
, req_buf
.buf
, req_buf
.len
);
502 strbuf_release(&req_buf
);
504 if (use_sideband
&& cmds_sent
) {
505 memset(&demux
, 0, sizeof(demux
));
506 demux
.proc
= sideband_demux
;
509 if (start_async(&demux
))
510 die("receive-pack: unable to fork off sideband demultiplexer");
514 if (new_refs
&& cmds_sent
) {
515 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
516 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
517 ref
->status
= REF_STATUS_NONE
;
519 finish_async(&demux
);
523 if (args
->stateless_rpc
&& cmds_sent
)
526 if (status_report
&& cmds_sent
)
527 ret
= receive_status(in
, remote_refs
);
530 if (args
->stateless_rpc
)
533 if (use_sideband
&& cmds_sent
) {
534 if (finish_async(&demux
)) {
535 error("error in sideband demultiplexer");
543 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
544 switch (ref
->status
) {
545 case REF_STATUS_NONE
:
546 case REF_STATUS_UPTODATE
:
556 static void verify_remote_names(int nr_heads
, const char **heads
)
560 for (i
= 0; i
< nr_heads
; i
++) {
561 const char *local
= heads
[i
];
562 const char *remote
= strrchr(heads
[i
], ':');
567 /* A matching refspec is okay. */
568 if (remote
== local
&& remote
[1] == '\0')
571 remote
= remote
? (remote
+ 1) : local
;
572 switch (check_ref_format(remote
)) {
574 case CHECK_REF_FORMAT_ONELEVEL
:
575 /* ok but a single level -- that is fine for
578 case CHECK_REF_FORMAT_WILDCARD
:
579 /* ok but ends with a pattern-match character */
582 die("remote part of refspec is not a valid name in %s",
587 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
589 int i
, nr_refspecs
= 0;
590 const char **refspecs
= NULL
;
591 const char *remote_name
= NULL
;
592 struct remote
*remote
= NULL
;
593 const char *dest
= NULL
;
595 struct child_process
*conn
;
596 struct extra_have_objects extra_have
;
597 struct ref
*remote_refs
, *local_refs
;
599 int helper_status
= 0;
601 const char *receivepack
= "git-receive-pack";
605 for (i
= 1; i
< argc
; i
++, argv
++) {
606 const char *arg
= *argv
;
609 if (!prefixcmp(arg
, "--receive-pack=")) {
610 receivepack
= arg
+ 15;
613 if (!prefixcmp(arg
, "--exec=")) {
614 receivepack
= arg
+ 7;
617 if (!prefixcmp(arg
, "--remote=")) {
618 remote_name
= arg
+ 9;
621 if (!strcmp(arg
, "--all")) {
625 if (!strcmp(arg
, "--dry-run")) {
629 if (!strcmp(arg
, "--mirror")) {
630 args
.send_mirror
= 1;
633 if (!strcmp(arg
, "--force")) {
634 args
.force_update
= 1;
637 if (!strcmp(arg
, "--verbose")) {
641 if (!strcmp(arg
, "--thin")) {
642 args
.use_thin_pack
= 1;
645 if (!strcmp(arg
, "--stateless-rpc")) {
646 args
.stateless_rpc
= 1;
649 if (!strcmp(arg
, "--helper-status")) {
653 usage(send_pack_usage
);
659 refspecs
= (const char **) argv
;
660 nr_refspecs
= argc
- i
;
664 usage(send_pack_usage
);
666 * --all and --mirror are incompatible; neither makes sense
669 if ((refspecs
&& (send_all
|| args
.send_mirror
)) ||
670 (send_all
&& args
.send_mirror
))
671 usage(send_pack_usage
);
674 remote
= remote_get(remote_name
);
675 if (!remote_has_url(remote
, dest
)) {
676 die("Destination %s is not a uri for %s",
681 if (args
.stateless_rpc
) {
686 conn
= git_connect(fd
, dest
, receivepack
,
687 args
.verbose
? CONNECT_VERBOSE
: 0);
690 memset(&extra_have
, 0, sizeof(extra_have
));
692 get_remote_heads(fd
[0], &remote_refs
, 0, NULL
, REF_NORMAL
,
695 verify_remote_names(nr_refspecs
, refspecs
);
697 local_refs
= get_local_heads();
699 flags
= MATCH_REFS_NONE
;
702 flags
|= MATCH_REFS_ALL
;
703 if (args
.send_mirror
)
704 flags
|= MATCH_REFS_MIRROR
;
707 if (match_refs(local_refs
, &remote_refs
, nr_refspecs
, refspecs
, flags
))
710 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
713 print_helper_status(remote_refs
);
718 ret
|= finish_connect(conn
);
721 print_push_status(dest
, remote_refs
);
723 if (!args
.dry_run
&& remote
) {
725 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
726 update_tracking_ref(remote
, ref
);
729 if (!ret
&& !refs_pushed(remote_refs
))
730 fprintf(stderr
, "Everything up-to-date\n");