6 #include "run-command.h"
10 #include "transport.h"
12 static const char send_pack_usage
[] =
13 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
14 " --all and explicit <ref> specification are mutually exclusive.";
16 static struct send_pack_args args
;
18 static int feed_object(const unsigned char *sha1
, int fd
, int negative
)
22 if (negative
&& !has_sha1_file(sha1
))
25 memcpy(buf
+ negative
, sha1_to_hex(sha1
), 40);
28 buf
[40 + negative
] = '\n';
29 return write_or_whine(fd
, buf
, 41 + negative
, "send-pack: send refs");
33 * Make a pack stream and spit it out into file descriptor fd
35 static int pack_objects(int fd
, struct ref
*refs
, struct extra_have_objects
*extra
, struct send_pack_args
*args
)
38 * The child becomes pack-objects --revs; we feed
39 * the revision parameters to it via its stdin and
40 * let its stdout go back to the other end.
42 const char *argv
[] = {
44 "--all-progress-implied",
53 struct child_process po
;
57 if (args
->use_thin_pack
)
59 if (args
->use_ofs_delta
)
60 argv
[i
++] = "--delta-base-offset";
64 argv
[i
++] = "--progress";
65 memset(&po
, 0, sizeof(po
));
68 po
.out
= args
->stateless_rpc
? -1 : fd
;
70 if (start_command(&po
))
71 die_errno("git pack-objects failed");
74 * We feed the pack-objects we just spawned with revision
75 * parameters by writing to the pipe.
77 for (i
= 0; i
< extra
->nr
; i
++)
78 if (!feed_object(extra
->array
[i
], po
.in
, 1))
82 if (!is_null_sha1(refs
->old_sha1
) &&
83 !feed_object(refs
->old_sha1
, po
.in
, 1))
85 if (!is_null_sha1(refs
->new_sha1
) &&
86 !feed_object(refs
->new_sha1
, po
.in
, 0))
93 if (args
->stateless_rpc
) {
94 char *buf
= xmalloc(LARGE_PACKET_MAX
);
96 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
99 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
106 if (finish_command(&po
))
111 static int receive_status(int in
, struct ref
*refs
)
116 int len
= packet_read_line(in
, line
, sizeof(line
));
117 if (len
< 10 || memcmp(line
, "unpack ", 7))
118 return error("did not receive remote status");
119 if (memcmp(line
, "unpack ok\n", 10)) {
120 char *p
= line
+ strlen(line
) - 1;
123 error("unpack failed: %s", line
+ 7);
130 len
= packet_read_line(in
, line
, sizeof(line
));
134 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
135 fprintf(stderr
, "protocol error: %s\n", line
);
140 line
[strlen(line
)-1] = '\0';
142 msg
= strchr(refname
, ' ');
146 /* first try searching at our hint, falling back to all refs */
148 hint
= find_ref_by_name(hint
, refname
);
150 hint
= find_ref_by_name(refs
, refname
);
152 warning("remote reported status on unknown ref: %s",
156 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
157 warning("remote reported status on unexpected ref: %s",
162 if (line
[0] == 'o' && line
[1] == 'k')
163 hint
->status
= REF_STATUS_OK
;
165 hint
->status
= REF_STATUS_REMOTE_REJECT
;
169 hint
->remote_status
= xstrdup(msg
);
170 /* start our next search from the next ref */
176 static void print_helper_status(struct ref
*ref
)
178 struct strbuf buf
= STRBUF_INIT
;
180 for (; ref
; ref
= ref
->next
) {
181 const char *msg
= NULL
;
184 switch(ref
->status
) {
185 case REF_STATUS_NONE
:
194 case REF_STATUS_UPTODATE
:
199 case REF_STATUS_REJECT_NONFASTFORWARD
:
201 msg
= "non-fast forward";
204 case REF_STATUS_REJECT_NODELETE
:
205 case REF_STATUS_REMOTE_REJECT
:
209 case REF_STATUS_EXPECTING_REPORT
:
215 strbuf_addf(&buf
, "%s %s", res
, ref
->name
);
216 if (ref
->remote_status
)
217 msg
= ref
->remote_status
;
219 strbuf_addch(&buf
, ' ');
220 quote_two_c_style(&buf
, "", msg
, 0);
222 strbuf_addch(&buf
, '\n');
224 safe_write(1, buf
.buf
, buf
.len
);
226 strbuf_release(&buf
);
229 static int sideband_demux(int in
, int out
, void *data
)
232 int ret
= recv_sideband("send-pack", fd
[0], out
);
237 int send_pack(struct send_pack_args
*args
,
238 int fd
[], struct child_process
*conn
,
239 struct ref
*remote_refs
,
240 struct extra_have_objects
*extra_have
)
244 struct strbuf req_buf
= STRBUF_INIT
;
247 int allow_deleting_refs
= 0;
248 int status_report
= 0;
249 int use_sideband
= 0;
250 unsigned cmds_sent
= 0;
254 /* Does the other end support the reporting? */
255 if (server_supports("report-status"))
257 if (server_supports("delete-refs"))
258 allow_deleting_refs
= 1;
259 if (server_supports("ofs-delta"))
260 args
->use_ofs_delta
= 1;
261 if (server_supports("side-band-64k"))
265 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
266 "Perhaps you should specify a branch such as 'master'.\n");
271 * Finally, tell the other end!
274 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
275 if (!ref
->peer_ref
&& !args
->send_mirror
)
278 /* Check for statuses set by set_ref_status_for_push() */
279 switch (ref
->status
) {
280 case REF_STATUS_REJECT_NONFASTFORWARD
:
281 case REF_STATUS_UPTODATE
:
287 if (ref
->deletion
&& !allow_deleting_refs
) {
288 ref
->status
= REF_STATUS_REJECT_NODELETE
;
296 ref
->status
= REF_STATUS_OK
;
298 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
299 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
301 if (!cmds_sent
&& (status_report
|| use_sideband
)) {
302 packet_buf_write(&req_buf
, "%s %s %s%c%s%s",
303 old_hex
, new_hex
, ref
->name
, 0,
304 status_report
? " report-status" : "",
305 use_sideband
? " side-band-64k" : "");
308 packet_buf_write(&req_buf
, "%s %s %s",
309 old_hex
, new_hex
, ref
->name
);
310 ref
->status
= status_report
?
311 REF_STATUS_EXPECTING_REPORT
:
317 if (args
->stateless_rpc
) {
318 if (!args
->dry_run
&& cmds_sent
) {
319 packet_buf_flush(&req_buf
);
320 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
323 safe_write(out
, req_buf
.buf
, req_buf
.len
);
326 strbuf_release(&req_buf
);
328 if (use_sideband
&& cmds_sent
) {
329 memset(&demux
, 0, sizeof(demux
));
330 demux
.proc
= sideband_demux
;
333 if (start_async(&demux
))
334 die("receive-pack: unable to fork off sideband demultiplexer");
338 if (new_refs
&& cmds_sent
) {
339 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
340 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
341 ref
->status
= REF_STATUS_NONE
;
343 finish_async(&demux
);
347 if (args
->stateless_rpc
&& cmds_sent
)
350 if (status_report
&& cmds_sent
)
351 ret
= receive_status(in
, remote_refs
);
354 if (args
->stateless_rpc
)
357 if (use_sideband
&& cmds_sent
) {
358 if (finish_async(&demux
)) {
359 error("error in sideband demultiplexer");
371 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
372 switch (ref
->status
) {
373 case REF_STATUS_NONE
:
374 case REF_STATUS_UPTODATE
:
384 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
386 int i
, nr_refspecs
= 0;
387 const char **refspecs
= NULL
;
388 const char *remote_name
= NULL
;
389 struct remote
*remote
= NULL
;
390 const char *dest
= NULL
;
392 struct child_process
*conn
;
393 struct extra_have_objects extra_have
;
394 struct ref
*remote_refs
, *local_refs
;
396 int helper_status
= 0;
398 const char *receivepack
= "git-receive-pack";
400 int nonfastforward
= 0;
403 for (i
= 1; i
< argc
; i
++, argv
++) {
404 const char *arg
= *argv
;
407 if (!prefixcmp(arg
, "--receive-pack=")) {
408 receivepack
= arg
+ 15;
411 if (!prefixcmp(arg
, "--exec=")) {
412 receivepack
= arg
+ 7;
415 if (!prefixcmp(arg
, "--remote=")) {
416 remote_name
= arg
+ 9;
419 if (!strcmp(arg
, "--all")) {
423 if (!strcmp(arg
, "--dry-run")) {
427 if (!strcmp(arg
, "--mirror")) {
428 args
.send_mirror
= 1;
431 if (!strcmp(arg
, "--force")) {
432 args
.force_update
= 1;
435 if (!strcmp(arg
, "--verbose")) {
439 if (!strcmp(arg
, "--thin")) {
440 args
.use_thin_pack
= 1;
443 if (!strcmp(arg
, "--stateless-rpc")) {
444 args
.stateless_rpc
= 1;
447 if (!strcmp(arg
, "--helper-status")) {
451 usage(send_pack_usage
);
457 refspecs
= (const char **) argv
;
458 nr_refspecs
= argc
- i
;
462 usage(send_pack_usage
);
464 * --all and --mirror are incompatible; neither makes sense
467 if ((refspecs
&& (send_all
|| args
.send_mirror
)) ||
468 (send_all
&& args
.send_mirror
))
469 usage(send_pack_usage
);
472 remote
= remote_get(remote_name
);
473 if (!remote_has_url(remote
, dest
)) {
474 die("Destination %s is not a uri for %s",
479 if (args
.stateless_rpc
) {
484 conn
= git_connect(fd
, dest
, receivepack
,
485 args
.verbose
? CONNECT_VERBOSE
: 0);
488 memset(&extra_have
, 0, sizeof(extra_have
));
490 get_remote_heads(fd
[0], &remote_refs
, 0, NULL
, REF_NORMAL
,
493 transport_verify_remote_names(nr_refspecs
, refspecs
);
495 local_refs
= get_local_heads();
497 flags
= MATCH_REFS_NONE
;
500 flags
|= MATCH_REFS_ALL
;
501 if (args
.send_mirror
)
502 flags
|= MATCH_REFS_MIRROR
;
505 if (match_refs(local_refs
, &remote_refs
, nr_refspecs
, refspecs
, flags
))
508 set_ref_status_for_push(remote_refs
, args
.send_mirror
,
511 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
514 print_helper_status(remote_refs
);
519 ret
|= finish_connect(conn
);
522 transport_print_push_status(dest
, remote_refs
, args
.verbose
, 0, &nonfastforward
);
524 if (!args
.dry_run
&& remote
) {
526 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
527 transport_update_tracking_ref(remote
, ref
, args
.verbose
);
530 if (!ret
&& !transport_refs_pushed(remote_refs
))
531 fprintf(stderr
, "Everything up-to-date\n");