1 /* export mbox from navymail store
2 * Copyright (C) 2011 Kirill Smelkov <kirr@navytux.spb.ru>
4 * This program is free software: you can Use, Study, Modify and Redistribute it
5 * under the terms of the GNU General Public License version 2. This program is
6 * distributed WITHOUT ANY WARRANTY. See COPYING file for full License terms.
9 * NOTE by default we export messages in reverse chronological order, because it
10 * is faster, and because it is what people are usually interested in.
12 * messages can be exported in original order with --original-order.
15 #include "git-compat-util.h"
16 #include "parse-options.h"
18 #include "navymail/navymail.h"
19 #include "navymail/gbox-walk.h"
21 /* XXX better naming for gbox */
22 static const char * const export_usage
[] = {
23 "navymail export [--original-order] <gbox>",
27 static int original_order
;
29 static const struct option export_options
[] = {
30 OPT_BOOLEAN( 0, "original-order", &original_order
, "export messages in original order"),
35 static void cat_blob(const unsigned char *blob_sha1
, const unsigned char *tree_sha1
)
37 enum object_type type
;
41 buf
= read_sha1_file(blob_sha1
, &type
, &size
);
43 die("navymail export: corrupt tree %s", sha1_to_hex(tree_sha1
));
45 die("navymail export: corrupt tree %s (got %s instead of %s)",
46 sha1_to_hex(tree_sha1
),
47 typename(type
), typename(OBJ_BLOB
));
49 write_or_die(1, buf
, size
);
53 static int export_gbox(const char *gbox
)
55 struct gbox_walk gbox_walk
= { .gbox
= gbox
, .original_order
= original_order
};
56 const unsigned char *sha1
;
58 prepare_gbox_walk(&gbox_walk
);
60 while ((sha1
= next_gbox_entry(&gbox_walk
)))
61 cat_blob(sha1
, gbox_walk
.tree
->object
.sha1
);
67 int cmd_export(int argc
, const char **argv
, const char *prefix
)
70 struct strbuf gbox_ref
= STRBUF_INIT
;
73 argc
= parse_options(argc
, argv
, NULL
/*prefix?*/, export_options
, export_usage
, 0);
75 /* git_config(git_default_config, NULL); ? */
78 usage_with_options(export_usage
, export_options
);
81 strbuf_addf(&gbox_ref
, "refs/mail/%s", gbox
); /* XXX don't hardcode mail/ here */
82 ret
= export_gbox(gbox_ref
.buf
); /* XXX or pass strbuf directly? */
83 strbuf_release(&gbox_ref
);