Sync with maint
[git/git.git] / send-pack.c
CommitLineData
f5d942e1
NTND
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"
47a59185 8#include "connect.h"
f5d942e1
NTND
9#include "send-pack.h"
10#include "quote.h"
11#include "transport.h"
12#include "version.h"
13eb4626 13#include "sha1-array.h"
f5d942e1
NTND
14
15static int feed_object(const unsigned char *sha1, int fd, int negative)
16{
17 char buf[42];
18
19 if (negative && !has_sha1_file(sha1))
20 return 1;
21
22 memcpy(buf + negative, sha1_to_hex(sha1), 40);
23 if (negative)
24 buf[0] = '^';
25 buf[40 + negative] = '\n';
26 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
27}
28
29/*
30 * Make a pack stream and spit it out into file descriptor fd
31 */
13eb4626 32static int pack_objects(int fd, struct ref *refs, struct sha1_array *extra, struct send_pack_args *args)
f5d942e1
NTND
33{
34 /*
35 * The child becomes pack-objects --revs; we feed
36 * the revision parameters to it via its stdin and
37 * let its stdout go back to the other end.
38 */
39 const char *argv[] = {
40 "pack-objects",
41 "--all-progress-implied",
42 "--revs",
43 "--stdout",
44 NULL,
45 NULL,
46 NULL,
47 NULL,
48 NULL,
49 };
d3180279 50 struct child_process po = CHILD_PROCESS_INIT;
f5d942e1
NTND
51 int i;
52
53 i = 4;
54 if (args->use_thin_pack)
55 argv[i++] = "--thin";
56 if (args->use_ofs_delta)
57 argv[i++] = "--delta-base-offset";
58 if (args->quiet || !args->progress)
59 argv[i++] = "-q";
60 if (args->progress)
61 argv[i++] = "--progress";
f5d942e1
NTND
62 po.argv = argv;
63 po.in = -1;
64 po.out = args->stateless_rpc ? -1 : fd;
65 po.git_cmd = 1;
66 if (start_command(&po))
67 die_errno("git pack-objects failed");
68
69 /*
70 * We feed the pack-objects we just spawned with revision
71 * parameters by writing to the pipe.
72 */
73 for (i = 0; i < extra->nr; i++)
13eb4626 74 if (!feed_object(extra->sha1[i], po.in, 1))
f5d942e1
NTND
75 break;
76
77 while (refs) {
78 if (!is_null_sha1(refs->old_sha1) &&
79 !feed_object(refs->old_sha1, po.in, 1))
80 break;
81 if (!is_null_sha1(refs->new_sha1) &&
82 !feed_object(refs->new_sha1, po.in, 0))
83 break;
84 refs = refs->next;
85 }
86
87 close(po.in);
88
89 if (args->stateless_rpc) {
90 char *buf = xmalloc(LARGE_PACKET_MAX);
91 while (1) {
92 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
93 if (n <= 0)
94 break;
95 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
96 }
97 free(buf);
98 close(po.out);
99 po.out = -1;
100 }
101
102 if (finish_command(&po))
103 return -1;
104 return 0;
105}
106
107static int receive_status(int in, struct ref *refs)
108{
109 struct ref *hint;
f5d942e1 110 int ret = 0;
74543a04 111 char *line = packet_read_line(in, NULL);
59556548 112 if (!starts_with(line, "unpack "))
f5d942e1 113 return error("did not receive remote status");
819b929d 114 if (strcmp(line, "unpack ok")) {
f5d942e1
NTND
115 error("unpack failed: %s", line + 7);
116 ret = -1;
117 }
118 hint = NULL;
119 while (1) {
120 char *refname;
121 char *msg;
74543a04
JK
122 line = packet_read_line(in, NULL);
123 if (!line)
f5d942e1 124 break;
59556548 125 if (!starts_with(line, "ok ") && !starts_with(line, "ng ")) {
8f9e3e49 126 error("invalid ref status from remote: %s", line);
f5d942e1
NTND
127 ret = -1;
128 break;
129 }
130
f5d942e1
NTND
131 refname = line + 3;
132 msg = strchr(refname, ' ');
133 if (msg)
134 *msg++ = '\0';
135
136 /* first try searching at our hint, falling back to all refs */
137 if (hint)
138 hint = find_ref_by_name(hint, refname);
139 if (!hint)
140 hint = find_ref_by_name(refs, refname);
141 if (!hint) {
142 warning("remote reported status on unknown ref: %s",
143 refname);
144 continue;
145 }
146 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
147 warning("remote reported status on unexpected ref: %s",
148 refname);
149 continue;
150 }
151
152 if (line[0] == 'o' && line[1] == 'k')
153 hint->status = REF_STATUS_OK;
154 else {
155 hint->status = REF_STATUS_REMOTE_REJECT;
156 ret = -1;
157 }
158 if (msg)
159 hint->remote_status = xstrdup(msg);
160 /* start our next search from the next ref */
161 hint = hint->next;
162 }
163 return ret;
164}
165
166static int sideband_demux(int in, int out, void *data)
167{
168 int *fd = data, ret;
169#ifdef NO_PTHREADS
170 close(fd[1]);
171#endif
172 ret = recv_sideband("send-pack", fd[0], out);
173 close(out);
174 return ret;
175}
176
f2c681cf
NTND
177static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
178{
179 struct strbuf *sb = cb;
180 if (graft->nr_parent == -1)
181 packet_buf_write(sb, "shallow %s\n", sha1_to_hex(graft->sha1));
182 return 0;
183}
184
16a2743c 185static void advertise_shallow_grafts_buf(struct strbuf *sb)
f2c681cf
NTND
186{
187 if (!is_repository_shallow())
188 return;
189 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
190}
191
f5d942e1
NTND
192int send_pack(struct send_pack_args *args,
193 int fd[], struct child_process *conn,
194 struct ref *remote_refs,
13eb4626 195 struct sha1_array *extra_have)
f5d942e1
NTND
196{
197 int in = fd[0];
198 int out = fd[1];
199 struct strbuf req_buf = STRBUF_INIT;
200 struct ref *ref;
201 int new_refs;
202 int allow_deleting_refs = 0;
203 int status_report = 0;
204 int use_sideband = 0;
205 int quiet_supported = 0;
206 int agent_supported = 0;
207 unsigned cmds_sent = 0;
208 int ret;
209 struct async demux;
210
211 /* Does the other end support the reporting? */
212 if (server_supports("report-status"))
213 status_report = 1;
214 if (server_supports("delete-refs"))
215 allow_deleting_refs = 1;
216 if (server_supports("ofs-delta"))
217 args->use_ofs_delta = 1;
218 if (server_supports("side-band-64k"))
219 use_sideband = 1;
220 if (server_supports("quiet"))
221 quiet_supported = 1;
222 if (server_supports("agent"))
223 agent_supported = 1;
1ba98a79
CMN
224 if (server_supports("no-thin"))
225 args->use_thin_pack = 0;
f5d942e1
NTND
226
227 if (!remote_refs) {
228 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
229 "Perhaps you should specify a branch such as 'master'.\n");
230 return 0;
231 }
232
5dbd7676 233 if (!args->dry_run)
f2c681cf 234 advertise_shallow_grafts_buf(&req_buf);
5dbd7676 235
f5d942e1
NTND
236 /*
237 * Finally, tell the other end!
238 */
239 new_refs = 0;
240 for (ref = remote_refs; ref; ref = ref->next) {
241 if (!ref->peer_ref && !args->send_mirror)
242 continue;
243
244 /* Check for statuses set by set_ref_status_for_push() */
245 switch (ref->status) {
246 case REF_STATUS_REJECT_NONFASTFORWARD:
dbfeddb1 247 case REF_STATUS_REJECT_ALREADY_EXISTS:
75e5c0dc
JH
248 case REF_STATUS_REJECT_FETCH_FIRST:
249 case REF_STATUS_REJECT_NEEDS_FORCE:
631b5ef2 250 case REF_STATUS_REJECT_STALE:
f5d942e1
NTND
251 case REF_STATUS_UPTODATE:
252 continue;
253 default:
254 ; /* do nothing */
255 }
256
257 if (ref->deletion && !allow_deleting_refs) {
258 ref->status = REF_STATUS_REJECT_NODELETE;
259 continue;
260 }
261
262 if (!ref->deletion)
263 new_refs++;
264
265 if (args->dry_run) {
266 ref->status = REF_STATUS_OK;
267 } else {
268 char *old_hex = sha1_to_hex(ref->old_sha1);
269 char *new_hex = sha1_to_hex(ref->new_sha1);
270 int quiet = quiet_supported && (args->quiet || !args->progress);
271
272 if (!cmds_sent && (status_report || use_sideband ||
273 quiet || agent_supported)) {
274 packet_buf_write(&req_buf,
275 "%s %s %s%c%s%s%s%s%s",
276 old_hex, new_hex, ref->name, 0,
277 status_report ? " report-status" : "",
278 use_sideband ? " side-band-64k" : "",
279 quiet ? " quiet" : "",
280 agent_supported ? " agent=" : "",
281 agent_supported ? git_user_agent_sanitized() : ""
282 );
283 }
284 else
285 packet_buf_write(&req_buf, "%s %s %s",
286 old_hex, new_hex, ref->name);
287 ref->status = status_report ?
288 REF_STATUS_EXPECTING_REPORT :
289 REF_STATUS_OK;
290 cmds_sent++;
291 }
292 }
293
294 if (args->stateless_rpc) {
f2c681cf 295 if (!args->dry_run && (cmds_sent || is_repository_shallow())) {
f5d942e1
NTND
296 packet_buf_flush(&req_buf);
297 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
298 }
299 } else {
cdf4fb8e 300 write_or_die(out, req_buf.buf, req_buf.len);
f5d942e1
NTND
301 packet_flush(out);
302 }
303 strbuf_release(&req_buf);
304
305 if (use_sideband && cmds_sent) {
306 memset(&demux, 0, sizeof(demux));
307 demux.proc = sideband_demux;
308 demux.data = fd;
309 demux.out = -1;
310 if (start_async(&demux))
311 die("send-pack: unable to fork off sideband demultiplexer");
312 in = demux.out;
313 }
314
315 if (new_refs && cmds_sent) {
316 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
317 for (ref = remote_refs; ref; ref = ref->next)
318 ref->status = REF_STATUS_NONE;
319 if (args->stateless_rpc)
320 close(out);
321 if (git_connection_is_socket(conn))
322 shutdown(fd[0], SHUT_WR);
323 if (use_sideband)
324 finish_async(&demux);
37cb1dd6 325 fd[1] = -1;
f5d942e1
NTND
326 return -1;
327 }
37cb1dd6
JL
328 if (!args->stateless_rpc)
329 /* Closed by pack_objects() via start_command() */
330 fd[1] = -1;
f5d942e1
NTND
331 }
332 if (args->stateless_rpc && cmds_sent)
333 packet_flush(out);
334
335 if (status_report && cmds_sent)
336 ret = receive_status(in, remote_refs);
337 else
338 ret = 0;
339 if (args->stateless_rpc)
340 packet_flush(out);
341
342 if (use_sideband && cmds_sent) {
343 if (finish_async(&demux)) {
344 error("error in sideband demultiplexer");
345 ret = -1;
346 }
347 close(demux.out);
348 }
349
350 if (ret < 0)
351 return ret;
352
353 if (args->porcelain)
354 return 0;
355
356 for (ref = remote_refs; ref; ref = ref->next) {
357 switch (ref->status) {
358 case REF_STATUS_NONE:
359 case REF_STATUS_UPTODATE:
360 case REF_STATUS_OK:
361 break;
362 default:
363 return -1;
364 }
365 }
366 return 0;
367}