[PATCH v3 3/4] cli: refactor "notmuch tag" query tagging into a separate function
authorJani Nikula <jani@nikula.org>
Mon, 26 Mar 2012 21:04:10 +0000 (00:04 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:45:46 +0000 (09:45 -0800)
cc/255cf74386b66c2ee315eeff90082b1200cd74 [new file with mode: 0644]

diff --git a/cc/255cf74386b66c2ee315eeff90082b1200cd74 b/cc/255cf74386b66c2ee315eeff90082b1200cd74
new file mode 100644 (file)
index 0000000..28822c0
--- /dev/null
@@ -0,0 +1,221 @@
+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 1A44E431FBF\r
+       for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:31 -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 mh+LsAt6J0Ju for <notmuch@notmuchmail.org>;\r
+       Mon, 26 Mar 2012 14:04:29 -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 9236A431FAF\r
+       for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:29 -0700 (PDT)\r
+Received: by lbok6 with SMTP id k6so4671094lbo.26\r
+       for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:26 -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=D56dZfuAcdnIg2Ra6n886KLjYTaJlZGih4CRIc3PEgo=;\r
+       b=UYN/18Rdyi5AJcDXnWDB7yY7U/D0TpGIG+Dz2jiux+8ozydFmzeaERdnuughK8QO09\r
+       ivCQglPqtZ8qiGvqv9IOCXDiPsFOWBumKh3A+Uy2xliv2Z+SmTABvic60WFoHlli0Svy\r
+       TvmtxMPckRbcWjLcXrnWqF4PvyZyOCezSKpMAGyDLDZsidyro6zSq9NzdbY0R8B/E3Cm\r
+       WGkXa2Q+n68OjD1LeuQaJXamg9yFBXJgkkXlfrmWY5dluWd9Vy1yWxHVK26GBrl9mRkq\r
+       btWHPPWQvWZRIjai0y2K+2EnNTdqBvaMTR1DSI6ByhAurN5wLJIhTzsF7QxBHZLTnZ/a\r
+       0UBQ==\r
+Received: by 10.152.145.135 with SMTP id su7mr17135117lab.5.1332795866454;\r
+       Mon, 26 Mar 2012 14:04:26 -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 oy17sm18177700lab.7.2012.03.26.14.04.24\r
+       (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 14:04:25 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 3/4] cli: refactor "notmuch tag" query tagging into a\r
+       separate function\r
+Date: Tue, 27 Mar 2012 00:04:10 +0300\r
+Message-Id:\r
+ <9c547e08d098cf75e52e6ecb7f2af2ca75730274.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
+ ALoCoQkeZlT38jroLnKl94fvz8v/POdErNgBl+HjVRSu/Bt1BgoB7/MtdSSSL6kz59jb0GYTNBts\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:31 -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-tag.c |  104 +++++++++++++++++++++++++++++++++------------------------\r
+ 1 files changed, 60 insertions(+), 44 deletions(-)\r
+\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 0a6b140..05feed3 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -110,6 +110,63 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+     return query_string;\r
+ }\r
+ \r
++/* Tag messages matching 'query_string' according to 'tag_ops', which\r
++ * must be an array of tagging operations terminated with an empty\r
++ * element. */\r
++static int\r
++tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
++         tag_operation_t *tag_ops, notmuch_bool_t synchronize_flags)\r
++{\r
++    notmuch_query_t *query;\r
++    notmuch_messages_t *messages;\r
++    notmuch_message_t *message;\r
++    int i;\r
++\r
++    /* Optimize the query so it excludes messages that already have\r
++     * the specified set of tags. */\r
++    query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
++    if (query_string == NULL) {\r
++      fprintf (stderr, "Out of memory.\n");\r
++      return 1;\r
++    }\r
++\r
++    query = notmuch_query_create (notmuch, query_string);\r
++    if (query == NULL) {\r
++      fprintf (stderr, "Out of memory.\n");\r
++      return 1;\r
++    }\r
++\r
++    /* tagging is not interested in any special sort order */\r
++    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
++\r
++    for (messages = notmuch_query_search_messages (query);\r
++       notmuch_messages_valid (messages) && !interrupted;\r
++       notmuch_messages_move_to_next (messages))\r
++    {\r
++      message = notmuch_messages_get (messages);\r
++\r
++      notmuch_message_freeze (message);\r
++\r
++      for (i = 0; tag_ops[i].tag; i++) {\r
++          if (tag_ops[i].remove)\r
++              notmuch_message_remove_tag (message, tag_ops[i].tag);\r
++          else\r
++              notmuch_message_add_tag (message, tag_ops[i].tag);\r
++      }\r
++\r
++      notmuch_message_thaw (message);\r
++\r
++      if (synchronize_flags)\r
++          notmuch_message_tags_to_maildir_flags (message);\r
++\r
++      notmuch_message_destroy (message);\r
++    }\r
++\r
++    notmuch_query_destroy (query);\r
++\r
++    return interrupted;\r
++}\r
++\r
+ int\r
+ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ {\r
+@@ -118,12 +175,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+     char *query_string;\r
+     notmuch_config_t *config;\r
+     notmuch_database_t *notmuch;\r
+-    notmuch_query_t *query;\r
+-    notmuch_messages_t *messages;\r
+-    notmuch_message_t *message;\r
+     struct sigaction action;\r
+     notmuch_bool_t synchronize_flags;\r
+     int i;\r
++    int ret;\r
+ \r
+     /* Setup our handler for SIGINT */\r
+     memset (&action, 0, sizeof (struct sigaction));\r
+@@ -170,14 +225,6 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+       return 1;\r
+     }\r
+ \r
+-    /* Optimize the query so it excludes messages that already have\r
+-     * the specified set of tags. */\r
+-    query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
+-    if (query_string == NULL) {\r
+-      fprintf (stderr, "Out of memory.\n");\r
+-      return 1;\r
+-    }\r
+-\r
+     config = notmuch_config_open (ctx, NULL, NULL);\r
+     if (config == NULL)\r
+       return 1;\r
+@@ -189,40 +236,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ \r
+     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+ \r
+-    query = notmuch_query_create (notmuch, query_string);\r
+-    if (query == NULL) {\r
+-      fprintf (stderr, "Out of memory.\n");\r
+-      return 1;\r
+-    }\r
+-\r
+-    /* tagging is not interested in any special sort order */\r
+-    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
++    ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);\r
+ \r
+-    for (messages = notmuch_query_search_messages (query);\r
+-       notmuch_messages_valid (messages) && !interrupted;\r
+-       notmuch_messages_move_to_next (messages))\r
+-    {\r
+-      message = notmuch_messages_get (messages);\r
+-\r
+-      notmuch_message_freeze (message);\r
+-\r
+-      for (i = 0; tag_ops[i].tag; i++) {\r
+-          if (tag_ops[i].remove)\r
+-              notmuch_message_remove_tag (message, tag_ops[i].tag);\r
+-          else\r
+-              notmuch_message_add_tag (message, tag_ops[i].tag);\r
+-      }\r
+-\r
+-      notmuch_message_thaw (message);\r
+-\r
+-      if (synchronize_flags)\r
+-          notmuch_message_tags_to_maildir_flags (message);\r
+-\r
+-      notmuch_message_destroy (message);\r
+-    }\r
+-\r
+-    notmuch_query_destroy (query);\r
+     notmuch_database_close (notmuch);\r
+ \r
+-    return interrupted;\r
++    return ret;\r
+ }\r
+-- \r
+1.7.5.4\r
+\r