--- /dev/null
+Return-Path: <novalazy@gmail.com>\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 1E52D431FCB\r
+ for <notmuch@notmuchmail.org>; Wed, 16 Apr 2014 06:00:51 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] 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 dltdtOkg6xEz for <notmuch@notmuchmail.org>;\r
+ Wed, 16 Apr 2014 06:00:45 -0700 (PDT)\r
+Received: from mail-pb0-f42.google.com (mail-pb0-f42.google.com\r
+ [209.85.160.42]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 5E3A6431FBF\r
+ for <notmuch@notmuchmail.org>; Wed, 16 Apr 2014 06:00:18 -0700 (PDT)\r
+Received: by mail-pb0-f42.google.com with SMTP id rr13so10952745pbb.1\r
+ for <notmuch@notmuchmail.org>; Wed, 16 Apr 2014 06:00:17 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=2nfJSnS2AN9Bu8cnvUWtOuFvcuU+svUvdOi7ZrvXAU4=;\r
+ b=GaNY3R7ByqpLBS/mVLV94PxkbUWev4Txz/L/ZnEWpiRHNuTYZ6ktvUuUUVAz0lKRPq\r
+ 25j356mEaMpu7j+Gbe6+iAzZqDb0tincEXAemXlvKr0FwErYFEKfUIJGuMgRaVegI/wP\r
+ ZE0TvTB6WbgvHlUyNgWMf+nS0QdoYnjqr5Dw32WOmR1vhP091EThyPOgQ5Wb1lKRuuZ2\r
+ Ek1h7y1mji4wIdhEznqJzJ52iuOTK1IGFWHawMKW4Lrhwd84EjUWRgf6T9DQiVRyy0ul\r
+ YXmplnKZp2j2c0ktOPsxuu4r5CF/Xx2Y2+OVrbGSLU86LDF9Bktq+AGxDEfOmoKIql1l\r
+ Et0Q==\r
+X-Received: by 10.68.240.5 with SMTP id vw5mr8230493pbc.113.1397653217736;\r
+ Wed, 16 Apr 2014 06:00:17 -0700 (PDT)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+ [220.233.42.215]) by mx.google.com with ESMTPSA id\r
+ dy7sm111054220pad.9.2014.04.16.06.00.15 for <multiple recipients>\r
+ (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Wed, 16 Apr 2014 06:00:16 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 08/10] cli: add insert --must-index option\r
+Date: Wed, 16 Apr 2014 22:59:23 +1000\r
+Message-Id: <1397653165-15620-9-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.8.4\r
+In-Reply-To: <1397653165-15620-1-git-send-email-novalazy@gmail.com>\r
+References: <1397653165-15620-1-git-send-email-novalazy@gmail.com>\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: Wed, 16 Apr 2014 13:00:51 -0000\r
+\r
+This option causes notmuch insert to fail (with exit code 3) on failure\r
+to index the message, or failure to set the tags on the message, or if\r
+closing (flushing) the database fails. Failure to sync tags to flags\r
+has no effect.\r
+---\r
+ notmuch-insert.c | 57 +++++++++++++++++++++++++++++++++++++++-----------------\r
+ 1 file changed, 40 insertions(+), 17 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 29d82c9..83257f4 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -31,7 +31,8 @@\r
+ enum {\r
+ INSERT_EXIT_SUCCESS = 0,\r
+ INSERT_EXIT_FAILURE = 1,\r
+- INSERT_EXIT_FAILED_WRITE = 2\r
++ INSERT_EXIT_FAILED_WRITE = 2,\r
++ INSERT_EXIT_FAILED_INDEX = 3\r
+ };\r
+ \r
+ static volatile sig_atomic_t interrupted;\r
+@@ -298,13 +299,15 @@ copy_stdin (int fdin, int fdout)\r
+ }\r
+ \r
+ /* Add the specified message file to the notmuch database, applying tags.\r
+- * The file is renamed to encode notmuch tags as maildir flags. */\r
+-static void\r
++ * If synchronize_flags is set then file is renamed to encode notmuch tags as\r
++ * maildir flags. */\r
++static notmuch_bool_t\r
+ add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
+ tag_op_list_t *tag_ops, notmuch_bool_t synchronize_flags)\r
+ {\r
+ notmuch_message_t *message;\r
+ notmuch_status_t status;\r
++ notmuch_status_t sync;\r
+ \r
+ status = notmuch_database_add_message (notmuch, path, &message);\r
+ switch (status) {\r
+@@ -324,23 +327,28 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path,\r
+ case NOTMUCH_STATUS_LAST_STATUS:\r
+ fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n",\r
+ path, notmuch_status_to_string (status));\r
+- return;\r
++ return FALSE;\r
+ }\r
+ \r
+ if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
+ /* Don't change tags of an existing message. */\r
+- if (synchronize_flags) {\r
+- status = notmuch_message_tags_to_maildir_flags (message);\r
+- if (status != NOTMUCH_STATUS_SUCCESS)\r
+- fprintf (stderr, "Error: failed to sync tags to maildir flags\n");\r
+- }\r
++ status = NOTMUCH_STATUS_SUCCESS;\r
+ } else {\r
+- tag_op_flag_t flags = synchronize_flags ? TAG_FLAG_MAILDIR_SYNC : 0;\r
++ status = tag_op_list_apply (message, tag_ops, 0);\r
++ }\r
+ \r
+- tag_op_list_apply (message, tag_ops, flags);\r
++ /* Call notmuch_message_tags_to_maildir_flags directly instead of doing it\r
++ * as part of tag_op_list_apply. For --must-index we want to succeed if\r
++ * tagging succeeds, but disregard whether synchronizing flags fails. */\r
++ if (status == NOTMUCH_STATUS_SUCCESS && synchronize_flags) {\r
++ sync = notmuch_message_tags_to_maildir_flags (message);\r
++ if (sync != NOTMUCH_STATUS_SUCCESS)\r
++ fprintf (stderr, "Error: failed to sync tags to maildir flags\n");\r
+ }\r
+ \r
+ notmuch_message_destroy (message);\r
++\r
++ return (status == NOTMUCH_STATUS_SUCCESS);\r
+ }\r
+ \r
+ static notmuch_bool_t\r
+@@ -400,15 +408,19 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ char *query_string = NULL;\r
+ const char *folder = NULL;\r
+ notmuch_bool_t create_folder = FALSE;\r
++ notmuch_bool_t must_index = FALSE;\r
+ notmuch_bool_t synchronize_flags;\r
+ const char *maildir;\r
+ char *newpath;\r
+ int opt_index;\r
+ unsigned int i;\r
++ notmuch_bool_t indexed;\r
++ notmuch_status_t status;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },\r
++ { NOTMUCH_OPT_BOOLEAN, &must_index, "must-index", 0, 0 },\r
+ { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
+ };\r
+ \r
+@@ -485,12 +497,23 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return INSERT_EXIT_FAILED_WRITE;\r
+ }\r
+ \r
+- /* Add the message to the index.\r
+- * Even if adding the message to the notmuch database fails,\r
+- * the message is on disk and we consider the delivery completed. */\r
+- add_file_to_database (notmuch, newpath, tag_ops,\r
++ /* Add the message to the index. */\r
++ indexed = add_file_to_database (notmuch, newpath, tag_ops,\r
+ synchronize_flags);\r
+ \r
+- notmuch_database_destroy (notmuch);\r
+- return INSERT_EXIT_SUCCESS;\r
++ /* If must_index is FALSE then succeed as the message is on disk.\r
++ * Otherwise message indexing and tagging must succeed, and the database\r
++ * must be flushed. Don't flush the database if there was an earlier\r
++ * error, so as to abandon the transaction (is there a better way?) */\r
++ if (! must_index) {\r
++ notmuch_database_destroy (notmuch);\r
++ return INSERT_EXIT_SUCCESS;\r
++ }\r
++ if (indexed) {\r
++ status = notmuch_database_destroy (notmuch);\r
++ if (status == NOTMUCH_STATUS_SUCCESS)\r
++ return INSERT_EXIT_SUCCESS;\r
++ }\r
++ unlink (newpath);\r
++ return INSERT_EXIT_FAILED_INDEX;\r
+ }\r
+-- \r
+1.8.4\r
+\r