[PATCH] This patch is a little finger excercise for working with git. I found a piece...
[notmuch-archives.git] / d5 / 76cb883131e72556a90c608fba8d1354bd78e8
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 184E6431FB6\r
6         for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:18:56 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id vcnwZlw11Z+M for <notmuch@notmuchmail.org>;\r
17         Sun, 25 Mar 2012 12:18:55 -0700 (PDT)\r
18 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
19         [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 0192B431FAF\r
22         for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:18:54 -0700 (PDT)\r
23 Received: by lbok6 with SMTP id k6so3804119lbo.26\r
24         for <notmuch@notmuchmail.org>; Sun, 25 Mar 2012 12:18:53 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
28         :in-reply-to:references:x-gm-message-state;\r
29         bh=6gUbW9OaAzQm0BVWcLLlrh5/O+2NTD0VMq73v5f8dto=;\r
30         b=LXP2HqdV6NV9D7e19P4gmIB1e+wRr4ZIRXmNS3PfWx+l/KNaeOh9gQc4GEhHPnHmPQ\r
31         zQQKA5jov2ZfAA1R4eDc7Y3qXzzZbFotJCSk86Wc9Zus6ubuWfYDz/4g+qw/kdomRC/l\r
32         lSQQdU3WqFWWpuYlfsPTu7UjmuCYR7+yAsh0fapGY3TWhDsu7Rjmv/udYhJOUkuJVzrz\r
33         XPW19kKVYo/4F1zk/O6w9e1IVF1bJMLq34u7e11LBdn2sg0s0z6EZBhFtvZC6/cKsbIU\r
34         jTLjxxWmbqX3hg5hbfzJ7LwZsmVS0rfHPT/DhR8IHN2ry2GfJXLu+DwxX0/1C/PSOhV/\r
35         vAhQ==\r
36 Received: by 10.112.45.106 with SMTP id l10mr3959585lbm.45.1332703133326;\r
37         Sun, 25 Mar 2012 12:18:53 -0700 (PDT)\r
38 Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
39         [84.248.80.253])\r
40         by mx.google.com with ESMTPS id py12sm14847041lab.4.2012.03.25.12.18.51\r
41         (version=SSLv3 cipher=OTHER); Sun, 25 Mar 2012 12:18:52 -0700 (PDT)\r
42 From: Jani Nikula <jani@nikula.org>\r
43 To: notmuch@notmuchmail.org\r
44 Subject: [PATCH v2 1/3] cli: refactor "notmuch tag" data structures for\r
45         tagging operations\r
46 Date: Sun, 25 Mar 2012 22:18:43 +0300\r
47 Message-Id:\r
48  <0862d8a5424d6d0da188db84340b027fdede4245.1332702915.git.jani@nikula.org>\r
49 X-Mailer: git-send-email 1.7.5.4\r
50 In-Reply-To: <cover.1332702915.git.jani@nikula.org>\r
51 References: <cover.1332702915.git.jani@nikula.org>\r
52 In-Reply-To: <cover.1332702915.git.jani@nikula.org>\r
53 References: <cover.1332702915.git.jani@nikula.org>\r
54 X-Gm-Message-State:\r
55  ALoCoQmwuO309MwoFQCHnbRQaebbqO6WWo55KsH71Q0ArKBSckJFdVQL3o4upmsnHDE5J2DjW/gk\r
56 X-BeenThere: notmuch@notmuchmail.org\r
57 X-Mailman-Version: 2.1.13\r
58 Precedence: list\r
59 List-Id: "Use and development of the notmuch mail system."\r
60         <notmuch.notmuchmail.org>\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
62         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
64 List-Post: <mailto:notmuch@notmuchmail.org>\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
68 X-List-Received-Date: Sun, 25 Mar 2012 19:18:56 -0000\r
69 \r
70 To simplify code, keep all tagging operations in a single array\r
71 instead of separate add and remove arrays. Apply tag changes in the\r
72 order specified on the command line, instead of first removing and\r
73 then adding the tags.\r
74 \r
75 This results in a minor functional change: If a tag is both added and\r
76 removed, the last specified operation is now used. Previously the tag\r
77 was always added.\r
78 \r
79 Signed-off-by: Jani Nikula <jani@nikula.org>\r
80 ---\r
81  notmuch-tag.c |   79 +++++++++++++++++++++++++-------------------------------\r
82  1 files changed, 35 insertions(+), 44 deletions(-)\r
83 \r
84 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
85 index 36b9b09..c39b235 100644\r
86 --- a/notmuch-tag.c\r
87 +++ b/notmuch-tag.c\r
88 @@ -53,10 +53,14 @@ _escape_tag (char *buf, const char *tag)\r
89      return buf;\r
90  }\r
91  \r
92 +typedef struct {\r
93 +    const char *tag;\r
94 +    notmuch_bool_t remove;\r
95 +} tag_operation_t;\r
96 +\r
97  static char *\r
98 -_optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
99 -                    int *add_tags, int add_tags_count,\r
100 -                    int *remove_tags, int remove_tags_count)\r
101 +_optimize_tag_query (void *ctx, const char *orig_query_string,\r
102 +                    const tag_operation_t *tag_ops)\r
103  {\r
104      /* This is subtler than it looks.  Xapian ignores the '-' operator\r
105       * at the beginning both queries and parenthesized groups and,\r
106 @@ -74,12 +78,9 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
107      /* Allocate a buffer for escaping tags.  This is large enough to\r
108       * hold a fully escaped tag with every character doubled plus\r
109       * enclosing quotes and a NUL. */\r
110 -    for (i = 0; i < add_tags_count; i++)\r
111 -       if (strlen (argv[add_tags[i]] + 1) > max_tag_len)\r
112 -           max_tag_len = strlen (argv[add_tags[i]] + 1);\r
113 -    for (i = 0; i < remove_tags_count; i++)\r
114 -       if (strlen (argv[remove_tags[i]] + 1) > max_tag_len)\r
115 -           max_tag_len = strlen (argv[remove_tags[i]] + 1);\r
116 +    for (i = 0; tag_ops[i].tag; i++)\r
117 +       if (strlen (tag_ops[i].tag) > max_tag_len)\r
118 +           max_tag_len = strlen (tag_ops[i].tag);\r
119      escaped = talloc_array(ctx, char, max_tag_len * 2 + 3);\r
120      if (!escaped)\r
121         return NULL;\r
122 @@ -90,16 +91,11 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
123      else\r
124         query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
125  \r
126 -    for (i = 0; i < add_tags_count && query_string; i++) {\r
127 -       query_string = talloc_asprintf_append_buffer (\r
128 -           query_string, "%snot tag:%s", join,\r
129 -           _escape_tag (escaped, argv[add_tags[i]] + 1));\r
130 -       join = " or ";\r
131 -    }\r
132 -    for (i = 0; i < remove_tags_count && query_string; i++) {\r
133 +    for (i = 0; tag_ops[i].tag && query_string; i++) {\r
134         query_string = talloc_asprintf_append_buffer (\r
135 -           query_string, "%stag:%s", join,\r
136 -           _escape_tag (escaped, argv[remove_tags[i]] + 1));\r
137 +           query_string, "%s%stag:%s", join,\r
138 +           tag_ops[i].remove ? "" : "not ",\r
139 +           _escape_tag (escaped, tag_ops[i].tag));\r
140         join = " or ";\r
141      }\r
142  \r
143 @@ -113,9 +109,8 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
144  int\r
145  notmuch_tag_command (void *ctx, int argc, char *argv[])\r
146  {\r
147 -    int *add_tags, *remove_tags;\r
148 -    int add_tags_count = 0;\r
149 -    int remove_tags_count = 0;\r
150 +    tag_operation_t *tag_ops;\r
151 +    int tag_ops_count = 0;\r
152      char *query_string;\r
153      notmuch_config_t *config;\r
154      notmuch_database_t *notmuch;\r
155 @@ -133,35 +128,33 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
156      action.sa_flags = SA_RESTART;\r
157      sigaction (SIGINT, &action, NULL);\r
158  \r
159 -    add_tags = talloc_size (ctx, argc * sizeof (int));\r
160 -    if (add_tags == NULL) {\r
161 -       fprintf (stderr, "Out of memory.\n");\r
162 -       return 1;\r
163 -    }\r
164 +    argc--; argv++; /* skip subcommand argument */\r
165  \r
166 -    remove_tags = talloc_size (ctx, argc * sizeof (int));\r
167 -    if (remove_tags == NULL) {\r
168 +    /* Array of tagging operations (add or remove), terminated with an\r
169 +     * empty element. */\r
170 +    tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);\r
171 +    if (tag_ops == NULL) {\r
172         fprintf (stderr, "Out of memory.\n");\r
173         return 1;\r
174      }\r
175  \r
176 -    argc--; argv++; /* skip subcommand argument */\r
177 -\r
178      for (i = 0; i < argc; i++) {\r
179         if (strcmp (argv[i], "--") == 0) {\r
180             i++;\r
181             break;\r
182         }\r
183 -       if (argv[i][0] == '+') {\r
184 -           add_tags[add_tags_count++] = i;\r
185 -       } else if (argv[i][0] == '-') {\r
186 -           remove_tags[remove_tags_count++] = i;\r
187 +       if (argv[i][0] == '+' || argv[i][0] == '-') {\r
188 +           tag_ops[tag_ops_count].tag = argv[i] + 1;\r
189 +           tag_ops[tag_ops_count].remove = argv[i][0] == '-';\r
190 +           tag_ops_count++;\r
191         } else {\r
192             break;\r
193         }\r
194      }\r
195  \r
196 -    if (add_tags_count == 0 && remove_tags_count == 0) {\r
197 +    tag_ops[tag_ops_count].tag = NULL;\r
198 +\r
199 +    if (tag_ops_count == 0) {\r
200         fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");\r
201         return 1;\r
202      }\r
203 @@ -175,9 +168,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
204  \r
205      /* Optimize the query so it excludes messages that already have\r
206       * the specified set of tags. */\r
207 -    query_string = _optimize_tag_query (ctx, query_string, argv,\r
208 -                                       add_tags, add_tags_count,\r
209 -                                       remove_tags, remove_tags_count);\r
210 +    query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
211      if (query_string == NULL) {\r
212         fprintf (stderr, "Out of memory.\n");\r
213         return 1;\r
214 @@ -211,12 +202,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
215  \r
216         notmuch_message_freeze (message);\r
217  \r
218 -       for (i = 0; i < remove_tags_count; i++)\r
219 -           notmuch_message_remove_tag (message,\r
220 -                                       argv[remove_tags[i]] + 1);\r
221 -\r
222 -       for (i = 0; i < add_tags_count; i++)\r
223 -           notmuch_message_add_tag (message, argv[add_tags[i]] + 1);\r
224 +       for (i = 0; tag_ops[i].tag; i++) {\r
225 +           if (tag_ops[i].remove)\r
226 +               notmuch_message_remove_tag (message, tag_ops[i].tag);\r
227 +           else\r
228 +               notmuch_message_add_tag (message, tag_ops[i].tag);\r
229 +       }\r
230  \r
231         notmuch_message_thaw (message);\r
232  \r
233 -- \r
234 1.7.5.4\r
235 \r