--- /dev/null
+Return-Path: <bremner@tethera.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 123BC6DE1502\r
+ for <notmuch@notmuchmail.org>; Sat, 9 Jan 2016 18:52:34 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.312\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.312 tagged_above=-999 required=5 tests=[AWL=0.239,\r
+ RP_MATCHES_RCVD=-0.55, SPF_PASS=-0.001] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 1dX5cdfp5IBH for <notmuch@notmuchmail.org>;\r
+ Sat, 9 Jan 2016 18:52:32 -0800 (PST)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id DD7476DE028A\r
+ for <notmuch@notmuchmail.org>; Sat, 9 Jan 2016 18:52:00 -0800 (PST)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1aI66h-0007EQ-De; Sat, 09 Jan 2016 21:51:51 -0500\r
+Received: (nullmailer pid 29661 invoked by uid 1000);\r
+ Sun, 10 Jan 2016 02:51:47 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [WIP patch 9/9] CLI: add optional metadata to dump output.\r
+Date: Sat, 9 Jan 2016 22:51:41 -0400\r
+Message-Id: <1452394301-29499-10-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.6.4\r
+In-Reply-To: <1452394301-29499-1-git-send-email-david@tethera.net>\r
+References: <1452394301-29499-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 10 Jan 2016 02:52:34 -0000\r
+\r
+This lacks at least documentation. Note that it changes the default dump\r
+output format, but doesn't break existing notmuch-restore. It might\r
+break user scripts though.\r
+---\r
+ notmuch-client.h | 6 ++++++\r
+ notmuch-dump.c | 45 ++++++++++++++++++++++++++++++++++++++++++---\r
+ notmuch-new.c | 2 +-\r
+ test/T590-metadata.sh | 8 ++++++++\r
+ 4 files changed, 57 insertions(+), 4 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 7c9a1ea..8ee6e0e 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -443,11 +443,17 @@ typedef enum dump_formats {\r
+ DUMP_FORMAT_SUP\r
+ } dump_format_t;\r
+ \r
++typedef enum dump_includes {\r
++ DUMP_INCLUDE_TAGS=1,\r
++ DUMP_INCLUDE_METADATA=2,\r
++} dump_include_t;\r
++\r
+ int\r
+ notmuch_database_dump (notmuch_database_t *notmuch,\r
+ const char *output_file_name,\r
+ const char *query_str,\r
+ dump_format_t output_format,\r
++ dump_include_t include,\r
+ notmuch_bool_t gzip_output);\r
+ \r
+ /* If status is non-zero (i.e. error) print appropriate\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index 829781f..a88b5d5 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -23,16 +23,47 @@\r
+ #include "string-util.h"\r
+ #include <zlib.h>\r
+ \r
++static notmuch_status_t\r
++database_dump_metadata(notmuch_database_t *notmuch, gzFile output)\r
++{\r
++ notmuch_metadata_class_t mclass;\r
++ notmuch_metadata_t *meta;\r
++ notmuch_status_t status;\r
++\r
++ for (mclass = NOTMUCH_METADATA_FIRST_CLASS; mclass < NOTMUCH_METADATA_LAST_CLASS; mclass++) {\r
++ status = notmuch_database_get_all_metadata (notmuch, NOTMUCH_METADATA_CONFIG, &meta);\r
++ if (status)\r
++ return status;\r
++\r
++ for (; notmuch_metadata_valid (meta); notmuch_metadata_move_to_next (meta)) {\r
++ /* FIXME hexencode key and values */\r
++ gzprintf(output, "#@ %s %s %s\n",\r
++ notmuch_metadata_prefix_string(mclass),\r
++ notmuch_metadata_key (meta), notmuch_metadata_value (meta));\r
++ }\r
++ notmuch_metadata_destroy (meta);\r
++ }\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
+ \r
+ static int\r
+ database_dump_file (notmuch_database_t *notmuch, gzFile output,\r
+- const char *query_str, int output_format)\r
++ const char *query_str, int output_format, int include)\r
+ {\r
+ notmuch_query_t *query;\r
+ notmuch_messages_t *messages;\r
+ notmuch_message_t *message;\r
+ notmuch_tags_t *tags;\r
+ \r
++ if (include | DUMP_INCLUDE_METADATA) {\r
++ if (print_status_database ("notmuch dump", notmuch,\r
++ database_dump_metadata(notmuch,output)))\r
++ return EXIT_FAILURE;\r
++ }\r
++\r
++ if (! (include & DUMP_INCLUDE_TAGS))\r
++ return EXIT_SUCCESS;\r
++\r
+ if (! query_str)\r
+ query_str = "";\r
+ \r
+@@ -130,6 +161,7 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
+ const char *output_file_name,\r
+ const char *query_str,\r
+ dump_format_t output_format,\r
++ dump_include_t include,\r
+ notmuch_bool_t gzip_output)\r
+ {\r
+ gzFile output = NULL;\r
+@@ -164,7 +196,7 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
+ goto DONE;\r
+ }\r
+ \r
+- ret = database_dump_file (notmuch, output, query_str, output_format);\r
++ ret = database_dump_file (notmuch, output, query_str, output_format, include);\r
+ if (ret) goto DONE;\r
+ \r
+ ret = gzflush (output, Z_FINISH);\r
+@@ -226,6 +258,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
+ int opt_index;\r
+ \r
+ int output_format = DUMP_FORMAT_BATCH_TAG;\r
++ int include = 0;\r
+ notmuch_bool_t gzip_output = 0;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+@@ -233,6 +266,9 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
+ (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },\r
+ { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
+ { 0, 0 } } },\r
++ { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'i',\r
++ (notmuch_keyword_t []){ { "metadata", DUMP_INCLUDE_METADATA },\r
++ { "tags", DUMP_INCLUDE_TAGS} } },\r
+ { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },\r
+ { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },\r
+@@ -245,6 +281,9 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
+ \r
+ notmuch_process_shared_options (argv[0]);\r
+ \r
++ if (include == 0)\r
++ include = DUMP_INCLUDE_METADATA | DUMP_INCLUDE_TAGS;\r
++\r
+ if (opt_index < argc) {\r
+ query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);\r
+ if (query_str == NULL) {\r
+@@ -254,7 +293,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
+ }\r
+ \r
+ ret = notmuch_database_dump (notmuch, output_file_name, query_str,\r
+- output_format, gzip_output);\r
++ output_format, include, gzip_output);\r
+ \r
+ notmuch_database_destroy (notmuch);\r
+ \r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index e503776..1f8050d 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -1041,7 +1041,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ }\r
+ \r
+ if (notmuch_database_dump (notmuch, backup_name, "",\r
+- DUMP_FORMAT_BATCH_TAG, TRUE)) {\r
++ DUMP_FORMAT_BATCH_TAG, DUMP_INCLUDE_METADATA | DUMP_INCLUDE_TAGS, TRUE)) {\r
+ fprintf (stderr, "Backup failed. Aborting upgrade.");\r
+ return EXIT_FAILURE;\r
+ }\r
+diff --git a/test/T590-metadata.sh b/test/T590-metadata.sh\r
+index 4d55298..45a49be 100755\r
+--- a/test/T590-metadata.sh\r
++++ b/test/T590-metadata.sh\r
+@@ -77,4 +77,12 @@ val = testvalue2\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
++test_begin_subtest "dump metadata"\r
++notmuch dump --include=metadata >OUTPUT\r
++cat <<'EOF' >EXPECTED\r
++#@ C testkey1 testvalue1\r
++#@ C testkey2 testvalue2\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
+ test_done\r
+-- \r
+2.6.4\r
+\r