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