Merge branch 'jk/colors'
[git] / send-pack.c
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"
14 #include "gpg-interface.h"
15
16 static int feed_object(const unsigned char *sha1, int fd, int negative)
17 {
18         char buf[42];
19
20         if (negative && !has_sha1_file(sha1))
21                 return 1;
22
23         memcpy(buf + negative, sha1_to_hex(sha1), 40);
24         if (negative)
25                 buf[0] = '^';
26         buf[40 + negative] = '\n';
27         return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
28 }
29
30 /*
31  * Make a pack stream and spit it out into file descriptor fd
32  */
33 static int pack_objects(int fd, struct ref *refs, struct sha1_array *extra, struct send_pack_args *args)
34 {
35         /*
36          * The child becomes pack-objects --revs; we feed
37          * the revision parameters to it via its stdin and
38          * let its stdout go back to the other end.
39          */
40         const char *argv[] = {
41                 "pack-objects",
42                 "--all-progress-implied",
43                 "--revs",
44                 "--stdout",
45                 NULL,
46                 NULL,
47                 NULL,
48                 NULL,
49                 NULL,
50                 NULL,
51         };
52         struct child_process po = CHILD_PROCESS_INIT;
53         int i;
54
55         i = 4;
56         if (args->use_thin_pack)
57                 argv[i++] = "--thin";
58         if (args->use_ofs_delta)
59                 argv[i++] = "--delta-base-offset";
60         if (args->quiet || !args->progress)
61                 argv[i++] = "-q";
62         if (args->progress)
63                 argv[i++] = "--progress";
64         if (is_repository_shallow())
65                 argv[i++] = "--shallow";
66         po.argv = argv;
67         po.in = -1;
68         po.out = args->stateless_rpc ? -1 : fd;
69         po.git_cmd = 1;
70         if (start_command(&po))
71                 die_errno("git pack-objects failed");
72
73         /*
74          * We feed the pack-objects we just spawned with revision
75          * parameters by writing to the pipe.
76          */
77         for (i = 0; i < extra->nr; i++)
78                 if (!feed_object(extra->sha1[i], po.in, 1))
79                         break;
80
81         while (refs) {
82                 if (!is_null_sha1(refs->old_sha1) &&
83                     !feed_object(refs->old_sha1, po.in, 1))
84                         break;
85                 if (!is_null_sha1(refs->new_sha1) &&
86                     !feed_object(refs->new_sha1, po.in, 0))
87                         break;
88                 refs = refs->next;
89         }
90
91         close(po.in);
92
93         if (args->stateless_rpc) {
94                 char *buf = xmalloc(LARGE_PACKET_MAX);
95                 while (1) {
96                         ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
97                         if (n <= 0)
98                                 break;
99                         send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
100                 }
101                 free(buf);
102                 close(po.out);
103                 po.out = -1;
104         }
105
106         if (finish_command(&po))
107                 return -1;
108         return 0;
109 }
110
111 static int receive_status(int in, struct ref *refs)
112 {
113         struct ref *hint;
114         int ret = 0;
115         char *line = packet_read_line(in, NULL);
116         if (!starts_with(line, "unpack "))
117                 return error("did not receive remote status");
118         if (strcmp(line, "unpack ok")) {
119                 error("unpack failed: %s", line + 7);
120                 ret = -1;
121         }
122         hint = NULL;
123         while (1) {
124                 char *refname;
125                 char *msg;
126                 line = packet_read_line(in, NULL);
127                 if (!line)
128                         break;
129                 if (!starts_with(line, "ok ") && !starts_with(line, "ng ")) {
130                         error("invalid ref status from remote: %s", line);
131                         ret = -1;
132                         break;
133                 }
134
135                 refname = line + 3;
136                 msg = strchr(refname, ' ');
137                 if (msg)
138                         *msg++ = '\0';
139
140                 /* first try searching at our hint, falling back to all refs */
141                 if (hint)
142                         hint = find_ref_by_name(hint, refname);
143                 if (!hint)
144                         hint = find_ref_by_name(refs, refname);
145                 if (!hint) {
146                         warning("remote reported status on unknown ref: %s",
147                                         refname);
148                         continue;
149                 }
150                 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
151                         warning("remote reported status on unexpected ref: %s",
152                                         refname);
153                         continue;
154                 }
155
156                 if (line[0] == 'o' && line[1] == 'k')
157                         hint->status = REF_STATUS_OK;
158                 else {
159                         hint->status = REF_STATUS_REMOTE_REJECT;
160                         ret = -1;
161                 }
162                 if (msg)
163                         hint->remote_status = xstrdup(msg);
164                 /* start our next search from the next ref */
165                 hint = hint->next;
166         }
167         return ret;
168 }
169
170 static int sideband_demux(int in, int out, void *data)
171 {
172         int *fd = data, ret;
173 #ifdef NO_PTHREADS
174         close(fd[1]);
175 #endif
176         ret = recv_sideband("send-pack", fd[0], out);
177         close(out);
178         return ret;
179 }
180
181 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
182 {
183         struct strbuf *sb = cb;
184         if (graft->nr_parent == -1)
185                 packet_buf_write(sb, "shallow %s\n", sha1_to_hex(graft->sha1));
186         return 0;
187 }
188
189 static void advertise_shallow_grafts_buf(struct strbuf *sb)
190 {
191         if (!is_repository_shallow())
192                 return;
193         for_each_commit_graft(advertise_shallow_grafts_cb, sb);
194 }
195
196 #define CHECK_REF_NO_PUSH -1
197 #define CHECK_REF_STATUS_REJECTED -2
198 #define CHECK_REF_UPTODATE -3
199 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
200 {
201         if (!ref->peer_ref && !args->send_mirror)
202                 return CHECK_REF_NO_PUSH;
203
204         /* Check for statuses set by set_ref_status_for_push() */
205         switch (ref->status) {
206         case REF_STATUS_REJECT_NONFASTFORWARD:
207         case REF_STATUS_REJECT_ALREADY_EXISTS:
208         case REF_STATUS_REJECT_FETCH_FIRST:
209         case REF_STATUS_REJECT_NEEDS_FORCE:
210         case REF_STATUS_REJECT_STALE:
211         case REF_STATUS_REJECT_NODELETE:
212                 return CHECK_REF_STATUS_REJECTED;
213         case REF_STATUS_UPTODATE:
214                 return CHECK_REF_UPTODATE;
215         default:
216                 return 0;
217         }
218 }
219
220 /*
221  * the beginning of the next line, or the end of buffer.
222  *
223  * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
224  * convert many similar uses found by "git grep -A4 memchr".
225  */
226 static const char *next_line(const char *line, size_t len)
227 {
228         const char *nl = memchr(line, '\n', len);
229         if (!nl)
230                 return line + len; /* incomplete line */
231         return nl + 1;
232 }
233
234 static int generate_push_cert(struct strbuf *req_buf,
235                               const struct ref *remote_refs,
236                               struct send_pack_args *args,
237                               const char *cap_string,
238                               const char *push_cert_nonce)
239 {
240         const struct ref *ref;
241         char *signing_key = xstrdup(get_signing_key());
242         const char *cp, *np;
243         struct strbuf cert = STRBUF_INIT;
244         int update_seen = 0;
245
246         strbuf_addf(&cert, "certificate version 0.1\n");
247         strbuf_addf(&cert, "pusher %s ", signing_key);
248         datestamp(&cert);
249         strbuf_addch(&cert, '\n');
250         if (args->url && *args->url) {
251                 char *anon_url = transport_anonymize_url(args->url);
252                 strbuf_addf(&cert, "pushee %s\n", anon_url);
253                 free(anon_url);
254         }
255         if (push_cert_nonce[0])
256                 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
257         strbuf_addstr(&cert, "\n");
258
259         for (ref = remote_refs; ref; ref = ref->next) {
260                 if (check_to_send_update(ref, args) < 0)
261                         continue;
262                 update_seen = 1;
263                 strbuf_addf(&cert, "%s %s %s\n",
264                             sha1_to_hex(ref->old_sha1),
265                             sha1_to_hex(ref->new_sha1),
266                             ref->name);
267         }
268         if (!update_seen)
269                 goto free_return;
270
271         if (sign_buffer(&cert, &cert, signing_key))
272                 die(_("failed to sign the push certificate"));
273
274         packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
275         for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
276                 np = next_line(cp, cert.buf + cert.len - cp);
277                 packet_buf_write(req_buf,
278                                  "%.*s", (int)(np - cp), cp);
279         }
280         packet_buf_write(req_buf, "push-cert-end\n");
281
282 free_return:
283         free(signing_key);
284         strbuf_release(&cert);
285         return update_seen;
286 }
287
288
289 static int atomic_push_failure(struct send_pack_args *args,
290                                struct ref *remote_refs,
291                                struct ref *failing_ref)
292 {
293         struct ref *ref;
294         /* Mark other refs as failed */
295         for (ref = remote_refs; ref; ref = ref->next) {
296                 if (!ref->peer_ref && !args->send_mirror)
297                         continue;
298
299                 switch (ref->status) {
300                 case REF_STATUS_EXPECTING_REPORT:
301                         ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
302                         continue;
303                 default:
304                         break; /* do nothing */
305                 }
306         }
307         return error("atomic push failed for ref %s. status: %d\n",
308                      failing_ref->name, failing_ref->status);
309 }
310
311 int send_pack(struct send_pack_args *args,
312               int fd[], struct child_process *conn,
313               struct ref *remote_refs,
314               struct sha1_array *extra_have)
315 {
316         int in = fd[0];
317         int out = fd[1];
318         struct strbuf req_buf = STRBUF_INIT;
319         struct strbuf cap_buf = STRBUF_INIT;
320         struct ref *ref;
321         int need_pack_data = 0;
322         int allow_deleting_refs = 0;
323         int status_report = 0;
324         int use_sideband = 0;
325         int quiet_supported = 0;
326         int agent_supported = 0;
327         int use_atomic = 0;
328         int atomic_supported = 0;
329         unsigned cmds_sent = 0;
330         int ret;
331         struct async demux;
332         const char *push_cert_nonce = NULL;
333
334         /* Does the other end support the reporting? */
335         if (server_supports("report-status"))
336                 status_report = 1;
337         if (server_supports("delete-refs"))
338                 allow_deleting_refs = 1;
339         if (server_supports("ofs-delta"))
340                 args->use_ofs_delta = 1;
341         if (server_supports("side-band-64k"))
342                 use_sideband = 1;
343         if (server_supports("quiet"))
344                 quiet_supported = 1;
345         if (server_supports("agent"))
346                 agent_supported = 1;
347         if (server_supports("no-thin"))
348                 args->use_thin_pack = 0;
349         if (server_supports("atomic"))
350                 atomic_supported = 1;
351         if (args->push_cert) {
352                 int len;
353
354                 push_cert_nonce = server_feature_value("push-cert", &len);
355                 if (!push_cert_nonce)
356                         die(_("the receiving end does not support --signed push"));
357                 push_cert_nonce = xmemdupz(push_cert_nonce, len);
358         }
359
360         if (!remote_refs) {
361                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
362                         "Perhaps you should specify a branch such as 'master'.\n");
363                 return 0;
364         }
365         if (args->atomic && !atomic_supported)
366                 die(_("the receiving end does not support --atomic push"));
367
368         use_atomic = atomic_supported && args->atomic;
369
370         if (status_report)
371                 strbuf_addstr(&cap_buf, " report-status");
372         if (use_sideband)
373                 strbuf_addstr(&cap_buf, " side-band-64k");
374         if (quiet_supported && (args->quiet || !args->progress))
375                 strbuf_addstr(&cap_buf, " quiet");
376         if (use_atomic)
377                 strbuf_addstr(&cap_buf, " atomic");
378         if (agent_supported)
379                 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
380
381         /*
382          * NEEDSWORK: why does delete-refs have to be so specific to
383          * send-pack machinery that set_ref_status_for_push() cannot
384          * set this bit for us???
385          */
386         for (ref = remote_refs; ref; ref = ref->next)
387                 if (ref->deletion && !allow_deleting_refs)
388                         ref->status = REF_STATUS_REJECT_NODELETE;
389
390         if (!args->dry_run)
391                 advertise_shallow_grafts_buf(&req_buf);
392
393         if (!args->dry_run && args->push_cert)
394                 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
395                                                cap_buf.buf, push_cert_nonce);
396
397         /*
398          * Clear the status for each ref and see if we need to send
399          * the pack data.
400          */
401         for (ref = remote_refs; ref; ref = ref->next) {
402                 switch (check_to_send_update(ref, args)) {
403                 case 0: /* no error */
404                         break;
405                 case CHECK_REF_STATUS_REJECTED:
406                         /*
407                          * When we know the server would reject a ref update if
408                          * we were to send it and we're trying to send the refs
409                          * atomically, abort the whole operation.
410                          */
411                         if (use_atomic)
412                                 return atomic_push_failure(args, remote_refs, ref);
413                         /* Fallthrough for non atomic case. */
414                 default:
415                         continue;
416                 }
417                 if (!ref->deletion)
418                         need_pack_data = 1;
419
420                 if (args->dry_run || !status_report)
421                         ref->status = REF_STATUS_OK;
422                 else
423                         ref->status = REF_STATUS_EXPECTING_REPORT;
424         }
425
426         /*
427          * Finally, tell the other end!
428          */
429         for (ref = remote_refs; ref; ref = ref->next) {
430                 char *old_hex, *new_hex;
431
432                 if (args->dry_run || args->push_cert)
433                         continue;
434
435                 if (check_to_send_update(ref, args) < 0)
436                         continue;
437
438                 old_hex = sha1_to_hex(ref->old_sha1);
439                 new_hex = sha1_to_hex(ref->new_sha1);
440                 if (!cmds_sent) {
441                         packet_buf_write(&req_buf,
442                                          "%s %s %s%c%s",
443                                          old_hex, new_hex, ref->name, 0,
444                                          cap_buf.buf);
445                         cmds_sent = 1;
446                 } else {
447                         packet_buf_write(&req_buf, "%s %s %s",
448                                          old_hex, new_hex, ref->name);
449                 }
450         }
451
452         if (args->stateless_rpc) {
453                 if (!args->dry_run && (cmds_sent || is_repository_shallow())) {
454                         packet_buf_flush(&req_buf);
455                         send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
456                 }
457         } else {
458                 write_or_die(out, req_buf.buf, req_buf.len);
459                 packet_flush(out);
460         }
461         strbuf_release(&req_buf);
462         strbuf_release(&cap_buf);
463
464         if (use_sideband && cmds_sent) {
465                 memset(&demux, 0, sizeof(demux));
466                 demux.proc = sideband_demux;
467                 demux.data = fd;
468                 demux.out = -1;
469                 if (start_async(&demux))
470                         die("send-pack: unable to fork off sideband demultiplexer");
471                 in = demux.out;
472         }
473
474         if (need_pack_data && cmds_sent) {
475                 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
476                         for (ref = remote_refs; ref; ref = ref->next)
477                                 ref->status = REF_STATUS_NONE;
478                         if (args->stateless_rpc)
479                                 close(out);
480                         if (git_connection_is_socket(conn))
481                                 shutdown(fd[0], SHUT_WR);
482                         if (use_sideband)
483                                 finish_async(&demux);
484                         fd[1] = -1;
485                         return -1;
486                 }
487                 if (!args->stateless_rpc)
488                         /* Closed by pack_objects() via start_command() */
489                         fd[1] = -1;
490         }
491         if (args->stateless_rpc && cmds_sent)
492                 packet_flush(out);
493
494         if (status_report && cmds_sent)
495                 ret = receive_status(in, remote_refs);
496         else
497                 ret = 0;
498         if (args->stateless_rpc)
499                 packet_flush(out);
500
501         if (use_sideband && cmds_sent) {
502                 if (finish_async(&demux)) {
503                         error("error in sideband demultiplexer");
504                         ret = -1;
505                 }
506                 close(demux.out);
507         }
508
509         if (ret < 0)
510                 return ret;
511
512         if (args->porcelain)
513                 return 0;
514
515         for (ref = remote_refs; ref; ref = ref->next) {
516                 switch (ref->status) {
517                 case REF_STATUS_NONE:
518                 case REF_STATUS_UPTODATE:
519                 case REF_STATUS_OK:
520                         break;
521                 default:
522                         return -1;
523                 }
524         }
525         return 0;
526 }