--- /dev/null
+Return-Path: <jani@nikula.org>\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 C42AB431FC9\r
+ for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14:56 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[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 lToymEdGhmG4 for <notmuch@notmuchmail.org>;\r
+ Sat, 24 Mar 2012 09:14:54 -0700 (PDT)\r
+Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com\r
+ [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 0173B431FD4\r
+ for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14:52 -0700 (PDT)\r
+Received: by mail-lpp01m010-f53.google.com with SMTP id c1so3315541lah.26\r
+ for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14:52 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=google.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
+ :in-reply-to:references:x-gm-message-state;\r
+ bh=DNhJ2W0qd3YGQ+F5rtC6xcltgt/nj69p1TcXdjNXmYI=;\r
+ b=b/2vVf+XYo3Vnnbk8FeBaGSc2bMEVkWwQmhApUf1dS/WhVYbsFgd5x4yA8Fmo0EpHo\r
+ hDcrUXbgXq3JsjeLDQn7tJR3iEZff6qrYioiHc8rHUS+oxiafH1+Fzf4p5KJA4grnGKm\r
+ hYHzGMufJmgYw9UaLSlBjC7Aw6/1NcvM1OXZOSiZ5uRDyRRr8AnDQj7sSmhjkpyszsd+\r
+ /gfMPl3rfabc3emECMuZrweBQGowdPNLuITAWaV9LBDmHkummbnr576J8f8pLP5DqYJj\r
+ DOfLW6sizElUGRgEDBInjkXZ2EB22ZYn5aYx5r8ZnwSBz/3S92FBPzUfQXmBY7zQNyee\r
+ NxeQ==\r
+Received: by 10.112.105.15 with SMTP id gi15mr6162628lbb.54.1332605692578;\r
+ Sat, 24 Mar 2012 09:14:52 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
+ [84.248.80.253])\r
+ by mx.google.com with ESMTPS id pw4sm11397961lab.8.2012.03.24.09.14.50\r
+ (version=SSLv3 cipher=OTHER); Sat, 24 Mar 2012 09:14:51 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 3/3] cli: refactor "notmuch restore" message tagging into a\r
+ separate function\r
+Date: Sat, 24 Mar 2012 18:14:37 +0200\r
+Message-Id:\r
+ <5adca8d3701d9d01b8cb67e589ce8516d4df9832.1332604895.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1332604895.git.jani@nikula.org>\r
+References: <cover.1332604895.git.jani@nikula.org>\r
+In-Reply-To: <cover.1332604895.git.jani@nikula.org>\r
+References: <cover.1332604895.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQmA5/y1N8IqeD7PpB2Hhujbj5H2WnUeRxgvjKimzh7cWvWeflMXtNeddrOXysQyjcmUsKpf\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, 24 Mar 2012 16:14:57 -0000\r
+\r
+Refactor to make tagging code easier to reuse in the future. No\r
+functional changes.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-client.h | 5 +++\r
+ notmuch-restore.c | 73 ++-------------------------------------------------\r
+ notmuch-tag.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 3 files changed, 83 insertions(+), 70 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index fa04fa2..7e3deee 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -203,6 +203,11 @@ json_quote_chararray (const void *ctx, const char *str, const size_t len);\r
+ char *\r
+ json_quote_str (const void *ctx, const char *str);\r
+ \r
++int\r
++tag_message (void *ctx, notmuch_database_t *notmuch, const char *message_id,\r
++ char *file_tags, notmuch_bool_t remove_all,\r
++ notmuch_bool_t synchronize_flags);\r
++\r
+ /* notmuch-config.c */\r
+ \r
+ typedef struct _notmuch_config notmuch_config_t;\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 87d9772..bd6b884 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -88,11 +88,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ \r
+ while ((line_len = getline (&line, &line_size, input)) != -1) {\r
+ regmatch_t match[3];\r
+- char *message_id, *file_tags, *tag, *next;\r
+- notmuch_message_t *message = NULL;\r
+- notmuch_status_t status;\r
+- notmuch_tags_t *db_tags;\r
+- char *db_tags_str;\r
++ char *message_id, *file_tags;\r
+ \r
+ chomp_newline (line);\r
+ \r
+@@ -109,72 +105,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ file_tags = xstrndup (line + match[2].rm_so,\r
+ match[2].rm_eo - match[2].rm_so);\r
+ \r
+- status = notmuch_database_find_message (notmuch, message_id, &message);\r
+- if (status || message == NULL) {\r
+- fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",\r
+- message ? "" : "missing ", message_id);\r
+- if (status)\r
+- fprintf (stderr, "%s\n",\r
+- notmuch_status_to_string(status));\r
+- goto NEXT_LINE;\r
+- }\r
+-\r
+- /* In order to detect missing messages, this check/optimization is\r
+- * intentionally done *after* first finding the message. */\r
+- if (accumulate && (file_tags == NULL || *file_tags == '\0'))\r
+- {\r
+- goto NEXT_LINE;\r
+- }\r
+-\r
+- db_tags_str = NULL;\r
+- for (db_tags = notmuch_message_get_tags (message);\r
+- notmuch_tags_valid (db_tags);\r
+- notmuch_tags_move_to_next (db_tags))\r
+- {\r
+- const char *tag = notmuch_tags_get (db_tags);\r
+-\r
+- if (db_tags_str)\r
+- db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);\r
+- else\r
+- db_tags_str = talloc_strdup (message, tag);\r
+- }\r
+-\r
+- if (((file_tags == NULL || *file_tags == '\0') &&\r
+- (db_tags_str == NULL || *db_tags_str == '\0')) ||\r
+- (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))\r
+- {\r
+- goto NEXT_LINE;\r
+- }\r
+-\r
+- notmuch_message_freeze (message);\r
+-\r
+- if (!accumulate)\r
+- notmuch_message_remove_all_tags (message);\r
+-\r
+- next = file_tags;\r
+- while (next) {\r
+- tag = strsep (&next, " ");\r
+- if (*tag == '\0')\r
+- continue;\r
+- status = notmuch_message_add_tag (message, tag);\r
+- if (status) {\r
+- fprintf (stderr,\r
+- "Error applying tag %s to message %s:\n",\r
+- tag, message_id);\r
+- fprintf (stderr, "%s\n",\r
+- notmuch_status_to_string (status));\r
+- }\r
+- }\r
+-\r
+- notmuch_message_thaw (message);\r
+-\r
+- if (synchronize_flags)\r
+- notmuch_message_tags_to_maildir_flags (message);\r
++ tag_message (ctx, notmuch, message_id, file_tags, !accumulate,\r
++ synchronize_flags);\r
+ \r
+- NEXT_LINE:\r
+- if (message)\r
+- notmuch_message_destroy (message);\r
+- message = NULL;\r
+ free (message_id);\r
+ free (file_tags);\r
+ }\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index c4e3f9d..3aeb878 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -161,6 +161,81 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
+ }\r
+ \r
+ int\r
++tag_message (void *ctx, notmuch_database_t *notmuch, const char *message_id,\r
++ char *file_tags, notmuch_bool_t remove_all,\r
++ notmuch_bool_t synchronize_flags)\r
++{\r
++ notmuch_status_t status;\r
++ notmuch_tags_t *db_tags;\r
++ char *db_tags_str;\r
++ notmuch_message_t *message = NULL;\r
++ const char *tag;\r
++ char *next;\r
++ int ret = 0;\r
++\r
++ status = notmuch_database_find_message (notmuch, message_id, &message);\r
++ if (status || message == NULL) {\r
++ fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",\r
++ message ? "" : "missing ", message_id);\r
++ if (status)\r
++ fprintf (stderr, "%s\n", notmuch_status_to_string(status));\r
++ return 1;\r
++ }\r
++\r
++ /* In order to detect missing messages, this check/optimization is\r
++ * intentionally done *after* first finding the message. */\r
++ if (!remove_all && (file_tags == NULL || *file_tags == '\0'))\r
++ goto DONE;\r
++\r
++ db_tags_str = NULL;\r
++ for (db_tags = notmuch_message_get_tags (message);\r
++ notmuch_tags_valid (db_tags);\r
++ notmuch_tags_move_to_next (db_tags)) {\r
++ tag = notmuch_tags_get (db_tags);\r
++\r
++ if (db_tags_str)\r
++ db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);\r
++ else\r
++ db_tags_str = talloc_strdup (message, tag);\r
++ }\r
++\r
++ if (((file_tags == NULL || *file_tags == '\0') &&\r
++ (db_tags_str == NULL || *db_tags_str == '\0')) ||\r
++ (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))\r
++ goto DONE;\r
++\r
++ notmuch_message_freeze (message);\r
++\r
++ if (remove_all)\r
++ notmuch_message_remove_all_tags (message);\r
++\r
++ next = file_tags;\r
++ while (next) {\r
++ tag = strsep (&next, " ");\r
++ if (*tag == '\0')\r
++ continue;\r
++ status = notmuch_message_add_tag (message, tag);\r
++ if (status) {\r
++ fprintf (stderr, "Error applying tag %s to message %s:\n",\r
++ tag, message_id);\r
++ fprintf (stderr, "%s\n", notmuch_status_to_string (status));\r
++ ret = 1;\r
++ }\r
++ }\r
++\r
++ notmuch_message_thaw (message);\r
++\r
++ if (synchronize_flags)\r
++ notmuch_message_tags_to_maildir_flags (message);\r
++\r
++DONE:\r
++ if (message)\r
++ notmuch_message_destroy (message);\r
++\r
++ return ret;\r
++}\r
++\r
++int\r
+ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ {\r
+ tag_operation_t *tag_ops;\r
+-- \r
+1.7.5.4\r
+\r