6 #include "run-command.h"
11 #include "transport.h"
13 #include "sha1-array.h"
15 static int config_use_sideband
= 1;
17 static int send_pack_config(const char *var
, const char *value
, void *unused
)
19 if (!strcmp("sendpack.sideband", var
))
20 config_use_sideband
= git_config_bool(var
, value
);
25 static int feed_object(const unsigned char *sha1
, int fd
, int negative
)
29 if (negative
&& !has_sha1_file(sha1
))
32 memcpy(buf
+ negative
, sha1_to_hex(sha1
), 40);
35 buf
[40 + negative
] = '\n';
36 return write_or_whine(fd
, buf
, 41 + negative
, "send-pack: send refs");
40 * Make a pack stream and spit it out into file descriptor fd
42 static int pack_objects(int fd
, struct ref
*refs
, struct sha1_array
*extra
, struct send_pack_args
*args
)
45 * The child becomes pack-objects --revs; we feed
46 * the revision parameters to it via its stdin and
47 * let its stdout go back to the other end.
49 const char *argv
[] = {
51 "--all-progress-implied",
60 struct child_process po
;
64 if (args
->use_thin_pack
)
66 if (args
->use_ofs_delta
)
67 argv
[i
++] = "--delta-base-offset";
68 if (args
->quiet
|| !args
->progress
)
71 argv
[i
++] = "--progress";
72 memset(&po
, 0, sizeof(po
));
75 po
.out
= args
->stateless_rpc
? -1 : fd
;
77 if (start_command(&po
))
78 die_errno("git pack-objects failed");
81 * We feed the pack-objects we just spawned with revision
82 * parameters by writing to the pipe.
84 for (i
= 0; i
< extra
->nr
; i
++)
85 if (!feed_object(extra
->sha1
[i
], po
.in
, 1))
89 if (!is_null_sha1(refs
->old_sha1
) &&
90 !feed_object(refs
->old_sha1
, po
.in
, 1))
92 if (!is_null_sha1(refs
->new_sha1
) &&
93 !feed_object(refs
->new_sha1
, po
.in
, 0))
100 if (args
->stateless_rpc
) {
101 char *buf
= xmalloc(LARGE_PACKET_MAX
);
103 ssize_t n
= xread(po
.out
, buf
, LARGE_PACKET_MAX
);
106 send_sideband(fd
, -1, buf
, n
, LARGE_PACKET_MAX
);
113 if (finish_command(&po
))
118 static int receive_status(int in
, struct ref
*refs
)
122 char *line
= packet_read_line(in
, NULL
);
123 if (!starts_with(line
, "unpack "))
124 return error("did not receive remote status");
125 if (strcmp(line
, "unpack ok")) {
126 error("unpack failed: %s", line
+ 7);
133 line
= packet_read_line(in
, NULL
);
136 if (!starts_with(line
, "ok ") && !starts_with(line
, "ng ")) {
137 error("invalid ref status from remote: %s", line
);
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 int sideband_demux(int in
, int out
, void *data
)
183 ret
= recv_sideband("send-pack", fd
[0], out
);
188 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
190 struct strbuf
*sb
= cb
;
191 if (graft
->nr_parent
== -1)
192 packet_buf_write(sb
, "shallow %s\n", sha1_to_hex(graft
->sha1
));
196 static void advertise_shallow_grafts_buf(struct strbuf
*sb
)
198 if (!is_repository_shallow())
200 for_each_commit_graft(advertise_shallow_grafts_cb
, sb
);
203 int send_pack(struct send_pack_args
*args
,
204 int fd
[], struct child_process
*conn
,
205 struct ref
*remote_refs
,
206 struct sha1_array
*extra_have
)
210 struct strbuf req_buf
= STRBUF_INIT
;
213 int allow_deleting_refs
= 0;
214 int status_report
= 0;
215 int use_sideband
= 0;
216 int quiet_supported
= 0;
217 int agent_supported
= 0;
218 unsigned cmds_sent
= 0;
222 git_config(send_pack_config
, NULL
);
224 /* Does the other end support the reporting? */
225 if (server_supports("report-status"))
227 if (server_supports("delete-refs"))
228 allow_deleting_refs
= 1;
229 if (server_supports("ofs-delta"))
230 args
->use_ofs_delta
= 1;
231 if (config_use_sideband
&& server_supports("side-band-64k"))
233 if (server_supports("quiet"))
235 if (server_supports("agent"))
237 if (server_supports("no-thin"))
238 args
->use_thin_pack
= 0;
241 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
242 "Perhaps you should specify a branch such as 'master'.\n");
247 advertise_shallow_grafts_buf(&req_buf
);
250 * Finally, tell the other end!
253 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
254 if (!ref
->peer_ref
&& !args
->send_mirror
)
257 /* Check for statuses set by set_ref_status_for_push() */
258 switch (ref
->status
) {
259 case REF_STATUS_REJECT_NONFASTFORWARD
:
260 case REF_STATUS_REJECT_ALREADY_EXISTS
:
261 case REF_STATUS_REJECT_FETCH_FIRST
:
262 case REF_STATUS_REJECT_NEEDS_FORCE
:
263 case REF_STATUS_REJECT_STALE
:
264 case REF_STATUS_UPTODATE
:
270 if (ref
->deletion
&& !allow_deleting_refs
) {
271 ref
->status
= REF_STATUS_REJECT_NODELETE
;
279 ref
->status
= REF_STATUS_OK
;
281 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
282 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
283 int quiet
= quiet_supported
&& (args
->quiet
|| !args
->progress
);
285 if (!cmds_sent
&& (status_report
|| use_sideband
||
286 quiet
|| agent_supported
)) {
287 packet_buf_write(&req_buf
,
288 "%s %s %s%c%s%s%s%s%s",
289 old_hex
, new_hex
, ref
->name
, 0,
290 status_report
? " report-status" : "",
291 use_sideband
? " side-band-64k" : "",
292 quiet
? " quiet" : "",
293 agent_supported
? " agent=" : "",
294 agent_supported
? git_user_agent_sanitized() : ""
298 packet_buf_write(&req_buf
, "%s %s %s",
299 old_hex
, new_hex
, ref
->name
);
300 ref
->status
= status_report
?
301 REF_STATUS_EXPECTING_REPORT
:
307 if (args
->stateless_rpc
) {
308 if (!args
->dry_run
&& (cmds_sent
|| is_repository_shallow())) {
309 packet_buf_flush(&req_buf
);
310 send_sideband(out
, -1, req_buf
.buf
, req_buf
.len
, LARGE_PACKET_MAX
);
313 write_or_die(out
, req_buf
.buf
, req_buf
.len
);
316 strbuf_release(&req_buf
);
318 if (use_sideband
&& cmds_sent
) {
319 memset(&demux
, 0, sizeof(demux
));
320 demux
.proc
= sideband_demux
;
323 if (start_async(&demux
))
324 die("send-pack: unable to fork off sideband demultiplexer");
328 if (new_refs
&& cmds_sent
) {
329 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
330 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
331 ref
->status
= REF_STATUS_NONE
;
332 if (args
->stateless_rpc
)
334 if (git_connection_is_socket(conn
))
335 shutdown(fd
[0], SHUT_WR
);
337 finish_async(&demux
);
341 if (!args
->stateless_rpc
)
342 /* Closed by pack_objects() via start_command() */
345 if (args
->stateless_rpc
&& cmds_sent
)
348 if (status_report
&& cmds_sent
)
349 ret
= receive_status(in
, remote_refs
);
352 if (args
->stateless_rpc
)
355 if (use_sideband
&& cmds_sent
) {
356 if (finish_async(&demux
)) {
357 error("error in sideband demultiplexer");
369 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
370 switch (ref
->status
) {
371 case REF_STATUS_NONE
:
372 case REF_STATUS_UPTODATE
: