Git 1.7.11-rc1
[git/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
2727b71f
JH
26static int parse_bundle_header(int fd, struct bundle_header *header,
27 const char *report_path)
f3fa1838 28{
e9ee84cf 29 struct strbuf buf = STRBUF_INIT;
e9ee84cf 30 int status = 0;
30415d50 31
e9ee84cf 32 /* The bundle header begins with the signature */
5e8617f5 33 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
e9ee84cf 34 strcmp(buf.buf, bundle_signature)) {
2727b71f 35 if (report_path)
8a1e7eac 36 error(_("'%s' does not look like a v2 bundle file"),
2727b71f 37 report_path);
e9ee84cf
JH
38 status = -1;
39 goto abort;
30415d50 40 }
e9ee84cf
JH
41
42 /* The bundle header ends with an empty line */
5e8617f5 43 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
e9ee84cf 44 buf.len && buf.buf[0] != '\n') {
30415d50 45 unsigned char sha1[20];
e9ee84cf
JH
46 int is_prereq = 0;
47
48 if (*buf.buf == '-') {
49 is_prereq = 1;
50 strbuf_remove(&buf, 0, 1);
30415d50 51 }
e9ee84cf 52 strbuf_rtrim(&buf);
30415d50 53
e9ee84cf
JH
54 /*
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 */
59 if (get_sha1_hex(buf.buf, sha1) ||
60 (40 <= buf.len && !isspace(buf.buf[40])) ||
61 (!is_prereq && buf.len <= 40)) {
2727b71f 62 if (report_path)
8a1e7eac 63 error(_("unrecognized header: %s%s (%d)"),
2727b71f 64 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
e9ee84cf
JH
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);
30415d50 72 }
30415d50 73 }
e9ee84cf
JH
74
75 abort:
76 if (status) {
77 close(fd);
78 fd = -1;
30415d50 79 }
e9ee84cf 80 strbuf_release(&buf);
30415d50
JS
81 return fd;
82}
83
2727b71f
JH
84int read_bundle_header(const char *path, struct bundle_header *header)
85{
86 int fd = open(path, O_RDONLY);
87
30415d50 88 if (fd < 0)
8a1e7eac 89 return error(_("could not open '%s'"), path);
2727b71f
JH
90 return parse_bundle_header(fd, header, path);
91}
92
93int is_bundle(const char *path, int quiet)
94{
95 struct bundle_header header;
96 int fd = open(path, O_RDONLY);
97
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);
30415d50
JS
105}
106
107static int list_refs(struct ref_list *r, int argc, const char **argv)
108{
109 int i;
110
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;
119 }
120 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
121 r->list[i].name);
122 }
123 return 0;
124}
125
126#define PREREQ_MARK (1u<<16)
127
128int 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 */
134 struct ref_list *p = &header->prerequisites;
135 struct rev_info revs;
a80aad7b 136 const char *argv[] = {NULL, "--all", NULL};
30415d50
JS
137 struct object_array refs;
138 struct commit *commit;
139 int i, ret = 0, req_nr;
8a1e7eac 140 const char *message = _("Repository lacks these prerequisite commits:");
30415d50
JS
141
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;
150 }
151 if (++ret == 1)
9db56f71 152 error("%s", message);
30415d50
JS
153 error("%s %s", sha1_to_hex(e->sha1), e->name);
154 }
155 if (revs.pending.nr != p->nr)
156 return ret;
157 req_nr = revs.pending.nr;
158 setup_revisions(2, argv, &revs, NULL);
159
5be78599
RS
160 refs = revs.pending;
161 revs.leak_pending = 1;
30415d50 162
3d51e1b5 163 if (prepare_revision_walk(&revs))
8a1e7eac 164 die(_("revision walk setup failed"));
30415d50
JS
165
166 i = req_nr;
167 while (i && (commit = get_revision(&revs)))
168 if (commit->object.flags & PREREQ_MARK)
169 i--;
170
171 for (i = 0; i < req_nr; i++)
172 if (!(refs.objects[i].item->flags & SHOWN)) {
173 if (++ret == 1)
9db56f71 174 error("%s", message);
30415d50
JS
175 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
176 refs.objects[i].name);
177 }
178
86a0a408 179 clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
5be78599 180 free(refs.objects);
30415d50
JS
181
182 if (verbose) {
183 struct ref_list *r;
184
185 r = &header->references;
8a1e7eac
NTND
186 printf_ln(Q_("The bundle contains %d ref",
187 "The bundle contains %d refs",
188 r->nr),
189 r->nr);
30415d50
JS
190 list_refs(r, 0, NULL);
191 r = &header->prerequisites;
8a1e7eac
NTND
192 printf_ln(Q_("The bundle requires this ref",
193 "The bundle requires these %d refs",
194 r->nr),
195 r->nr);
30415d50
JS
196 list_refs(r, 0, NULL);
197 }
198 return ret;
199}
200
201int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
202{
203 return list_refs(&header->references, argc, argv);
204}
205
c9a42c4a
JS
206static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
207{
208 unsigned long size;
209 enum object_type type;
210 char *buf, *line, *lineend;
211 unsigned long date;
212
213 if (revs->max_age == -1 && revs->min_age == -1)
214 return 1;
215
216 buf = read_sha1_file(tag->sha1, &type, &size);
217 if (!buf)
218 return 1;
219 line = memmem(buf, size, "\ntagger ", 8);
220 if (!line++)
221 return 1;
222 lineend = memchr(line, buf + size - line, '\n');
223 line = memchr(line, lineend ? lineend - line : buf + size - line, '>');
224 if (!line++)
225 return 1;
226 date = strtoul(line, NULL, 10);
227 free(buf);
228 return (revs->max_age == -1 || revs->max_age < date) &&
229 (revs->min_age == -1 || revs->min_age > date);
230}
231
30415d50
JS
232int create_bundle(struct bundle_header *header, const char *path,
233 int argc, const char **argv)
234{
235 static struct lock_file lock;
236 int bundle_fd = -1;
237 int bundle_to_stdout;
238 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
787d2a78 239 const char **argv_pack = xmalloc(6 * sizeof(const char *));
30415d50 240 int i, ref_count = 0;
bc2fed49 241 struct strbuf buf = STRBUF_INIT;
30415d50
JS
242 struct rev_info revs;
243 struct child_process rls;
244 FILE *rls_fout;
245
246 bundle_to_stdout = !strcmp(path, "-");
247 if (bundle_to_stdout)
248 bundle_fd = 1;
249 else
acd3b9ec
JH
250 bundle_fd = hold_lock_file_for_update(&lock, path,
251 LOCK_DIE_ON_ERROR);
30415d50
JS
252
253 /* write signature */
254 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
255
256 /* init revs to list objects for pack-objects later */
257 save_commit_buffer = 0;
258 init_revisions(&revs, NULL);
259
260 /* write prerequisites */
261 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
262 argv_boundary[0] = "rev-list";
263 argv_boundary[1] = "--boundary";
264 argv_boundary[2] = "--pretty=oneline";
265 argv_boundary[argc + 2] = NULL;
266 memset(&rls, 0, sizeof(rls));
267 rls.argv = argv_boundary;
268 rls.out = -1;
269 rls.git_cmd = 1;
270 if (start_command(&rls))
271 return -1;
41698375 272 rls_fout = xfdopen(rls.out, "r");
bc2fed49 273 while (strbuf_getwholeline(&buf, rls_fout, '\n') != EOF) {
30415d50 274 unsigned char sha1[20];
bc2fed49
TR
275 if (buf.len > 0 && buf.buf[0] == '-') {
276 write_or_die(bundle_fd, buf.buf, buf.len);
277 if (!get_sha1_hex(buf.buf + 1, sha1)) {
30415d50
JS
278 struct object *object = parse_object(sha1);
279 object->flags |= UNINTERESTING;
efe4be12 280 add_pending_object(&revs, object, xstrdup(buf.buf));
30415d50 281 }
bc2fed49 282 } else if (!get_sha1_hex(buf.buf, sha1)) {
30415d50
JS
283 struct object *object = parse_object(sha1);
284 object->flags |= SHOWN;
285 }
286 }
bc2fed49 287 strbuf_release(&buf);
30415d50
JS
288 fclose(rls_fout);
289 if (finish_command(&rls))
8a1e7eac 290 return error(_("rev-list died"));
30415d50
JS
291
292 /* write references */
293 argc = setup_revisions(argc, argv, &revs, NULL);
22568f0a 294
8b3dce56 295 if (argc > 1)
1be65eda 296 return error(_("unrecognized argument: %s"), argv[1]);
30415d50 297
b2a6d1c6
JH
298 object_array_remove_duplicates(&revs.pending);
299
30415d50
JS
300 for (i = 0; i < revs.pending.nr; i++) {
301 struct object_array_entry *e = revs.pending.objects + i;
302 unsigned char sha1[20];
303 char *ref;
fa303836
JH
304 const char *display_ref;
305 int flag;
30415d50
JS
306
307 if (e->item->flags & UNINTERESTING)
308 continue;
309 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
310 continue;
c6893323 311 if (read_ref_full(e->name, sha1, 1, &flag))
fa303836
JH
312 flag = 0;
313 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
314
c9a42c4a
JS
315 if (e->item->type == OBJ_TAG &&
316 !is_tag_in_date_range(e->item, &revs)) {
317 e->item->flags |= UNINTERESTING;
318 continue;
319 }
320
30415d50
JS
321 /*
322 * Make sure the refs we wrote out is correct; --max-count and
323 * other limiting options could have prevented all the tips
324 * from getting output.
325 *
326 * Non commit objects such as tags and blobs do not have
327 * this issue as they are not affected by those extra
328 * constraints.
329 */
330 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
8a1e7eac 331 warning(_("ref '%s' is excluded by the rev-list options"),
30415d50
JS
332 e->name);
333 free(ref);
334 continue;
335 }
336 /*
337 * If you run "git bundle create bndl v1.0..v2.0", the
338 * name of the positive ref is "v2.0" but that is the
339 * commit that is referenced by the tag, and not the tag
340 * itself.
341 */
342 if (hashcmp(sha1, e->item->sha1)) {
343 /*
344 * Is this the positive end of a range expressed
345 * in terms of a tag (e.g. v2.0 from the range
346 * "v1.0..v2.0")?
347 */
348 struct commit *one = lookup_commit_reference(sha1);
349 struct object *obj;
350
351 if (e->item == &(one->object)) {
352 /*
353 * Need to include e->name as an
354 * independent ref to the pack-objects
355 * input, so that the tag is included
356 * in the output; otherwise we would
357 * end up triggering "empty bundle"
358 * error.
359 */
360 obj = parse_object(sha1);
361 obj->flags |= SHOWN;
362 add_pending_object(&revs, obj, e->name);
363 }
364 free(ref);
365 continue;
366 }
367
368 ref_count++;
369 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
370 write_or_die(bundle_fd, " ", 1);
fa303836 371 write_or_die(bundle_fd, display_ref, strlen(display_ref));
30415d50
JS
372 write_or_die(bundle_fd, "\n", 1);
373 free(ref);
374 }
375 if (!ref_count)
8a1e7eac 376 die(_("Refusing to create empty bundle."));
30415d50
JS
377
378 /* end header */
379 write_or_die(bundle_fd, "\n", 1);
380
381 /* write pack */
382 argv_pack[0] = "pack-objects";
4f366275 383 argv_pack[1] = "--all-progress-implied";
30415d50
JS
384 argv_pack[2] = "--stdout";
385 argv_pack[3] = "--thin";
787d2a78
SP
386 argv_pack[4] = "--delta-base-offset";
387 argv_pack[5] = NULL;
30415d50
JS
388 memset(&rls, 0, sizeof(rls));
389 rls.argv = argv_pack;
390 rls.in = -1;
391 rls.out = bundle_fd;
392 rls.git_cmd = 1;
393 if (start_command(&rls))
8a1e7eac 394 return error(_("Could not spawn pack-objects"));
4ed7cd3a
BC
395
396 /*
397 * start_command closed bundle_fd if it was > 1
398 * so set the lock fd to -1 so commit_lock_file()
399 * won't fail trying to close it.
400 */
401 lock.fd = -1;
402
30415d50
JS
403 for (i = 0; i < revs.pending.nr; i++) {
404 struct object *object = revs.pending.objects[i].item;
405 if (object->flags & UNINTERESTING)
95693d45
JM
406 write_or_die(rls.in, "^", 1);
407 write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
408 write_or_die(rls.in, "\n", 1);
30415d50 409 }
e72ae288 410 close(rls.in);
30415d50 411 if (finish_command(&rls))
8a1e7eac 412 return error(_("pack-objects died"));
0f5cdf65
CH
413 if (!bundle_to_stdout) {
414 if (commit_lock_file(&lock))
8a1e7eac 415 die_errno(_("cannot create '%s'"), path);
0f5cdf65 416 }
c20181e3 417 return 0;
30415d50
JS
418}
419
be042aff 420int unbundle(struct bundle_header *header, int bundle_fd, int flags)
30415d50
JS
421{
422 const char *argv_index_pack[] = {"index-pack",
be042aff 423 "--fix-thin", "--stdin", NULL, NULL};
30415d50
JS
424 struct child_process ip;
425
be042aff
JH
426 if (flags & BUNDLE_VERBOSE)
427 argv_index_pack[3] = "-v";
428
30415d50
JS
429 if (verify_bundle(header, 0))
430 return -1;
431 memset(&ip, 0, sizeof(ip));
432 ip.argv = argv_index_pack;
433 ip.in = bundle_fd;
434 ip.no_stdout = 1;
435 ip.git_cmd = 1;
436 if (run_command(&ip))
8a1e7eac 437 return error(_("index-pack died"));
30415d50
JS
438 return 0;
439}