]> rtime.felk.cvut.cz Git - git.git/blob - builtin-send-pack.c
MinGW: Quote arguments for subprocesses that contain a single-quote
[git.git] / builtin-send-pack.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "run-command.h"
6 #include "remote.h"
7 #include "send-pack.h"
8
9 static const char send_pack_usage[] =
10 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
11 "  --all and explicit <ref> specification are mutually exclusive.";
12
13 static struct send_pack_args args = {
14         /* .receivepack = */ "git-receive-pack",
15 };
16
17 static int feed_object(const unsigned char *sha1, int fd, int negative)
18 {
19         char buf[42];
20
21         if (negative && !has_sha1_file(sha1))
22                 return 1;
23
24         memcpy(buf + negative, sha1_to_hex(sha1), 40);
25         if (negative)
26                 buf[0] = '^';
27         buf[40 + negative] = '\n';
28         return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
29 }
30
31 /*
32  * Make a pack stream and spit it out into file descriptor fd
33  */
34 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra)
35 {
36         /*
37          * The child becomes pack-objects --revs; we feed
38          * the revision parameters to it via its stdin and
39          * let its stdout go back to the other end.
40          */
41         const char *argv[] = {
42                 "pack-objects",
43                 "--all-progress",
44                 "--revs",
45                 "--stdout",
46                 NULL,
47                 NULL,
48         };
49         struct child_process po;
50         int i;
51
52         if (args.use_thin_pack)
53                 argv[4] = "--thin";
54         memset(&po, 0, sizeof(po));
55         po.argv = argv;
56         po.in = -1;
57         po.out = fd;
58         po.git_cmd = 1;
59         if (start_command(&po))
60                 die("git pack-objects failed (%s)", strerror(errno));
61
62         /*
63          * We feed the pack-objects we just spawned with revision
64          * parameters by writing to the pipe.
65          */
66         for (i = 0; i < extra->nr; i++)
67                 if (!feed_object(extra->array[i], po.in, 1))
68                         break;
69
70         while (refs) {
71                 if (!is_null_sha1(refs->old_sha1) &&
72                     !feed_object(refs->old_sha1, po.in, 1))
73                         break;
74                 if (!is_null_sha1(refs->new_sha1) &&
75                     !feed_object(refs->new_sha1, po.in, 0))
76                         break;
77                 refs = refs->next;
78         }
79
80         close(po.in);
81         if (finish_command(&po))
82                 return error("pack-objects died with strange error");
83         return 0;
84 }
85
86 static struct ref *remote_refs, **remote_tail;
87
88 static int receive_status(int in, struct ref *refs)
89 {
90         struct ref *hint;
91         char line[1000];
92         int ret = 0;
93         int len = packet_read_line(in, line, sizeof(line));
94         if (len < 10 || memcmp(line, "unpack ", 7))
95                 return error("did not receive remote status");
96         if (memcmp(line, "unpack ok\n", 10)) {
97                 char *p = line + strlen(line) - 1;
98                 if (*p == '\n')
99                         *p = '\0';
100                 error("unpack failed: %s", line + 7);
101                 ret = -1;
102         }
103         hint = NULL;
104         while (1) {
105                 char *refname;
106                 char *msg;
107                 len = packet_read_line(in, line, sizeof(line));
108                 if (!len)
109                         break;
110                 if (len < 3 ||
111                     (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
112                         fprintf(stderr, "protocol error: %s\n", line);
113                         ret = -1;
114                         break;
115                 }
116
117                 line[strlen(line)-1] = '\0';
118                 refname = line + 3;
119                 msg = strchr(refname, ' ');
120                 if (msg)
121                         *msg++ = '\0';
122
123                 /* first try searching at our hint, falling back to all refs */
124                 if (hint)
125                         hint = find_ref_by_name(hint, refname);
126                 if (!hint)
127                         hint = find_ref_by_name(refs, refname);
128                 if (!hint) {
129                         warning("remote reported status on unknown ref: %s",
130                                         refname);
131                         continue;
132                 }
133                 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
134                         warning("remote reported status on unexpected ref: %s",
135                                         refname);
136                         continue;
137                 }
138
139                 if (line[0] == 'o' && line[1] == 'k')
140                         hint->status = REF_STATUS_OK;
141                 else {
142                         hint->status = REF_STATUS_REMOTE_REJECT;
143                         ret = -1;
144                 }
145                 if (msg)
146                         hint->remote_status = xstrdup(msg);
147                 /* start our next search from the next ref */
148                 hint = hint->next;
149         }
150         return ret;
151 }
152
153 static void update_tracking_ref(struct remote *remote, struct ref *ref)
154 {
155         struct refspec rs;
156
157         if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
158                 return;
159
160         rs.src = ref->name;
161         rs.dst = NULL;
162
163         if (!remote_find_tracking(remote, &rs)) {
164                 if (args.verbose)
165                         fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
166                 if (ref->deletion) {
167                         delete_ref(rs.dst, NULL, 0);
168                 } else
169                         update_ref("update by push", rs.dst,
170                                         ref->new_sha1, NULL, 0, 0);
171                 free(rs.dst);
172         }
173 }
174
175 static const char *prettify_ref(const struct ref *ref)
176 {
177         const char *name = ref->name;
178         return name + (
179                 !prefixcmp(name, "refs/heads/") ? 11 :
180                 !prefixcmp(name, "refs/tags/") ? 10 :
181                 !prefixcmp(name, "refs/remotes/") ? 13 :
182                 0);
183 }
184
185 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
186
187 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
188 {
189         fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
190         if (from)
191                 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
192         else
193                 fputs(prettify_ref(to), stderr);
194         if (msg) {
195                 fputs(" (", stderr);
196                 fputs(msg, stderr);
197                 fputc(')', stderr);
198         }
199         fputc('\n', stderr);
200 }
201
202 static const char *status_abbrev(unsigned char sha1[20])
203 {
204         return find_unique_abbrev(sha1, DEFAULT_ABBREV);
205 }
206
207 static void print_ok_ref_status(struct ref *ref)
208 {
209         if (ref->deletion)
210                 print_ref_status('-', "[deleted]", ref, NULL, NULL);
211         else if (is_null_sha1(ref->old_sha1))
212                 print_ref_status('*',
213                         (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
214                           "[new branch]"),
215                         ref, ref->peer_ref, NULL);
216         else {
217                 char quickref[84];
218                 char type;
219                 const char *msg;
220
221                 strcpy(quickref, status_abbrev(ref->old_sha1));
222                 if (ref->nonfastforward) {
223                         strcat(quickref, "...");
224                         type = '+';
225                         msg = "forced update";
226                 } else {
227                         strcat(quickref, "..");
228                         type = ' ';
229                         msg = NULL;
230                 }
231                 strcat(quickref, status_abbrev(ref->new_sha1));
232
233                 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
234         }
235 }
236
237 static int print_one_push_status(struct ref *ref, const char *dest, int count)
238 {
239         if (!count)
240                 fprintf(stderr, "To %s\n", dest);
241
242         switch(ref->status) {
243         case REF_STATUS_NONE:
244                 print_ref_status('X', "[no match]", ref, NULL, NULL);
245                 break;
246         case REF_STATUS_REJECT_NODELETE:
247                 print_ref_status('!', "[rejected]", ref, NULL,
248                                 "remote does not support deleting refs");
249                 break;
250         case REF_STATUS_UPTODATE:
251                 print_ref_status('=', "[up to date]", ref,
252                                 ref->peer_ref, NULL);
253                 break;
254         case REF_STATUS_REJECT_NONFASTFORWARD:
255                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
256                                 "non-fast forward");
257                 break;
258         case REF_STATUS_REMOTE_REJECT:
259                 print_ref_status('!', "[remote rejected]", ref,
260                                 ref->deletion ? NULL : ref->peer_ref,
261                                 ref->remote_status);
262                 break;
263         case REF_STATUS_EXPECTING_REPORT:
264                 print_ref_status('!', "[remote failure]", ref,
265                                 ref->deletion ? NULL : ref->peer_ref,
266                                 "remote failed to report status");
267                 break;
268         case REF_STATUS_OK:
269                 print_ok_ref_status(ref);
270                 break;
271         }
272
273         return 1;
274 }
275
276 static void print_push_status(const char *dest, struct ref *refs)
277 {
278         struct ref *ref;
279         int n = 0;
280
281         if (args.verbose) {
282                 for (ref = refs; ref; ref = ref->next)
283                         if (ref->status == REF_STATUS_UPTODATE)
284                                 n += print_one_push_status(ref, dest, n);
285         }
286
287         for (ref = refs; ref; ref = ref->next)
288                 if (ref->status == REF_STATUS_OK)
289                         n += print_one_push_status(ref, dest, n);
290
291         for (ref = refs; ref; ref = ref->next) {
292                 if (ref->status != REF_STATUS_NONE &&
293                     ref->status != REF_STATUS_UPTODATE &&
294                     ref->status != REF_STATUS_OK)
295                         n += print_one_push_status(ref, dest, n);
296         }
297 }
298
299 static int refs_pushed(struct ref *ref)
300 {
301         for (; ref; ref = ref->next) {
302                 switch(ref->status) {
303                 case REF_STATUS_NONE:
304                 case REF_STATUS_UPTODATE:
305                         break;
306                 default:
307                         return 1;
308                 }
309         }
310         return 0;
311 }
312
313 static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
314 {
315         struct ref *ref, *local_refs;
316         int new_refs;
317         int ask_for_status_report = 0;
318         int allow_deleting_refs = 0;
319         int expect_status_report = 0;
320         int flags = MATCH_REFS_NONE;
321         int ret;
322         struct extra_have_objects extra_have;
323
324         memset(&extra_have, 0, sizeof(extra_have));
325         if (args.send_all)
326                 flags |= MATCH_REFS_ALL;
327         if (args.send_mirror)
328                 flags |= MATCH_REFS_MIRROR;
329
330         /* No funny business with the matcher */
331         remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL,
332                                        &extra_have);
333         local_refs = get_local_heads();
334
335         /* Does the other end support the reporting? */
336         if (server_supports("report-status"))
337                 ask_for_status_report = 1;
338         if (server_supports("delete-refs"))
339                 allow_deleting_refs = 1;
340
341         /* match them up */
342         if (!remote_tail)
343                 remote_tail = &remote_refs;
344         if (match_refs(local_refs, remote_refs, &remote_tail,
345                        nr_refspec, refspec, flags)) {
346                 close(out);
347                 return -1;
348         }
349
350         if (!remote_refs) {
351                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
352                         "Perhaps you should specify a branch such as 'master'.\n");
353                 close(out);
354                 return 0;
355         }
356
357         /*
358          * Finally, tell the other end!
359          */
360         new_refs = 0;
361         for (ref = remote_refs; ref; ref = ref->next) {
362
363                 if (ref->peer_ref)
364                         hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
365                 else if (!args.send_mirror)
366                         continue;
367
368                 ref->deletion = is_null_sha1(ref->new_sha1);
369                 if (ref->deletion && !allow_deleting_refs) {
370                         ref->status = REF_STATUS_REJECT_NODELETE;
371                         continue;
372                 }
373                 if (!ref->deletion &&
374                     !hashcmp(ref->old_sha1, ref->new_sha1)) {
375                         ref->status = REF_STATUS_UPTODATE;
376                         continue;
377                 }
378
379                 /* This part determines what can overwrite what.
380                  * The rules are:
381                  *
382                  * (0) you can always use --force or +A:B notation to
383                  *     selectively force individual ref pairs.
384                  *
385                  * (1) if the old thing does not exist, it is OK.
386                  *
387                  * (2) if you do not have the old thing, you are not allowed
388                  *     to overwrite it; you would not know what you are losing
389                  *     otherwise.
390                  *
391                  * (3) if both new and old are commit-ish, and new is a
392                  *     descendant of old, it is OK.
393                  *
394                  * (4) regardless of all of the above, removing :B is
395                  *     always allowed.
396                  */
397
398                 ref->nonfastforward =
399                     !ref->deletion &&
400                     !is_null_sha1(ref->old_sha1) &&
401                     (!has_sha1_file(ref->old_sha1)
402                       || !ref_newer(ref->new_sha1, ref->old_sha1));
403
404                 if (ref->nonfastforward && !ref->force && !args.force_update) {
405                         ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
406                         continue;
407                 }
408
409                 if (!ref->deletion)
410                         new_refs++;
411
412                 if (!args.dry_run) {
413                         char *old_hex = sha1_to_hex(ref->old_sha1);
414                         char *new_hex = sha1_to_hex(ref->new_sha1);
415
416                         if (ask_for_status_report) {
417                                 packet_write(out, "%s %s %s%c%s",
418                                         old_hex, new_hex, ref->name, 0,
419                                         "report-status");
420                                 ask_for_status_report = 0;
421                                 expect_status_report = 1;
422                         }
423                         else
424                                 packet_write(out, "%s %s %s",
425                                         old_hex, new_hex, ref->name);
426                 }
427                 ref->status = expect_status_report ?
428                         REF_STATUS_EXPECTING_REPORT :
429                         REF_STATUS_OK;
430         }
431
432         packet_flush(out);
433         if (new_refs && !args.dry_run) {
434                 if (pack_objects(out, remote_refs, &extra_have) < 0)
435                         return -1;
436         }
437         else
438                 close(out);
439
440         if (expect_status_report)
441                 ret = receive_status(in, remote_refs);
442         else
443                 ret = 0;
444
445         print_push_status(dest, remote_refs);
446
447         if (!args.dry_run && remote) {
448                 for (ref = remote_refs; ref; ref = ref->next)
449                         update_tracking_ref(remote, ref);
450         }
451
452         if (!refs_pushed(remote_refs))
453                 fprintf(stderr, "Everything up-to-date\n");
454         if (ret < 0)
455                 return ret;
456         for (ref = remote_refs; ref; ref = ref->next) {
457                 switch (ref->status) {
458                 case REF_STATUS_NONE:
459                 case REF_STATUS_UPTODATE:
460                 case REF_STATUS_OK:
461                         break;
462                 default:
463                         return -1;
464                 }
465         }
466         return 0;
467 }
468
469 static void verify_remote_names(int nr_heads, const char **heads)
470 {
471         int i;
472
473         for (i = 0; i < nr_heads; i++) {
474                 const char *local = heads[i];
475                 const char *remote = strrchr(heads[i], ':');
476
477                 if (*local == '+')
478                         local++;
479
480                 /* A matching refspec is okay.  */
481                 if (remote == local && remote[1] == '\0')
482                         continue;
483
484                 remote = remote ? (remote + 1) : local;
485                 switch (check_ref_format(remote)) {
486                 case 0: /* ok */
487                 case CHECK_REF_FORMAT_ONELEVEL:
488                         /* ok but a single level -- that is fine for
489                          * a match pattern.
490                          */
491                 case CHECK_REF_FORMAT_WILDCARD:
492                         /* ok but ends with a pattern-match character */
493                         continue;
494                 }
495                 die("remote part of refspec is not a valid name in %s",
496                     heads[i]);
497         }
498 }
499
500 int cmd_send_pack(int argc, const char **argv, const char *prefix)
501 {
502         int i, nr_heads = 0;
503         const char **heads = NULL;
504         const char *remote_name = NULL;
505         struct remote *remote = NULL;
506         const char *dest = NULL;
507
508         argv++;
509         for (i = 1; i < argc; i++, argv++) {
510                 const char *arg = *argv;
511
512                 if (*arg == '-') {
513                         if (!prefixcmp(arg, "--receive-pack=")) {
514                                 args.receivepack = arg + 15;
515                                 continue;
516                         }
517                         if (!prefixcmp(arg, "--exec=")) {
518                                 args.receivepack = arg + 7;
519                                 continue;
520                         }
521                         if (!prefixcmp(arg, "--remote=")) {
522                                 remote_name = arg + 9;
523                                 continue;
524                         }
525                         if (!strcmp(arg, "--all")) {
526                                 args.send_all = 1;
527                                 continue;
528                         }
529                         if (!strcmp(arg, "--dry-run")) {
530                                 args.dry_run = 1;
531                                 continue;
532                         }
533                         if (!strcmp(arg, "--mirror")) {
534                                 args.send_mirror = 1;
535                                 continue;
536                         }
537                         if (!strcmp(arg, "--force")) {
538                                 args.force_update = 1;
539                                 continue;
540                         }
541                         if (!strcmp(arg, "--verbose")) {
542                                 args.verbose = 1;
543                                 continue;
544                         }
545                         if (!strcmp(arg, "--thin")) {
546                                 args.use_thin_pack = 1;
547                                 continue;
548                         }
549                         usage(send_pack_usage);
550                 }
551                 if (!dest) {
552                         dest = arg;
553                         continue;
554                 }
555                 heads = (const char **) argv;
556                 nr_heads = argc - i;
557                 break;
558         }
559         if (!dest)
560                 usage(send_pack_usage);
561         /*
562          * --all and --mirror are incompatible; neither makes sense
563          * with any refspecs.
564          */
565         if ((heads && (args.send_all || args.send_mirror)) ||
566                                         (args.send_all && args.send_mirror))
567                 usage(send_pack_usage);
568
569         if (remote_name) {
570                 remote = remote_get(remote_name);
571                 if (!remote_has_url(remote, dest)) {
572                         die("Destination %s is not a uri for %s",
573                             dest, remote_name);
574                 }
575         }
576
577         return send_pack(&args, dest, remote, nr_heads, heads);
578 }
579
580 int send_pack(struct send_pack_args *my_args,
581               const char *dest, struct remote *remote,
582               int nr_heads, const char **heads)
583 {
584         int fd[2], ret;
585         struct child_process *conn;
586
587         memcpy(&args, my_args, sizeof(args));
588
589         verify_remote_names(nr_heads, heads);
590
591         conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
592         ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads);
593         close(fd[0]);
594         /* do_send_pack always closes fd[1] */
595         ret |= finish_connect(conn);
596         return !!ret;
597 }