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 unsigned cmds_sent
= 0;
259 /* Does the other end support the reporting? */
260 if (server_supports("report-status"))
262 if (server_supports("delete-refs"))
263 allow_deleting_refs
= 1;
264 if (server_supports("ofs-delta"))
265 args
->use_ofs_delta
= 1;
266 if (server_supports("side-band-64k"))
268 if (server_supports("quiet"))
272 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
273 "Perhaps you should specify a branch such as 'master'.\n");
278 * Finally, tell the other end!
281 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
282 if (!ref
->peer_ref
&& !args
->send_mirror
)
285 /* Check for statuses set by set_ref_status_for_push() */
286 switch (ref
->status
) {
287 case REF_STATUS_REJECT_NONFASTFORWARD
:
288 case REF_STATUS_UPTODATE
:
294 if (ref
->deletion
&& !allow_deleting_refs
) {
295 ref
->status
= REF_STATUS_REJECT_NODELETE
;
303 ref
->status
= REF_STATUS_OK
;
305 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
306 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
307 int quiet
= quiet_supported
&& (args
->quiet
|| !args
->progress
);
309 if (!cmds_sent
&& (status_report
|| use_sideband
|| args
->quiet
)) {
310 packet_buf_write(&req_buf
,
311 "%s %s %s%c%s%s%s agent=%s",
312 old_hex
, new_hex
, ref
->name
, 0,
313 status_report
? " report-status" : "",
314 use_sideband
? " side-band-64k" : "",
315 quiet
? " quiet" : "",
316 git_user_agent_sanitized());
319 packet_buf_write(&req_buf
, "%s %s %s",
320 old_hex
, new_hex
, ref
->name
);
321 ref
->status
= status_report
?
322 REF_STATUS_EXPECTING_REPORT
:
328 if (args
->stateless_rpc
) {
329 if (!args
->dry_run
&& cmds_sent
) {
330 packet_buf_flush(&req_buf
);
331 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
334 safe_write(out
, req_buf
.buf
, req_buf
.len
);
337 strbuf_release(&req_buf
);
339 if (use_sideband
&& cmds_sent
) {
340 memset(&demux
, 0, sizeof(demux
));
341 demux
.proc
= sideband_demux
;
344 if (start_async(&demux
))
345 die("send-pack: unable to fork off sideband demultiplexer");
349 if (new_refs
&& cmds_sent
) {
350 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
351 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
352 ref
->status
= REF_STATUS_NONE
;
353 if (args
->stateless_rpc
)
355 if (git_connection_is_socket(conn
))
356 shutdown(fd
[0], SHUT_WR
);
358 finish_async(&demux
);
362 if (args
->stateless_rpc
&& cmds_sent
)
365 if (status_report
&& cmds_sent
)
366 ret
= receive_status(in
, remote_refs
);
369 if (args
->stateless_rpc
)
372 if (use_sideband
&& cmds_sent
) {
373 if (finish_async(&demux
)) {
374 error("error in sideband demultiplexer");
386 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
387 switch (ref
->status
) {
388 case REF_STATUS_NONE
:
389 case REF_STATUS_UPTODATE
:
399 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
401 int i
, nr_refspecs
= 0;
402 const char **refspecs
= NULL
;
403 const char *remote_name
= NULL
;
404 struct remote
*remote
= NULL
;
405 const char *dest
= NULL
;
407 struct child_process
*conn
;
408 struct extra_have_objects extra_have
;
409 struct ref
*remote_refs
, *local_refs
;
411 int helper_status
= 0;
413 const char *receivepack
= "git-receive-pack";
415 int nonfastforward
= 0;
419 for (i
= 1; i
< argc
; i
++, argv
++) {
420 const char *arg
= *argv
;
423 if (!prefixcmp(arg
, "--receive-pack=")) {
424 receivepack
= arg
+ 15;
427 if (!prefixcmp(arg
, "--exec=")) {
428 receivepack
= arg
+ 7;
431 if (!prefixcmp(arg
, "--remote=")) {
432 remote_name
= arg
+ 9;
435 if (!strcmp(arg
, "--all")) {
439 if (!strcmp(arg
, "--dry-run")) {
443 if (!strcmp(arg
, "--mirror")) {
444 args
.send_mirror
= 1;
447 if (!strcmp(arg
, "--force")) {
448 args
.force_update
= 1;
451 if (!strcmp(arg
, "--quiet")) {
455 if (!strcmp(arg
, "--verbose")) {
459 if (!strcmp(arg
, "--progress")) {
463 if (!strcmp(arg
, "--no-progress")) {
467 if (!strcmp(arg
, "--thin")) {
468 args
.use_thin_pack
= 1;
471 if (!strcmp(arg
, "--stateless-rpc")) {
472 args
.stateless_rpc
= 1;
475 if (!strcmp(arg
, "--helper-status")) {
479 usage(send_pack_usage
);
485 refspecs
= (const char **) argv
;
486 nr_refspecs
= argc
- i
;
490 usage(send_pack_usage
);
492 * --all and --mirror are incompatible; neither makes sense
495 if ((refspecs
&& (send_all
|| args
.send_mirror
)) ||
496 (send_all
&& args
.send_mirror
))
497 usage(send_pack_usage
);
500 remote
= remote_get(remote_name
);
501 if (!remote_has_url(remote
, dest
)) {
502 die("Destination %s is not a uri for %s",
508 progress
= !args
.quiet
&& isatty(2);
509 args
.progress
= progress
;
511 if (args
.stateless_rpc
) {
516 conn
= git_connect(fd
, dest
, receivepack
,
517 args
.verbose
? CONNECT_VERBOSE
: 0);
520 memset(&extra_have
, 0, sizeof(extra_have
));
522 get_remote_heads(fd
[0], &remote_refs
, REF_NORMAL
, &extra_have
);
524 transport_verify_remote_names(nr_refspecs
, refspecs
);
526 local_refs
= get_local_heads();
528 flags
= MATCH_REFS_NONE
;
531 flags
|= MATCH_REFS_ALL
;
532 if (args
.send_mirror
)
533 flags
|= MATCH_REFS_MIRROR
;
536 if (match_push_refs(local_refs
, &remote_refs
, nr_refspecs
, refspecs
, flags
))
539 set_ref_status_for_push(remote_refs
, args
.send_mirror
,
542 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
545 print_helper_status(remote_refs
);
550 ret
|= finish_connect(conn
);
553 transport_print_push_status(dest
, remote_refs
, args
.verbose
, 0, &nonfastforward
);
555 if (!args
.dry_run
&& remote
) {
557 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
558 transport_update_tracking_ref(remote
, ref
, args
.verbose
);
561 if (!ret
&& !transport_refs_pushed(remote_refs
))
562 fprintf(stderr
, "Everything up-to-date\n");