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",
52 struct child_process po
;
56 if (args
->use_thin_pack
)
58 if (args
->use_ofs_delta
)
59 argv
[i
++] = "--delta-base-offset";
62 memset(&po
, 0, sizeof(po
));
65 po
.out
= args
->stateless_rpc
? -1 : fd
;
67 if (start_command(&po
))
68 die_errno("git pack-objects failed");
71 * We feed the pack-objects we just spawned with revision
72 * parameters by writing to the pipe.
74 for (i
= 0; i
< extra
->nr
; i
++)
75 if (!feed_object(extra
->array
[i
], po
.in
, 1))
79 if (!is_null_sha1(refs
->old_sha1
) &&
80 !feed_object(refs
->old_sha1
, po
.in
, 1))
82 if (!is_null_sha1(refs
->new_sha1
) &&
83 !feed_object(refs
->new_sha1
, po
.in
, 0))
90 if (args
->stateless_rpc
) {
91 char *buf
= xmalloc(LARGE_PACKET_MAX
);
93 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
96 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
103 if (finish_command(&po
))
108 static int receive_status(int in
, struct ref
*refs
)
113 int len
= packet_read_line(in
, line
, sizeof(line
));
114 if (len
< 10 || memcmp(line
, "unpack ", 7))
115 return error("did not receive remote status");
116 if (memcmp(line
, "unpack ok\n", 10)) {
117 char *p
= line
+ strlen(line
) - 1;
120 error("unpack failed: %s", line
+ 7);
127 len
= packet_read_line(in
, line
, sizeof(line
));
131 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
132 fprintf(stderr
, "protocol error: %s\n", line
);
137 line
[strlen(line
)-1] = '\0';
139 msg
= strchr(refname
, ' ');
143 /* first try searching at our hint, falling back to all refs */
145 hint
= find_ref_by_name(hint
, refname
);
147 hint
= find_ref_by_name(refs
, refname
);
149 warning("remote reported status on unknown ref: %s",
153 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
154 warning("remote reported status on unexpected ref: %s",
159 if (line
[0] == 'o' && line
[1] == 'k')
160 hint
->status
= REF_STATUS_OK
;
162 hint
->status
= REF_STATUS_REMOTE_REJECT
;
166 hint
->remote_status
= xstrdup(msg
);
167 /* start our next search from the next ref */
173 static void print_helper_status(struct ref
*ref
)
175 struct strbuf buf
= STRBUF_INIT
;
177 for (; ref
; ref
= ref
->next
) {
178 const char *msg
= NULL
;
181 switch(ref
->status
) {
182 case REF_STATUS_NONE
:
191 case REF_STATUS_UPTODATE
:
196 case REF_STATUS_REJECT_NONFASTFORWARD
:
198 msg
= "non-fast forward";
201 case REF_STATUS_REJECT_NODELETE
:
202 case REF_STATUS_REMOTE_REJECT
:
206 case REF_STATUS_EXPECTING_REPORT
:
212 strbuf_addf(&buf
, "%s %s", res
, ref
->name
);
213 if (ref
->remote_status
)
214 msg
= ref
->remote_status
;
216 strbuf_addch(&buf
, ' ');
217 quote_two_c_style(&buf
, "", msg
, 0);
219 strbuf_addch(&buf
, '\n');
221 safe_write(1, buf
.buf
, buf
.len
);
223 strbuf_release(&buf
);
226 static int sideband_demux(int in
, int out
, void *data
)
229 int ret
= recv_sideband("send-pack", fd
[0], out
);
234 int send_pack(struct send_pack_args
*args
,
235 int fd
[], struct child_process
*conn
,
236 struct ref
*remote_refs
,
237 struct extra_have_objects
*extra_have
)
241 struct strbuf req_buf
= STRBUF_INIT
;
244 int allow_deleting_refs
= 0;
245 int status_report
= 0;
246 int use_sideband
= 0;
247 unsigned cmds_sent
= 0;
251 /* Does the other end support the reporting? */
252 if (server_supports("report-status"))
254 if (server_supports("delete-refs"))
255 allow_deleting_refs
= 1;
256 if (server_supports("ofs-delta"))
257 args
->use_ofs_delta
= 1;
258 if (server_supports("side-band-64k"))
262 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
263 "Perhaps you should specify a branch such as 'master'.\n");
268 * Finally, tell the other end!
271 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
272 if (!ref
->peer_ref
&& !args
->send_mirror
)
275 /* Check for statuses set by set_ref_status_for_push() */
276 switch (ref
->status
) {
277 case REF_STATUS_REJECT_NONFASTFORWARD
:
278 case REF_STATUS_UPTODATE
:
284 if (ref
->deletion
&& !allow_deleting_refs
) {
285 ref
->status
= REF_STATUS_REJECT_NODELETE
;
293 ref
->status
= REF_STATUS_OK
;
295 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
296 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
298 if (!cmds_sent
&& (status_report
|| use_sideband
)) {
299 packet_buf_write(&req_buf
, "%s %s %s%c%s%s",
300 old_hex
, new_hex
, ref
->name
, 0,
301 status_report
? " report-status" : "",
302 use_sideband
? " side-band-64k" : "");
305 packet_buf_write(&req_buf
, "%s %s %s",
306 old_hex
, new_hex
, ref
->name
);
307 ref
->status
= status_report
?
308 REF_STATUS_EXPECTING_REPORT
:
314 if (args
->stateless_rpc
) {
315 if (!args
->dry_run
&& cmds_sent
) {
316 packet_buf_flush(&req_buf
);
317 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
320 safe_write(out
, req_buf
.buf
, req_buf
.len
);
323 strbuf_release(&req_buf
);
325 if (use_sideband
&& cmds_sent
) {
326 memset(&demux
, 0, sizeof(demux
));
327 demux
.proc
= sideband_demux
;
330 if (start_async(&demux
))
331 die("receive-pack: unable to fork off sideband demultiplexer");
335 if (new_refs
&& cmds_sent
) {
336 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
337 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
338 ref
->status
= REF_STATUS_NONE
;
340 finish_async(&demux
);
344 if (args
->stateless_rpc
&& cmds_sent
)
347 if (status_report
&& cmds_sent
)
348 ret
= receive_status(in
, remote_refs
);
351 if (args
->stateless_rpc
)
354 if (use_sideband
&& cmds_sent
) {
355 if (finish_async(&demux
)) {
356 error("error in sideband demultiplexer");
368 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
369 switch (ref
->status
) {
370 case REF_STATUS_NONE
:
371 case REF_STATUS_UPTODATE
:
381 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
383 int i
, nr_refspecs
= 0;
384 const char **refspecs
= NULL
;
385 const char *remote_name
= NULL
;
386 struct remote
*remote
= NULL
;
387 const char *dest
= NULL
;
389 struct child_process
*conn
;
390 struct extra_have_objects extra_have
;
391 struct ref
*remote_refs
, *local_refs
;
393 int helper_status
= 0;
395 const char *receivepack
= "git-receive-pack";
397 int nonfastforward
= 0;
400 for (i
= 1; i
< argc
; i
++, argv
++) {
401 const char *arg
= *argv
;
404 if (!prefixcmp(arg
, "--receive-pack=")) {
405 receivepack
= arg
+ 15;
408 if (!prefixcmp(arg
, "--exec=")) {
409 receivepack
= arg
+ 7;
412 if (!prefixcmp(arg
, "--remote=")) {
413 remote_name
= arg
+ 9;
416 if (!strcmp(arg
, "--all")) {
420 if (!strcmp(arg
, "--dry-run")) {
424 if (!strcmp(arg
, "--mirror")) {
425 args
.send_mirror
= 1;
428 if (!strcmp(arg
, "--force")) {
429 args
.force_update
= 1;
432 if (!strcmp(arg
, "--verbose")) {
436 if (!strcmp(arg
, "--thin")) {
437 args
.use_thin_pack
= 1;
440 if (!strcmp(arg
, "--stateless-rpc")) {
441 args
.stateless_rpc
= 1;
444 if (!strcmp(arg
, "--helper-status")) {
448 usage(send_pack_usage
);
454 refspecs
= (const char **) argv
;
455 nr_refspecs
= argc
- i
;
459 usage(send_pack_usage
);
461 * --all and --mirror are incompatible; neither makes sense
464 if ((refspecs
&& (send_all
|| args
.send_mirror
)) ||
465 (send_all
&& args
.send_mirror
))
466 usage(send_pack_usage
);
469 remote
= remote_get(remote_name
);
470 if (!remote_has_url(remote
, dest
)) {
471 die("Destination %s is not a uri for %s",
476 if (args
.stateless_rpc
) {
481 conn
= git_connect(fd
, dest
, receivepack
,
482 args
.verbose
? CONNECT_VERBOSE
: 0);
485 memset(&extra_have
, 0, sizeof(extra_have
));
487 get_remote_heads(fd
[0], &remote_refs
, 0, NULL
, REF_NORMAL
,
490 transport_verify_remote_names(nr_refspecs
, refspecs
);
492 local_refs
= get_local_heads();
494 flags
= MATCH_REFS_NONE
;
497 flags
|= MATCH_REFS_ALL
;
498 if (args
.send_mirror
)
499 flags
|= MATCH_REFS_MIRROR
;
502 if (match_refs(local_refs
, &remote_refs
, nr_refspecs
, refspecs
, flags
))
505 set_ref_status_for_push(remote_refs
, args
.send_mirror
,
508 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
511 print_helper_status(remote_refs
);
516 ret
|= finish_connect(conn
);
519 transport_print_push_status(dest
, remote_refs
, args
.verbose
, 0, &nonfastforward
);
521 if (!args
.dry_run
&& remote
) {
523 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
524 transport_update_tracking_ref(remote
, ref
, args
.verbose
);
527 if (!ret
&& !transport_refs_pushed(remote_refs
))
528 fprintf(stderr
, "Everything up-to-date\n");