--- /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 1CDF1431FCB\r
+ for <notmuch@notmuchmail.org>; Sat, 8 Dec 2012 07:08:20 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ 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 FH7svC+sRZIr for <notmuch@notmuchmail.org>;\r
+ Sat, 8 Dec 2012 07:08:18 -0800 (PST)\r
+Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
+ (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id A09B0431E82\r
+ for <notmuch@notmuchmail.org>; Sat, 8 Dec 2012 07:08:14 -0800 (PST)\r
+Received: from fctnnbsc30w-142167090129.dhcp-dynamic.fibreop.nb.bellaliant.net\r
+ ([142.167.90.129] helo=zancas.localnet)\r
+ by tesseract.cs.unb.ca with esmtpsa\r
+ (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1ThM0j-0005dK-E1; Sat, 08 Dec 2012 11:08:13 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1ThM0d-0005Gf-Vm; Sat, 08 Dec 2012 11:08:08 -0400\r
+From: david@tethera.net\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v4 01/10] notmuch-dump: add --format=(batch-tag|sup)\r
+Date: Sat, 8 Dec 2012 11:07:47 -0400\r
+Message-Id: <1354979276-20099-2-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1354979276-20099-1-git-send-email-david@tethera.net>\r
+References: <1354979276-20099-1-git-send-email-david@tethera.net>\r
+X-Spam_bar: -\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: Sat, 08 Dec 2012 15:08:20 -0000\r
+\r
+From: David Bremner <bremner@debian.org>\r
+\r
+sup is the old format, and remains the default, at least until\r
+restore is converted to parse this format.\r
+\r
+Each line of the batch-tag format is modelled on the syntax of notmuch tag:\r
+- "notmuch tag" is omitted from the front of the line\r
+- The dump format only uses query strings of a single message-id.\r
+- Each space seperated tag/message-id is 'hex-encoded' to remove\r
+ trouble-making characters.\r
+- It is permitted (and will be useful) for there to be no tags before\r
+ the query.\r
+\r
+In particular this format won't have the same problem with e.g. spaces\r
+in message-ids or tags; they will be round-trip-able.\r
+---\r
+ dump-restore-private.h | 13 +++++++++++++\r
+ notmuch-dump.c | 48 ++++++++++++++++++++++++++++++++++++++++++------\r
+ 2 files changed, 55 insertions(+), 6 deletions(-)\r
+ create mode 100644 dump-restore-private.h\r
+\r
+diff --git a/dump-restore-private.h b/dump-restore-private.h\r
+new file mode 100644\r
+index 0000000..896a004\r
+--- /dev/null\r
++++ b/dump-restore-private.h\r
+@@ -0,0 +1,13 @@\r
++#ifndef DUMP_RESTORE_PRIVATE_H\r
++#define DUMP_RESTORE_PRIVATE_H\r
++\r
++#include "hex-escape.h"\r
++#include "command-line-arguments.h"\r
++\r
++typedef enum dump_formats {\r
++ DUMP_FORMAT_AUTO,\r
++ DUMP_FORMAT_BATCH_TAG,\r
++ DUMP_FORMAT_SUP\r
++} dump_format_t;\r
++\r
++#endif\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index 88f598a..d2dad40 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -19,6 +19,7 @@\r
+ */\r
+ \r
+ #include "notmuch-client.h"\r
++#include "dump-restore-private.h"\r
+ \r
+ int\r
+ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
+@@ -43,7 +44,13 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
+ char *output_file_name = NULL;\r
+ int opt_index;\r
+ \r
++ int output_format = DUMP_FORMAT_SUP;\r
++\r
+ notmuch_opt_desc_t options[] = {\r
++ { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',\r
++ (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },\r
++ { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
++ { 0, 0 } } },\r
+ { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },\r
+ { 0, 0, 0, 0, 0 }\r
+ };\r
+@@ -83,27 +90,56 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
+ */\r
+ notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
+ \r
++ char *buffer = NULL;\r
++ size_t buffer_size = 0;\r
++\r
+ for (messages = notmuch_query_search_messages (query);\r
+ notmuch_messages_valid (messages);\r
+ notmuch_messages_move_to_next (messages)) {\r
+ int first = 1;\r
++ const char *message_id;\r
++\r
+ message = notmuch_messages_get (messages);\r
++ message_id = notmuch_message_get_message_id (message);\r
+ \r
+- fprintf (output,\r
+- "%s (", notmuch_message_get_message_id (message));\r
++ if (output_format == DUMP_FORMAT_SUP) {\r
++ fprintf (output, "%s (", message_id);\r
++ }\r
+ \r
+ for (tags = notmuch_message_get_tags (message);\r
+ notmuch_tags_valid (tags);\r
+ notmuch_tags_move_to_next (tags)) {\r
+- if (! first)\r
+- fprintf (output, " ");\r
++ const char *tag_str = notmuch_tags_get (tags);\r
+ \r
+- fprintf (output, "%s", notmuch_tags_get (tags));\r
++ if (! first)\r
++ fputs (" ", output);\r
+ \r
+ first = 0;\r
++\r
++ if (output_format == DUMP_FORMAT_SUP) {\r
++ fputs (tag_str, output);\r
++ } else {\r
++ if (hex_encode (notmuch, tag_str,\r
++ &buffer, &buffer_size) != HEX_SUCCESS) {\r
++ fprintf (stderr, "Error: failed to hex-encode tag %s\n",\r
++ tag_str);\r
++ return 1;\r
++ }\r
++ fprintf (output, "+%s", buffer);\r
++ }\r
+ }\r
+ \r
+- fprintf (output, ")\n");\r
++ if (output_format == DUMP_FORMAT_SUP) {\r
++ fputs (")\n", output);\r
++ } else {\r
++ if (hex_encode (notmuch, message_id,\r
++ &buffer, &buffer_size) != HEX_SUCCESS) {\r
++ fprintf (stderr, "Error: failed to hex-encode msg-id %s\n",\r
++ message_id);\r
++ return 1;\r
++ }\r
++ fprintf (output, " -- id:%s\n", buffer);\r
++ }\r
+ \r
+ notmuch_message_destroy (message);\r
+ }\r
+-- \r
+1.7.10.4\r
+\r