7 #include "list-objects.h"
8 #include "run-command.h"
11 static const char bundle_signature[] = "# v2 git bundle\n";
13 static void add_to_ref_list(const unsigned char *sha1, const char *name,
14 struct ref_list *list)
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]));
21 memcpy(list->list[list->nr].sha1, sha1, 20);
22 list->list[list->nr].name = xstrdup(name);
26 /* Eventually this should go to strbuf.[ch] */
27 static int strbuf_readline_fd(struct strbuf *sb, int fd)
33 ssize_t len = xread(fd, &ch, 1);
43 int read_bundle_header(const char *path, struct bundle_header *header)
45 struct strbuf buf = STRBUF_INIT;
46 int fd = open(path, O_RDONLY);
50 return error("could not open '%s'", path);
52 /* The bundle header begins with the signature */
53 if (strbuf_readline_fd(&buf, fd) ||
54 strcmp(buf.buf, bundle_signature)) {
55 error("'%s' does not look like a v2 bundle file", path);
60 /* The bundle header ends with an empty line */
61 while (!strbuf_readline_fd(&buf, fd) &&
62 buf.len && buf.buf[0] != '\n') {
63 unsigned char sha1[20];
66 if (*buf.buf == '-') {
68 strbuf_remove(&buf, 0, 1);
73 * Tip lines have object name, SP, and refname.
74 * Prerequisites have object name that is optionally
75 * followed by SP and subject line.
77 if (get_sha1_hex(buf.buf, sha1) ||
78 (40 <= buf.len && !isspace(buf.buf[40])) ||
79 (!is_prereq && buf.len <= 40)) {
80 error("unrecognized header: %s%s (%d)",
81 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
86 add_to_ref_list(sha1, "", &header->prerequisites);
88 add_to_ref_list(sha1, buf.buf + 41, &header->references);
101 static int list_refs(struct ref_list *r, int argc, const char **argv)
105 for (i = 0; i < r->nr; i++) {
108 for (j = 1; j < argc; j++)
109 if (!strcmp(r->list[i].name, argv[j]))
114 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
120 #define PREREQ_MARK (1u<<16)
122 int verify_bundle(struct bundle_header *header, int verbose)
125 * Do fast check, then if any prereqs are missing then go line by line
126 * to be verbose about the errors
128 struct ref_list *p = &header->prerequisites;
129 struct rev_info revs;
130 const char *argv[] = {NULL, "--all", NULL};
131 struct object_array refs;
132 struct commit *commit;
133 int i, ret = 0, req_nr;
134 const char *message = "Repository lacks these prerequisite commits:";
136 init_revisions(&revs, NULL);
137 for (i = 0; i < p->nr; i++) {
138 struct ref_list_entry *e = p->list + i;
139 struct object *o = parse_object(e->sha1);
141 o->flags |= PREREQ_MARK;
142 add_pending_object(&revs, o, e->name);
146 error("%s", message);
147 error("%s %s", sha1_to_hex(e->sha1), e->name);
149 if (revs.pending.nr != p->nr)
151 req_nr = revs.pending.nr;
152 setup_revisions(2, argv, &revs, NULL);
154 memset(&refs, 0, sizeof(struct object_array));
155 for (i = 0; i < revs.pending.nr; i++) {
156 struct object_array_entry *e = revs.pending.objects + i;
157 add_object_array(e->item, e->name, &refs);
160 if (prepare_revision_walk(&revs))
161 die("revision walk setup failed");
164 while (i && (commit = get_revision(&revs)))
165 if (commit->object.flags & PREREQ_MARK)
168 for (i = 0; i < req_nr; i++)
169 if (!(refs.objects[i].item->flags & SHOWN)) {
171 error("%s", message);
172 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
173 refs.objects[i].name);
176 for (i = 0; i < refs.nr; i++)
177 clear_commit_marks((struct commit *)refs.objects[i].item, -1);
182 r = &header->references;
183 printf("The bundle contains %d ref%s\n",
184 r->nr, (1 < r->nr) ? "s" : "");
185 list_refs(r, 0, NULL);
186 r = &header->prerequisites;
187 printf("The bundle requires these %d ref%s\n",
188 r->nr, (1 < r->nr) ? "s" : "");
189 list_refs(r, 0, NULL);
194 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
196 return list_refs(&header->references, argc, argv);
199 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
202 enum object_type type;
203 char *buf, *line, *lineend;
206 if (revs->max_age == -1 && revs->min_age == -1)
209 buf = read_sha1_file(tag->sha1, &type, &size);
212 line = memmem(buf, size, "\ntagger ", 8);
215 lineend = memchr(line, buf + size - line, '\n');
216 line = memchr(line, lineend ? lineend - line : buf + size - line, '>');
219 date = strtoul(line, NULL, 10);
221 return (revs->max_age == -1 || revs->max_age < date) &&
222 (revs->min_age == -1 || revs->min_age > date);
225 int create_bundle(struct bundle_header *header, const char *path,
226 int argc, const char **argv)
228 static struct lock_file lock;
230 int bundle_to_stdout;
231 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
232 const char **argv_pack = xmalloc(6 * sizeof(const char *));
233 int i, ref_count = 0;
235 struct rev_info revs;
236 struct child_process rls;
239 bundle_to_stdout = !strcmp(path, "-");
240 if (bundle_to_stdout)
243 bundle_fd = hold_lock_file_for_update(&lock, path,
246 /* write signature */
247 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
249 /* init revs to list objects for pack-objects later */
250 save_commit_buffer = 0;
251 init_revisions(&revs, NULL);
253 /* write prerequisites */
254 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
255 argv_boundary[0] = "rev-list";
256 argv_boundary[1] = "--boundary";
257 argv_boundary[2] = "--pretty=oneline";
258 argv_boundary[argc + 2] = NULL;
259 memset(&rls, 0, sizeof(rls));
260 rls.argv = argv_boundary;
263 if (start_command(&rls))
265 rls_fout = xfdopen(rls.out, "r");
266 while (fgets(buffer, sizeof(buffer), rls_fout)) {
267 unsigned char sha1[20];
268 if (buffer[0] == '-') {
269 write_or_die(bundle_fd, buffer, strlen(buffer));
270 if (!get_sha1_hex(buffer + 1, sha1)) {
271 struct object *object = parse_object(sha1);
272 object->flags |= UNINTERESTING;
273 add_pending_object(&revs, object, buffer);
275 } else if (!get_sha1_hex(buffer, sha1)) {
276 struct object *object = parse_object(sha1);
277 object->flags |= SHOWN;
281 if (finish_command(&rls))
282 return error("rev-list died");
284 /* write references */
285 argc = setup_revisions(argc, argv, &revs, NULL);
288 return error("unrecognized argument: %s'", argv[1]);
290 object_array_remove_duplicates(&revs.pending);
292 for (i = 0; i < revs.pending.nr; i++) {
293 struct object_array_entry *e = revs.pending.objects + i;
294 unsigned char sha1[20];
296 const char *display_ref;
299 if (e->item->flags & UNINTERESTING)
301 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
303 if (!resolve_ref(e->name, sha1, 1, &flag))
305 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
307 if (e->item->type == OBJ_TAG &&
308 !is_tag_in_date_range(e->item, &revs)) {
309 e->item->flags |= UNINTERESTING;
314 * Make sure the refs we wrote out is correct; --max-count and
315 * other limiting options could have prevented all the tips
316 * from getting output.
318 * Non commit objects such as tags and blobs do not have
319 * this issue as they are not affected by those extra
322 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
323 warning("ref '%s' is excluded by the rev-list options",
329 * If you run "git bundle create bndl v1.0..v2.0", the
330 * name of the positive ref is "v2.0" but that is the
331 * commit that is referenced by the tag, and not the tag
334 if (hashcmp(sha1, e->item->sha1)) {
336 * Is this the positive end of a range expressed
337 * in terms of a tag (e.g. v2.0 from the range
340 struct commit *one = lookup_commit_reference(sha1);
343 if (e->item == &(one->object)) {
345 * Need to include e->name as an
346 * independent ref to the pack-objects
347 * input, so that the tag is included
348 * in the output; otherwise we would
349 * end up triggering "empty bundle"
352 obj = parse_object(sha1);
354 add_pending_object(&revs, obj, e->name);
361 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
362 write_or_die(bundle_fd, " ", 1);
363 write_or_die(bundle_fd, display_ref, strlen(display_ref));
364 write_or_die(bundle_fd, "\n", 1);
368 die ("Refusing to create empty bundle.");
371 write_or_die(bundle_fd, "\n", 1);
374 argv_pack[0] = "pack-objects";
375 argv_pack[1] = "--all-progress-implied";
376 argv_pack[2] = "--stdout";
377 argv_pack[3] = "--thin";
378 argv_pack[4] = "--delta-base-offset";
380 memset(&rls, 0, sizeof(rls));
381 rls.argv = argv_pack;
385 if (start_command(&rls))
386 return error("Could not spawn pack-objects");
389 * start_command closed bundle_fd if it was > 1
390 * so set the lock fd to -1 so commit_lock_file()
391 * won't fail trying to close it.
395 for (i = 0; i < revs.pending.nr; i++) {
396 struct object *object = revs.pending.objects[i].item;
397 if (object->flags & UNINTERESTING)
398 write_or_die(rls.in, "^", 1);
399 write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
400 write_or_die(rls.in, "\n", 1);
403 if (finish_command(&rls))
404 return error ("pack-objects died");
405 if (!bundle_to_stdout) {
406 if (commit_lock_file(&lock))
407 die_errno("cannot create '%s'", path);
412 int unbundle(struct bundle_header *header, int bundle_fd)
414 const char *argv_index_pack[] = {"index-pack",
415 "--fix-thin", "--stdin", NULL};
416 struct child_process ip;
418 if (verify_bundle(header, 0))
420 memset(&ip, 0, sizeof(ip));
421 ip.argv = argv_index_pack;
425 if (run_command(&ip))
426 return error("index-pack died");