6 #include "run-command.h"
10 #include "transport.h"
13 static const char send_pack_usage
[] =
14 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
15 " --all and explicit <ref> specification are mutually exclusive.";
17 static struct send_pack_args args
;
19 static int feed_object(const unsigned char *sha1
, int fd
, int negative
)
23 if (negative
&& !has_sha1_file(sha1
))
26 memcpy(buf
+ negative
, sha1_to_hex(sha1
), 40);
29 buf
[40 + negative
] = '\n';
30 return write_or_whine(fd
, buf
, 41 + negative
, "send-pack: send refs");
34 * Make a pack stream and spit it out into file descriptor fd
36 static int pack_objects(int fd
, struct ref
*refs
, struct extra_have_objects
*extra
, struct send_pack_args
*args
)
39 * The child becomes pack-objects --revs; we feed
40 * the revision parameters to it via its stdin and
41 * let its stdout go back to the other end.
43 const char *argv
[] = {
45 "--all-progress-implied",
54 struct child_process po
;
58 if (args
->use_thin_pack
)
60 if (args
->use_ofs_delta
)
61 argv
[i
++] = "--delta-base-offset";
62 if (args
->quiet
|| !args
->progress
)
65 argv
[i
++] = "--progress";
66 memset(&po
, 0, sizeof(po
));
69 po
.out
= args
->stateless_rpc
? -1 : fd
;
71 if (start_command(&po
))
72 die_errno("git pack-objects failed");
75 * We feed the pack-objects we just spawned with revision
76 * parameters by writing to the pipe.
78 for (i
= 0; i
< extra
->nr
; i
++)
79 if (!feed_object(extra
->array
[i
], po
.in
, 1))
83 if (!is_null_sha1(refs
->old_sha1
) &&
84 !feed_object(refs
->old_sha1
, po
.in
, 1))
86 if (!is_null_sha1(refs
->new_sha1
) &&
87 !feed_object(refs
->new_sha1
, po
.in
, 0))
94 if (args
->stateless_rpc
) {
95 char *buf
= xmalloc(LARGE_PACKET_MAX
);
97 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
100 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
107 if (finish_command(&po
))
112 static int receive_status(int in
, struct ref
*refs
)
117 int len
= packet_read_line(in
, line
, sizeof(line
));
118 if (len
< 10 || memcmp(line
, "unpack ", 7))
119 return error("did not receive remote status");
120 if (memcmp(line
, "unpack ok\n", 10)) {
121 char *p
= line
+ strlen(line
) - 1;
124 error("unpack failed: %s", line
+ 7);
131 len
= packet_read_line(in
, line
, sizeof(line
));
135 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
136 fprintf(stderr
, "protocol error: %s\n", line
);
141 line
[strlen(line
)-1] = '\0';
143 msg
= strchr(refname
, ' ');
147 /* first try searching at our hint, falling back to all refs */
149 hint
= find_ref_by_name(hint
, refname
);
151 hint
= find_ref_by_name(refs
, refname
);
153 warning("remote reported status on unknown ref: %s",
157 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
158 warning("remote reported status on unexpected ref: %s",
163 if (line
[0] == 'o' && line
[1] == 'k')
164 hint
->status
= REF_STATUS_OK
;
166 hint
->status
= REF_STATUS_REMOTE_REJECT
;
170 hint
->remote_status
= xstrdup(msg
);
171 /* start our next search from the next ref */
177 static void print_helper_status(struct ref
*ref
)
179 struct strbuf buf
= STRBUF_INIT
;
181 for (; ref
; ref
= ref
->next
) {
182 const char *msg
= NULL
;
185 switch(ref
->status
) {
186 case REF_STATUS_NONE
:
195 case REF_STATUS_UPTODATE
:
200 case REF_STATUS_REJECT_NONFASTFORWARD
:
202 msg
= "non-fast forward";
205 case REF_STATUS_REJECT_NODELETE
:
206 case REF_STATUS_REMOTE_REJECT
:
210 case REF_STATUS_EXPECTING_REPORT
:
216 strbuf_addf(&buf
, "%s %s", res
, ref
->name
);
217 if (ref
->remote_status
)
218 msg
= ref
->remote_status
;
220 strbuf_addch(&buf
, ' ');
221 quote_two_c_style(&buf
, "", msg
, 0);
223 strbuf_addch(&buf
, '\n');
225 safe_write(1, buf
.buf
, buf
.len
);
227 strbuf_release(&buf
);
230 static int sideband_demux(int in
, int out
, void *data
)
236 ret
= recv_sideband("send-pack", fd
[0], out
);
241 int send_pack(struct send_pack_args
*args
,
242 int fd
[], struct child_process
*conn
,
243 struct ref
*remote_refs
,
244 struct extra_have_objects
*extra_have
)
248 struct strbuf req_buf
= STRBUF_INIT
;
251 int allow_deleting_refs
= 0;
252 int status_report
= 0;
253 int use_sideband
= 0;
254 int quiet_supported
= 0;
255 int agent_supported
= 0;
256 unsigned cmds_sent
= 0;
260 /* Does the other end support the reporting? */
261 if (server_supports("report-status"))
263 if (server_supports("delete-refs"))
264 allow_deleting_refs
= 1;
265 if (server_supports("ofs-delta"))
266 args
->use_ofs_delta
= 1;
267 if (server_supports("side-band-64k"))
269 if (server_supports("quiet"))
271 if (server_supports("agent"))
275 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
276 "Perhaps you should specify a branch such as 'master'.\n");
281 * Finally, tell the other end!
284 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
285 if (!ref
->peer_ref
&& !args
->send_mirror
)
288 /* Check for statuses set by set_ref_status_for_push() */
289 switch (ref
->status
) {
290 case REF_STATUS_REJECT_NONFASTFORWARD
:
291 case REF_STATUS_UPTODATE
:
297 if (ref
->deletion
&& !allow_deleting_refs
) {
298 ref
->status
= REF_STATUS_REJECT_NODELETE
;
306 ref
->status
= REF_STATUS_OK
;
308 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
309 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
310 int quiet
= quiet_supported
&& (args
->quiet
|| !args
->progress
);
312 if (!cmds_sent
&& (status_report
|| use_sideband
||
313 quiet
|| agent_supported
)) {
314 packet_buf_write(&req_buf
,
315 "%s %s %s%c%s%s%s%s%s",
316 old_hex
, new_hex
, ref
->name
, 0,
317 status_report
? " report-status" : "",
318 use_sideband
? " side-band-64k" : "",
319 quiet
? " quiet" : "",
320 agent_supported
? " agent=" : "",
321 agent_supported
? git_user_agent_sanitized() : ""
325 packet_buf_write(&req_buf
, "%s %s %s",
326 old_hex
, new_hex
, ref
->name
);
327 ref
->status
= status_report
?
328 REF_STATUS_EXPECTING_REPORT
:
334 if (args
->stateless_rpc
) {
335 if (!args
->dry_run
&& cmds_sent
) {
336 packet_buf_flush(&req_buf
);
337 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
340 safe_write(out
, req_buf
.buf
, req_buf
.len
);
343 strbuf_release(&req_buf
);
345 if (use_sideband
&& cmds_sent
) {
346 memset(&demux
, 0, sizeof(demux
));
347 demux
.proc
= sideband_demux
;
350 if (start_async(&demux
))
351 die("send-pack: unable to fork off sideband demultiplexer");
355 if (new_refs
&& cmds_sent
) {
356 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
357 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
358 ref
->status
= REF_STATUS_NONE
;
359 if (args
->stateless_rpc
)
361 if (git_connection_is_socket(conn
))
362 shutdown(fd
[0], SHUT_WR
);
364 finish_async(&demux
);
368 if (args
->stateless_rpc
&& cmds_sent
)
371 if (status_report
&& cmds_sent
)
372 ret
= receive_status(in
, remote_refs
);
375 if (args
->stateless_rpc
)
378 if (use_sideband
&& cmds_sent
) {
379 if (finish_async(&demux
)) {
380 error("error in sideband demultiplexer");
392 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
393 switch (ref
->status
) {
394 case REF_STATUS_NONE
:
395 case REF_STATUS_UPTODATE
:
405 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
407 int i
, nr_refspecs
= 0;
408 const char **refspecs
= NULL
;
409 const char *remote_name
= NULL
;
410 struct remote
*remote
= NULL
;
411 const char *dest
= NULL
;
413 struct child_process
*conn
;
414 struct extra_have_objects extra_have
;
415 struct ref
*remote_refs
, *local_refs
;
417 int helper_status
= 0;
419 const char *receivepack
= "git-receive-pack";
421 int nonfastforward
= 0;
425 for (i
= 1; i
< argc
; i
++, argv
++) {
426 const char *arg
= *argv
;
429 if (!prefixcmp(arg
, "--receive-pack=")) {
430 receivepack
= arg
+ 15;
433 if (!prefixcmp(arg
, "--exec=")) {
434 receivepack
= arg
+ 7;
437 if (!prefixcmp(arg
, "--remote=")) {
438 remote_name
= arg
+ 9;
441 if (!strcmp(arg
, "--all")) {
445 if (!strcmp(arg
, "--dry-run")) {
449 if (!strcmp(arg
, "--mirror")) {
450 args
.send_mirror
= 1;
453 if (!strcmp(arg
, "--force")) {
454 args
.force_update
= 1;
457 if (!strcmp(arg
, "--quiet")) {
461 if (!strcmp(arg
, "--verbose")) {
465 if (!strcmp(arg
, "--progress")) {
469 if (!strcmp(arg
, "--no-progress")) {
473 if (!strcmp(arg
, "--thin")) {
474 args
.use_thin_pack
= 1;
477 if (!strcmp(arg
, "--stateless-rpc")) {
478 args
.stateless_rpc
= 1;
481 if (!strcmp(arg
, "--helper-status")) {
485 usage(send_pack_usage
);
491 refspecs
= (const char **) argv
;
492 nr_refspecs
= argc
- i
;
496 usage(send_pack_usage
);
498 * --all and --mirror are incompatible; neither makes sense
501 if ((refspecs
&& (send_all
|| args
.send_mirror
)) ||
502 (send_all
&& args
.send_mirror
))
503 usage(send_pack_usage
);
506 remote
= remote_get(remote_name
);
507 if (!remote_has_url(remote
, dest
)) {
508 die("Destination %s is not a uri for %s",
514 progress
= !args
.quiet
&& isatty(2);
515 args
.progress
= progress
;
517 if (args
.stateless_rpc
) {
522 conn
= git_connect(fd
, dest
, receivepack
,
523 args
.verbose
? CONNECT_VERBOSE
: 0);
526 memset(&extra_have
, 0, sizeof(extra_have
));
528 get_remote_heads(fd
[0], &remote_refs
, REF_NORMAL
, &extra_have
);
530 transport_verify_remote_names(nr_refspecs
, refspecs
);
532 local_refs
= get_local_heads();
534 flags
= MATCH_REFS_NONE
;
537 flags
|= MATCH_REFS_ALL
;
538 if (args
.send_mirror
)
539 flags
|= MATCH_REFS_MIRROR
;
542 if (match_push_refs(local_refs
, &remote_refs
, nr_refspecs
, refspecs
, flags
))
545 set_ref_status_for_push(remote_refs
, args
.send_mirror
,
548 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
551 print_helper_status(remote_refs
);
556 ret
|= finish_connect(conn
);
559 transport_print_push_status(dest
, remote_refs
, args
.verbose
, 0, &nonfastforward
);
561 if (!args
.dry_run
&& remote
) {
563 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
564 transport_update_tracking_ref(remote
, ref
, args
.verbose
);
567 if (!ret
&& !transport_refs_pushed(remote_refs
))
568 fprintf(stderr
, "Everything up-to-date\n");