refactor duplicated code in builtin-send-pack.c and transport.c
[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         }
102
103         if (finish_command(&po))
104                 return error("pack-objects died with strange error");
105         return 0;
106 }
107
108 static int receive_status(int in, struct ref *refs)
109 {
110         struct ref *hint;
111         char line[1000];
112         int ret = 0;
113         int len = packet_read_line(in, line, sizeof(line));
114         if (len < 10 || memcmp(line, "unpack ", 7))
115                 return error("did not receive remote status");
116         if (memcmp(line, "unpack ok\n", 10)) {
117                 char *p = line + strlen(line) - 1;
118                 if (*p == '\n')
119                         *p = '\0';
120                 error("unpack failed: %s", line + 7);
121                 ret = -1;
122         }
123         hint = NULL;
124         while (1) {
125                 char *refname;
126                 char *msg;
127                 len = packet_read_line(in, line, sizeof(line));
128                 if (!len)
129                         break;
130                 if (len < 3 ||
131                     (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
132                         fprintf(stderr, "protocol error: %s\n", line);
133                         ret = -1;
134                         break;
135                 }
136
137                 line[strlen(line)-1] = '\0';
138                 refname = line + 3;
139                 msg = strchr(refname, ' ');
140                 if (msg)
141                         *msg++ = '\0';
142
143                 /* first try searching at our hint, falling back to all refs */
144                 if (hint)
145                         hint = find_ref_by_name(hint, refname);
146                 if (!hint)
147                         hint = find_ref_by_name(refs, refname);
148                 if (!hint) {
149                         warning("remote reported status on unknown ref: %s",
150                                         refname);
151                         continue;
152                 }
153                 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
154                         warning("remote reported status on unexpected ref: %s",
155                                         refname);
156                         continue;
157                 }
158
159                 if (line[0] == 'o' && line[1] == 'k')
160                         hint->status = REF_STATUS_OK;
161                 else {
162                         hint->status = REF_STATUS_REMOTE_REJECT;
163                         ret = -1;
164                 }
165                 if (msg)
166                         hint->remote_status = xstrdup(msg);
167                 /* start our next search from the next ref */
168                 hint = hint->next;
169         }
170         return ret;
171 }
172
173 static void print_helper_status(struct ref *ref)
174 {
175         struct strbuf buf = STRBUF_INIT;
176
177         for (; ref; ref = ref->next) {
178                 const char *msg = NULL;
179                 const char *res;
180
181                 switch(ref->status) {
182                 case REF_STATUS_NONE:
183                         res = "error";
184                         msg = "no match";
185                         break;
186
187                 case REF_STATUS_OK:
188                         res = "ok";
189                         break;
190
191                 case REF_STATUS_UPTODATE:
192                         res = "ok";
193                         msg = "up to date";
194                         break;
195
196                 case REF_STATUS_REJECT_NONFASTFORWARD:
197                         res = "error";
198                         msg = "non-fast forward";
199                         break;
200
201                 case REF_STATUS_REJECT_NODELETE:
202                 case REF_STATUS_REMOTE_REJECT:
203                         res = "error";
204                         break;
205
206                 case REF_STATUS_EXPECTING_REPORT:
207                 default:
208                         continue;
209                 }
210
211                 strbuf_reset(&buf);
212                 strbuf_addf(&buf, "%s %s", res, ref->name);
213                 if (ref->remote_status)
214                         msg = ref->remote_status;
215                 if (msg) {
216                         strbuf_addch(&buf, ' ');
217                         quote_two_c_style(&buf, "", msg, 0);
218                 }
219                 strbuf_addch(&buf, '\n');
220
221                 safe_write(1, buf.buf, buf.len);
222         }
223         strbuf_release(&buf);
224 }
225
226 int send_pack(struct send_pack_args *args,
227               int fd[], struct child_process *conn,
228               struct ref *remote_refs,
229               struct extra_have_objects *extra_have)
230 {
231         int in = fd[0];
232         int out = fd[1];
233         struct strbuf req_buf = STRBUF_INIT;
234         struct ref *ref;
235         int new_refs;
236         int ask_for_status_report = 0;
237         int allow_deleting_refs = 0;
238         int expect_status_report = 0;
239         int ret;
240
241         /* Does the other end support the reporting? */
242         if (server_supports("report-status"))
243                 ask_for_status_report = 1;
244         if (server_supports("delete-refs"))
245                 allow_deleting_refs = 1;
246         if (server_supports("ofs-delta"))
247                 args->use_ofs_delta = 1;
248
249         if (!remote_refs) {
250                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
251                         "Perhaps you should specify a branch such as 'master'.\n");
252                 return 0;
253         }
254
255         /*
256          * Finally, tell the other end!
257          */
258         new_refs = 0;
259         for (ref = remote_refs; ref; ref = ref->next) {
260                 if (!ref->peer_ref && !args->send_mirror)
261                         continue;
262
263                 /* Check for statuses set by set_ref_status_for_push() */
264                 switch (ref->status) {
265                 case REF_STATUS_REJECT_NONFASTFORWARD:
266                 case REF_STATUS_UPTODATE:
267                         continue;
268                 default:
269                         ; /* do nothing */
270                 }
271
272                 if (ref->deletion && !allow_deleting_refs) {
273                         ref->status = REF_STATUS_REJECT_NODELETE;
274                         continue;
275                 }
276
277                 if (!ref->deletion)
278                         new_refs++;
279
280                 if (!args->dry_run) {
281                         char *old_hex = sha1_to_hex(ref->old_sha1);
282                         char *new_hex = sha1_to_hex(ref->new_sha1);
283
284                         if (ask_for_status_report) {
285                                 packet_buf_write(&req_buf, "%s %s %s%c%s",
286                                         old_hex, new_hex, ref->name, 0,
287                                         "report-status");
288                                 ask_for_status_report = 0;
289                                 expect_status_report = 1;
290                         }
291                         else
292                                 packet_buf_write(&req_buf, "%s %s %s",
293                                         old_hex, new_hex, ref->name);
294                 }
295                 ref->status = expect_status_report ?
296                         REF_STATUS_EXPECTING_REPORT :
297                         REF_STATUS_OK;
298         }
299
300         if (args->stateless_rpc) {
301                 if (!args->dry_run) {
302                         packet_buf_flush(&req_buf);
303                         send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
304                 }
305         } else {
306                 safe_write(out, req_buf.buf, req_buf.len);
307                 packet_flush(out);
308         }
309         strbuf_release(&req_buf);
310
311         if (new_refs && !args->dry_run) {
312                 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
313                         for (ref = remote_refs; ref; ref = ref->next)
314                                 ref->status = REF_STATUS_NONE;
315                         return -1;
316                 }
317         }
318         if (args->stateless_rpc && !args->dry_run)
319                 packet_flush(out);
320
321         if (expect_status_report)
322                 ret = receive_status(in, remote_refs);
323         else
324                 ret = 0;
325         if (args->stateless_rpc)
326                 packet_flush(out);
327
328         if (ret < 0)
329                 return ret;
330         for (ref = remote_refs; ref; ref = ref->next) {
331                 switch (ref->status) {
332                 case REF_STATUS_NONE:
333                 case REF_STATUS_UPTODATE:
334                 case REF_STATUS_OK:
335                         break;
336                 default:
337                         return -1;
338                 }
339         }
340         return 0;
341 }
342
343 int cmd_send_pack(int argc, const char **argv, const char *prefix)
344 {
345         int i, nr_refspecs = 0;
346         const char **refspecs = NULL;
347         const char *remote_name = NULL;
348         struct remote *remote = NULL;
349         const char *dest = NULL;
350         int fd[2];
351         struct child_process *conn;
352         struct extra_have_objects extra_have;
353         struct ref *remote_refs, *local_refs;
354         int ret;
355         int helper_status = 0;
356         int send_all = 0;
357         const char *receivepack = "git-receive-pack";
358         int flags;
359         int nonfastforward = 0;
360
361         argv++;
362         for (i = 1; i < argc; i++, argv++) {
363                 const char *arg = *argv;
364
365                 if (*arg == '-') {
366                         if (!prefixcmp(arg, "--receive-pack=")) {
367                                 receivepack = arg + 15;
368                                 continue;
369                         }
370                         if (!prefixcmp(arg, "--exec=")) {
371                                 receivepack = arg + 7;
372                                 continue;
373                         }
374                         if (!prefixcmp(arg, "--remote=")) {
375                                 remote_name = arg + 9;
376                                 continue;
377                         }
378                         if (!strcmp(arg, "--all")) {
379                                 send_all = 1;
380                                 continue;
381                         }
382                         if (!strcmp(arg, "--dry-run")) {
383                                 args.dry_run = 1;
384                                 continue;
385                         }
386                         if (!strcmp(arg, "--mirror")) {
387                                 args.send_mirror = 1;
388                                 continue;
389                         }
390                         if (!strcmp(arg, "--force")) {
391                                 args.force_update = 1;
392                                 continue;
393                         }
394                         if (!strcmp(arg, "--verbose")) {
395                                 args.verbose = 1;
396                                 continue;
397                         }
398                         if (!strcmp(arg, "--thin")) {
399                                 args.use_thin_pack = 1;
400                                 continue;
401                         }
402                         if (!strcmp(arg, "--stateless-rpc")) {
403                                 args.stateless_rpc = 1;
404                                 continue;
405                         }
406                         if (!strcmp(arg, "--helper-status")) {
407                                 helper_status = 1;
408                                 continue;
409                         }
410                         usage(send_pack_usage);
411                 }
412                 if (!dest) {
413                         dest = arg;
414                         continue;
415                 }
416                 refspecs = (const char **) argv;
417                 nr_refspecs = argc - i;
418                 break;
419         }
420         if (!dest)
421                 usage(send_pack_usage);
422         /*
423          * --all and --mirror are incompatible; neither makes sense
424          * with any refspecs.
425          */
426         if ((refspecs && (send_all || args.send_mirror)) ||
427             (send_all && args.send_mirror))
428                 usage(send_pack_usage);
429
430         if (remote_name) {
431                 remote = remote_get(remote_name);
432                 if (!remote_has_url(remote, dest)) {
433                         die("Destination %s is not a uri for %s",
434                             dest, remote_name);
435                 }
436         }
437
438         if (args.stateless_rpc) {
439                 conn = NULL;
440                 fd[0] = 0;
441                 fd[1] = 1;
442         } else {
443                 conn = git_connect(fd, dest, receivepack,
444                         args.verbose ? CONNECT_VERBOSE : 0);
445         }
446
447         memset(&extra_have, 0, sizeof(extra_have));
448
449         get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
450                          &extra_have);
451
452         transport_verify_remote_names(nr_refspecs, refspecs);
453
454         local_refs = get_local_heads();
455
456         flags = MATCH_REFS_NONE;
457
458         if (send_all)
459                 flags |= MATCH_REFS_ALL;
460         if (args.send_mirror)
461                 flags |= MATCH_REFS_MIRROR;
462
463         /* match them up */
464         if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
465                 return -1;
466
467         set_ref_status_for_push(remote_refs, args.send_mirror,
468                 args.force_update);
469
470         ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
471
472         if (helper_status)
473                 print_helper_status(remote_refs);
474
475         close(fd[1]);
476         close(fd[0]);
477
478         ret |= finish_connect(conn);
479
480         if (!helper_status)
481                 transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
482
483         if (!args.dry_run && remote) {
484                 struct ref *ref;
485                 for (ref = remote_refs; ref; ref = ref->next)
486                         transport_update_tracking_ref(remote, ref, args.verbose);
487         }
488
489         if (!ret && !transport_refs_pushed(remote_refs))
490                 fprintf(stderr, "Everything up-to-date\n");
491
492         return ret;
493 }