clone: print advice on checking out detached HEAD
[git/debian.git] / bundle.c
blobb8acf3c18b600f1f413f95744ad281e3879b3f6e
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"
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);
23 list->nr++;
26 /* Eventually this should go to strbuf.[ch] */
27 static int strbuf_readline_fd(struct strbuf *sb, int fd)
29 strbuf_reset(sb);
31 while (1) {
32 char ch;
33 ssize_t len = xread(fd, &ch, 1);
34 if (len <= 0)
35 return len;
36 strbuf_addch(sb, ch);
37 if (ch == '\n')
38 break;
40 return 0;
43 static int parse_bundle_header(int fd, struct bundle_header *header,
44 const char *report_path)
46 struct strbuf buf = STRBUF_INIT;
47 int status = 0;
49 /* The bundle header begins with the signature */
50 if (strbuf_readline_fd(&buf, fd) ||
51 strcmp(buf.buf, bundle_signature)) {
52 if (report_path)
53 error("'%s' does not look like a v2 bundle file",
54 report_path);
55 status = -1;
56 goto abort;
59 /* The bundle header ends with an empty line */
60 while (!strbuf_readline_fd(&buf, fd) &&
61 buf.len && buf.buf[0] != '\n') {
62 unsigned char sha1[20];
63 int is_prereq = 0;
65 if (*buf.buf == '-') {
66 is_prereq = 1;
67 strbuf_remove(&buf, 0, 1);
69 strbuf_rtrim(&buf);
72 * Tip lines have object name, SP, and refname.
73 * Prerequisites have object name that is optionally
74 * followed by SP and subject line.
76 if (get_sha1_hex(buf.buf, sha1) ||
77 (40 <= buf.len && !isspace(buf.buf[40])) ||
78 (!is_prereq && buf.len <= 40)) {
79 if (report_path)
80 error("unrecognized header: %s%s (%d)",
81 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
82 status = -1;
83 break;
84 } else {
85 if (is_prereq)
86 add_to_ref_list(sha1, "", &header->prerequisites);
87 else
88 add_to_ref_list(sha1, buf.buf + 41, &header->references);
92 abort:
93 if (status) {
94 close(fd);
95 fd = -1;
97 strbuf_release(&buf);
98 return fd;
101 int read_bundle_header(const char *path, struct bundle_header *header)
103 int fd = open(path, O_RDONLY);
105 if (fd < 0)
106 return error("could not open '%s'", path);
107 return parse_bundle_header(fd, header, path);
110 int is_bundle(const char *path, int quiet)
112 struct bundle_header header;
113 int fd = open(path, O_RDONLY);
115 if (fd < 0)
116 return 0;
117 memset(&header, 0, sizeof(header));
118 fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
119 if (fd >= 0)
120 close(fd);
121 return (fd >= 0);
124 static int list_refs(struct ref_list *r, int argc, const char **argv)
126 int i;
128 for (i = 0; i < r->nr; i++) {
129 if (argc > 1) {
130 int j;
131 for (j = 1; j < argc; j++)
132 if (!strcmp(r->list[i].name, argv[j]))
133 break;
134 if (j == argc)
135 continue;
137 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
138 r->list[i].name);
140 return 0;
143 #define PREREQ_MARK (1u<<16)
145 int verify_bundle(struct bundle_header *header, int verbose)
148 * Do fast check, then if any prereqs are missing then go line by line
149 * to be verbose about the errors
151 struct ref_list *p = &header->prerequisites;
152 struct rev_info revs;
153 const char *argv[] = {NULL, "--all", NULL};
154 struct object_array refs;
155 struct commit *commit;
156 int i, ret = 0, req_nr;
157 const char *message = "Repository lacks these prerequisite commits:";
159 init_revisions(&revs, NULL);
160 for (i = 0; i < p->nr; i++) {
161 struct ref_list_entry *e = p->list + i;
162 struct object *o = parse_object(e->sha1);
163 if (o) {
164 o->flags |= PREREQ_MARK;
165 add_pending_object(&revs, o, e->name);
166 continue;
168 if (++ret == 1)
169 error("%s", message);
170 error("%s %s", sha1_to_hex(e->sha1), e->name);
172 if (revs.pending.nr != p->nr)
173 return ret;
174 req_nr = revs.pending.nr;
175 setup_revisions(2, argv, &revs, NULL);
177 refs = revs.pending;
178 revs.leak_pending = 1;
180 if (prepare_revision_walk(&revs))
181 die("revision walk setup failed");
183 i = req_nr;
184 while (i && (commit = get_revision(&revs)))
185 if (commit->object.flags & PREREQ_MARK)
186 i--;
188 for (i = 0; i < req_nr; i++)
189 if (!(refs.objects[i].item->flags & SHOWN)) {
190 if (++ret == 1)
191 error("%s", message);
192 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
193 refs.objects[i].name);
196 clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
197 free(refs.objects);
199 if (verbose) {
200 struct ref_list *r;
202 r = &header->references;
203 printf("The bundle contains %d ref%s\n",
204 r->nr, (1 < r->nr) ? "s" : "");
205 list_refs(r, 0, NULL);
206 r = &header->prerequisites;
207 printf("The bundle requires these %d ref%s\n",
208 r->nr, (1 < r->nr) ? "s" : "");
209 list_refs(r, 0, NULL);
211 return ret;
214 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
216 return list_refs(&header->references, argc, argv);
219 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
221 unsigned long size;
222 enum object_type type;
223 char *buf, *line, *lineend;
224 unsigned long date;
226 if (revs->max_age == -1 && revs->min_age == -1)
227 return 1;
229 buf = read_sha1_file(tag->sha1, &type, &size);
230 if (!buf)
231 return 1;
232 line = memmem(buf, size, "\ntagger ", 8);
233 if (!line++)
234 return 1;
235 lineend = memchr(line, buf + size - line, '\n');
236 line = memchr(line, lineend ? lineend - line : buf + size - line, '>');
237 if (!line++)
238 return 1;
239 date = strtoul(line, NULL, 10);
240 free(buf);
241 return (revs->max_age == -1 || revs->max_age < date) &&
242 (revs->min_age == -1 || revs->min_age > date);
245 int create_bundle(struct bundle_header *header, const char *path,
246 int argc, const char **argv)
248 static struct lock_file lock;
249 int bundle_fd = -1;
250 int bundle_to_stdout;
251 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
252 const char **argv_pack = xmalloc(6 * sizeof(const char *));
253 int i, ref_count = 0;
254 char buffer[1024];
255 struct rev_info revs;
256 struct child_process rls;
257 FILE *rls_fout;
259 bundle_to_stdout = !strcmp(path, "-");
260 if (bundle_to_stdout)
261 bundle_fd = 1;
262 else
263 bundle_fd = hold_lock_file_for_update(&lock, path,
264 LOCK_DIE_ON_ERROR);
266 /* write signature */
267 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
269 /* init revs to list objects for pack-objects later */
270 save_commit_buffer = 0;
271 init_revisions(&revs, NULL);
273 /* write prerequisites */
274 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
275 argv_boundary[0] = "rev-list";
276 argv_boundary[1] = "--boundary";
277 argv_boundary[2] = "--pretty=oneline";
278 argv_boundary[argc + 2] = NULL;
279 memset(&rls, 0, sizeof(rls));
280 rls.argv = argv_boundary;
281 rls.out = -1;
282 rls.git_cmd = 1;
283 if (start_command(&rls))
284 return -1;
285 rls_fout = xfdopen(rls.out, "r");
286 while (fgets(buffer, sizeof(buffer), rls_fout)) {
287 unsigned char sha1[20];
288 if (buffer[0] == '-') {
289 write_or_die(bundle_fd, buffer, strlen(buffer));
290 if (!get_sha1_hex(buffer + 1, sha1)) {
291 struct object *object = parse_object(sha1);
292 object->flags |= UNINTERESTING;
293 add_pending_object(&revs, object, buffer);
295 } else if (!get_sha1_hex(buffer, sha1)) {
296 struct object *object = parse_object(sha1);
297 object->flags |= SHOWN;
300 fclose(rls_fout);
301 if (finish_command(&rls))
302 return error("rev-list died");
304 /* write references */
305 argc = setup_revisions(argc, argv, &revs, NULL);
307 if (argc > 1)
308 return error("unrecognized argument: %s'", argv[1]);
310 object_array_remove_duplicates(&revs.pending);
312 for (i = 0; i < revs.pending.nr; i++) {
313 struct object_array_entry *e = revs.pending.objects + i;
314 unsigned char sha1[20];
315 char *ref;
316 const char *display_ref;
317 int flag;
319 if (e->item->flags & UNINTERESTING)
320 continue;
321 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
322 continue;
323 if (read_ref_full(e->name, sha1, 1, &flag))
324 flag = 0;
325 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
327 if (e->item->type == OBJ_TAG &&
328 !is_tag_in_date_range(e->item, &revs)) {
329 e->item->flags |= UNINTERESTING;
330 continue;
334 * Make sure the refs we wrote out is correct; --max-count and
335 * other limiting options could have prevented all the tips
336 * from getting output.
338 * Non commit objects such as tags and blobs do not have
339 * this issue as they are not affected by those extra
340 * constraints.
342 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
343 warning("ref '%s' is excluded by the rev-list options",
344 e->name);
345 free(ref);
346 continue;
349 * If you run "git bundle create bndl v1.0..v2.0", the
350 * name of the positive ref is "v2.0" but that is the
351 * commit that is referenced by the tag, and not the tag
352 * itself.
354 if (hashcmp(sha1, e->item->sha1)) {
356 * Is this the positive end of a range expressed
357 * in terms of a tag (e.g. v2.0 from the range
358 * "v1.0..v2.0")?
360 struct commit *one = lookup_commit_reference(sha1);
361 struct object *obj;
363 if (e->item == &(one->object)) {
365 * Need to include e->name as an
366 * independent ref to the pack-objects
367 * input, so that the tag is included
368 * in the output; otherwise we would
369 * end up triggering "empty bundle"
370 * error.
372 obj = parse_object(sha1);
373 obj->flags |= SHOWN;
374 add_pending_object(&revs, obj, e->name);
376 free(ref);
377 continue;
380 ref_count++;
381 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
382 write_or_die(bundle_fd, " ", 1);
383 write_or_die(bundle_fd, display_ref, strlen(display_ref));
384 write_or_die(bundle_fd, "\n", 1);
385 free(ref);
387 if (!ref_count)
388 die ("Refusing to create empty bundle.");
390 /* end header */
391 write_or_die(bundle_fd, "\n", 1);
393 /* write pack */
394 argv_pack[0] = "pack-objects";
395 argv_pack[1] = "--all-progress-implied";
396 argv_pack[2] = "--stdout";
397 argv_pack[3] = "--thin";
398 argv_pack[4] = "--delta-base-offset";
399 argv_pack[5] = NULL;
400 memset(&rls, 0, sizeof(rls));
401 rls.argv = argv_pack;
402 rls.in = -1;
403 rls.out = bundle_fd;
404 rls.git_cmd = 1;
405 if (start_command(&rls))
406 return error("Could not spawn pack-objects");
409 * start_command closed bundle_fd if it was > 1
410 * so set the lock fd to -1 so commit_lock_file()
411 * won't fail trying to close it.
413 lock.fd = -1;
415 for (i = 0; i < revs.pending.nr; i++) {
416 struct object *object = revs.pending.objects[i].item;
417 if (object->flags & UNINTERESTING)
418 write_or_die(rls.in, "^", 1);
419 write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
420 write_or_die(rls.in, "\n", 1);
422 close(rls.in);
423 if (finish_command(&rls))
424 return error ("pack-objects died");
425 if (!bundle_to_stdout) {
426 if (commit_lock_file(&lock))
427 die_errno("cannot create '%s'", path);
429 return 0;
432 int unbundle(struct bundle_header *header, int bundle_fd, int flags)
434 const char *argv_index_pack[] = {"index-pack",
435 "--fix-thin", "--stdin", NULL, NULL};
436 struct child_process ip;
438 if (flags & BUNDLE_VERBOSE)
439 argv_index_pack[3] = "-v";
441 if (verify_bundle(header, 0))
442 return -1;
443 memset(&ip, 0, sizeof(ip));
444 ip.argv = argv_index_pack;
445 ip.in = bundle_fd;
446 ip.no_stdout = 1;
447 ip.git_cmd = 1;
448 if (run_command(&ip))
449 return error("index-pack died");
450 return 0;