Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 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" |
Junio C Hamano | fa30383 | 2007-11-22 16:51:18 -0800 | [diff] [blame] | 9 | #include "refs.h" |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 10 | |
| 11 | static const char bundle_signature[] = "# v2 git bundle\n"; |
| 12 | |
| 13 | static 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 */ |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 27 | int read_bundle_header(const char *path, struct bundle_header *header) |
| 28 | { |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 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 | |
Jim Meyering | 872c930 | 2008-01-04 18:37:41 +0100 | [diff] [blame] | 51 | if (len && buffer[len - 1] == '\n') |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 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 | |
| 72 | static 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 | |
| 93 | int 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; |
Nguyễn Thái Ngọc Duy | a80aad7 | 2009-05-21 19:32:44 +1000 | [diff] [blame] | 101 | const char *argv[] = {NULL, "--all", NULL}; |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 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) |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 117 | error("%s", message); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 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 | |
Martin Koegler | 3d51e1b | 2008-02-18 08:31:56 +0100 | [diff] [blame] | 131 | if (prepare_revision_walk(&revs)) |
| 132 | die("revision walk setup failed"); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 133 | |
| 134 | i = req_nr; |
| 135 | while (i && (commit = get_revision(&revs))) |
| 136 | if (commit->object.flags & PREREQ_MARK) |
| 137 | i--; |
| 138 | |
| 139 | for (i = 0; i < req_nr; i++) |
| 140 | if (!(refs.objects[i].item->flags & SHOWN)) { |
| 141 | if (++ret == 1) |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 142 | error("%s", message); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 143 | error("%s %s", sha1_to_hex(refs.objects[i].item->sha1), |
| 144 | refs.objects[i].name); |
| 145 | } |
| 146 | |
| 147 | for (i = 0; i < refs.nr; i++) |
| 148 | clear_commit_marks((struct commit *)refs.objects[i].item, -1); |
| 149 | |
| 150 | if (verbose) { |
| 151 | struct ref_list *r; |
| 152 | |
| 153 | r = &header->references; |
| 154 | printf("The bundle contains %d ref%s\n", |
| 155 | r->nr, (1 < r->nr) ? "s" : ""); |
| 156 | list_refs(r, 0, NULL); |
| 157 | r = &header->prerequisites; |
| 158 | printf("The bundle requires these %d ref%s\n", |
| 159 | r->nr, (1 < r->nr) ? "s" : ""); |
| 160 | list_refs(r, 0, NULL); |
| 161 | } |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | int list_bundle_refs(struct bundle_header *header, int argc, const char **argv) |
| 166 | { |
| 167 | return list_refs(&header->references, argc, argv); |
| 168 | } |
| 169 | |
Johannes Schindelin | c9a42c4 | 2009-01-02 19:08:46 +0100 | [diff] [blame] | 170 | static int is_tag_in_date_range(struct object *tag, struct rev_info *revs) |
| 171 | { |
| 172 | unsigned long size; |
| 173 | enum object_type type; |
| 174 | char *buf, *line, *lineend; |
| 175 | unsigned long date; |
| 176 | |
| 177 | if (revs->max_age == -1 && revs->min_age == -1) |
| 178 | return 1; |
| 179 | |
| 180 | buf = read_sha1_file(tag->sha1, &type, &size); |
| 181 | if (!buf) |
| 182 | return 1; |
| 183 | line = memmem(buf, size, "\ntagger ", 8); |
| 184 | if (!line++) |
| 185 | return 1; |
| 186 | lineend = memchr(line, buf + size - line, '\n'); |
| 187 | line = memchr(line, lineend ? lineend - line : buf + size - line, '>'); |
| 188 | if (!line++) |
| 189 | return 1; |
| 190 | date = strtoul(line, NULL, 10); |
| 191 | free(buf); |
| 192 | return (revs->max_age == -1 || revs->max_age < date) && |
| 193 | (revs->min_age == -1 || revs->min_age > date); |
| 194 | } |
| 195 | |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 196 | int create_bundle(struct bundle_header *header, const char *path, |
| 197 | int argc, const char **argv) |
| 198 | { |
| 199 | static struct lock_file lock; |
| 200 | int bundle_fd = -1; |
| 201 | int bundle_to_stdout; |
| 202 | const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *)); |
| 203 | const char **argv_pack = xmalloc(5 * sizeof(const char *)); |
| 204 | int i, ref_count = 0; |
| 205 | char buffer[1024]; |
| 206 | struct rev_info revs; |
Adam Brewster | 22568f0 | 2008-07-05 17:26:40 -0400 | [diff] [blame] | 207 | int read_from_stdin = 0; |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 208 | struct child_process rls; |
| 209 | FILE *rls_fout; |
| 210 | |
| 211 | bundle_to_stdout = !strcmp(path, "-"); |
| 212 | if (bundle_to_stdout) |
| 213 | bundle_fd = 1; |
| 214 | else |
Junio C Hamano | acd3b9e | 2008-10-17 15:44:39 -0700 | [diff] [blame] | 215 | bundle_fd = hold_lock_file_for_update(&lock, path, |
| 216 | LOCK_DIE_ON_ERROR); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 217 | |
| 218 | /* write signature */ |
| 219 | write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature)); |
| 220 | |
| 221 | /* init revs to list objects for pack-objects later */ |
| 222 | save_commit_buffer = 0; |
| 223 | init_revisions(&revs, NULL); |
| 224 | |
| 225 | /* write prerequisites */ |
| 226 | memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *)); |
| 227 | argv_boundary[0] = "rev-list"; |
| 228 | argv_boundary[1] = "--boundary"; |
| 229 | argv_boundary[2] = "--pretty=oneline"; |
| 230 | argv_boundary[argc + 2] = NULL; |
| 231 | memset(&rls, 0, sizeof(rls)); |
| 232 | rls.argv = argv_boundary; |
| 233 | rls.out = -1; |
| 234 | rls.git_cmd = 1; |
| 235 | if (start_command(&rls)) |
| 236 | return -1; |
| 237 | rls_fout = fdopen(rls.out, "r"); |
| 238 | while (fgets(buffer, sizeof(buffer), rls_fout)) { |
| 239 | unsigned char sha1[20]; |
| 240 | if (buffer[0] == '-') { |
| 241 | write_or_die(bundle_fd, buffer, strlen(buffer)); |
| 242 | if (!get_sha1_hex(buffer + 1, sha1)) { |
| 243 | struct object *object = parse_object(sha1); |
| 244 | object->flags |= UNINTERESTING; |
| 245 | add_pending_object(&revs, object, buffer); |
| 246 | } |
| 247 | } else if (!get_sha1_hex(buffer, sha1)) { |
| 248 | struct object *object = parse_object(sha1); |
| 249 | object->flags |= SHOWN; |
| 250 | } |
| 251 | } |
| 252 | fclose(rls_fout); |
| 253 | if (finish_command(&rls)) |
| 254 | return error("rev-list died"); |
| 255 | |
| 256 | /* write references */ |
| 257 | argc = setup_revisions(argc, argv, &revs, NULL); |
Adam Brewster | 22568f0 | 2008-07-05 17:26:40 -0400 | [diff] [blame] | 258 | |
| 259 | for (i = 1; i < argc; i++) { |
| 260 | if (!strcmp(argv[i], "--stdin")) { |
| 261 | if (read_from_stdin++) |
| 262 | die("--stdin given twice?"); |
| 263 | read_revisions_from_stdin(&revs); |
| 264 | continue; |
| 265 | } |
| 266 | return error("unrecognized argument: %s'", argv[i]); |
| 267 | } |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 268 | |
Junio C Hamano | b2a6d1c | 2009-01-17 22:27:08 -0800 | [diff] [blame] | 269 | object_array_remove_duplicates(&revs.pending); |
| 270 | |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 271 | for (i = 0; i < revs.pending.nr; i++) { |
| 272 | struct object_array_entry *e = revs.pending.objects + i; |
| 273 | unsigned char sha1[20]; |
| 274 | char *ref; |
Junio C Hamano | fa30383 | 2007-11-22 16:51:18 -0800 | [diff] [blame] | 275 | const char *display_ref; |
| 276 | int flag; |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 277 | |
| 278 | if (e->item->flags & UNINTERESTING) |
| 279 | continue; |
| 280 | if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1) |
| 281 | continue; |
Junio C Hamano | fa30383 | 2007-11-22 16:51:18 -0800 | [diff] [blame] | 282 | if (!resolve_ref(e->name, sha1, 1, &flag)) |
| 283 | flag = 0; |
| 284 | display_ref = (flag & REF_ISSYMREF) ? e->name : ref; |
| 285 | |
Johannes Schindelin | c9a42c4 | 2009-01-02 19:08:46 +0100 | [diff] [blame] | 286 | if (e->item->type == OBJ_TAG && |
| 287 | !is_tag_in_date_range(e->item, &revs)) { |
| 288 | e->item->flags |= UNINTERESTING; |
| 289 | continue; |
| 290 | } |
| 291 | |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 292 | /* |
| 293 | * Make sure the refs we wrote out is correct; --max-count and |
| 294 | * other limiting options could have prevented all the tips |
| 295 | * from getting output. |
| 296 | * |
| 297 | * Non commit objects such as tags and blobs do not have |
| 298 | * this issue as they are not affected by those extra |
| 299 | * constraints. |
| 300 | */ |
| 301 | if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) { |
| 302 | warning("ref '%s' is excluded by the rev-list options", |
| 303 | e->name); |
| 304 | free(ref); |
| 305 | continue; |
| 306 | } |
| 307 | /* |
| 308 | * If you run "git bundle create bndl v1.0..v2.0", the |
| 309 | * name of the positive ref is "v2.0" but that is the |
| 310 | * commit that is referenced by the tag, and not the tag |
| 311 | * itself. |
| 312 | */ |
| 313 | if (hashcmp(sha1, e->item->sha1)) { |
| 314 | /* |
| 315 | * Is this the positive end of a range expressed |
| 316 | * in terms of a tag (e.g. v2.0 from the range |
| 317 | * "v1.0..v2.0")? |
| 318 | */ |
| 319 | struct commit *one = lookup_commit_reference(sha1); |
| 320 | struct object *obj; |
| 321 | |
| 322 | if (e->item == &(one->object)) { |
| 323 | /* |
| 324 | * Need to include e->name as an |
| 325 | * independent ref to the pack-objects |
| 326 | * input, so that the tag is included |
| 327 | * in the output; otherwise we would |
| 328 | * end up triggering "empty bundle" |
| 329 | * error. |
| 330 | */ |
| 331 | obj = parse_object(sha1); |
| 332 | obj->flags |= SHOWN; |
| 333 | add_pending_object(&revs, obj, e->name); |
| 334 | } |
| 335 | free(ref); |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | ref_count++; |
| 340 | write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40); |
| 341 | write_or_die(bundle_fd, " ", 1); |
Junio C Hamano | fa30383 | 2007-11-22 16:51:18 -0800 | [diff] [blame] | 342 | write_or_die(bundle_fd, display_ref, strlen(display_ref)); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 343 | write_or_die(bundle_fd, "\n", 1); |
| 344 | free(ref); |
| 345 | } |
| 346 | if (!ref_count) |
| 347 | die ("Refusing to create empty bundle."); |
| 348 | |
| 349 | /* end header */ |
| 350 | write_or_die(bundle_fd, "\n", 1); |
| 351 | |
| 352 | /* write pack */ |
| 353 | argv_pack[0] = "pack-objects"; |
| 354 | argv_pack[1] = "--all-progress"; |
| 355 | argv_pack[2] = "--stdout"; |
| 356 | argv_pack[3] = "--thin"; |
| 357 | argv_pack[4] = NULL; |
| 358 | memset(&rls, 0, sizeof(rls)); |
| 359 | rls.argv = argv_pack; |
| 360 | rls.in = -1; |
| 361 | rls.out = bundle_fd; |
| 362 | rls.git_cmd = 1; |
| 363 | if (start_command(&rls)) |
| 364 | return error("Could not spawn pack-objects"); |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * start_command closed bundle_fd if it was > 1 |
| 368 | * so set the lock fd to -1 so commit_lock_file() |
| 369 | * won't fail trying to close it. |
| 370 | */ |
| 371 | lock.fd = -1; |
| 372 | |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 373 | for (i = 0; i < revs.pending.nr; i++) { |
| 374 | struct object *object = revs.pending.objects[i].item; |
| 375 | if (object->flags & UNINTERESTING) |
Jim Meyering | 95693d4 | 2008-01-10 09:54:25 +0100 | [diff] [blame] | 376 | write_or_die(rls.in, "^", 1); |
| 377 | write_or_die(rls.in, sha1_to_hex(object->sha1), 40); |
| 378 | write_or_die(rls.in, "\n", 1); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 379 | } |
Johannes Sixt | e72ae28 | 2008-02-16 18:36:38 +0100 | [diff] [blame] | 380 | close(rls.in); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 381 | if (finish_command(&rls)) |
| 382 | return error ("pack-objects died"); |
Johannes Sixt | c20181e | 2008-02-21 23:42:56 +0100 | [diff] [blame] | 383 | if (!bundle_to_stdout) |
| 384 | commit_lock_file(&lock); |
| 385 | return 0; |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | int unbundle(struct bundle_header *header, int bundle_fd) |
| 389 | { |
| 390 | const char *argv_index_pack[] = {"index-pack", |
| 391 | "--fix-thin", "--stdin", NULL}; |
| 392 | struct child_process ip; |
| 393 | |
| 394 | if (verify_bundle(header, 0)) |
| 395 | return -1; |
| 396 | memset(&ip, 0, sizeof(ip)); |
| 397 | ip.argv = argv_index_pack; |
| 398 | ip.in = bundle_fd; |
| 399 | ip.no_stdout = 1; |
| 400 | ip.git_cmd = 1; |
| 401 | if (run_command(&ip)) |
| 402 | return error("index-pack died"); |
| 403 | return 0; |
| 404 | } |