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