6 #include "run-command.h"
11 #include "transport.h"
13 #include "sha1-array.h"
14 #include "gpg-interface.h"
17 int option_parse_push_signed(const struct option
*opt
,
18 const char *arg
, int unset
)
21 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
24 switch (git_parse_maybe_bool(arg
)) {
26 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_ALWAYS
;
29 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_NEVER
;
32 if (!strcasecmp("if-asked", arg
)) {
33 *(int *)(opt
->value
) = SEND_PACK_PUSH_CERT_IF_ASKED
;
36 die("bad %s argument: %s", opt
->long_name
, arg
);
39 static void feed_object(const unsigned char *sha1
, FILE *fh
, int negative
)
41 if (negative
&& !has_sha1_file(sha1
))
46 fputs(sha1_to_hex(sha1
), fh
);
51 * Make a pack stream and spit it out into file descriptor fd
53 static int pack_objects(int fd
, struct ref
*refs
, struct sha1_array
*extra
, struct send_pack_args
*args
)
56 * The child becomes pack-objects --revs; we feed
57 * the revision parameters to it via its stdin and
58 * let its stdout go back to the other end.
60 const char *argv
[] = {
62 "--all-progress-implied",
72 struct child_process po
= CHILD_PROCESS_INIT
;
77 if (args
->use_thin_pack
)
79 if (args
->use_ofs_delta
)
80 argv
[i
++] = "--delta-base-offset";
81 if (args
->quiet
|| !args
->progress
)
84 argv
[i
++] = "--progress";
85 if (is_repository_shallow())
86 argv
[i
++] = "--shallow";
89 po
.out
= args
->stateless_rpc
? -1 : fd
;
91 if (start_command(&po
))
92 die_errno("git pack-objects failed");
95 * We feed the pack-objects we just spawned with revision
96 * parameters by writing to the pipe.
98 po_in
= xfdopen(po
.in
, "w");
99 for (i
= 0; i
< extra
->nr
; i
++)
100 feed_object(extra
->sha1
[i
], po_in
, 1);
103 if (!is_null_oid(&refs
->old_oid
))
104 feed_object(refs
->old_oid
.hash
, po_in
, 1);
105 if (!is_null_oid(&refs
->new_oid
))
106 feed_object(refs
->new_oid
.hash
, po_in
, 0);
112 die_errno("error writing to pack-objects");
115 if (args
->stateless_rpc
) {
116 char *buf
= xmalloc(LARGE_PACKET_MAX
);
118 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
121 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
128 if (finish_command(&po
))
133 static int receive_status(int in
, struct ref
*refs
)
137 char *line
= packet_read_line(in
, NULL
);
138 if (!starts_with(line
, "unpack "))
139 return error("did not receive remote status");
140 if (strcmp(line
, "unpack ok")) {
141 error("unpack failed: %s", line
+ 7);
148 line
= packet_read_line(in
, NULL
);
151 if (!starts_with(line
, "ok ") && !starts_with(line
, "ng ")) {
152 error("invalid ref status from remote: %s", line
);
158 msg
= strchr(refname
, ' ');
162 /* first try searching at our hint, falling back to all refs */
164 hint
= find_ref_by_name(hint
, refname
);
166 hint
= find_ref_by_name(refs
, refname
);
168 warning("remote reported status on unknown ref: %s",
172 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
173 warning("remote reported status on unexpected ref: %s",
178 if (line
[0] == 'o' && line
[1] == 'k')
179 hint
->status
= REF_STATUS_OK
;
181 hint
->status
= REF_STATUS_REMOTE_REJECT
;
185 hint
->remote_status
= xstrdup(msg
);
186 /* start our next search from the next ref */
192 static int sideband_demux(int in
, int out
, void *data
)
198 ret
= recv_sideband("send-pack", fd
[0], out
);
203 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
205 struct strbuf
*sb
= cb
;
206 if (graft
->nr_parent
== -1)
207 packet_buf_write(sb
, "shallow %s\n", oid_to_hex(&graft
->oid
));
211 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
213 if (!is_repository_shallow())
215 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
218 #define CHECK_REF_NO_PUSH -1
219 #define CHECK_REF_STATUS_REJECTED -2
220 #define CHECK_REF_UPTODATE -3
221 static int check_to_send_update(const struct ref
*ref
, const struct send_pack_args
*args
)
223 if (!ref
->peer_ref
&& !args
->send_mirror
)
224 return CHECK_REF_NO_PUSH
;
226 /* Check for statuses set by set_ref_status_for_push() */
227 switch (ref
->status
) {
228 case REF_STATUS_REJECT_NONFASTFORWARD
:
229 case REF_STATUS_REJECT_ALREADY_EXISTS
:
230 case REF_STATUS_REJECT_FETCH_FIRST
:
231 case REF_STATUS_REJECT_NEEDS_FORCE
:
232 case REF_STATUS_REJECT_STALE
:
233 case REF_STATUS_REJECT_NODELETE
:
234 return CHECK_REF_STATUS_REJECTED
;
235 case REF_STATUS_UPTODATE
:
236 return CHECK_REF_UPTODATE
;
243 * the beginning of the next line, or the end of buffer.
245 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
246 * convert many similar uses found by "git grep -A4 memchr".
248 static const char *next_line(const char *line
, size_t len
)
250 const char *nl
= memchr(line
, '\n', len
);
252 return line
+ len
; /* incomplete line */
256 static int generate_push_cert(struct strbuf
*req_buf
,
257 const struct ref
*remote_refs
,
258 struct send_pack_args
*args
,
259 const char *cap_string
,
260 const char *push_cert_nonce
)
262 const struct ref
*ref
;
263 char *signing_key
= xstrdup(get_signing_key());
265 struct strbuf cert
= STRBUF_INIT
;
268 strbuf_addstr(&cert
, "certificate version 0.1\n");
269 strbuf_addf(&cert
, "pusher %s ", signing_key
);
271 strbuf_addch(&cert
, '\n');
272 if (args
->url
&& *args
->url
) {
273 char *anon_url
= transport_anonymize_url(args
->url
);
274 strbuf_addf(&cert
, "pushee %s\n", anon_url
);
277 if (push_cert_nonce
[0])
278 strbuf_addf(&cert
, "nonce %s\n", push_cert_nonce
);
279 strbuf_addstr(&cert
, "\n");
281 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
282 if (check_to_send_update(ref
, args
) < 0)
285 strbuf_addf(&cert
, "%s %s %s\n",
286 oid_to_hex(&ref
->old_oid
),
287 oid_to_hex(&ref
->new_oid
),
293 if (sign_buffer(&cert
, &cert
, signing_key
))
294 die(_("failed to sign the push certificate"));
296 packet_buf_write(req_buf
, "push-cert%c%s", 0, cap_string
);
297 for (cp
= cert
.buf
; cp
< cert
.buf
+ cert
.len
; cp
= np
) {
298 np
= next_line(cp
, cert
.buf
+ cert
.len
- cp
);
299 packet_buf_write(req_buf
,
300 "%.*s", (int)(np
- cp
), cp
);
302 packet_buf_write(req_buf
, "push-cert-end\n");
306 strbuf_release(&cert
);
311 static int atomic_push_failure(struct send_pack_args
*args
,
312 struct ref
*remote_refs
,
313 struct ref
*failing_ref
)
316 /* Mark other refs as failed */
317 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
318 if (!ref
->peer_ref
&& !args
->send_mirror
)
321 switch (ref
->status
) {
322 case REF_STATUS_EXPECTING_REPORT
:
323 ref
->status
= REF_STATUS_ATOMIC_PUSH_FAILED
;
326 break; /* do nothing */
329 return error("atomic push failed for ref %s. status: %d\n",
330 failing_ref
->name
, failing_ref
->status
);
333 #define NONCE_LEN_LIMIT 256
335 static void reject_invalid_nonce(const char *nonce
, int len
)
339 if (NONCE_LEN_LIMIT
<= len
)
340 die("the receiving end asked to sign an invalid nonce <%.*s>",
343 for (i
= 0; i
< len
; i
++) {
344 int ch
= nonce
[i
] & 0xFF;
346 ch
== '-' || ch
== '.' ||
347 ch
== '/' || ch
== '+' ||
348 ch
== '=' || ch
== '_')
350 die("the receiving end asked to sign an invalid nonce <%.*s>",
355 int send_pack(struct send_pack_args
*args
,
356 int fd
[], struct child_process
*conn
,
357 struct ref
*remote_refs
,
358 struct sha1_array
*extra_have
)
362 struct strbuf req_buf
= STRBUF_INIT
;
363 struct strbuf cap_buf
= STRBUF_INIT
;
365 int need_pack_data
= 0;
366 int allow_deleting_refs
= 0;
367 int status_report
= 0;
368 int use_sideband
= 0;
369 int quiet_supported
= 0;
370 int agent_supported
= 0;
372 int atomic_supported
= 0;
373 unsigned cmds_sent
= 0;
376 const char *push_cert_nonce
= NULL
;
378 /* Does the other end support the reporting? */
379 if (server_supports("report-status"))
381 if (server_supports("delete-refs"))
382 allow_deleting_refs
= 1;
383 if (server_supports("ofs-delta"))
384 args
->use_ofs_delta
= 1;
385 if (server_supports("side-band-64k"))
387 if (server_supports("quiet"))
389 if (server_supports("agent"))
391 if (server_supports("no-thin"))
392 args
->use_thin_pack
= 0;
393 if (server_supports("atomic"))
394 atomic_supported
= 1;
396 if (args
->push_cert
!= SEND_PACK_PUSH_CERT_NEVER
) {
398 push_cert_nonce
= server_feature_value("push-cert", &len
);
399 if (push_cert_nonce
) {
400 reject_invalid_nonce(push_cert_nonce
, len
);
401 push_cert_nonce
= xmemdupz(push_cert_nonce
, len
);
402 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_ALWAYS
) {
403 die(_("the receiving end does not support --signed push"));
404 } else if (args
->push_cert
== SEND_PACK_PUSH_CERT_IF_ASKED
) {
405 warning(_("not sending a push certificate since the"
406 " receiving end does not support --signed"
412 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
413 "Perhaps you should specify a branch such as 'master'.\n");
416 if (args
->atomic
&& !atomic_supported
)
417 die(_("the receiving end does not support --atomic push"));
419 use_atomic
= atomic_supported
&& args
->atomic
;
422 strbuf_addstr(&cap_buf
, " report-status");
424 strbuf_addstr(&cap_buf
, " side-band-64k");
425 if (quiet_supported
&& (args
->quiet
|| !args
->progress
))
426 strbuf_addstr(&cap_buf
, " quiet");
428 strbuf_addstr(&cap_buf
, " atomic");
430 strbuf_addf(&cap_buf
, " agent=%s", git_user_agent_sanitized());
433 * NEEDSWORK: why does delete-refs have to be so specific to
434 * send-pack machinery that set_ref_status_for_push() cannot
435 * set this bit for us???
437 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
438 if (ref
->deletion
&& !allow_deleting_refs
)
439 ref
->status
= REF_STATUS_REJECT_NODELETE
;
442 advertise_shallow_grafts_buf(&req_buf
);
444 if (!args
->dry_run
&& push_cert_nonce
)
445 cmds_sent
= generate_push_cert(&req_buf
, remote_refs
, args
,
446 cap_buf
.buf
, push_cert_nonce
);
449 * Clear the status for each ref and see if we need to send
452 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
453 switch (check_to_send_update(ref
, args
)) {
454 case 0: /* no error */
456 case CHECK_REF_STATUS_REJECTED
:
458 * When we know the server would reject a ref update if
459 * we were to send it and we're trying to send the refs
460 * atomically, abort the whole operation.
463 return atomic_push_failure(args
, remote_refs
, ref
);
464 /* Fallthrough for non atomic case. */
471 if (args
->dry_run
|| !status_report
)
472 ref
->status
= REF_STATUS_OK
;
474 ref
->status
= REF_STATUS_EXPECTING_REPORT
;
478 * Finally, tell the other end!
480 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
481 char *old_hex
, *new_hex
;
483 if (args
->dry_run
|| push_cert_nonce
)
486 if (check_to_send_update(ref
, args
) < 0)
489 old_hex
= oid_to_hex(&ref
->old_oid
);
490 new_hex
= oid_to_hex(&ref
->new_oid
);
492 packet_buf_write(&req_buf
,
494 old_hex
, new_hex
, ref
->name
, 0,
498 packet_buf_write(&req_buf
, "%s %s %s",
499 old_hex
, new_hex
, ref
->name
);
503 if (args
->stateless_rpc
) {
504 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow())) {
505 packet_buf_flush(&req_buf
);
506 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
509 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
512 strbuf_release(&req_buf
);
513 strbuf_release(&cap_buf
);
515 if (use_sideband
&& cmds_sent
) {
516 memset(&demux
, 0, sizeof(demux
));
517 demux
.proc
= sideband_demux
;
520 demux
.isolate_sigpipe
= 1;
521 if (start_async(&demux
))
522 die("send-pack: unable to fork off sideband demultiplexer");
526 if (need_pack_data
&& cmds_sent
) {
527 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
528 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
529 ref
->status
= REF_STATUS_NONE
;
530 if (args
->stateless_rpc
)
532 if (git_connection_is_socket(conn
))
533 shutdown(fd
[0], SHUT_WR
);
536 finish_async(&demux
);
541 if (!args
->stateless_rpc
)
542 /* Closed by pack_objects() via start_command() */
545 if (args
->stateless_rpc
&& cmds_sent
)
548 if (status_report
&& cmds_sent
)
549 ret
= receive_status(in
, remote_refs
);
552 if (args
->stateless_rpc
)
555 if (use_sideband
&& cmds_sent
) {
557 if (finish_async(&demux
)) {
558 error("error in sideband demultiplexer");
569 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
570 switch (ref
->status
) {
571 case REF_STATUS_NONE
:
572 case REF_STATUS_UPTODATE
: