]> git.ipfire.org Git - thirdparty/git.git/blame - bundle.c
Git 1.7.8-rc2
[thirdparty/git.git] / bundle.c
CommitLineData
30415d50
JS
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"
fa303836 9#include "refs.h"
30415d50
JS
10
11static const char bundle_signature[] = "# v2 git bundle\n";
12
13static 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
e9ee84cf
JH
26/* Eventually this should go to strbuf.[ch] */
27static int strbuf_readline_fd(struct strbuf *sb, int fd)
f3fa1838 28{
e9ee84cf 29 strbuf_reset(sb);
30415d50 30
e9ee84cf
JH
31 while (1) {
32 char ch;
33 ssize_t len = xread(fd, &ch, 1);
34 if (len < 0)
35 return -1;
36 strbuf_addch(sb, ch);
37 if (ch == '\n')
38 break;
30415d50 39 }
e9ee84cf
JH
40 return 0;
41}
42
2727b71f
JH
43static int parse_bundle_header(int fd, struct bundle_header *header,
44 const char *report_path)
f3fa1838 45{
e9ee84cf 46 struct strbuf buf = STRBUF_INIT;
e9ee84cf 47 int status = 0;
30415d50 48
e9ee84cf
JH
49 /* The bundle header begins with the signature */
50 if (strbuf_readline_fd(&buf, fd) ||
51 strcmp(buf.buf, bundle_signature)) {
2727b71f
JH
52 if (report_path)
53 error("'%s' does not look like a v2 bundle file",
54 report_path);
e9ee84cf
JH
55 status = -1;
56 goto abort;
30415d50 57 }
e9ee84cf
JH
58
59 /* The bundle header ends with an empty line */
60 while (!strbuf_readline_fd(&buf, fd) &&
61 buf.len && buf.buf[0] != '\n') {
30415d50 62 unsigned char sha1[20];
e9ee84cf
JH
63 int is_prereq = 0;
64
65 if (*buf.buf == '-') {
66 is_prereq = 1;
67 strbuf_remove(&buf, 0, 1);
30415d50 68 }
e9ee84cf 69 strbuf_rtrim(&buf);
30415d50 70
e9ee84cf
JH
71 /*
72 * Tip lines have object name, SP, and refname.
73 * Prerequisites have object name that is optionally
74 * followed by SP and subject line.
75 */
76 if (get_sha1_hex(buf.buf, sha1) ||
77 (40 <= buf.len && !isspace(buf.buf[40])) ||
78 (!is_prereq && buf.len <= 40)) {
2727b71f
JH
79 if (report_path)
80 error("unrecognized header: %s%s (%d)",
81 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
e9ee84cf
JH
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);
30415d50 89 }
30415d50 90 }
e9ee84cf
JH
91
92 abort:
93 if (status) {
94 close(fd);
95 fd = -1;
30415d50 96 }
e9ee84cf 97 strbuf_release(&buf);
30415d50
JS
98 return fd;
99}
100
2727b71f
JH
101int read_bundle_header(const char *path, struct bundle_header *header)
102{
103 int fd = open(path, O_RDONLY);
104
30415d50
JS
105 if (fd < 0)
106 return error("could not open '%s'", path);
2727b71f
JH
107 return parse_bundle_header(fd, header, path);
108}
109
110int is_bundle(const char *path, int quiet)
111{
112 struct bundle_header header;
113 int fd = open(path, O_RDONLY);
114
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);
30415d50
JS
122}
123
124static int list_refs(struct ref_list *r, int argc, const char **argv)
125{
126 int i;
127
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;
136 }
137 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
138 r->list[i].name);
139 }
140 return 0;
141}
142
143#define PREREQ_MARK (1u<<16)
144
145int verify_bundle(struct bundle_header *header, int verbose)
146{
147 /*
148 * Do fast check, then if any prereqs are missing then go line by line
149 * to be verbose about the errors
150 */
151 struct ref_list *p = &header->prerequisites;
152 struct rev_info revs;
a80aad7b 153 const char *argv[] = {NULL, "--all", NULL};
30415d50
JS
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:";
158
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;
167 }
168 if (++ret == 1)
9db56f71 169 error("%s", message);
30415d50
JS
170 error("%s %s", sha1_to_hex(e->sha1), e->name);
171 }
172 if (revs.pending.nr != p->nr)
173 return ret;
174 req_nr = revs.pending.nr;
175 setup_revisions(2, argv, &revs, NULL);
176
5be78599
RS
177 refs = revs.pending;
178 revs.leak_pending = 1;
30415d50 179
3d51e1b5
MK
180 if (prepare_revision_walk(&revs))
181 die("revision walk setup failed");
30415d50
JS
182
183 i = req_nr;
184 while (i && (commit = get_revision(&revs)))
185 if (commit->object.flags & PREREQ_MARK)
186 i--;
187
188 for (i = 0; i < req_nr; i++)
189 if (!(refs.objects[i].item->flags & SHOWN)) {
190 if (++ret == 1)
9db56f71 191 error("%s", message);
30415d50
JS
192 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
193 refs.objects[i].name);
194 }
195
86a0a408 196 clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
5be78599 197 free(refs.objects);
30415d50
JS
198
199 if (verbose) {
200 struct ref_list *r;
201
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);
210 }
211 return ret;
212}
213
214int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
215{
216 return list_refs(&header->references, argc, argv);
217}
218
c9a42c4a
JS
219static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
220{
221 unsigned long size;
222 enum object_type type;
223 char *buf, *line, *lineend;
224 unsigned long date;
225
226 if (revs->max_age == -1 && revs->min_age == -1)
227 return 1;
228
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);
243}
244
30415d50
JS
245int create_bundle(struct bundle_header *header, const char *path,
246 int argc, const char **argv)
247{
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 *));
787d2a78 252 const char **argv_pack = xmalloc(6 * sizeof(const char *));
30415d50
JS
253 int i, ref_count = 0;
254 char buffer[1024];
255 struct rev_info revs;
256 struct child_process rls;
257 FILE *rls_fout;
258
259 bundle_to_stdout = !strcmp(path, "-");
260 if (bundle_to_stdout)
261 bundle_fd = 1;
262 else
acd3b9ec
JH
263 bundle_fd = hold_lock_file_for_update(&lock, path,
264 LOCK_DIE_ON_ERROR);
30415d50
JS
265
266 /* write signature */
267 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
268
269 /* init revs to list objects for pack-objects later */
270 save_commit_buffer = 0;
271 init_revisions(&revs, NULL);
272
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;
41698375 285 rls_fout = xfdopen(rls.out, "r");
30415d50
JS
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);
294 }
295 } else if (!get_sha1_hex(buffer, sha1)) {
296 struct object *object = parse_object(sha1);
297 object->flags |= SHOWN;
298 }
299 }
300 fclose(rls_fout);
301 if (finish_command(&rls))
302 return error("rev-list died");
303
304 /* write references */
305 argc = setup_revisions(argc, argv, &revs, NULL);
22568f0a 306
8b3dce56
JH
307 if (argc > 1)
308 return error("unrecognized argument: %s'", argv[1]);
30415d50 309
b2a6d1c6
JH
310 object_array_remove_duplicates(&revs.pending);
311
30415d50
JS
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;
fa303836
JH
316 const char *display_ref;
317 int flag;
30415d50
JS
318
319 if (e->item->flags & UNINTERESTING)
320 continue;
321 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
322 continue;
fa303836
JH
323 if (!resolve_ref(e->name, sha1, 1, &flag))
324 flag = 0;
325 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
326
c9a42c4a
JS
327 if (e->item->type == OBJ_TAG &&
328 !is_tag_in_date_range(e->item, &revs)) {
329 e->item->flags |= UNINTERESTING;
330 continue;
331 }
332
30415d50
JS
333 /*
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.
337 *
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.
341 */
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;
347 }
348 /*
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.
353 */
354 if (hashcmp(sha1, e->item->sha1)) {
355 /*
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")?
359 */
360 struct commit *one = lookup_commit_reference(sha1);
361 struct object *obj;
362
363 if (e->item == &(one->object)) {
364 /*
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.
371 */
372 obj = parse_object(sha1);
373 obj->flags |= SHOWN;
374 add_pending_object(&revs, obj, e->name);
375 }
376 free(ref);
377 continue;
378 }
379
380 ref_count++;
381 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
382 write_or_die(bundle_fd, " ", 1);
fa303836 383 write_or_die(bundle_fd, display_ref, strlen(display_ref));
30415d50
JS
384 write_or_die(bundle_fd, "\n", 1);
385 free(ref);
386 }
387 if (!ref_count)
388 die ("Refusing to create empty bundle.");
389
390 /* end header */
391 write_or_die(bundle_fd, "\n", 1);
392
393 /* write pack */
394 argv_pack[0] = "pack-objects";
4f366275 395 argv_pack[1] = "--all-progress-implied";
30415d50
JS
396 argv_pack[2] = "--stdout";
397 argv_pack[3] = "--thin";
787d2a78
SP
398 argv_pack[4] = "--delta-base-offset";
399 argv_pack[5] = NULL;
30415d50
JS
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");
4ed7cd3a
BC
407
408 /*
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.
412 */
413 lock.fd = -1;
414
30415d50
JS
415 for (i = 0; i < revs.pending.nr; i++) {
416 struct object *object = revs.pending.objects[i].item;
417 if (object->flags & UNINTERESTING)
95693d45
JM
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);
30415d50 421 }
e72ae288 422 close(rls.in);
30415d50
JS
423 if (finish_command(&rls))
424 return error ("pack-objects died");
0f5cdf65
CH
425 if (!bundle_to_stdout) {
426 if (commit_lock_file(&lock))
427 die_errno("cannot create '%s'", path);
428 }
c20181e3 429 return 0;
30415d50
JS
430}
431
be042aff 432int unbundle(struct bundle_header *header, int bundle_fd, int flags)
30415d50
JS
433{
434 const char *argv_index_pack[] = {"index-pack",
be042aff 435 "--fix-thin", "--stdin", NULL, NULL};
30415d50
JS
436 struct child_process ip;
437
be042aff
JH
438 if (flags & BUNDLE_VERBOSE)
439 argv_index_pack[3] = "-v";
440
30415d50
JS
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;
451}