config.c: mark die_bad_number as NORETURN
[git/mingw/4msysgit.git] / send-pack.c
blobaace1fc51debf72ea0042335dcdac4e59b06caea
1 #include "builtin.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "run-command.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "send-pack.h"
10 #include "quote.h"
11 #include "transport.h"
12 #include "version.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);
22 return 0;
25 static int feed_object(const unsigned char *sha1, int fd, int negative)
27 char buf[42];
29 if (negative && !has_sha1_file(sha1))
30 return 1;
32 memcpy(buf + negative, sha1_to_hex(sha1), 40);
33 if (negative)
34 buf[0] = '^';
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[] = {
50 "pack-objects",
51 "--all-progress-implied",
52 "--revs",
53 "--stdout",
54 NULL,
55 NULL,
56 NULL,
57 NULL,
58 NULL,
60 struct child_process po;
61 int i;
63 i = 4;
64 if (args->use_thin_pack)
65 argv[i++] = "--thin";
66 if (args->use_ofs_delta)
67 argv[i++] = "--delta-base-offset";
68 if (args->quiet || !args->progress)
69 argv[i++] = "-q";
70 if (args->progress)
71 argv[i++] = "--progress";
72 memset(&po, 0, sizeof(po));
73 po.argv = argv;
74 po.in = -1;
75 po.out = args->stateless_rpc ? -1 : fd;
76 po.git_cmd = 1;
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))
86 break;
88 while (refs) {
89 if (!is_null_sha1(refs->old_sha1) &&
90 !feed_object(refs->old_sha1, po.in, 1))
91 break;
92 if (!is_null_sha1(refs->new_sha1) &&
93 !feed_object(refs->new_sha1, po.in, 0))
94 break;
95 refs = refs->next;
98 close(po.in);
100 if (args->stateless_rpc) {
101 char *buf = xmalloc(LARGE_PACKET_MAX);
102 while (1) {
103 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
104 if (n <= 0)
105 break;
106 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
108 free(buf);
109 close(po.out);
110 po.out = -1;
113 if (finish_command(&po))
114 return -1;
115 return 0;
118 static int receive_status(int in, struct ref *refs)
120 struct ref *hint;
121 int ret = 0;
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);
127 ret = -1;
129 hint = NULL;
130 while (1) {
131 char *refname;
132 char *msg;
133 line = packet_read_line(in, NULL);
134 if (!line)
135 break;
136 if (!starts_with(line, "ok ") && !starts_with(line, "ng ")) {
137 error("invalid ref status from remote: %s", line);
138 ret = -1;
139 break;
142 refname = line + 3;
143 msg = strchr(refname, ' ');
144 if (msg)
145 *msg++ = '\0';
147 /* first try searching at our hint, falling back to all refs */
148 if (hint)
149 hint = find_ref_by_name(hint, refname);
150 if (!hint)
151 hint = find_ref_by_name(refs, refname);
152 if (!hint) {
153 warning("remote reported status on unknown ref: %s",
154 refname);
155 continue;
157 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
158 warning("remote reported status on unexpected ref: %s",
159 refname);
160 continue;
163 if (line[0] == 'o' && line[1] == 'k')
164 hint->status = REF_STATUS_OK;
165 else {
166 hint->status = REF_STATUS_REMOTE_REJECT;
167 ret = -1;
169 if (msg)
170 hint->remote_status = xstrdup(msg);
171 /* start our next search from the next ref */
172 hint = hint->next;
174 return ret;
177 static int sideband_demux(int in, int out, void *data)
179 int *fd = data, ret;
180 #ifdef NO_PTHREADS
181 close(fd[1]);
182 #endif
183 ret = recv_sideband("send-pack", fd[0], out);
184 close(out);
185 return ret;
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));
193 return 0;
196 static void advertise_shallow_grafts_buf(struct strbuf *sb)
198 if (!is_repository_shallow())
199 return;
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)
208 int in = fd[0];
209 int out = fd[1];
210 struct strbuf req_buf = STRBUF_INIT;
211 struct ref *ref;
212 int new_refs;
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;
219 int ret;
220 struct async demux;
222 git_config(send_pack_config, NULL);
224 /* Does the other end support the reporting? */
225 if (server_supports("report-status"))
226 status_report = 1;
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"))
232 use_sideband = 1;
233 if (server_supports("quiet"))
234 quiet_supported = 1;
235 if (server_supports("agent"))
236 agent_supported = 1;
237 if (server_supports("no-thin"))
238 args->use_thin_pack = 0;
240 if (!remote_refs) {
241 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
242 "Perhaps you should specify a branch such as 'master'.\n");
243 return 0;
246 if (!args->dry_run)
247 advertise_shallow_grafts_buf(&req_buf);
250 * Finally, tell the other end!
252 new_refs = 0;
253 for (ref = remote_refs; ref; ref = ref->next) {
254 if (!ref->peer_ref && !args->send_mirror)
255 continue;
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:
265 continue;
266 default:
267 ; /* do nothing */
270 if (ref->deletion && !allow_deleting_refs) {
271 ref->status = REF_STATUS_REJECT_NODELETE;
272 continue;
275 if (!ref->deletion)
276 new_refs++;
278 if (args->dry_run) {
279 ref->status = REF_STATUS_OK;
280 } else {
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() : ""
297 else
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 :
302 REF_STATUS_OK;
303 cmds_sent++;
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);
312 } else {
313 write_or_die(out, req_buf.buf, req_buf.len);
314 packet_flush(out);
316 strbuf_release(&req_buf);
318 if (use_sideband && cmds_sent) {
319 memset(&demux, 0, sizeof(demux));
320 demux.proc = sideband_demux;
321 demux.data = fd;
322 demux.out = -1;
323 if (start_async(&demux))
324 die("send-pack: unable to fork off sideband demultiplexer");
325 in = demux.out;
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)
333 close(out);
334 if (git_connection_is_socket(conn))
335 shutdown(fd[0], SHUT_WR);
336 if (use_sideband)
337 finish_async(&demux);
338 fd[1] = -1;
339 return -1;
341 if (!args->stateless_rpc)
342 /* Closed by pack_objects() via start_command() */
343 fd[1] = -1;
345 if (args->stateless_rpc && cmds_sent)
346 packet_flush(out);
348 if (status_report && cmds_sent)
349 ret = receive_status(in, remote_refs);
350 else
351 ret = 0;
352 if (args->stateless_rpc)
353 packet_flush(out);
355 if (use_sideband && cmds_sent) {
356 if (finish_async(&demux)) {
357 error("error in sideband demultiplexer");
358 ret = -1;
360 close(demux.out);
363 if (ret < 0)
364 return ret;
366 if (args->porcelain)
367 return 0;
369 for (ref = remote_refs; ref; ref = ref->next) {
370 switch (ref->status) {
371 case REF_STATUS_NONE:
372 case REF_STATUS_UPTODATE:
373 case REF_STATUS_OK:
374 break;
375 default:
376 return -1;
379 return 0;