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