bundle.con commit bundle: add parse_bundle_header() helper function (2727b71)
   1#include "cache.h"
   2#include "bundle.h"
   3#include "object.h"
   4#include "commit.h"
   5#include "diff.h"
   6#include "revision.h"
   7#include "list-objects.h"
   8#include "run-command.h"
   9#include "refs.h"
  10
  11static const char bundle_signature[] = "# v2 git bundle\n";
  12
  13static void add_to_ref_list(const unsigned char *sha1, const char *name,
  14                struct ref_list *list)
  15{
  16        if (list->nr + 1 >= list->alloc) {
  17                list->alloc = alloc_nr(list->nr + 1);
  18                list->list = xrealloc(list->list,
  19                                list->alloc * sizeof(list->list[0]));
  20        }
  21        memcpy(list->list[list->nr].sha1, sha1, 20);
  22        list->list[list->nr].name = xstrdup(name);
  23        list->nr++;
  24}
  25
  26/* Eventually this should go to strbuf.[ch] */
  27static int strbuf_readline_fd(struct strbuf *sb, int fd)
  28{
  29        strbuf_reset(sb);
  30
  31        while (1) {
  32                char ch;
  33                ssize_t len = xread(fd, &ch, 1);
  34                if (len < 0)
  35                        return -1;
  36                strbuf_addch(sb, ch);
  37                if (ch == '\n')
  38                        break;
  39        }
  40        return 0;
  41}
  42
  43static int parse_bundle_header(int fd, struct bundle_header *header,
  44                               const char *report_path)
  45{
  46        struct strbuf buf = STRBUF_INIT;
  47        int status = 0;
  48
  49        /* The bundle header begins with the signature */
  50        if (strbuf_readline_fd(&buf, fd) ||
  51            strcmp(buf.buf, bundle_signature)) {
  52                if (report_path)
  53                        error("'%s' does not look like a v2 bundle file",
  54                              report_path);
  55                status = -1;
  56                goto abort;
  57        }
  58
  59        /* The bundle header ends with an empty line */
  60        while (!strbuf_readline_fd(&buf, fd) &&
  61               buf.len && buf.buf[0] != '\n') {
  62                unsigned char sha1[20];
  63                int is_prereq = 0;
  64
  65                if (*buf.buf == '-') {
  66                        is_prereq = 1;
  67                        strbuf_remove(&buf, 0, 1);
  68                }
  69                strbuf_rtrim(&buf);
  70
  71                /*
  72                 * Tip lines have object name, SP, and refname.
  73                 * Prerequisites have object name that is optionally
  74                 * followed by SP and subject line.
  75                 */
  76                if (get_sha1_hex(buf.buf, sha1) ||
  77                    (40 <= buf.len && !isspace(buf.buf[40])) ||
  78                    (!is_prereq && buf.len <= 40)) {
  79                        if (report_path)
  80                                error("unrecognized header: %s%s (%d)",
  81                                      (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
  82                        status = -1;
  83                        break;
  84                } else {
  85                        if (is_prereq)
  86                                add_to_ref_list(sha1, "", &header->prerequisites);
  87                        else
  88                                add_to_ref_list(sha1, buf.buf + 41, &header->references);
  89                }
  90        }
  91
  92 abort:
  93        if (status) {
  94                close(fd);
  95                fd = -1;
  96        }
  97        strbuf_release(&buf);
  98        return fd;
  99}
 100
 101int read_bundle_header(const char *path, struct bundle_header *header)
 102{
 103        int fd = open(path, O_RDONLY);
 104
 105        if (fd < 0)
 106                return error("could not open '%s'", path);
 107        return parse_bundle_header(fd, header, path);
 108}
 109
 110int is_bundle(const char *path, int quiet)
 111{
 112        struct bundle_header header;
 113        int fd = open(path, O_RDONLY);
 114
 115        if (fd < 0)
 116                return 0;
 117        memset(&header, 0, sizeof(header));
 118        fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
 119        if (fd >= 0)
 120                close(fd);
 121        return (fd >= 0);
 122}
 123
 124static int list_refs(struct ref_list *r, int argc, const char **argv)
 125{
 126        int i;
 127
 128        for (i = 0; i < r->nr; i++) {
 129                if (argc > 1) {
 130                        int j;
 131                        for (j = 1; j < argc; j++)
 132                                if (!strcmp(r->list[i].name, argv[j]))
 133                                        break;
 134                        if (j == argc)
 135                                continue;
 136                }
 137                printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
 138                                r->list[i].name);
 139        }
 140        return 0;
 141}
 142
 143#define PREREQ_MARK (1u<<16)
 144
 145int verify_bundle(struct bundle_header *header, int verbose)
 146{
 147        /*
 148         * Do fast check, then if any prereqs are missing then go line by line
 149         * to be verbose about the errors
 150         */
 151        struct ref_list *p = &header->prerequisites;
 152        struct rev_info revs;
 153        const char *argv[] = {NULL, "--all", NULL};
 154        struct object_array refs;
 155        struct commit *commit;
 156        int i, ret = 0, req_nr;
 157        const char *message = "Repository lacks these prerequisite commits:";
 158
 159        init_revisions(&revs, NULL);
 160        for (i = 0; i < p->nr; i++) {
 161                struct ref_list_entry *e = p->list + i;
 162                struct object *o = parse_object(e->sha1);
 163                if (o) {
 164                        o->flags |= PREREQ_MARK;
 165                        add_pending_object(&revs, o, e->name);
 166                        continue;
 167                }
 168                if (++ret == 1)
 169                        error("%s", message);
 170                error("%s %s", sha1_to_hex(e->sha1), e->name);
 171        }
 172        if (revs.pending.nr != p->nr)
 173                return ret;
 174        req_nr = revs.pending.nr;
 175        setup_revisions(2, argv, &revs, NULL);
 176
 177        memset(&refs, 0, sizeof(struct object_array));
 178        for (i = 0; i < revs.pending.nr; i++) {
 179                struct object_array_entry *e = revs.pending.objects + i;
 180                add_object_array(e->item, e->name, &refs);
 181        }
 182
 183        if (prepare_revision_walk(&revs))
 184                die("revision walk setup failed");
 185
 186        i = req_nr;
 187        while (i && (commit = get_revision(&revs)))
 188                if (commit->object.flags & PREREQ_MARK)
 189                        i--;
 190
 191        for (i = 0; i < req_nr; i++)
 192                if (!(refs.objects[i].item->flags & SHOWN)) {
 193                        if (++ret == 1)
 194                                error("%s", message);
 195                        error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
 196                                refs.objects[i].name);
 197                }
 198
 199        for (i = 0; i < refs.nr; i++)
 200                clear_commit_marks((struct commit *)refs.objects[i].item, -1);
 201
 202        if (verbose) {
 203                struct ref_list *r;
 204
 205                r = &header->references;
 206                printf("The bundle contains %d ref%s\n",
 207                       r->nr, (1 < r->nr) ? "s" : "");
 208                list_refs(r, 0, NULL);
 209                r = &header->prerequisites;
 210                printf("The bundle requires these %d ref%s\n",
 211                       r->nr, (1 < r->nr) ? "s" : "");
 212                list_refs(r, 0, NULL);
 213        }
 214        return ret;
 215}
 216
 217int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
 218{
 219        return list_refs(&header->references, argc, argv);
 220}
 221
 222static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
 223{
 224        unsigned long size;
 225        enum object_type type;
 226        char *buf, *line, *lineend;
 227        unsigned long date;
 228
 229        if (revs->max_age == -1 && revs->min_age == -1)
 230                return 1;
 231
 232        buf = read_sha1_file(tag->sha1, &type, &size);
 233        if (!buf)
 234                return 1;
 235        line = memmem(buf, size, "\ntagger ", 8);
 236        if (!line++)
 237                return 1;
 238        lineend = memchr(line, buf + size - line, '\n');
 239        line = memchr(line, lineend ? lineend - line : buf + size - line, '>');
 240        if (!line++)
 241                return 1;
 242        date = strtoul(line, NULL, 10);
 243        free(buf);
 244        return (revs->max_age == -1 || revs->max_age < date) &&
 245                (revs->min_age == -1 || revs->min_age > date);
 246}
 247
 248int create_bundle(struct bundle_header *header, const char *path,
 249                int argc, const char **argv)
 250{
 251        static struct lock_file lock;
 252        int bundle_fd = -1;
 253        int bundle_to_stdout;
 254        const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
 255        const char **argv_pack = xmalloc(6 * sizeof(const char *));
 256        int i, ref_count = 0;
 257        char buffer[1024];
 258        struct rev_info revs;
 259        struct child_process rls;
 260        FILE *rls_fout;
 261
 262        bundle_to_stdout = !strcmp(path, "-");
 263        if (bundle_to_stdout)
 264                bundle_fd = 1;
 265        else
 266                bundle_fd = hold_lock_file_for_update(&lock, path,
 267                                                      LOCK_DIE_ON_ERROR);
 268
 269        /* write signature */
 270        write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
 271
 272        /* init revs to list objects for pack-objects later */
 273        save_commit_buffer = 0;
 274        init_revisions(&revs, NULL);
 275
 276        /* write prerequisites */
 277        memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
 278        argv_boundary[0] = "rev-list";
 279        argv_boundary[1] = "--boundary";
 280        argv_boundary[2] = "--pretty=oneline";
 281        argv_boundary[argc + 2] = NULL;
 282        memset(&rls, 0, sizeof(rls));
 283        rls.argv = argv_boundary;
 284        rls.out = -1;
 285        rls.git_cmd = 1;
 286        if (start_command(&rls))
 287                return -1;
 288        rls_fout = xfdopen(rls.out, "r");
 289        while (fgets(buffer, sizeof(buffer), rls_fout)) {
 290                unsigned char sha1[20];
 291                if (buffer[0] == '-') {
 292                        write_or_die(bundle_fd, buffer, strlen(buffer));
 293                        if (!get_sha1_hex(buffer + 1, sha1)) {
 294                                struct object *object = parse_object(sha1);
 295                                object->flags |= UNINTERESTING;
 296                                add_pending_object(&revs, object, buffer);
 297                        }
 298                } else if (!get_sha1_hex(buffer, sha1)) {
 299                        struct object *object = parse_object(sha1);
 300                        object->flags |= SHOWN;
 301                }
 302        }
 303        fclose(rls_fout);
 304        if (finish_command(&rls))
 305                return error("rev-list died");
 306
 307        /* write references */
 308        argc = setup_revisions(argc, argv, &revs, NULL);
 309
 310        if (argc > 1)
 311                return error("unrecognized argument: %s'", argv[1]);
 312
 313        object_array_remove_duplicates(&revs.pending);
 314
 315        for (i = 0; i < revs.pending.nr; i++) {
 316                struct object_array_entry *e = revs.pending.objects + i;
 317                unsigned char sha1[20];
 318                char *ref;
 319                const char *display_ref;
 320                int flag;
 321
 322                if (e->item->flags & UNINTERESTING)
 323                        continue;
 324                if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
 325                        continue;
 326                if (!resolve_ref(e->name, sha1, 1, &flag))
 327                        flag = 0;
 328                display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
 329
 330                if (e->item->type == OBJ_TAG &&
 331                                !is_tag_in_date_range(e->item, &revs)) {
 332                        e->item->flags |= UNINTERESTING;
 333                        continue;
 334                }
 335
 336                /*
 337                 * Make sure the refs we wrote out is correct; --max-count and
 338                 * other limiting options could have prevented all the tips
 339                 * from getting output.
 340                 *
 341                 * Non commit objects such as tags and blobs do not have
 342                 * this issue as they are not affected by those extra
 343                 * constraints.
 344                 */
 345                if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
 346                        warning("ref '%s' is excluded by the rev-list options",
 347                                e->name);
 348                        free(ref);
 349                        continue;
 350                }
 351                /*
 352                 * If you run "git bundle create bndl v1.0..v2.0", the
 353                 * name of the positive ref is "v2.0" but that is the
 354                 * commit that is referenced by the tag, and not the tag
 355                 * itself.
 356                 */
 357                if (hashcmp(sha1, e->item->sha1)) {
 358                        /*
 359                         * Is this the positive end of a range expressed
 360                         * in terms of a tag (e.g. v2.0 from the range
 361                         * "v1.0..v2.0")?
 362                         */
 363                        struct commit *one = lookup_commit_reference(sha1);
 364                        struct object *obj;
 365
 366                        if (e->item == &(one->object)) {
 367                                /*
 368                                 * Need to include e->name as an
 369                                 * independent ref to the pack-objects
 370                                 * input, so that the tag is included
 371                                 * in the output; otherwise we would
 372                                 * end up triggering "empty bundle"
 373                                 * error.
 374                                 */
 375                                obj = parse_object(sha1);
 376                                obj->flags |= SHOWN;
 377                                add_pending_object(&revs, obj, e->name);
 378                        }
 379                        free(ref);
 380                        continue;
 381                }
 382
 383                ref_count++;
 384                write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
 385                write_or_die(bundle_fd, " ", 1);
 386                write_or_die(bundle_fd, display_ref, strlen(display_ref));
 387                write_or_die(bundle_fd, "\n", 1);
 388                free(ref);
 389        }
 390        if (!ref_count)
 391                die ("Refusing to create empty bundle.");
 392
 393        /* end header */
 394        write_or_die(bundle_fd, "\n", 1);
 395
 396        /* write pack */
 397        argv_pack[0] = "pack-objects";
 398        argv_pack[1] = "--all-progress-implied";
 399        argv_pack[2] = "--stdout";
 400        argv_pack[3] = "--thin";
 401        argv_pack[4] = "--delta-base-offset";
 402        argv_pack[5] = NULL;
 403        memset(&rls, 0, sizeof(rls));
 404        rls.argv = argv_pack;
 405        rls.in = -1;
 406        rls.out = bundle_fd;
 407        rls.git_cmd = 1;
 408        if (start_command(&rls))
 409                return error("Could not spawn pack-objects");
 410
 411        /*
 412         * start_command closed bundle_fd if it was > 1
 413         * so set the lock fd to -1 so commit_lock_file()
 414         * won't fail trying to close it.
 415         */
 416        lock.fd = -1;
 417
 418        for (i = 0; i < revs.pending.nr; i++) {
 419                struct object *object = revs.pending.objects[i].item;
 420                if (object->flags & UNINTERESTING)
 421                        write_or_die(rls.in, "^", 1);
 422                write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
 423                write_or_die(rls.in, "\n", 1);
 424        }
 425        close(rls.in);
 426        if (finish_command(&rls))
 427                return error ("pack-objects died");
 428        if (!bundle_to_stdout) {
 429                if (commit_lock_file(&lock))
 430                        die_errno("cannot create '%s'", path);
 431        }
 432        return 0;
 433}
 434
 435int unbundle(struct bundle_header *header, int bundle_fd)
 436{
 437        const char *argv_index_pack[] = {"index-pack",
 438                "--fix-thin", "--stdin", NULL};
 439        struct child_process ip;
 440
 441        if (verify_bundle(header, 0))
 442                return -1;
 443        memset(&ip, 0, sizeof(ip));
 444        ip.argv = argv_index_pack;
 445        ip.in = bundle_fd;
 446        ip.no_stdout = 1;
 447        ip.git_cmd = 1;
 448        if (run_command(&ip))
 449                return error("index-pack died");
 450        return 0;
 451}