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"
1011
static const char bundle_signature[] = "# v2 git bundle\n";
1213
static void add_to_ref_list(const unsigned char *sha1, const char *name,
14struct ref_list *list)
15{
16if (list->nr + 1 >= list->alloc) {
17list->alloc = alloc_nr(list->nr + 1);
18list->list = xrealloc(list->list,
19list->alloc * sizeof(list->list[0]));
20}
21memcpy(list->list[list->nr].sha1, sha1, 20);
22list->list[list->nr].name = xstrdup(name);
23list->nr++;
24}
2526
static int parse_bundle_header(int fd, struct bundle_header *header,
27const char *report_path)
28{
29struct strbuf buf = STRBUF_INIT;
30int status = 0;
3132
/* The bundle header begins with the signature */
33if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
34strcmp(buf.buf, bundle_signature)) {
35if (report_path)
36error("'%s' does not look like a v2 bundle file",
37report_path);
38status = -1;
39goto abort;
40}
4142
/* The bundle header ends with an empty line */
43while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
44buf.len && buf.buf[0] != '\n') {
45unsigned char sha1[20];
46int is_prereq = 0;
4748
if (*buf.buf == '-') {
49is_prereq = 1;
50strbuf_remove(&buf, 0, 1);
51}
52strbuf_rtrim(&buf);
5354
/*
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*/
59if (get_sha1_hex(buf.buf, sha1) ||
60(40 <= buf.len && !isspace(buf.buf[40])) ||
61(!is_prereq && buf.len <= 40)) {
62if (report_path)
63error("unrecognized header: %s%s (%d)",
64(is_prereq ? "-" : ""), buf.buf, (int)buf.len);
65status = -1;
66break;
67} else {
68if (is_prereq)
69add_to_ref_list(sha1, "", &header->prerequisites);
70else
71add_to_ref_list(sha1, buf.buf + 41, &header->references);
72}
73}
7475
abort:
76if (status) {
77close(fd);
78fd = -1;
79}
80strbuf_release(&buf);
81return fd;
82}
8384
int read_bundle_header(const char *path, struct bundle_header *header)
85{
86int fd = open(path, O_RDONLY);
8788
if (fd < 0)
89return error("could not open '%s'", path);
90return parse_bundle_header(fd, header, path);
91}
9293
int is_bundle(const char *path, int quiet)
94{
95struct bundle_header header;
96int fd = open(path, O_RDONLY);
9798
if (fd < 0)
99return 0;
100memset(&header, 0, sizeof(header));
101fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
102if (fd >= 0)
103close(fd);
104return (fd >= 0);
105}
106107
static int list_refs(struct ref_list *r, int argc, const char **argv)
108{
109int i;
110111
for (i = 0; i < r->nr; i++) {
112if (argc > 1) {
113int j;
114for (j = 1; j < argc; j++)
115if (!strcmp(r->list[i].name, argv[j]))
116break;
117if (j == argc)
118continue;
119}
120printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
121r->list[i].name);
122}
123return 0;
124}
125126
#define PREREQ_MARK (1u<<16)
127128
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*/
134struct ref_list *p = &header->prerequisites;
135struct rev_info revs;
136const char *argv[] = {NULL, "--all", NULL};
137struct object_array refs;
138struct commit *commit;
139int i, ret = 0, req_nr;
140const char *message = "Repository lacks these prerequisite commits:";
141142
init_revisions(&revs, NULL);
143for (i = 0; i < p->nr; i++) {
144struct ref_list_entry *e = p->list + i;
145struct object *o = parse_object(e->sha1);
146if (o) {
147o->flags |= PREREQ_MARK;
148add_pending_object(&revs, o, e->name);
149continue;
150}
151if (++ret == 1)
152error("%s", message);
153error("%s %s", sha1_to_hex(e->sha1), e->name);
154}
155if (revs.pending.nr != p->nr)
156return ret;
157req_nr = revs.pending.nr;
158setup_revisions(2, argv, &revs, NULL);
159160
refs = revs.pending;
161revs.leak_pending = 1;
162163
if (prepare_revision_walk(&revs))
164die("revision walk setup failed");
165166
i = req_nr;
167while (i && (commit = get_revision(&revs)))
168if (commit->object.flags & PREREQ_MARK)
169i--;
170171
for (i = 0; i < req_nr; i++)
172if (!(refs.objects[i].item->flags & SHOWN)) {
173if (++ret == 1)
174error("%s", message);
175error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
176refs.objects[i].name);
177}
178179
clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
180free(refs.objects);
181182
if (verbose) {
183struct ref_list *r;
184185
r = &header->references;
186printf("The bundle contains %d ref%s\n",
187r->nr, (1 < r->nr) ? "s" : "");
188list_refs(r, 0, NULL);
189r = &header->prerequisites;
190printf("The bundle requires these %d ref%s\n",
191r->nr, (1 < r->nr) ? "s" : "");
192list_refs(r, 0, NULL);
193}
194return ret;
195}
196197
int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
198{
199return list_refs(&header->references, argc, argv);
200}
201202
static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
203{
204unsigned long size;
205enum object_type type;
206char *buf, *line, *lineend;
207unsigned long date;
208209
if (revs->max_age == -1 && revs->min_age == -1)
210return 1;
211212
buf = read_sha1_file(tag->sha1, &type, &size);
213if (!buf)
214return 1;
215line = memmem(buf, size, "\ntagger ", 8);
216if (!line++)
217return 1;
218lineend = memchr(line, buf + size - line, '\n');
219line = memchr(line, lineend ? lineend - line : buf + size - line, '>');
220if (!line++)
221return 1;
222date = strtoul(line, NULL, 10);
223free(buf);
224return (revs->max_age == -1 || revs->max_age < date) &&
225(revs->min_age == -1 || revs->min_age > date);
226}
227228
int create_bundle(struct bundle_header *header, const char *path,
229int argc, const char **argv)
230{
231static struct lock_file lock;
232int bundle_fd = -1;
233int bundle_to_stdout;
234const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
235const char **argv_pack = xmalloc(6 * sizeof(const char *));
236int i, ref_count = 0;
237struct strbuf buf = STRBUF_INIT;
238struct rev_info revs;
239struct child_process rls;
240FILE *rls_fout;
241242
bundle_to_stdout = !strcmp(path, "-");
243if (bundle_to_stdout)
244bundle_fd = 1;
245else
246bundle_fd = hold_lock_file_for_update(&lock, path,
247LOCK_DIE_ON_ERROR);
248249
/* write signature */
250write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
251252
/* init revs to list objects for pack-objects later */
253save_commit_buffer = 0;
254init_revisions(&revs, NULL);
255256
/* write prerequisites */
257memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
258argv_boundary[0] = "rev-list";
259argv_boundary[1] = "--boundary";
260argv_boundary[2] = "--pretty=oneline";
261argv_boundary[argc + 2] = NULL;
262memset(&rls, 0, sizeof(rls));
263rls.argv = argv_boundary;
264rls.out = -1;
265rls.git_cmd = 1;
266if (start_command(&rls))
267return -1;
268rls_fout = xfdopen(rls.out, "r");
269while (strbuf_getwholeline(&buf, rls_fout, '\n') != EOF) {
270unsigned char sha1[20];
271if (buf.len > 0 && buf.buf[0] == '-') {
272write_or_die(bundle_fd, buf.buf, buf.len);
273if (!get_sha1_hex(buf.buf + 1, sha1)) {
274struct object *object = parse_object(sha1);
275object->flags |= UNINTERESTING;
276add_pending_object(&revs, object, buf.buf);
277}
278} else if (!get_sha1_hex(buf.buf, sha1)) {
279struct object *object = parse_object(sha1);
280object->flags |= SHOWN;
281}
282}
283strbuf_release(&buf);
284fclose(rls_fout);
285if (finish_command(&rls))
286return error("rev-list died");
287288
/* write references */
289argc = setup_revisions(argc, argv, &revs, NULL);
290291
if (argc > 1)
292return error("unrecognized argument: %s'", argv[1]);
293294
object_array_remove_duplicates(&revs.pending);
295296
for (i = 0; i < revs.pending.nr; i++) {
297struct object_array_entry *e = revs.pending.objects + i;
298unsigned char sha1[20];
299char *ref;
300const char *display_ref;
301int flag;
302303
if (e->item->flags & UNINTERESTING)
304continue;
305if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
306continue;
307if (!resolve_ref(e->name, sha1, 1, &flag))
308flag = 0;
309display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
310311
if (e->item->type == OBJ_TAG &&
312!is_tag_in_date_range(e->item, &revs)) {
313e->item->flags |= UNINTERESTING;
314continue;
315}
316317
/*
318* Make sure the refs we wrote out is correct; --max-count and
319* other limiting options could have prevented all the tips
320* from getting output.
321*
322* Non commit objects such as tags and blobs do not have
323* this issue as they are not affected by those extra
324* constraints.
325*/
326if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
327warning("ref '%s' is excluded by the rev-list options",
328e->name);
329free(ref);
330continue;
331}
332/*
333* If you run "git bundle create bndl v1.0..v2.0", the
334* name of the positive ref is "v2.0" but that is the
335* commit that is referenced by the tag, and not the tag
336* itself.
337*/
338if (hashcmp(sha1, e->item->sha1)) {
339/*
340* Is this the positive end of a range expressed
341* in terms of a tag (e.g. v2.0 from the range
342* "v1.0..v2.0")?
343*/
344struct commit *one = lookup_commit_reference(sha1);
345struct object *obj;
346347
if (e->item == &(one->object)) {
348/*
349* Need to include e->name as an
350* independent ref to the pack-objects
351* input, so that the tag is included
352* in the output; otherwise we would
353* end up triggering "empty bundle"
354* error.
355*/
356obj = parse_object(sha1);
357obj->flags |= SHOWN;
358add_pending_object(&revs, obj, e->name);
359}
360free(ref);
361continue;
362}
363364
ref_count++;
365write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
366write_or_die(bundle_fd, " ", 1);
367write_or_die(bundle_fd, display_ref, strlen(display_ref));
368write_or_die(bundle_fd, "\n", 1);
369free(ref);
370}
371if (!ref_count)
372die ("Refusing to create empty bundle.");
373374
/* end header */
375write_or_die(bundle_fd, "\n", 1);
376377
/* write pack */
378argv_pack[0] = "pack-objects";
379argv_pack[1] = "--all-progress-implied";
380argv_pack[2] = "--stdout";
381argv_pack[3] = "--thin";
382argv_pack[4] = "--delta-base-offset";
383argv_pack[5] = NULL;
384memset(&rls, 0, sizeof(rls));
385rls.argv = argv_pack;
386rls.in = -1;
387rls.out = bundle_fd;
388rls.git_cmd = 1;
389if (start_command(&rls))
390return error("Could not spawn pack-objects");
391392
/*
393* start_command closed bundle_fd if it was > 1
394* so set the lock fd to -1 so commit_lock_file()
395* won't fail trying to close it.
396*/
397lock.fd = -1;
398399
for (i = 0; i < revs.pending.nr; i++) {
400struct object *object = revs.pending.objects[i].item;
401if (object->flags & UNINTERESTING)
402write_or_die(rls.in, "^", 1);
403write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
404write_or_die(rls.in, "\n", 1);
405}
406close(rls.in);
407if (finish_command(&rls))
408return error ("pack-objects died");
409if (!bundle_to_stdout) {
410if (commit_lock_file(&lock))
411die_errno("cannot create '%s'", path);
412}
413return 0;
414}
415416
int unbundle(struct bundle_header *header, int bundle_fd, int flags)
417{
418const char *argv_index_pack[] = {"index-pack",
419"--fix-thin", "--stdin", NULL, NULL};
420struct child_process ip;
421422
if (flags & BUNDLE_VERBOSE)
423argv_index_pack[3] = "-v";
424425
if (verify_bundle(header, 0))
426return -1;
427memset(&ip, 0, sizeof(ip));
428ip.argv = argv_index_pack;
429ip.in = bundle_fd;
430ip.no_stdout = 1;
431ip.git_cmd = 1;
432if (run_command(&ip))
433return error("index-pack died");
434return 0;
435}