[PATCH v2 3/3] cli: refactor "notmuch restore" message tagging into a separate function
authorJani Nikula <jani@nikula.org>
Sun, 25 Mar 2012 19:18:45 +0000 (22:18 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:45:44 +0000 (09:45 -0800)
55/843a9fc41f566f4058e0b3960cb817cc8b6f04 [new file with mode: 0644]

diff --git a/55/843a9fc41f566f4058e0b3960cb817cc8b6f04 b/55/843a9fc41f566f4058e0b3960cb817cc8b6f04
new file mode 100644 (file)
index 0000000..d26530b
--- /dev/null
@@ -0,0 +1,254 @@
+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 9B41B431FBF\r
+       for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:19:04 -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 dmZkproJ7BTN for <notmuch@notmuchmail.org>;\r
+       Sun, 25 Mar 2012 12:19:02 -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 DA772431FC9\r
+       for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:19:00 -0700 (PDT)\r
+Received: by mail-lpp01m010-f53.google.com with SMTP id c1so3755652lah.26\r
+       for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:19:00 -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=AJ9+WRqb66WzxQqfmZhdToi+tz7frzO3owJaCAzw6FY=;\r
+       b=CBPvjNkL/OPI3AyMSoc/IoQECDSH1t8RY+AUJUMFbArba4Znrd7eJyZdOm6UPQ3x2P\r
+       aLR/0jlMZW6kfByLJ8XsSmIcUrv0y48TjfJTagWrOCsJx+tw4RSr1d1cq5Wh6EjjTNAS\r
+       y5nhWxkx8Kx15q6GJtr7dgekYYjvH6NDR2dEhpPhCy6KuCscex7ODrYP5zNcLaMElgJs\r
+       SwpUsrgQJD661NHLIdJ7xByihkaNcimdLRQ/WYoRHz6NZp9Um2F4eVqdSq94mCmExKAx\r
+       0rLKzxZjpl92KIsgur1FLHeZGvnProyKQwFKQ33StTX5OscvIRfYqFyIXcZUBsSze+dF\r
+       nBZQ==\r
+Received: by 10.112.38.170 with SMTP id h10mr4706096lbk.73.1332703140476;\r
+       Sun, 25 Mar 2012 12:19:00 -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 pw4sm14836501lab.8.2012.03.25.12.18.58\r
+       (version=SSLv3 cipher=OTHER); Sun, 25 Mar 2012 12:18:59 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 3/3] cli: refactor "notmuch restore" message tagging into a\r
+       separate function\r
+Date: Sun, 25 Mar 2012 22:18:45 +0300\r
+Message-Id:\r
+ <4e41657c8c748db5b29aaa627eebed5c2618d56a.1332702915.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1332702915.git.jani@nikula.org>\r
+References: <cover.1332702915.git.jani@nikula.org>\r
+In-Reply-To: <cover.1332702915.git.jani@nikula.org>\r
+References: <cover.1332702915.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQk76XxzqWex54pW3ZTr1UbDHnFxFosj+caKd3Gvh8J9ghbLUwcZMw517kjKSqJAgcICe/QF\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, 25 Mar 2012 19:19:04 -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..2e965af 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -21,6 +21,81 @@\r
+ #include "notmuch-client.h"\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_restore_command (unused (void *ctx), int argc, char *argv[])\r
+ {\r
+     notmuch_config_t *config;\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 (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
+-- \r
+1.7.5.4\r
+\r