Merge branch 'js/maint-1.6.6-send-pack-stateless-rpc-deadlock-fix' into js/maint...
[git] / builtin-send-pack.c
1 #include "cache.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 "send-pack.h"
9 #include "quote.h"
10 #include "transport.h"
11
12 static const char send_pack_usage[] =
13 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
14 "  --all and explicit <ref> specification are mutually exclusive.";
15
16 static struct send_pack_args args;
17
18 static int feed_object(const unsigned char *sha1, int fd, int negative)
19 {
20         char buf[42];
21
22         if (negative && !has_sha1_file(sha1))
23                 return 1;
24
25         memcpy(buf + negative, sha1_to_hex(sha1), 40);
26         if (negative)
27                 buf[0] = '^';
28         buf[40 + negative] = '\n';
29         return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
30 }
31
32 /*
33  * Make a pack stream and spit it out into file descriptor fd
34  */
35 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
36 {
37         /*
38          * The child becomes pack-objects --revs; we feed
39          * the revision parameters to it via its stdin and
40          * let its stdout go back to the other end.
41          */
42         const char *argv[] = {
43                 "pack-objects",
44                 "--all-progress-implied",
45                 "--revs",
46                 "--stdout",
47                 NULL,
48                 NULL,
49                 NULL,
50                 NULL,
51         };
52         struct child_process po;
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)
61                 argv[i++] = "-q";
62         memset(&po, 0, sizeof(po));
63         po.argv = argv;
64         po.in = -1;
65         po.out = args->stateless_rpc ? -1 : fd;
66         po.git_cmd = 1;
67         if (start_command(&po))
68                 die_errno("git pack-objects failed");
69
70         /*
71          * We feed the pack-objects we just spawned with revision
72          * parameters by writing to the pipe.
73          */
74         for (i = 0; i < extra->nr; i++)
75                 if (!feed_object(extra->array[i], po.in, 1))
76                         break;
77
78         while (refs) {
79                 if (!is_null_sha1(refs->old_sha1) &&
80                     !feed_object(refs->old_sha1, po.in, 1))
81                         break;
82                 if (!is_null_sha1(refs->new_sha1) &&
83                     !feed_object(refs->new_sha1, po.in, 0))
84                         break;
85                 refs = refs->next;
86         }
87
88         close(po.in);
89
90         if (args->stateless_rpc) {
91                 char *buf = xmalloc(LARGE_PACKET_MAX);
92                 while (1) {
93                         ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
94                         if (n <= 0)
95                                 break;
96                         send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
97                 }
98                 free(buf);
99                 close(po.out);
100                 po.out = -1;
101                 close(fd);
102         }
103
104         if (finish_command(&po))
105                 return error("pack-objects died with strange error");
106         return 0;
107 }
108
109 static int receive_status(int in, struct ref *refs)
110 {
111         struct ref *hint;
112         char line[1000];
113         int ret = 0;
114         int len = packet_read_line(in, line, sizeof(line));
115         if (len < 10 || memcmp(line, "unpack ", 7))
116                 return error("did not receive remote status");
117         if (memcmp(line, "unpack ok\n", 10)) {
118                 char *p = line + strlen(line) - 1;
119                 if (*p == '\n')
120                         *p = '\0';
121                 error("unpack failed: %s", line + 7);
122                 ret = -1;
123         }
124         hint = NULL;
125         while (1) {
126                 char *refname;
127                 char *msg;
128                 len = packet_read_line(in, line, sizeof(line));
129                 if (!len)
130                         break;
131                 if (len < 3 ||
132                     (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
133                         fprintf(stderr, "protocol error: %s\n", line);
134                         ret = -1;
135                         break;
136                 }
137
138                 line[strlen(line)-1] = '\0';
139                 refname = line + 3;
140                 msg = strchr(refname, ' ');
141                 if (msg)
142                         *msg++ = '\0';
143
144                 /* first try searching at our hint, falling back to all refs */
145                 if (hint)
146                         hint = find_ref_by_name(hint, refname);
147                 if (!hint)
148                         hint = find_ref_by_name(refs, refname);
149                 if (!hint) {
150                         warning("remote reported status on unknown ref: %s",
151                                         refname);
152                         continue;
153                 }
154                 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
155                         warning("remote reported status on unexpected ref: %s",
156                                         refname);
157                         continue;
158                 }
159
160                 if (line[0] == 'o' && line[1] == 'k')
161                         hint->status = REF_STATUS_OK;
162                 else {
163                         hint->status = REF_STATUS_REMOTE_REJECT;
164                         ret = -1;
165                 }
166                 if (msg)
167                         hint->remote_status = xstrdup(msg);
168                 /* start our next search from the next ref */
169                 hint = hint->next;
170         }
171         return ret;
172 }
173
174 static void print_helper_status(struct ref *ref)
175 {
176         struct strbuf buf = STRBUF_INIT;
177
178         for (; ref; ref = ref->next) {
179                 const char *msg = NULL;
180                 const char *res;
181
182                 switch(ref->status) {
183                 case REF_STATUS_NONE:
184                         res = "error";
185                         msg = "no match";
186                         break;
187
188                 case REF_STATUS_OK:
189                         res = "ok";
190                         break;
191
192                 case REF_STATUS_UPTODATE:
193                         res = "ok";
194                         msg = "up to date";
195                         break;
196
197                 case REF_STATUS_REJECT_NONFASTFORWARD:
198                         res = "error";
199                         msg = "non-fast forward";
200                         break;
201
202                 case REF_STATUS_REJECT_NODELETE:
203                 case REF_STATUS_REMOTE_REJECT:
204                         res = "error";
205                         break;
206
207                 case REF_STATUS_EXPECTING_REPORT:
208                 default:
209                         continue;
210                 }
211
212                 strbuf_reset(&buf);
213                 strbuf_addf(&buf, "%s %s", res, ref->name);
214                 if (ref->remote_status)
215                         msg = ref->remote_status;
216                 if (msg) {
217                         strbuf_addch(&buf, ' ');
218                         quote_two_c_style(&buf, "", msg, 0);
219                 }
220                 strbuf_addch(&buf, '\n');
221
222                 safe_write(1, buf.buf, buf.len);
223         }
224         strbuf_release(&buf);
225 }
226
227 static int sideband_demux(int in, int out, void *data)
228 {
229         int *fd = data;
230 #ifdef NO_PTHREADS
231         close(fd[1]);
232 #endif
233         int ret = recv_sideband("send-pack", fd[0], out);
234         close(out);
235         return ret;
236 }
237
238 int send_pack(struct send_pack_args *args,
239               int fd[], struct child_process *conn,
240               struct ref *remote_refs,
241               struct extra_have_objects *extra_have)
242 {
243         int in = fd[0];
244         int out = fd[1];
245         struct strbuf req_buf = STRBUF_INIT;
246         struct ref *ref;
247         int new_refs;
248         int allow_deleting_refs = 0;
249         int status_report = 0;
250         int use_sideband = 0;
251         unsigned cmds_sent = 0;
252         int ret;
253         struct async demux;
254
255         /* Does the other end support the reporting? */
256         if (server_supports("report-status"))
257                 status_report = 1;
258         if (server_supports("delete-refs"))
259                 allow_deleting_refs = 1;
260         if (server_supports("ofs-delta"))
261                 args->use_ofs_delta = 1;
262         if (server_supports("side-band-64k"))
263                 use_sideband = 1;
264
265         if (!remote_refs) {
266                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
267                         "Perhaps you should specify a branch such as 'master'.\n");
268                 return 0;
269         }
270
271         /*
272          * Finally, tell the other end!
273          */
274         new_refs = 0;
275         for (ref = remote_refs; ref; ref = ref->next) {
276                 if (!ref->peer_ref && !args->send_mirror)
277                         continue;
278
279                 /* Check for statuses set by set_ref_status_for_push() */
280                 switch (ref->status) {
281                 case REF_STATUS_REJECT_NONFASTFORWARD:
282                 case REF_STATUS_UPTODATE:
283                         continue;
284                 default:
285                         ; /* do nothing */
286                 }
287
288                 if (ref->deletion && !allow_deleting_refs) {
289                         ref->status = REF_STATUS_REJECT_NODELETE;
290                         continue;
291                 }
292
293                 if (!ref->deletion)
294                         new_refs++;
295
296                 if (args->dry_run) {
297                         ref->status = REF_STATUS_OK;
298                 } else {
299                         char *old_hex = sha1_to_hex(ref->old_sha1);
300                         char *new_hex = sha1_to_hex(ref->new_sha1);
301
302                         if (!cmds_sent && (status_report || use_sideband)) {
303                                 packet_buf_write(&req_buf, "%s %s %s%c%s%s",
304                                         old_hex, new_hex, ref->name, 0,
305                                         status_report ? " report-status" : "",
306                                         use_sideband ? " side-band-64k" : "");
307                         }
308                         else
309                                 packet_buf_write(&req_buf, "%s %s %s",
310                                         old_hex, new_hex, ref->name);
311                         ref->status = status_report ?
312                                 REF_STATUS_EXPECTING_REPORT :
313                                 REF_STATUS_OK;
314                         cmds_sent++;
315                 }
316         }
317
318         if (args->stateless_rpc) {
319                 if (!args->dry_run && cmds_sent) {
320                         packet_buf_flush(&req_buf);
321                         send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
322                 }
323         } else {
324                 safe_write(out, req_buf.buf, req_buf.len);
325                 packet_flush(out);
326         }
327         strbuf_release(&req_buf);
328
329         if (use_sideband && cmds_sent) {
330                 memset(&demux, 0, sizeof(demux));
331                 demux.proc = sideband_demux;
332                 demux.data = fd;
333                 demux.out = -1;
334                 if (start_async(&demux))
335                         die("receive-pack: unable to fork off sideband demultiplexer");
336                 in = demux.out;
337         }
338
339         if (new_refs && cmds_sent) {
340                 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
341                         for (ref = remote_refs; ref; ref = ref->next)
342                                 ref->status = REF_STATUS_NONE;
343                         if (use_sideband)
344                                 finish_async(&demux);
345                         return -1;
346                 }
347         }
348         if (args->stateless_rpc && cmds_sent)
349                 packet_flush(out);
350
351         if (status_report && cmds_sent)
352                 ret = receive_status(in, remote_refs);
353         else
354                 ret = 0;
355         if (args->stateless_rpc)
356                 packet_flush(out);
357
358         if (use_sideband && cmds_sent) {
359                 if (finish_async(&demux)) {
360                         error("error in sideband demultiplexer");
361                         ret = -1;
362                 }
363                 close(demux.out);
364         }
365
366         if (ret < 0)
367                 return ret;
368         for (ref = remote_refs; ref; ref = ref->next) {
369                 switch (ref->status) {
370                 case REF_STATUS_NONE:
371                 case REF_STATUS_UPTODATE:
372                 case REF_STATUS_OK:
373                         break;
374                 default:
375                         return -1;
376                 }
377         }
378         return 0;
379 }
380
381 int cmd_send_pack(int argc, const char **argv, const char *prefix)
382 {
383         int i, nr_refspecs = 0;
384         const char **refspecs = NULL;
385         const char *remote_name = NULL;
386         struct remote *remote = NULL;
387         const char *dest = NULL;
388         int fd[2];
389         struct child_process *conn;
390         struct extra_have_objects extra_have;
391         struct ref *remote_refs, *local_refs;
392         int ret;
393         int helper_status = 0;
394         int send_all = 0;
395         const char *receivepack = "git-receive-pack";
396         int flags;
397         int nonfastforward = 0;
398
399         argv++;
400         for (i = 1; i < argc; i++, argv++) {
401                 const char *arg = *argv;
402
403                 if (*arg == '-') {
404                         if (!prefixcmp(arg, "--receive-pack=")) {
405                                 receivepack = arg + 15;
406                                 continue;
407                         }
408                         if (!prefixcmp(arg, "--exec=")) {
409                                 receivepack = arg + 7;
410                                 continue;
411                         }
412                         if (!prefixcmp(arg, "--remote=")) {
413                                 remote_name = arg + 9;
414                                 continue;
415                         }
416                         if (!strcmp(arg, "--all")) {
417                                 send_all = 1;
418                                 continue;
419                         }
420                         if (!strcmp(arg, "--dry-run")) {
421                                 args.dry_run = 1;
422                                 continue;
423                         }
424                         if (!strcmp(arg, "--mirror")) {
425                                 args.send_mirror = 1;
426                                 continue;
427                         }
428                         if (!strcmp(arg, "--force")) {
429                                 args.force_update = 1;
430                                 continue;
431                         }
432                         if (!strcmp(arg, "--verbose")) {
433                                 args.verbose = 1;
434                                 continue;
435                         }
436                         if (!strcmp(arg, "--thin")) {
437                                 args.use_thin_pack = 1;
438                                 continue;
439                         }
440                         if (!strcmp(arg, "--stateless-rpc")) {
441                                 args.stateless_rpc = 1;
442                                 continue;
443                         }
444                         if (!strcmp(arg, "--helper-status")) {
445                                 helper_status = 1;
446                                 continue;
447                         }
448                         usage(send_pack_usage);
449                 }
450                 if (!dest) {
451                         dest = arg;
452                         continue;
453                 }
454                 refspecs = (const char **) argv;
455                 nr_refspecs = argc - i;
456                 break;
457         }
458         if (!dest)
459                 usage(send_pack_usage);
460         /*
461          * --all and --mirror are incompatible; neither makes sense
462          * with any refspecs.
463          */
464         if ((refspecs && (send_all || args.send_mirror)) ||
465             (send_all && args.send_mirror))
466                 usage(send_pack_usage);
467
468         if (remote_name) {
469                 remote = remote_get(remote_name);
470                 if (!remote_has_url(remote, dest)) {
471                         die("Destination %s is not a uri for %s",
472                             dest, remote_name);
473                 }
474         }
475
476         if (args.stateless_rpc) {
477                 conn = NULL;
478                 fd[0] = 0;
479                 fd[1] = 1;
480         } else {
481                 conn = git_connect(fd, dest, receivepack,
482                         args.verbose ? CONNECT_VERBOSE : 0);
483         }
484
485         memset(&extra_have, 0, sizeof(extra_have));
486
487         get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
488                          &extra_have);
489
490         transport_verify_remote_names(nr_refspecs, refspecs);
491
492         local_refs = get_local_heads();
493
494         flags = MATCH_REFS_NONE;
495
496         if (send_all)
497                 flags |= MATCH_REFS_ALL;
498         if (args.send_mirror)
499                 flags |= MATCH_REFS_MIRROR;
500
501         /* match them up */
502         if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
503                 return -1;
504
505         set_ref_status_for_push(remote_refs, args.send_mirror,
506                 args.force_update);
507
508         ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
509
510         if (helper_status)
511                 print_helper_status(remote_refs);
512
513         close(fd[1]);
514         close(fd[0]);
515
516         ret |= finish_connect(conn);
517
518         if (!helper_status)
519                 transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
520
521         if (!args.dry_run && remote) {
522                 struct ref *ref;
523                 for (ref = remote_refs; ref; ref = ref->next)
524                         transport_update_tracking_ref(remote, ref, args.verbose);
525         }
526
527         if (!ret && !transport_refs_pushed(remote_refs))
528                 fprintf(stderr, "Everything up-to-date\n");
529
530         return ret;
531 }