--- /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 56BED431FAF\r
+ for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:32 -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 sKa5cwRN89ih for <notmuch@notmuchmail.org>;\r
+ Mon, 26 Mar 2012 14:04:30 -0700 (PDT)\r
+Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
+ [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 51A6D431FBC\r
+ for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:30 -0700 (PDT)\r
+Received: by mail-lb0-f181.google.com with SMTP id k6so4671094lbo.26\r
+ for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:29 -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=NbxBC2Q6wsUiVSAxHgQmlCZt5DEP72f9wsAhbaQEXU4=;\r
+ b=NUzT/SswmIKb1xfmTlGUjSwI8bxgFC5Gulz0/CettLlKNbi9YGNHGhaxqgveSo4Y4d\r
+ URZkCaSIWry+HNG4MH5km+BiekLR0RwjNJNMeZIo/ScZbkvMe9FH38LG6pTvzhHy/USe\r
+ OCaw74x0lSBCtnfYx56JwJRgE9gX+xnw9hpDKN0LGmkQtsqKn3mwttCeQ53j/RLNHYjh\r
+ fS0zsM87FyFfYUFi5o5dkaAz+HJgkjX4WXzVuEzroeMQEWpaCaZzpn29lu0f3Fro0fj8\r
+ 7Ob85ZGXdwx8kI/T78NdEBfUsbtqCbfDERm7rj5YCu1GIkJMeqmHyVWDKx3dhMQ9cd8O\r
+ LaCQ==\r
+Received: by 10.152.127.136 with SMTP id ng8mr17304075lab.16.1332795869902;\r
+ Mon, 26 Mar 2012 14:04:29 -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 cr11sm23301458lbb.0.2012.03.26.14.04.27\r
+ (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 14:04:28 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 4/4] cli: refactor "notmuch restore" message tagging into a\r
+ separate function\r
+Date: Tue, 27 Mar 2012 00:04:11 +0300\r
+Message-Id:\r
+ <62224861c399973dc60f33e616a61b0d641b0cab.1332795415.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
+References: <cover.1332795415.git.jani@nikula.org>\r
+In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
+References: <cover.1332795415.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQlEAVp9ZAewx8KVONkZE1AhzFlvmaL2iqaYkxCagCdKR3lgDvg7hQmNDfvRfQPw3o4+/9wI\r
+Cc: tomi.ollila@iki.fi\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: Mon, 26 Mar 2012 21:04:32 -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-restore.c | 148 ++++++++++++++++++++++++++++-------------------------\r
+ 1 files changed, 78 insertions(+), 70 deletions(-)\r
+\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 87d9772..d3b9246 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -20,6 +20,81 @@\r
+ \r
+ #include "notmuch-client.h"\r
+ \r
++static int\r
++tag_message (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_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ {\r
+@@ -88,11 +163,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 +180,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 (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
+-- \r
+1.7.5.4\r
+\r