--- /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 olra.theworths.org (Postfix) with ESMTP id C7824429E30\r
+ for <notmuch@notmuchmail.org>; Sun, 18 Dec 2011 05:15:42 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id VxMiOGte2CmW for <notmuch@notmuchmail.org>;\r
+ Sun, 18 Dec 2011 05:15:41 -0800 (PST)\r
+Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 56F27431FB6\r
+ for <notmuch@notmuchmail.org>; Sun, 18 Dec 2011 05:15:41 -0800 (PST)\r
+Received: from zancas.localnet\r
+ (fctnnbsc36w-156034079193.pppoe-dynamic.High-Speed.nb.bellaliant.net\r
+ [156.34.79.193]) (authenticated bits=0)\r
+ by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pBIDFduk017885\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO);\r
+ Sun, 18 Dec 2011 09:15:40 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.77)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1RcGaZ-0008RY-3F; Sun, 18 Dec 2011 09:15:39 -0400\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH Draft 2 8/9] notmuch-restore: auto detect format of backup\r
+Date: Sun, 18 Dec 2011 09:15:10 -0400\r
+Message-Id: <1324214111-32079-9-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.7.3\r
+In-Reply-To: <1324214111-32079-1-git-send-email-david@tethera.net>\r
+References: <id:87sjkmuck7.fsf@gmail.com>\r
+ <1324214111-32079-1-git-send-email-david@tethera.net>\r
+Cc: David Bremner <bremner@debian.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 18 Dec 2011 13:15:43 -0000\r
+\r
+From: David Bremner <bremner@debian.org>\r
+\r
+The simple heuristic relies on the fact that '(' is not part of the\r
+character set used by hex-escape. Since hex-escape is designed to be\r
+OK for pathnames (and shells), this seems like a reasonable\r
+assumption.\r
+\r
+In principle the --format argument to notmuch-restore is notmuch\r
+needed at this point, but it adds literally 5 lines of argument\r
+description, so I left it.\r
+---\r
+ dump-restore-private.h | 5 +++--\r
+ notmuch-restore.c | 45 ++++++++++++++++++++++++++++++++-------------\r
+ 2 files changed, 35 insertions(+), 15 deletions(-)\r
+\r
+diff --git a/dump-restore-private.h b/dump-restore-private.h\r
+index 34a5022..67795e5 100644\r
+--- a/dump-restore-private.h\r
++++ b/dump-restore-private.h\r
+@@ -5,8 +5,9 @@\r
+ #include "command-line-arguments.h"\r
+ \r
+ typedef enum dump_formats {\r
+- DUMP_FORMAT_SUP,\r
+- DUMP_FORMAT_NOTMUCH\r
++ DUMP_FORMAT_AUTO,\r
++ DUMP_FORMAT_NOTMUCH,\r
++ DUMP_FORMAT_SUP\r
+ } dump_format_t;\r
+ \r
+ #endif\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index f618b47..340f31f 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -36,7 +36,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ regex_t regex;\r
+ int rerr;\r
+ int opt_index;\r
+- int input_format = DUMP_FORMAT_SUP;\r
++ int input_format = DUMP_FORMAT_AUTO;\r
+ \r
+ config = notmuch_config_open (ctx, NULL, NULL);\r
+ if (config == NULL)\r
+@@ -50,7 +50,11 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+- FORMAT_DESC (input_format),\r
++ { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',\r
++ (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },\r
++ { "notmuch", DUMP_FORMAT_NOTMUCH },\r
++ { "sup", DUMP_FORMAT_SUP },\r
++ {0, 0} } },\r
+ { NOTMUCH_OPT_POSITION, &input_file_name, 0, 0, 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &accumulate, "accumulate", 'a', 0 },\r
+ { 0, 0, 0, 0, 0 }\r
+@@ -80,17 +84,6 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ return 1;\r
+ }\r
+ \r
+- /* Dump output is one line per message. We match a sequence of\r
+- * non-space characters for the message-id, then one or more\r
+- * spaces, then a list of space-separated tags as a sequence of\r
+- * characters within literal '(' and ')'. */\r
+- if (input_format == DUMP_FORMAT_SUP) {\r
+- if ( xregcomp (®ex,\r
+- "^([^ ]+) \\(([^)]*)\\)$",\r
+- REG_EXTENDED) )\r
+- INTERNAL_ERROR("compile time constant regex failed.");\r
+- }\r
+-\r
+ \r
+ /* These are out here to re-use the buffers with hex_decode */\r
+ \r
+@@ -98,6 +91,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ size_t message_id_size = 0;\r
+ char *tag = NULL;\r
+ size_t tag_size = 0;\r
++ notmuch_bool_t first_line = TRUE;\r
+ \r
+ while ((line_len = getline (&line, &line_size, input)) != -1) {\r
+ regmatch_t match[3];\r
+@@ -109,6 +103,30 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ char *db_tags_str;\r
+ \r
+ chomp_newline (line);\r
++ if (first_line && input_format == DUMP_FORMAT_AUTO) {\r
++ char *p;\r
++\r
++ for (p = line; *p; p++) {\r
++ if (*p == '(')\r
++ input_format = DUMP_FORMAT_SUP;\r
++ }\r
++\r
++ if (input_format == DUMP_FORMAT_AUTO)\r
++ input_format = DUMP_FORMAT_NOTMUCH;\r
++\r
++ }\r
++\r
++ /* sup dump output is one line per message. We match a\r
++ * sequence of non-space characters for the message-id, then\r
++ * one or more spaces, then a list of space-separated tags as\r
++ * a sequence of characters within literal '(' and ')'. */\r
++ if (first_line && input_format == DUMP_FORMAT_SUP) {\r
++ if ( xregcomp (®ex,\r
++ "^([^ ]+) \\(([^)]*)\\)$",\r
++ REG_EXTENDED) )\r
++ INTERNAL_ERROR("compile time constant regex failed.");\r
++ }\r
++\r
+ \r
+ /* Silently ignore blank lines */\r
+ \r
+@@ -141,6 +159,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ file_tags = xstrdup (p);\r
+ }\r
+ \r
++ first_line = FALSE;\r
+ \r
+ status = notmuch_database_find_message (notmuch, message_id, &message);\r
+ if (status || message == NULL) {\r
+-- \r
+1.7.7.3\r
+\r