--- /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 13B0F6DE0362\r
+ for <notmuch@notmuchmail.org>; Sat, 30 Apr 2016 18:24:56 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.015\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.015 tagged_above=-999 required=5\r
+ tests=[AWL=-0.004, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
+ 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 kCCEivaTvHoA for <notmuch@notmuchmail.org>;\r
+ Sat, 30 Apr 2016 18:24:48 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id EE39E6DE026C\r
+ for <notmuch@notmuchmail.org>; Sat, 30 Apr 2016 18:24:47 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1awg7m-0006bY-PT; Sat, 30 Apr 2016 21:24:42 -0400\r
+Received: (nullmailer pid 29955 invoked by uid 1000);\r
+ Sun, 01 May 2016 01:24:43 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v3 08/11] CLI: optionally restore config data.\r
+Date: Sat, 30 Apr 2016 22:24:36 -0300\r
+Message-Id: <1462065879-29860-9-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.8.0.rc3\r
+In-Reply-To: <1462065879-29860-1-git-send-email-david@tethera.net>\r
+References: <1462065879-29860-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, 01 May 2016 01:24:56 -0000\r
+\r
+The default to restore config data seems safest, especially since\r
+currently we have no config data to mess up.\r
+---\r
+ doc/man1/notmuch-restore.rst | 18 +++++++++++++++\r
+ notmuch-restore.c | 53 ++++++++++++++++++++++++++++++++++++++++++++\r
+ test/T590-libconfig.sh | 11 +++++++++\r
+ 3 files changed, 82 insertions(+)\r
+\r
+diff --git a/doc/man1/notmuch-restore.rst b/doc/man1/notmuch-restore.rst\r
+index 362e262..87fa22e 100644\r
+--- a/doc/man1/notmuch-restore.rst\r
++++ b/doc/man1/notmuch-restore.rst\r
+@@ -50,6 +50,24 @@ Supported options for **restore** include\r
+ format, this heuristic, based the fact that batch-tag format\r
+ contains no parentheses, should be accurate.\r
+ \r
++ ``--include=(config|tags)``\r
++\r
++ Control what kind of metadata is restored.\r
++\r
++ **config**\r
++\r
++ Restore configuration data to the database. Each configuration line starts\r
++ with "#@ ", followed by a space seperated key-value pair.\r
++ Both key and value are hex encoded if needed.\r
++\r
++ **tags**\r
++\r
++ Output per-message metadata, namely tags. See *format* above\r
++ for more details.\r
++\r
++ The default is to restore both tags and configuration\r
++ information\r
++\r
+ ``--input=``\ <filename>\r
+ Read input from given file instead of stdin.\r
+ \r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 9abc64f..e06fbde 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -24,6 +24,38 @@\r
+ #include "string-util.h"\r
+ #include "zlib-extra.h"\r
+ \r
++static int\r
++process_config_line(notmuch_database_t *notmuch, const char* line){\r
++ const char *key_p, *val_p;\r
++ char *key, *val;\r
++ size_t key_len,val_len;\r
++ const char *delim=" \t\n";\r
++ int ret = EXIT_FAILURE;\r
++\r
++ void *local = talloc_new(NULL);\r
++\r
++ key_p = strtok_len_c(line, delim, &key_len);\r
++ val_p = strtok_len_c(key_p+key_len, delim, &val_len);\r
++\r
++ key = talloc_strndup(local, key_p, key_len);\r
++ val = talloc_strndup(local, val_p, val_len);\r
++ if (hex_decode_inplace (key) != HEX_SUCCESS ||\r
++ hex_decode_inplace (val) != HEX_SUCCESS ) {\r
++ fprintf (stderr, "hex decoding failure on line %s\n", line);\r
++ goto DONE;\r
++ }\r
++\r
++ if (print_status_database ("notmuch restore", notmuch,\r
++ notmuch_database_set_config (notmuch, key, val)))\r
++ goto DONE;\r
++\r
++ ret = EXIT_SUCCESS;\r
++\r
++ DONE:\r
++ talloc_free (local);\r
++ return ret;\r
++}\r
++\r
+ static regex_t regex;\r
+ \r
+ /* Non-zero return indicates an error in retrieving the message,\r
+@@ -137,6 +169,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+ \r
+ int ret = 0;\r
+ int opt_index;\r
++ int include=0;\r
+ int input_format = DUMP_FORMAT_AUTO;\r
+ \r
+ if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+@@ -152,6 +185,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
+ { "sup", DUMP_FORMAT_SUP },\r
+ { 0, 0 } } },\r
++ { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',\r
++ (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },\r
++ { "tags", DUMP_INCLUDE_TAGS} } },\r
++\r
+ { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &accumulate, "accumulate", 'a', 0 },\r
+ { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },\r
+@@ -167,6 +204,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+ notmuch_process_shared_options (argv[0]);\r
+ notmuch_exit_if_unmatched_db_uuid (notmuch);\r
+ \r
++ if (include == 0) {\r
++ include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS;\r
++ }\r
++\r
+ name_for_error = input_file_name ? input_file_name : "stdin";\r
+ \r
+ if (! accumulate)\r
+@@ -225,11 +266,23 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+ ret = EXIT_FAILURE;\r
+ goto DONE;\r
+ }\r
++\r
++ if ((include & DUMP_INCLUDE_CONFIG) && line_len >= 2 && line[0] == '#' && line[1] == '@') {\r
++ ret = process_config_line(notmuch, line+2);\r
++ if (ret)\r
++ goto DONE;\r
++ }\r
++\r
+ } while ((line_len == 0) ||\r
+ (line[0] == '#') ||\r
+ /* the cast is safe because we checked about for line_len < 0 */\r
+ (strspn (line, " \t\n") == (unsigned)line_len));\r
+ \r
++ if (! (include & DUMP_INCLUDE_TAGS)) {\r
++ ret = EXIT_SUCCESS;\r
++ goto DONE;\r
++ }\r
++\r
+ char *p;\r
+ for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {\r
+ if (*p == '(')\r
+diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
+index 5ea5300..9c1e566 100755\r
+--- a/test/T590-libconfig.sh\r
++++ b/test/T590-libconfig.sh\r
+@@ -132,4 +132,15 @@ cat <<'EOF' >EXPECTED\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
++test_begin_subtest "restore config"\r
++notmuch dump --include=config >EXPECTED\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ RUN(notmuch_database_set_config (db, "testkey1", "mutatedvalue"));\r
++}\r
++EOF\r
++notmuch restore --include=config <EXPECTED\r
++notmuch dump --include=config >OUTPUT\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
+ test_done\r
+-- \r
+2.8.0.rc3\r
+\r