bundle.con commit Add merge-subtree back (1736855)
   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"};
 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(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        memset(&refs, 0, sizeof(struct object_array));
 126        for (i = 0; i < revs.pending.nr; i++) {
 127                struct object_array_entry *e = revs.pending.objects + i;
 128                add_object_array(e->item, e->name, &refs);
 129        }
 130
 131        prepare_revision_walk(&revs);
 132
 133        i = req_nr;
 134        while (i && (commit = get_revision(&revs)))
 135                if (commit->object.flags & PREREQ_MARK)
 136                        i--;
 137
 138        for (i = 0; i < req_nr; i++)
 139                if (!(refs.objects[i].item->flags & SHOWN)) {
 140                        if (++ret == 1)
 141                                error(message);
 142                        error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
 143                                refs.objects[i].name);
 144                }
 145
 146        for (i = 0; i < refs.nr; i++)
 147                clear_commit_marks((struct commit *)refs.objects[i].item, -1);
 148
 149        if (verbose) {
 150                struct ref_list *r;
 151
 152                r = &header->references;
 153                printf("The bundle contains %d ref%s\n",
 154                       r->nr, (1 < r->nr) ? "s" : "");
 155                list_refs(r, 0, NULL);
 156                r = &header->prerequisites;
 157                printf("The bundle requires these %d ref%s\n",
 158                       r->nr, (1 < r->nr) ? "s" : "");
 159                list_refs(r, 0, NULL);
 160        }
 161        return ret;
 162}
 163
 164int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
 165{
 166        return list_refs(&header->references, argc, argv);
 167}
 168
 169int create_bundle(struct bundle_header *header, const char *path,
 170                int argc, const char **argv)
 171{
 172        static struct lock_file lock;
 173        int bundle_fd = -1;
 174        int bundle_to_stdout;
 175        const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
 176        const char **argv_pack = xmalloc(5 * sizeof(const char *));
 177        int i, ref_count = 0;
 178        char buffer[1024];
 179        struct rev_info revs;
 180        struct child_process rls;
 181        FILE *rls_fout;
 182
 183        bundle_to_stdout = !strcmp(path, "-");
 184        if (bundle_to_stdout)
 185                bundle_fd = 1;
 186        else
 187                bundle_fd = hold_lock_file_for_update(&lock, path, 1);
 188
 189        /* write signature */
 190        write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
 191
 192        /* init revs to list objects for pack-objects later */
 193        save_commit_buffer = 0;
 194        init_revisions(&revs, NULL);
 195
 196        /* write prerequisites */
 197        memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
 198        argv_boundary[0] = "rev-list";
 199        argv_boundary[1] = "--boundary";
 200        argv_boundary[2] = "--pretty=oneline";
 201        argv_boundary[argc + 2] = NULL;
 202        memset(&rls, 0, sizeof(rls));
 203        rls.argv = argv_boundary;
 204        rls.out = -1;
 205        rls.git_cmd = 1;
 206        if (start_command(&rls))
 207                return -1;
 208        rls_fout = fdopen(rls.out, "r");
 209        while (fgets(buffer, sizeof(buffer), rls_fout)) {
 210                unsigned char sha1[20];
 211                if (buffer[0] == '-') {
 212                        write_or_die(bundle_fd, buffer, strlen(buffer));
 213                        if (!get_sha1_hex(buffer + 1, sha1)) {
 214                                struct object *object = parse_object(sha1);
 215                                object->flags |= UNINTERESTING;
 216                                add_pending_object(&revs, object, buffer);
 217                        }
 218                } else if (!get_sha1_hex(buffer, sha1)) {
 219                        struct object *object = parse_object(sha1);
 220                        object->flags |= SHOWN;
 221                }
 222        }
 223        fclose(rls_fout);
 224        if (finish_command(&rls))
 225                return error("rev-list died");
 226
 227        /* write references */
 228        argc = setup_revisions(argc, argv, &revs, NULL);
 229        if (argc > 1)
 230                return error("unrecognized argument: %s'", argv[1]);
 231
 232        for (i = 0; i < revs.pending.nr; i++) {
 233                struct object_array_entry *e = revs.pending.objects + i;
 234                unsigned char sha1[20];
 235                char *ref;
 236                const char *display_ref;
 237                int flag;
 238
 239                if (e->item->flags & UNINTERESTING)
 240                        continue;
 241                if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
 242                        continue;
 243                if (!resolve_ref(e->name, sha1, 1, &flag))
 244                        flag = 0;
 245                display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
 246
 247                /*
 248                 * Make sure the refs we wrote out is correct; --max-count and
 249                 * other limiting options could have prevented all the tips
 250                 * from getting output.
 251                 *
 252                 * Non commit objects such as tags and blobs do not have
 253                 * this issue as they are not affected by those extra
 254                 * constraints.
 255                 */
 256                if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
 257                        warning("ref '%s' is excluded by the rev-list options",
 258                                e->name);
 259                        free(ref);
 260                        continue;
 261                }
 262                /*
 263                 * If you run "git bundle create bndl v1.0..v2.0", the
 264                 * name of the positive ref is "v2.0" but that is the
 265                 * commit that is referenced by the tag, and not the tag
 266                 * itself.
 267                 */
 268                if (hashcmp(sha1, e->item->sha1)) {
 269                        /*
 270                         * Is this the positive end of a range expressed
 271                         * in terms of a tag (e.g. v2.0 from the range
 272                         * "v1.0..v2.0")?
 273                         */
 274                        struct commit *one = lookup_commit_reference(sha1);
 275                        struct object *obj;
 276
 277                        if (e->item == &(one->object)) {
 278                                /*
 279                                 * Need to include e->name as an
 280                                 * independent ref to the pack-objects
 281                                 * input, so that the tag is included
 282                                 * in the output; otherwise we would
 283                                 * end up triggering "empty bundle"
 284                                 * error.
 285                                 */
 286                                obj = parse_object(sha1);
 287                                obj->flags |= SHOWN;
 288                                add_pending_object(&revs, obj, e->name);
 289                        }
 290                        free(ref);
 291                        continue;
 292                }
 293
 294                ref_count++;
 295                write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
 296                write_or_die(bundle_fd, " ", 1);
 297                write_or_die(bundle_fd, display_ref, strlen(display_ref));
 298                write_or_die(bundle_fd, "\n", 1);
 299                free(ref);
 300        }
 301        if (!ref_count)
 302                die ("Refusing to create empty bundle.");
 303
 304        /* end header */
 305        write_or_die(bundle_fd, "\n", 1);
 306
 307        /* write pack */
 308        argv_pack[0] = "pack-objects";
 309        argv_pack[1] = "--all-progress";
 310        argv_pack[2] = "--stdout";
 311        argv_pack[3] = "--thin";
 312        argv_pack[4] = NULL;
 313        memset(&rls, 0, sizeof(rls));
 314        rls.argv = argv_pack;
 315        rls.in = -1;
 316        rls.out = bundle_fd;
 317        rls.git_cmd = 1;
 318        if (start_command(&rls))
 319                return error("Could not spawn pack-objects");
 320
 321        /*
 322         * start_command closed bundle_fd if it was > 1
 323         * so set the lock fd to -1 so commit_lock_file()
 324         * won't fail trying to close it.
 325         */
 326        lock.fd = -1;
 327
 328        for (i = 0; i < revs.pending.nr; i++) {
 329                struct object *object = revs.pending.objects[i].item;
 330                if (object->flags & UNINTERESTING)
 331                        write_or_die(rls.in, "^", 1);
 332                write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
 333                write_or_die(rls.in, "\n", 1);
 334        }
 335        if (finish_command(&rls))
 336                return error ("pack-objects died");
 337
 338        return bundle_to_stdout ? close(bundle_fd) : commit_lock_file(&lock);
 339}
 340
 341int unbundle(struct bundle_header *header, int bundle_fd)
 342{
 343        const char *argv_index_pack[] = {"index-pack",
 344                "--fix-thin", "--stdin", NULL};
 345        struct child_process ip;
 346
 347        if (verify_bundle(header, 0))
 348                return -1;
 349        memset(&ip, 0, sizeof(ip));
 350        ip.argv = argv_index_pack;
 351        ip.in = bundle_fd;
 352        ip.no_stdout = 1;
 353        ip.git_cmd = 1;
 354        if (run_command(&ip))
 355                return error("index-pack died");
 356        return 0;
 357}