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 C42AB431FC9
\r
6 for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14: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
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 lToymEdGhmG4 for <notmuch@notmuchmail.org>;
\r
17 Sat, 24 Mar 2012 09:14:54 -0700 (PDT)
\r
18 Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com
\r
19 [209.85.215.53]) (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 0173B431FD4
\r
22 for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14:52 -0700 (PDT)
\r
23 Received: by mail-lpp01m010-f53.google.com with SMTP id c1so3315541lah.26
\r
24 for <notmuch@notmuchmail.org>; Sat, 24 Mar 2012 09:14:52 -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=DNhJ2W0qd3YGQ+F5rtC6xcltgt/nj69p1TcXdjNXmYI=;
\r
30 b=b/2vVf+XYo3Vnnbk8FeBaGSc2bMEVkWwQmhApUf1dS/WhVYbsFgd5x4yA8Fmo0EpHo
\r
31 hDcrUXbgXq3JsjeLDQn7tJR3iEZff6qrYioiHc8rHUS+oxiafH1+Fzf4p5KJA4grnGKm
\r
32 hYHzGMufJmgYw9UaLSlBjC7Aw6/1NcvM1OXZOSiZ5uRDyRRr8AnDQj7sSmhjkpyszsd+
\r
33 /gfMPl3rfabc3emECMuZrweBQGowdPNLuITAWaV9LBDmHkummbnr576J8f8pLP5DqYJj
\r
34 DOfLW6sizElUGRgEDBInjkXZ2EB22ZYn5aYx5r8ZnwSBz/3S92FBPzUfQXmBY7zQNyee
\r
36 Received: by 10.112.105.15 with SMTP id gi15mr6162628lbb.54.1332605692578;
\r
37 Sat, 24 Mar 2012 09:14:52 -0700 (PDT)
\r
38 Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.
\r
40 by mx.google.com with ESMTPS id pw4sm11397961lab.8.2012.03.24.09.14.50
\r
41 (version=SSLv3 cipher=OTHER); Sat, 24 Mar 2012 09:14:51 -0700 (PDT)
\r
42 From: Jani Nikula <jani@nikula.org>
\r
43 To: notmuch@notmuchmail.org
\r
44 Subject: [PATCH 3/3] cli: refactor "notmuch restore" message tagging into a
\r
46 Date: Sat, 24 Mar 2012 18:14:37 +0200
\r
48 <5adca8d3701d9d01b8cb67e589ce8516d4df9832.1332604895.git.jani@nikula.org>
\r
49 X-Mailer: git-send-email 1.7.5.4
\r
50 In-Reply-To: <cover.1332604895.git.jani@nikula.org>
\r
51 References: <cover.1332604895.git.jani@nikula.org>
\r
52 In-Reply-To: <cover.1332604895.git.jani@nikula.org>
\r
53 References: <cover.1332604895.git.jani@nikula.org>
\r
55 ALoCoQmA5/y1N8IqeD7PpB2Hhujbj5H2WnUeRxgvjKimzh7cWvWeflMXtNeddrOXysQyjcmUsKpf
\r
56 X-BeenThere: notmuch@notmuchmail.org
\r
57 X-Mailman-Version: 2.1.13
\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: Sat, 24 Mar 2012 16:14:57 -0000
\r
70 Refactor to make tagging code easier to reuse in the future. No
\r
73 Signed-off-by: Jani Nikula <jani@nikula.org>
\r
75 notmuch-client.h | 5 +++
\r
76 notmuch-restore.c | 73 ++-------------------------------------------------
\r
77 notmuch-tag.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
78 3 files changed, 83 insertions(+), 70 deletions(-)
\r
80 diff --git a/notmuch-client.h b/notmuch-client.h
\r
81 index fa04fa2..7e3deee 100644
\r
82 --- a/notmuch-client.h
\r
83 +++ b/notmuch-client.h
\r
84 @@ -203,6 +203,11 @@ json_quote_chararray (const void *ctx, const char *str, const size_t len);
\r
86 json_quote_str (const void *ctx, const char *str);
\r
89 +tag_message (void *ctx, notmuch_database_t *notmuch, const char *message_id,
\r
90 + char *file_tags, notmuch_bool_t remove_all,
\r
91 + notmuch_bool_t synchronize_flags);
\r
93 /* notmuch-config.c */
\r
95 typedef struct _notmuch_config notmuch_config_t;
\r
96 diff --git a/notmuch-restore.c b/notmuch-restore.c
\r
97 index 87d9772..bd6b884 100644
\r
98 --- a/notmuch-restore.c
\r
99 +++ b/notmuch-restore.c
\r
100 @@ -88,11 +88,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
102 while ((line_len = getline (&line, &line_size, input)) != -1) {
\r
103 regmatch_t match[3];
\r
104 - char *message_id, *file_tags, *tag, *next;
\r
105 - notmuch_message_t *message = NULL;
\r
106 - notmuch_status_t status;
\r
107 - notmuch_tags_t *db_tags;
\r
108 - char *db_tags_str;
\r
109 + char *message_id, *file_tags;
\r
111 chomp_newline (line);
\r
113 @@ -109,72 +105,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
114 file_tags = xstrndup (line + match[2].rm_so,
\r
115 match[2].rm_eo - match[2].rm_so);
\r
117 - status = notmuch_database_find_message (notmuch, message_id, &message);
\r
118 - if (status || message == NULL) {
\r
119 - fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
\r
120 - message ? "" : "missing ", message_id);
\r
122 - fprintf (stderr, "%s\n",
\r
123 - notmuch_status_to_string(status));
\r
127 - /* In order to detect missing messages, this check/optimization is
\r
128 - * intentionally done *after* first finding the message. */
\r
129 - if (accumulate && (file_tags == NULL || *file_tags == '\0'))
\r
134 - db_tags_str = NULL;
\r
135 - for (db_tags = notmuch_message_get_tags (message);
\r
136 - notmuch_tags_valid (db_tags);
\r
137 - notmuch_tags_move_to_next (db_tags))
\r
139 - const char *tag = notmuch_tags_get (db_tags);
\r
142 - db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
\r
144 - db_tags_str = talloc_strdup (message, tag);
\r
147 - if (((file_tags == NULL || *file_tags == '\0') &&
\r
148 - (db_tags_str == NULL || *db_tags_str == '\0')) ||
\r
149 - (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
\r
154 - notmuch_message_freeze (message);
\r
157 - notmuch_message_remove_all_tags (message);
\r
159 - next = file_tags;
\r
161 - tag = strsep (&next, " ");
\r
162 - if (*tag == '\0')
\r
164 - status = notmuch_message_add_tag (message, tag);
\r
167 - "Error applying tag %s to message %s:\n",
\r
168 - tag, message_id);
\r
169 - fprintf (stderr, "%s\n",
\r
170 - notmuch_status_to_string (status));
\r
174 - notmuch_message_thaw (message);
\r
176 - if (synchronize_flags)
\r
177 - notmuch_message_tags_to_maildir_flags (message);
\r
178 + tag_message (ctx, notmuch, message_id, file_tags, !accumulate,
\r
179 + synchronize_flags);
\r
183 - notmuch_message_destroy (message);
\r
188 diff --git a/notmuch-tag.c b/notmuch-tag.c
\r
189 index c4e3f9d..3aeb878 100644
\r
190 --- a/notmuch-tag.c
\r
191 +++ b/notmuch-tag.c
\r
192 @@ -161,6 +161,81 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
\r
196 +tag_message (void *ctx, notmuch_database_t *notmuch, const char *message_id,
\r
197 + char *file_tags, notmuch_bool_t remove_all,
\r
198 + notmuch_bool_t synchronize_flags)
\r
200 + notmuch_status_t status;
\r
201 + notmuch_tags_t *db_tags;
\r
202 + char *db_tags_str;
\r
203 + notmuch_message_t *message = NULL;
\r
208 + status = notmuch_database_find_message (notmuch, message_id, &message);
\r
209 + if (status || message == NULL) {
\r
210 + fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
\r
211 + message ? "" : "missing ", message_id);
\r
213 + fprintf (stderr, "%s\n", notmuch_status_to_string(status));
\r
217 + /* In order to detect missing messages, this check/optimization is
\r
218 + * intentionally done *after* first finding the message. */
\r
219 + if (!remove_all && (file_tags == NULL || *file_tags == '\0'))
\r
222 + db_tags_str = NULL;
\r
223 + for (db_tags = notmuch_message_get_tags (message);
\r
224 + notmuch_tags_valid (db_tags);
\r
225 + notmuch_tags_move_to_next (db_tags)) {
\r
226 + tag = notmuch_tags_get (db_tags);
\r
229 + db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
\r
231 + db_tags_str = talloc_strdup (message, tag);
\r
234 + if (((file_tags == NULL || *file_tags == '\0') &&
\r
235 + (db_tags_str == NULL || *db_tags_str == '\0')) ||
\r
236 + (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
\r
239 + notmuch_message_freeze (message);
\r
242 + notmuch_message_remove_all_tags (message);
\r
244 + next = file_tags;
\r
246 + tag = strsep (&next, " ");
\r
247 + if (*tag == '\0')
\r
249 + status = notmuch_message_add_tag (message, tag);
\r
251 + fprintf (stderr, "Error applying tag %s to message %s:\n",
\r
252 + tag, message_id);
\r
253 + fprintf (stderr, "%s\n", notmuch_status_to_string (status));
\r
258 + notmuch_message_thaw (message);
\r
260 + if (synchronize_flags)
\r
261 + notmuch_message_tags_to_maildir_flags (message);
\r
265 + notmuch_message_destroy (message);
\r
271 notmuch_tag_command (void *ctx, int argc, char *argv[])
\r
273 tag_operation_t *tag_ops;
\r