[PATCH v2 1/3] cli: refactor "notmuch tag" data structures for tagging operations
authorJani Nikula <jani@nikula.org>
Sun, 25 Mar 2012 19:18:43 +0000 (22:18 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:45:43 +0000 (09:45 -0800)
d5/76cb883131e72556a90c608fba8d1354bd78e8 [new file with mode: 0644]

diff --git a/d5/76cb883131e72556a90c608fba8d1354bd78e8 b/d5/76cb883131e72556a90c608fba8d1354bd78e8
new file mode 100644 (file)
index 0000000..2379326
--- /dev/null
@@ -0,0 +1,235 @@
+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 184E6431FB6\r
+       for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:18: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 vcnwZlw11Z+M for <notmuch@notmuchmail.org>;\r
+       Sun, 25 Mar 2012 12:18:55 -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 0192B431FAF\r
+       for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:18:54 -0700 (PDT)\r
+Received: by lbok6 with SMTP id k6so3804119lbo.26\r
+       for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:18:53 -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=6gUbW9OaAzQm0BVWcLLlrh5/O+2NTD0VMq73v5f8dto=;\r
+       b=LXP2HqdV6NV9D7e19P4gmIB1e+wRr4ZIRXmNS3PfWx+l/KNaeOh9gQc4GEhHPnHmPQ\r
+       zQQKA5jov2ZfAA1R4eDc7Y3qXzzZbFotJCSk86Wc9Zus6ubuWfYDz/4g+qw/kdomRC/l\r
+       lSQQdU3WqFWWpuYlfsPTu7UjmuCYR7+yAsh0fapGY3TWhDsu7Rjmv/udYhJOUkuJVzrz\r
+       XPW19kKVYo/4F1zk/O6w9e1IVF1bJMLq34u7e11LBdn2sg0s0z6EZBhFtvZC6/cKsbIU\r
+       jTLjxxWmbqX3hg5hbfzJ7LwZsmVS0rfHPT/DhR8IHN2ry2GfJXLu+DwxX0/1C/PSOhV/\r
+       vAhQ==\r
+Received: by 10.112.45.106 with SMTP id l10mr3959585lbm.45.1332703133326;\r
+       Sun, 25 Mar 2012 12:18:53 -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 py12sm14847041lab.4.2012.03.25.12.18.51\r
+       (version=SSLv3 cipher=OTHER); Sun, 25 Mar 2012 12:18:52 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/3] cli: refactor "notmuch tag" data structures for\r
+       tagging operations\r
+Date: Sun, 25 Mar 2012 22:18:43 +0300\r
+Message-Id:\r
+ <0862d8a5424d6d0da188db84340b027fdede4245.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
+ ALoCoQmwuO309MwoFQCHnbRQaebbqO6WWo55KsH71Q0ArKBSckJFdVQL3o4upmsnHDE5J2DjW/gk\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:18:56 -0000\r
+\r
+To simplify code, keep all tagging operations in a single array\r
+instead of separate add and remove arrays. Apply tag changes in the\r
+order specified on the command line, instead of first removing and\r
+then adding the tags.\r
+\r
+This results in a minor functional change: If a tag is both added and\r
+removed, the last specified operation is now used. Previously the tag\r
+was always added.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-tag.c |   79 +++++++++++++++++++++++++-------------------------------\r
+ 1 files changed, 35 insertions(+), 44 deletions(-)\r
+\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 36b9b09..c39b235 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -53,10 +53,14 @@ _escape_tag (char *buf, const char *tag)\r
+     return buf;\r
+ }\r
+ \r
++typedef struct {\r
++    const char *tag;\r
++    notmuch_bool_t remove;\r
++} tag_operation_t;\r
++\r
+ static char *\r
+-_optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+-                   int *add_tags, int add_tags_count,\r
+-                   int *remove_tags, int remove_tags_count)\r
++_optimize_tag_query (void *ctx, const char *orig_query_string,\r
++                   const tag_operation_t *tag_ops)\r
+ {\r
+     /* This is subtler than it looks.  Xapian ignores the '-' operator\r
+      * at the beginning both queries and parenthesized groups and,\r
+@@ -74,12 +78,9 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+     /* Allocate a buffer for escaping tags.  This is large enough to\r
+      * hold a fully escaped tag with every character doubled plus\r
+      * enclosing quotes and a NUL. */\r
+-    for (i = 0; i < add_tags_count; i++)\r
+-      if (strlen (argv[add_tags[i]] + 1) > max_tag_len)\r
+-          max_tag_len = strlen (argv[add_tags[i]] + 1);\r
+-    for (i = 0; i < remove_tags_count; i++)\r
+-      if (strlen (argv[remove_tags[i]] + 1) > max_tag_len)\r
+-          max_tag_len = strlen (argv[remove_tags[i]] + 1);\r
++    for (i = 0; tag_ops[i].tag; i++)\r
++      if (strlen (tag_ops[i].tag) > max_tag_len)\r
++          max_tag_len = strlen (tag_ops[i].tag);\r
+     escaped = talloc_array(ctx, char, max_tag_len * 2 + 3);\r
+     if (!escaped)\r
+       return NULL;\r
+@@ -90,16 +91,11 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+     else\r
+       query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
+ \r
+-    for (i = 0; i < add_tags_count && query_string; i++) {\r
+-      query_string = talloc_asprintf_append_buffer (\r
+-          query_string, "%snot tag:%s", join,\r
+-          _escape_tag (escaped, argv[add_tags[i]] + 1));\r
+-      join = " or ";\r
+-    }\r
+-    for (i = 0; i < remove_tags_count && query_string; i++) {\r
++    for (i = 0; tag_ops[i].tag && query_string; i++) {\r
+       query_string = talloc_asprintf_append_buffer (\r
+-          query_string, "%stag:%s", join,\r
+-          _escape_tag (escaped, argv[remove_tags[i]] + 1));\r
++          query_string, "%s%stag:%s", join,\r
++          tag_ops[i].remove ? "" : "not ",\r
++          _escape_tag (escaped, tag_ops[i].tag));\r
+       join = " or ";\r
+     }\r
+ \r
+@@ -113,9 +109,8 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+ int\r
+ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ {\r
+-    int *add_tags, *remove_tags;\r
+-    int add_tags_count = 0;\r
+-    int remove_tags_count = 0;\r
++    tag_operation_t *tag_ops;\r
++    int tag_ops_count = 0;\r
+     char *query_string;\r
+     notmuch_config_t *config;\r
+     notmuch_database_t *notmuch;\r
+@@ -133,35 +128,33 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+     action.sa_flags = SA_RESTART;\r
+     sigaction (SIGINT, &action, NULL);\r
+ \r
+-    add_tags = talloc_size (ctx, argc * sizeof (int));\r
+-    if (add_tags == NULL) {\r
+-      fprintf (stderr, "Out of memory.\n");\r
+-      return 1;\r
+-    }\r
++    argc--; argv++; /* skip subcommand argument */\r
+ \r
+-    remove_tags = talloc_size (ctx, argc * sizeof (int));\r
+-    if (remove_tags == NULL) {\r
++    /* Array of tagging operations (add or remove), terminated with an\r
++     * empty element. */\r
++    tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);\r
++    if (tag_ops == NULL) {\r
+       fprintf (stderr, "Out of memory.\n");\r
+       return 1;\r
+     }\r
+ \r
+-    argc--; argv++; /* skip subcommand argument */\r
+-\r
+     for (i = 0; i < argc; i++) {\r
+       if (strcmp (argv[i], "--") == 0) {\r
+           i++;\r
+           break;\r
+       }\r
+-      if (argv[i][0] == '+') {\r
+-          add_tags[add_tags_count++] = i;\r
+-      } else if (argv[i][0] == '-') {\r
+-          remove_tags[remove_tags_count++] = i;\r
++      if (argv[i][0] == '+' || argv[i][0] == '-') {\r
++          tag_ops[tag_ops_count].tag = argv[i] + 1;\r
++          tag_ops[tag_ops_count].remove = argv[i][0] == '-';\r
++          tag_ops_count++;\r
+       } else {\r
+           break;\r
+       }\r
+     }\r
+ \r
+-    if (add_tags_count == 0 && remove_tags_count == 0) {\r
++    tag_ops[tag_ops_count].tag = NULL;\r
++\r
++    if (tag_ops_count == 0) {\r
+       fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");\r
+       return 1;\r
+     }\r
+@@ -175,9 +168,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\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, argv,\r
+-                                      add_tags, add_tags_count,\r
+-                                      remove_tags, remove_tags_count);\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
+@@ -211,12 +202,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ \r
+       notmuch_message_freeze (message);\r
+ \r
+-      for (i = 0; i < remove_tags_count; i++)\r
+-          notmuch_message_remove_tag (message,\r
+-                                      argv[remove_tags[i]] + 1);\r
+-\r
+-      for (i = 0; i < add_tags_count; i++)\r
+-          notmuch_message_add_tag (message, argv[add_tags[i]] + 1);\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
+-- \r
+1.7.5.4\r
+\r