6 #include "run-command.h"
10 #include "transport.h"
13 static int feed_object(const unsigned char *sha1
, int fd
, int negative
)
17 if (negative
&& !has_sha1_file(sha1
))
20 memcpy(buf
+ negative
, sha1_to_hex(sha1
), 40);
23 buf
[40 + negative
] = '\n';
24 return write_or_whine(fd
, buf
, 41 + negative
, "send-pack: send refs");
28 * Make a pack stream and spit it out into file descriptor fd
30 static int pack_objects(int fd
, struct ref
*refs
, struct extra_have_objects
*extra
, struct send_pack_args
*args
)
33 * The child becomes pack-objects --revs; we feed
34 * the revision parameters to it via its stdin and
35 * let its stdout go back to the other end.
37 const char *argv
[] = {
39 "--all-progress-implied",
48 struct child_process po
;
52 if (args
->use_thin_pack
)
54 if (args
->use_ofs_delta
)
55 argv
[i
++] = "--delta-base-offset";
56 if (args
->quiet
|| !args
->progress
)
59 argv
[i
++] = "--progress";
60 memset(&po
, 0, sizeof(po
));
63 po
.out
= args
->stateless_rpc
? -1 : fd
;
65 if (start_command(&po
))
66 die_errno("git pack-objects failed");
69 * We feed the pack-objects we just spawned with revision
70 * parameters by writing to the pipe.
72 for (i
= 0; i
< extra
->nr
; i
++)
73 if (!feed_object(extra
->array
[i
], po
.in
, 1))
77 if (!is_null_sha1(refs
->old_sha1
) &&
78 !feed_object(refs
->old_sha1
, po
.in
, 1))
80 if (!is_null_sha1(refs
->new_sha1
) &&
81 !feed_object(refs
->new_sha1
, po
.in
, 0))
88 if (args
->stateless_rpc
) {
89 char *buf
= xmalloc(LARGE_PACKET_MAX
);
91 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
94 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
101 if (finish_command(&po
))
106 static int receive_status(int in
, struct ref
*refs
)
110 char *line
= packet_read_line(in
, NULL
);
111 if (prefixcmp(line
, "unpack "))
112 return error("did not receive remote status");
113 if (strcmp(line
, "unpack ok")) {
114 error("unpack failed: %s", line
+ 7);
121 line
= packet_read_line(in
, NULL
);
124 if (prefixcmp(line
, "ok ") && prefixcmp(line
, "ng ")) {
125 error("invalid ref status from remote: %s", line
);
131 msg
= strchr(refname
, ' ');
135 /* first try searching at our hint, falling back to all refs */
137 hint
= find_ref_by_name(hint
, refname
);
139 hint
= find_ref_by_name(refs
, refname
);
141 warning("remote reported status on unknown ref: %s",
145 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
146 warning("remote reported status on unexpected ref: %s",
151 if (line
[0] == 'o' && line
[1] == 'k')
152 hint
->status
= REF_STATUS_OK
;
154 hint
->status
= REF_STATUS_REMOTE_REJECT
;
158 hint
->remote_status
= xstrdup(msg
);
159 /* start our next search from the next ref */
165 static int sideband_demux(int in
, int out
, void *data
)
171 ret
= recv_sideband("send-pack", fd
[0], out
);
176 int send_pack(struct send_pack_args
*args
,
177 int fd
[], struct child_process
*conn
,
178 struct ref
*remote_refs
,
179 struct extra_have_objects
*extra_have
)
183 struct strbuf req_buf
= STRBUF_INIT
;
186 int allow_deleting_refs
= 0;
187 int status_report
= 0;
188 int use_sideband
= 0;
189 int quiet_supported
= 0;
190 int agent_supported
= 0;
191 unsigned cmds_sent
= 0;
195 /* Does the other end support the reporting? */
196 if (server_supports("report-status"))
198 if (server_supports("delete-refs"))
199 allow_deleting_refs
= 1;
200 if (server_supports("ofs-delta"))
201 args
->use_ofs_delta
= 1;
202 if (server_supports("side-band-64k"))
204 if (server_supports("quiet"))
206 if (server_supports("agent"))
210 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
211 "Perhaps you should specify a branch such as 'master'.\n");
216 * Finally, tell the other end!
219 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
220 if (!ref
->peer_ref
&& !args
->send_mirror
)
223 /* Check for statuses set by set_ref_status_for_push() */
224 switch (ref
->status
) {
225 case REF_STATUS_REJECT_NONFASTFORWARD
:
226 case REF_STATUS_REJECT_ALREADY_EXISTS
:
227 case REF_STATUS_REJECT_FETCH_FIRST
:
228 case REF_STATUS_REJECT_NEEDS_FORCE
:
229 case REF_STATUS_UPTODATE
:
235 if (ref
->deletion
&& !allow_deleting_refs
) {
236 ref
->status
= REF_STATUS_REJECT_NODELETE
;
244 ref
->status
= REF_STATUS_OK
;
246 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
247 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
248 int quiet
= quiet_supported
&& (args
->quiet
|| !args
->progress
);
250 if (!cmds_sent
&& (status_report
|| use_sideband
||
251 quiet
|| agent_supported
)) {
252 packet_buf_write(&req_buf
,
253 "%s %s %s%c%s%s%s%s%s",
254 old_hex
, new_hex
, ref
->name
, 0,
255 status_report
? " report-status" : "",
256 use_sideband
? " side-band-64k" : "",
257 quiet
? " quiet" : "",
258 agent_supported
? " agent=" : "",
259 agent_supported
? git_user_agent_sanitized() : ""
263 packet_buf_write(&req_buf
, "%s %s %s",
264 old_hex
, new_hex
, ref
->name
);
265 ref
->status
= status_report
?
266 REF_STATUS_EXPECTING_REPORT
:
272 if (args
->stateless_rpc
) {
273 if (!args
->dry_run
&& cmds_sent
) {
274 packet_buf_flush(&req_buf
);
275 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
278 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
281 strbuf_release(&req_buf
);
283 if (use_sideband
&& cmds_sent
) {
284 memset(&demux
, 0, sizeof(demux
));
285 demux
.proc
= sideband_demux
;
288 if (start_async(&demux
))
289 die("send-pack: unable to fork off sideband demultiplexer");
293 if (new_refs
&& cmds_sent
) {
294 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
295 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
296 ref
->status
= REF_STATUS_NONE
;
297 if (args
->stateless_rpc
)
299 if (git_connection_is_socket(conn
))
300 shutdown(fd
[0], SHUT_WR
);
302 finish_async(&demux
);
306 if (args
->stateless_rpc
&& cmds_sent
)
309 if (status_report
&& cmds_sent
)
310 ret
= receive_status(in
, remote_refs
);
313 if (args
->stateless_rpc
)
316 if (use_sideband
&& cmds_sent
) {
317 if (finish_async(&demux
)) {
318 error("error in sideband demultiplexer");
330 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
331 switch (ref
->status
) {
332 case REF_STATUS_NONE
:
333 case REF_STATUS_UPTODATE
: