bundle: put strbuf_readline_fd in strbuf.c with adjustments
[git/mingw/4msysgit.git] / bundle.c
blob0542093ee8df41a0e4d80a770b0a84a9b9fc6f3e
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 static int parse_bundle_header(int fd, struct bundle_header *header,
27 const char *report_path)
29 struct strbuf buf = STRBUF_INIT;
30 int status = 0;
32 /* The bundle header begins with the signature */
33 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
34 strcmp(buf.buf, bundle_signature)) {
35 if (report_path)
36 error("'%s' does not look like a v2 bundle file",
37 report_path);
38 status = -1;
39 goto abort;
42 /* The bundle header ends with an empty line */
43 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
44 buf.len && buf.buf[0] != '\n') {
45 unsigned char sha1[20];
46 int is_prereq = 0;
48 if (*buf.buf == '-') {
49 is_prereq = 1;
50 strbuf_remove(&buf, 0, 1);
52 strbuf_rtrim(&buf);
55 * Tip lines have object name, SP, and refname.
56 * Prerequisites have object name that is optionally
57 * followed by SP and subject line.
59 if (get_sha1_hex(buf.buf, sha1) ||
60 (40 <= buf.len && !isspace(buf.buf[40])) ||
61 (!is_prereq && buf.len <= 40)) {
62 if (report_path)
63 error("unrecognized header: %s%s (%d)",
64 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
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);
75 abort:
76 if (status) {
77 close(fd);
78 fd = -1;
80 strbuf_release(&buf);
81 return fd;
84 int read_bundle_header(const char *path, struct bundle_header *header)
86 int fd = open(path, O_RDONLY);
88 if (fd < 0)
89 return error("could not open '%s'", path);
90 return parse_bundle_header(fd, header, path);
93 int is_bundle(const char *path, int quiet)
95 struct bundle_header header;
96 int fd = open(path, O_RDONLY);
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);
107 static int list_refs(struct ref_list *r, int argc, const char **argv)
109 int i;
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;
120 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
121 r->list[i].name);
123 return 0;
126 #define PREREQ_MARK (1u<<16)
128 int verify_bundle(struct bundle_header *header, int verbose)
131 * Do fast check, then if any prereqs are missing then go line by line
132 * to be verbose about the errors
134 struct ref_list *p = &header->prerequisites;
135 struct rev_info revs;
136 const char *argv[] = {NULL, "--all", NULL};
137 struct object_array refs;
138 struct commit *commit;
139 int i, ret = 0, req_nr;
140 const char *message = "Repository lacks these prerequisite commits:";
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;
151 if (++ret == 1)
152 error("%s", message);
153 error("%s %s", sha1_to_hex(e->sha1), e->name);
155 if (revs.pending.nr != p->nr)
156 return ret;
157 req_nr = revs.pending.nr;
158 setup_revisions(2, argv, &revs, NULL);
160 refs = revs.pending;
161 revs.leak_pending = 1;
163 if (prepare_revision_walk(&revs))
164 die("revision walk setup failed");
166 i = req_nr;
167 while (i && (commit = get_revision(&revs)))
168 if (commit->object.flags & PREREQ_MARK)
169 i--;
171 for (i = 0; i < req_nr; i++)
172 if (!(refs.objects[i].item->flags & SHOWN)) {
173 if (++ret == 1)
174 error("%s", message);
175 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
176 refs.objects[i].name);
179 clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
180 free(refs.objects);
182 if (verbose) {
183 struct ref_list *r;
185 r = &header->references;
186 printf("The bundle contains %d ref%s\n",
187 r->nr, (1 < r->nr) ? "s" : "");
188 list_refs(r, 0, NULL);
189 r = &header->prerequisites;
190 printf("The bundle requires these %d ref%s\n",
191 r->nr, (1 < r->nr) ? "s" : "");
192 list_refs(r, 0, NULL);
194 return ret;
197 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
199 return list_refs(&header->references, argc, argv);
202 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
204 unsigned long size;
205 enum object_type type;
206 char *buf, *line, *lineend;
207 unsigned long date;
209 if (revs->max_age == -1 && revs->min_age == -1)
210 return 1;
212 buf = read_sha1_file(tag->sha1, &type, &size);
213 if (!buf)
214 return 1;
215 line = memmem(buf, size, "\ntagger ", 8);
216 if (!line++)
217 return 1;
218 lineend = memchr(line, buf + size - line, '\n');
219 line = memchr(line, lineend ? lineend - line : buf + size - line, '>');
220 if (!line++)
221 return 1;
222 date = strtoul(line, NULL, 10);
223 free(buf);
224 return (revs->max_age == -1 || revs->max_age < date) &&
225 (revs->min_age == -1 || revs->min_age > date);
228 int create_bundle(struct bundle_header *header, const char *path,
229 int argc, const char **argv)
231 static struct lock_file lock;
232 int bundle_fd = -1;
233 int bundle_to_stdout;
234 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
235 const char **argv_pack = xmalloc(6 * sizeof(const char *));
236 int i, ref_count = 0;
237 char buffer[1024];
238 struct rev_info revs;
239 struct child_process rls;
240 FILE *rls_fout;
242 bundle_to_stdout = !strcmp(path, "-");
243 if (bundle_to_stdout)
244 bundle_fd = 1;
245 else
246 bundle_fd = hold_lock_file_for_update(&lock, path,
247 LOCK_DIE_ON_ERROR);
249 /* write signature */
250 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
252 /* init revs to list objects for pack-objects later */
253 save_commit_buffer = 0;
254 init_revisions(&revs, NULL);
256 /* write prerequisites */
257 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
258 argv_boundary[0] = "rev-list";
259 argv_boundary[1] = "--boundary";
260 argv_boundary[2] = "--pretty=oneline";
261 argv_boundary[argc + 2] = NULL;
262 memset(&rls, 0, sizeof(rls));
263 rls.argv = argv_boundary;
264 rls.out = -1;
265 rls.git_cmd = 1;
266 if (start_command(&rls))
267 return -1;
268 rls_fout = xfdopen(rls.out, "r");
269 while (fgets(buffer, sizeof(buffer), rls_fout)) {
270 unsigned char sha1[20];
271 if (buffer[0] == '-') {
272 write_or_die(bundle_fd, buffer, strlen(buffer));
273 if (!get_sha1_hex(buffer + 1, sha1)) {
274 struct object *object = parse_object(sha1);
275 object->flags |= UNINTERESTING;
276 add_pending_object(&revs, object, buffer);
278 } else if (!get_sha1_hex(buffer, sha1)) {
279 struct object *object = parse_object(sha1);
280 object->flags |= SHOWN;
283 fclose(rls_fout);
284 if (finish_command(&rls))
285 return error("rev-list died");
287 /* write references */
288 argc = setup_revisions(argc, argv, &revs, NULL);
290 if (argc > 1)
291 return error("unrecognized argument: %s'", argv[1]);
293 object_array_remove_duplicates(&revs.pending);
295 for (i = 0; i < revs.pending.nr; i++) {
296 struct object_array_entry *e = revs.pending.objects + i;
297 unsigned char sha1[20];
298 char *ref;
299 const char *display_ref;
300 int flag;
302 if (e->item->flags & UNINTERESTING)
303 continue;
304 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
305 continue;
306 if (!resolve_ref(e->name, sha1, 1, &flag))
307 flag = 0;
308 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
310 if (e->item->type == OBJ_TAG &&
311 !is_tag_in_date_range(e->item, &revs)) {
312 e->item->flags |= UNINTERESTING;
313 continue;
317 * Make sure the refs we wrote out is correct; --max-count and
318 * other limiting options could have prevented all the tips
319 * from getting output.
321 * Non commit objects such as tags and blobs do not have
322 * this issue as they are not affected by those extra
323 * constraints.
325 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
326 warning("ref '%s' is excluded by the rev-list options",
327 e->name);
328 free(ref);
329 continue;
332 * If you run "git bundle create bndl v1.0..v2.0", the
333 * name of the positive ref is "v2.0" but that is the
334 * commit that is referenced by the tag, and not the tag
335 * itself.
337 if (hashcmp(sha1, e->item->sha1)) {
339 * Is this the positive end of a range expressed
340 * in terms of a tag (e.g. v2.0 from the range
341 * "v1.0..v2.0")?
343 struct commit *one = lookup_commit_reference(sha1);
344 struct object *obj;
346 if (e->item == &(one->object)) {
348 * Need to include e->name as an
349 * independent ref to the pack-objects
350 * input, so that the tag is included
351 * in the output; otherwise we would
352 * end up triggering "empty bundle"
353 * error.
355 obj = parse_object(sha1);
356 obj->flags |= SHOWN;
357 add_pending_object(&revs, obj, e->name);
359 free(ref);
360 continue;
363 ref_count++;
364 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
365 write_or_die(bundle_fd, " ", 1);
366 write_or_die(bundle_fd, display_ref, strlen(display_ref));
367 write_or_die(bundle_fd, "\n", 1);
368 free(ref);
370 if (!ref_count)
371 die ("Refusing to create empty bundle.");
373 /* end header */
374 write_or_die(bundle_fd, "\n", 1);
376 /* write pack */
377 argv_pack[0] = "pack-objects";
378 argv_pack[1] = "--all-progress-implied";
379 argv_pack[2] = "--stdout";
380 argv_pack[3] = "--thin";
381 argv_pack[4] = "--delta-base-offset";
382 argv_pack[5] = NULL;
383 memset(&rls, 0, sizeof(rls));
384 rls.argv = argv_pack;
385 rls.in = -1;
386 rls.out = bundle_fd;
387 rls.git_cmd = 1;
388 if (start_command(&rls))
389 return error("Could not spawn pack-objects");
392 * start_command closed bundle_fd if it was > 1
393 * so set the lock fd to -1 so commit_lock_file()
394 * won't fail trying to close it.
396 lock.fd = -1;
398 for (i = 0; i < revs.pending.nr; i++) {
399 struct object *object = revs.pending.objects[i].item;
400 if (object->flags & UNINTERESTING)
401 write_or_die(rls.in, "^", 1);
402 write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
403 write_or_die(rls.in, "\n", 1);
405 close(rls.in);
406 if (finish_command(&rls))
407 return error ("pack-objects died");
408 if (!bundle_to_stdout) {
409 if (commit_lock_file(&lock))
410 die_errno("cannot create '%s'", path);
412 return 0;
415 int unbundle(struct bundle_header *header, int bundle_fd, int flags)
417 const char *argv_index_pack[] = {"index-pack",
418 "--fix-thin", "--stdin", NULL, NULL};
419 struct child_process ip;
421 if (flags & BUNDLE_VERBOSE)
422 argv_index_pack[3] = "-v";
424 if (verify_bundle(header, 0))
425 return -1;
426 memset(&ip, 0, sizeof(ip));
427 ip.argv = argv_index_pack;
428 ip.in = bundle_fd;
429 ip.no_stdout = 1;
430 ip.git_cmd = 1;
431 if (run_command(&ip))
432 return error("index-pack died");
433 return 0;