From a3666f58ffc565f62ae59e2856072564a191a352 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Mon, 26 Mar 2012 07:38:47 +0000 Subject: [PATCH] Re: [PATCH v2 2/3] cli: refactor "notmuch tag" query tagging into a separate function --- 54/bd89fd3aff18403a8df50fce7db300611bd16b | 307 ++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 54/bd89fd3aff18403a8df50fce7db300611bd16b diff --git a/54/bd89fd3aff18403a8df50fce7db300611bd16b b/54/bd89fd3aff18403a8df50fce7db300611bd16b new file mode 100644 index 000000000..9a6486bf3 --- /dev/null +++ b/54/bd89fd3aff18403a8df50fce7db300611bd16b @@ -0,0 +1,307 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 199E4431FAF + for ; Mon, 26 Mar 2012 00:38:55 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id lZu4Blej6gwZ for ; + Mon, 26 Mar 2012 00:38:53 -0700 (PDT) +Received: from mail-qc0-f181.google.com (mail-qc0-f181.google.com + [209.85.216.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id BED14431FAE + for ; Mon, 26 Mar 2012 00:38:53 -0700 (PDT) +Received: by qcsk26 with SMTP id k26so3922863qcs.26 + for ; Mon, 26 Mar 2012 00:38:52 -0700 (PDT) +X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=google.com; s=20120113; + h=from:to:subject:in-reply-to:references:user-agent:date:message-id + :mime-version:content-type:x-gm-message-state; + bh=V7j4+ucUhifx3J1KTLdK4+EMoK6sy/GsEoEyPscaQLE=; + b=gUESWePjwYXxff94eBnJDvWZrKFDdZwMPJ6qLH4XP94i608mOMt133KC1Aqb71VTfu + MbVXRrTzKVwshxFERlZR+em4r4JluWMQWKeTSjPeaVo8choQGJLkZy56o17/ov/pG5/8 + +m31WbLJ8nUuYqMV8ox8KbfjVZr7wtxf7vM70BdV9HxzMzF2IZy3922xlW+mJUdxcP9c + s+r4AIZM6Wt08iMjPc708OrkiSqPLfzG9oOMU756ZMpmoi6MGCspVDv6+Ck1F7RQQnFP + t/SM5AItaoqlxngyKy+X6xGThkYCHuO8rIZN0c0f7y+erHLIdTrXLIeN+488Q9GU8krS + Sk1A== +Received: by 10.224.187.2 with SMTP id cu2mr26045500qab.88.1332747531994; + Mon, 26 Mar 2012 00:38:51 -0700 (PDT) +Received: from localhost (nikula.org. [92.243.24.172]) + by mx.google.com with ESMTPS id cv8sm358404qab.12.2012.03.26.00.38.49 + (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 00:38:51 -0700 (PDT) +From: Jani Nikula +To: Tomi Ollila , notmuch@notmuchmail.org +Subject: Re: [PATCH v2 2/3] cli: refactor "notmuch tag" query tagging into a + separate function +In-Reply-To: +References: + <584067da00dd9f04a7f3982b76fd4b4918e4fe61.1332702915.git.jani@nikula.org> + + <87limo2xpk.fsf@nikula.org>User-Agent: Notmuch/0.12+71~gdacf76b + (http://notmuchmail.org) Emacs/23.3.1 + (x86_64-unknown-linux-gnu) +User-Agent: Notmuch/0.11.1+222~ga47a98c (http://notmuchmail.org) Emacs/23.1.1 + (i686-pc-linux-gnu) +Date: Mon, 26 Mar 2012 07:38:47 +0000 +Message-ID: <87ty1b4xrs.fsf@nikula.org> +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +X-Gm-Message-State: + ALoCoQkemWZT0HI2+q2nD9ShizwmMfsODTc4VDhzWfcKaU6t+6L12Vf50nS3WmAXhXHd0PHJBjOd +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Mon, 26 Mar 2012 07:38:55 -0000 + +On Mon, 26 Mar 2012 00:26:50 +0300, Tomi Ollila wrote: +> Jani Nikula writes: +> +> > On Sun, 25 Mar 2012 23:45:39 +0300, Tomi Ollila wrote: +> >> Jani Nikula writes: +> >> +> >> > Refactor to make tagging code easier to reuse in the future. No +> >> > functional changes. +> >> > +> >> > Signed-off-by: Jani Nikula +> >> > --- +> >> +> >> tag_query() is pretty generic name for a function which (also) does +> >> tagging operations (adds and removes tags based on tag_ops). +> > +> > Mmmh, if you think of "tag" as a verb, it fairly accurately describes +> > what the function does: tag (messages matching this) query (according +> > given arguments). I don't mind changing, but can't come up with anything +> > better right now either. notmuch_{search,query}_change_tags()? Meh? +> +> Hmmm, yes... -- tag...query... ok, I can live with that if no-one +> else get same impression I got first... +> +> > +> >> If, however, tag_opts != NULL (as is needs to be) but tag_opts[0] == NULL +> >> (now tag_query() would not do any tagging operations, but +> >> optimize_tag_query would mangle query string ( '*' to '()' and +> >> 'any_other' to '( any_other ) and ()' ) +> > +> > I'm not sure I follow you. AFAICT the behaviour does not change from +> > what it was before, apart from the tag addition and removal being mixed +> > together. +> +> The thing is that notmuch_tag_command () check that there is at least +> one tagging operation to be done, but tag_query () doesn't do such +> checking... +> +> >> I.e. IMO the function name should be more spesific & the fact that this +> >> function needs tag changing data in tag_ops array should be documented. +> > +> > Documentation good. :) +> +> ... therefore the requirement that there needs to tagging information +> in tag_ops is in place. + +Actually, no. In the future (the follow-up patches are in my local +tree...) tag_query() should work also when there are no tagging +operations. I'll either need to fix tag_query() or _optimize_tag_query() +to handle this gracefully. Thanks for spelling this out for me! :) + +> >> Minor thing in patch #1: +> >> +> >> + tag_ops[tag_ops_count].remove = argv[i][0] == '-'; +> >> +> >> would be more clearer as: +> >> +> >> + tag_ops[tag_ops_count].remove = (argv[i][0] == '-'); +> > +> > I usually don't add braces where they aren't needed, but can do here. +> +> Assigment is much lower in precedence than equality comparison -- but +> as this is so seldom-used construct, humans read (with understanding) +> the code faster with this added clarity. + +Sounds reasonable. + +BR, +Jani. + + + +> +> >> Everything else LGTM. +> > +> > Thanks for the review, +> > Jani. +> > +> >> +> >> Tomi +> +> Tomi +> +> >> +> >> +> >> > notmuch-tag.c | 101 ++++++++++++++++++++++++++++++++------------------------- +> >> > 1 files changed, 57 insertions(+), 44 deletions(-) +> >> > +> >> > diff --git a/notmuch-tag.c b/notmuch-tag.c +> >> > index c39b235..edbfdec 100644 +> >> > --- a/notmuch-tag.c +> >> > +++ b/notmuch-tag.c +> >> > @@ -106,6 +106,60 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, +> >> > return query_string; +> >> > } +> >> > +> >> > +static int +> >> > +tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string, +> >> > + tag_operation_t *tag_ops, notmuch_bool_t synchronize_flags) +> >> > +{ +> >> > + notmuch_query_t *query; +> >> > + notmuch_messages_t *messages; +> >> > + notmuch_message_t *message; +> >> > + int i; +> >> > + +> >> > + /* Optimize the query so it excludes messages that already have +> >> > + * the specified set of tags. */ +> >> > + query_string = _optimize_tag_query (ctx, query_string, tag_ops); +> >> > + if (query_string == NULL) { +> >> > + fprintf (stderr, "Out of memory.\n"); +> >> > + return 1; +> >> > + } +> >> > + +> >> > + query = notmuch_query_create (notmuch, query_string); +> >> > + if (query == NULL) { +> >> > + fprintf (stderr, "Out of memory.\n"); +> >> > + return 1; +> >> > + } +> >> > + +> >> > + /* tagging is not interested in any special sort order */ +> >> > + notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED); +> >> > + +> >> > + for (messages = notmuch_query_search_messages (query); +> >> > + notmuch_messages_valid (messages) && !interrupted; +> >> > + notmuch_messages_move_to_next (messages)) +> >> > + { +> >> > + message = notmuch_messages_get (messages); +> >> > + +> >> > + notmuch_message_freeze (message); +> >> > + +> >> > + for (i = 0; tag_ops[i].tag; i++) { +> >> > + if (tag_ops[i].remove) +> >> > + notmuch_message_remove_tag (message, tag_ops[i].tag); +> >> > + else +> >> > + notmuch_message_add_tag (message, tag_ops[i].tag); +> >> > + } +> >> > + +> >> > + notmuch_message_thaw (message); +> >> > + +> >> > + if (synchronize_flags) +> >> > + notmuch_message_tags_to_maildir_flags (message); +> >> > + +> >> > + notmuch_message_destroy (message); +> >> > + } +> >> > + +> >> > + notmuch_query_destroy (query); +> >> > + +> >> > + return interrupted; +> >> > +} +> >> > + +> >> > int +> >> > notmuch_tag_command (void *ctx, int argc, char *argv[]) +> >> > { +> >> > @@ -114,12 +168,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]) +> >> > char *query_string; +> >> > notmuch_config_t *config; +> >> > notmuch_database_t *notmuch; +> >> > - notmuch_query_t *query; +> >> > - notmuch_messages_t *messages; +> >> > - notmuch_message_t *message; +> >> > struct sigaction action; +> >> > notmuch_bool_t synchronize_flags; +> >> > int i; +> >> > + int ret; +> >> > +> >> > /* Setup our handler for SIGINT */ +> >> > memset (&action, 0, sizeof (struct sigaction)); +> >> > @@ -166,14 +218,6 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]) +> >> > return 1; +> >> > } +> >> > +> >> > - /* Optimize the query so it excludes messages that already have +> >> > - * the specified set of tags. */ +> >> > - query_string = _optimize_tag_query (ctx, query_string, tag_ops); +> >> > - if (query_string == NULL) { +> >> > - fprintf (stderr, "Out of memory.\n"); +> >> > - return 1; +> >> > - } +> >> > - +> >> > config = notmuch_config_open (ctx, NULL, NULL); +> >> > if (config == NULL) +> >> > return 1; +> >> > @@ -185,40 +229,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]) +> >> > +> >> > synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config); +> >> > +> >> > - query = notmuch_query_create (notmuch, query_string); +> >> > - if (query == NULL) { +> >> > - fprintf (stderr, "Out of memory.\n"); +> >> > - return 1; +> >> > - } +> >> > - +> >> > - /* tagging is not interested in any special sort order */ +> >> > - notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED); +> >> > + ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags); +> >> > +> >> > - for (messages = notmuch_query_search_messages (query); +> >> > - notmuch_messages_valid (messages) && !interrupted; +> >> > - notmuch_messages_move_to_next (messages)) +> >> > - { +> >> > - message = notmuch_messages_get (messages); +> >> > - +> >> > - notmuch_message_freeze (message); +> >> > - +> >> > - for (i = 0; tag_ops[i].tag; i++) { +> >> > - if (tag_ops[i].remove) +> >> > - notmuch_message_remove_tag (message, tag_ops[i].tag); +> >> > - else +> >> > - notmuch_message_add_tag (message, tag_ops[i].tag); +> >> > - } +> >> > - +> >> > - notmuch_message_thaw (message); +> >> > - +> >> > - if (synchronize_flags) +> >> > - notmuch_message_tags_to_maildir_flags (message); +> >> > - +> >> > - notmuch_message_destroy (message); +> >> > - } +> >> > - +> >> > - notmuch_query_destroy (query); +> >> > notmuch_database_close (notmuch); +> >> > +> >> > - return interrupted; +> >> > + return ret; +> >> > } +> >> > -- +> >> > 1.7.5.4 +> >> > +> >> > _______________________________________________ +> >> > notmuch mailing list +> >> > notmuch@notmuchmail.org +> >> > http://notmuchmail.org/mailman/listinfo/notmuch +> > _______________________________________________ +> > notmuch mailing list +> > notmuch@notmuchmail.org +> > http://notmuchmail.org/mailman/listinfo/notmuch -- 2.26.2