Re: [Patch v2 10/17] cli: add support for batch tagging operations to "notmuch tag"
authorJani Nikula <jani@nikula.org>
Sat, 1 Dec 2012 23:55:08 +0000 (01:55 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:51:17 +0000 (09:51 -0800)
c2/065670c0646fff250a097d57a5a65a7d0ce4e1 [new file with mode: 0644]

diff --git a/c2/065670c0646fff250a097d57a5a65a7d0ce4e1 b/c2/065670c0646fff250a097d57a5a65a7d0ce4e1
new file mode 100644 (file)
index 0000000..73199fb
--- /dev/null
@@ -0,0 +1,426 @@
+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 145DA431FBC\r
+       for <notmuch@notmuchmail.org>; Sat,  1 Dec 2012 15:55:21 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\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 kU0BRRFbzT-y for <notmuch@notmuchmail.org>;\r
+       Sat,  1 Dec 2012 15:55:16 -0800 (PST)\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 372DF431FAF\r
+       for <notmuch@notmuchmail.org>; Sat,  1 Dec 2012 15:55:14 -0800 (PST)\r
+Received: by mail-lb0-f181.google.com with SMTP id ge1so1529986lbb.26\r
+       for <notmuch@notmuchmail.org>; Sat, 01 Dec 2012 15:55:12 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=from:to:subject:in-reply-to:references:user-agent:date:message-id\r
+       :mime-version:content-type:x-gm-message-state;\r
+       bh=ONNu+sYs0df+E3a3ejedQlZQ5+FSDB+eJC2K+2TOmvc=;\r
+       b=pDvOzdA534r4J5olJ9tb9iyJf31FAdj4O7d6EMeUPq0DbY1wEaZMO6jsjPy7yNYJlO\r
+       6xNIupcKelG4a+UJz+uRi3XPZ6OZ/YfAxZboNMBaaALY2NeO6pPH/5NW1IZv2L7pzwm1\r
+       BP+JTJcic+loJqdKUPnlK4PemLTOOgxKe4IwfcMxZikqvlIo2/3fQN4dNnDik0w6rqkl\r
+       5bPnzoAPPebZmybpzOlR/HgNNX7Ex9rjaIfL4o6plcaU0M09j8ah4pW/rZ4U2gyY29Ku\r
+       MPmTSNlhyFn0Ahi+QlmSejq1nICuGhmPx265s7BTSW9tSnpwQMpYrMMa+C5FXOstPI9d\r
+       Zvlg==\r
+Received: by 10.152.106.237 with SMTP id gx13mr5280898lab.46.1354406112610;\r
+       Sat, 01 Dec 2012 15:55:12 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.\r
+       [80.223.81.27])\r
+       by mx.google.com with ESMTPS id ly18sm3496814lab.15.2012.12.01.15.55.10\r
+       (version=SSLv3 cipher=OTHER); Sat, 01 Dec 2012 15:55:11 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: david@tethera.net, notmuch@notmuchmail.org\r
+Subject: Re: [Patch v2 10/17] cli: add support for batch tagging operations to\r
+       "notmuch tag"\r
+In-Reply-To: <1353792017-31459-11-git-send-email-david@tethera.net>\r
+References: <1353792017-31459-1-git-send-email-david@tethera.net>\r
+       <1353792017-31459-11-git-send-email-david@tethera.net>\r
+User-Agent: Notmuch/0.14+124~g3b17402 (http://notmuchmail.org) Emacs/23.4.1\r
+       (i686-pc-linux-gnu)\r
+Date: Sun, 02 Dec 2012 01:55:08 +0200\r
+Message-ID: <87txs5qqeb.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Gm-Message-State:\r
+ ALoCoQnKINTcKCPDprUnE8Kf86nURozPaT0p+W+F8mDEOoAnKJ/OtNLbDWImwKdGLDVQuUXb5b3P\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: Sat, 01 Dec 2012 23:55:21 -0000\r
+\r
+\r
+Hi David, overall looks good. The tag-util abstractions are paying off\r
+nicely here.\r
+\r
+First two high level bikesheds: 1) This could be split in two patches,\r
+first one adding the tag-util usage to normal operation and second one\r
+adding the batch tagging, and 2) I think it would be neater if the batch\r
+tagging was in a separate function instead of inline in the top level\r
+function. I'm not insisting though; we should probably just go with\r
+this, and the latter part can be refactored later if needed.\r
+\r
+Some minor nits inline.\r
+\r
+BR,\r
+Jani.\r
+\r
+On Sat, 24 Nov 2012, david@tethera.net wrote:\r
+> From: Jani Nikula <jani@nikula.org>\r
+>\r
+> Add support for batch tagging operations through stdin to "notmuch\r
+> tag". This can be enabled with the new --stdin command line option to\r
+\r
+--batch\r
+\r
+> "notmuch tag". The input must consist of lines of the format:\r
+>\r
+> +<tag>|-<tag> [...] [--] <search-terms>\r
+>\r
+> Each line is interpreted similarly to "notmuch tag" command line\r
+> arguments. The delimiter is one or more spaces ' '. Any characters in\r
+> <tag> and <search-terms> MAY be hex encoded with %NN where NN is the\r
+> hexadecimal value of the character. Any ' ' and '%' characters in\r
+> <tag> and <search-terms> MUST be hex encoded (using %20 and %25,\r
+> respectively). Any characters that are not part of <tag> or\r
+> <search-terms> MUST NOT be hex encoded.\r
+>\r
+> Leading and trailing space ' ' is ignored. Empty lines and lines\r
+> beginning with '#' are ignored.\r
+>\r
+> Signed-off-by: Jani Nikula <jani@nikula.org>\r
+>\r
+> Hacked-like-crazy-by: David Bremner <david@tethera.net>\r
+> ---\r
+>  notmuch-tag.c |  194 +++++++++++++++++++++++++++++++++++----------------------\r
+>  1 file changed, 118 insertions(+), 76 deletions(-)\r
+>\r
+> diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+> index 88d559b..8a8af0b 100644\r
+> --- a/notmuch-tag.c\r
+> +++ b/notmuch-tag.c\r
+> @@ -19,6 +19,7 @@\r
+>   */\r
+>  \r
+>  #include "notmuch-client.h"\r
+> +#include "tag-util.h"\r
+>  \r
+>  static volatile sig_atomic_t interrupted;\r
+>  \r
+> @@ -54,14 +55,9 @@ _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,\r
+> -                 const tag_operation_t *tag_ops)\r
+> +                 const tag_op_list_t *list)\r
+>  {\r
+>      /* This is subtler than it looks.  Xapian ignores the '-' operator\r
+>       * at the beginning both queries and parenthesized groups and,\r
+> @@ -73,19 +69,20 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+>  \r
+>      char *escaped, *query_string;\r
+>      const char *join = "";\r
+> -    int i;\r
+> +    size_t i;\r
+>      unsigned int max_tag_len = 0;\r
+>  \r
+>      /* Don't optimize if there are no tag changes. */\r
+> -    if (tag_ops[0].tag == NULL)\r
+> +    if (tag_op_list_size (list) == 0)\r
+>      return talloc_strdup (ctx, orig_query_string);\r
+>  \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; 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
+> +    for (i = 0; i < tag_op_list_size (list); i++)\r
+> +    if (strlen (tag_op_list_tag (list, i)) > max_tag_len)\r
+> +        max_tag_len = strlen (tag_op_list_tag (list, i));\r
+> +\r
+>      escaped = talloc_array (ctx, char, max_tag_len * 2 + 3);\r
+>      if (! escaped)\r
+>      return NULL;\r
+> @@ -96,11 +93,11 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+>      else\r
+>      query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
+>  \r
+> -    for (i = 0; tag_ops[i].tag && query_string; i++) {\r
+> +    for (i = 0; i < tag_op_list_size (list) && query_string; i++) {\r
+>      query_string = talloc_asprintf_append_buffer (\r
+>          query_string, "%s%stag:%s", join,\r
+> -        tag_ops[i].remove ? "" : "not ",\r
+> -        _escape_tag (escaped, tag_ops[i].tag));\r
+> +        tag_op_list_isremove (list, i) ? "" : "not ",\r
+> +        _escape_tag (escaped, tag_op_list_tag (list, i)));\r
+>      join = " or ";\r
+>      }\r
+>  \r
+> @@ -116,12 +113,11 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\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
+> +       tag_op_list_t *tag_ops, tag_op_flag_t 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
+> @@ -144,21 +140,7 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
+>       notmuch_messages_valid (messages) && ! interrupted;\r
+>       notmuch_messages_move_to_next (messages)) {\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
+> +    tag_op_list_apply (message, tag_ops, flags);\r
+>      notmuch_message_destroy (message);\r
+>      }\r
+>  \r
+> @@ -170,15 +152,17 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
+>  int\r
+>  notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+>  {\r
+> -    tag_operation_t *tag_ops;\r
+> -    int tag_ops_count = 0;\r
+> -    char *query_string;\r
+> +    tag_op_list_t *tag_ops = NULL;\r
+> +    char *query_string = NULL;\r
+>      notmuch_config_t *config;\r
+>      notmuch_database_t *notmuch;\r
+>      struct sigaction action;\r
+> -    notmuch_bool_t synchronize_flags;\r
+> -    int i;\r
+> -    int ret;\r
+> +    tag_op_flag_t synchronize_flags = TAG_FLAG_NONE;\r
+\r
+Maybe make that tag_flags or something so we don't need to rename it if\r
+we ever use the other flags here?\r
+\r
+> +    notmuch_bool_t batch = FALSE;\r
+> +    FILE *input = stdin;\r
+> +    char *input_file_name = NULL;\r
+> +    int i, opt_index;\r
+> +    int ret = 0;\r
+>  \r
+>      /* Setup our handler for SIGINT */\r
+>      memset (&action, 0, sizeof (struct sigaction));\r
+> @@ -187,53 +171,76 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+>      action.sa_flags = SA_RESTART;\r
+>      sigaction (SIGINT, &action, NULL);\r
+>  \r
+> -    argc--; argv++; /* skip subcommand argument */\r
+> +    notmuch_opt_desc_t options[] = {\r
+> +    { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },\r
+> +    { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
+> +    { 0, 0, 0, 0, 0 }\r
+> +    };\r
+>  \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
+> +    opt_index = parse_arguments (argc, argv, options, 1);\r
+> +    if (opt_index < 0)\r
+>      return 1;\r
+> +\r
+> +    if (input_file_name) {\r
+> +    batch = TRUE;\r
+> +    input = fopen (input_file_name, "r");\r
+> +    if (input == NULL) {\r
+> +        fprintf (stderr, "Error opening %s for reading: %s\n",\r
+> +                 input_file_name, strerror (errno));\r
+> +        return 1;\r
+> +    }\r
+>      }\r
+>  \r
+> -    for (i = 0; i < argc; i++) {\r
+> -    if (strcmp (argv[i], "--") == 0) {\r
+> -        i++;\r
+> -        break;\r
+> +    if (batch) {\r
+> +    if (opt_index != argc) {\r
+> +        fprintf (stderr, "Can't specify both cmdline and stdin!\n");\r
+> +        return 1;\r
+> +    }\r
+> +    } else {\r
+> +    /* Array of tagging operations (add or remove), terminated with an\r
+> +     * empty element. */\r
+> +    tag_ops = tag_op_list_create (ctx);\r
+> +    if (tag_ops == NULL) {\r
+> +        fprintf (stderr, "Out of memory.\n");\r
+> +        return 1;\r
+>      }\r
+> -    if (argv[i][0] == '+' || argv[i][0] == '-') {\r
+> -        if (argv[i][0] == '+' && argv[i][1] == '\0') {\r
+> -            fprintf (stderr, "Error: tag names cannot be empty.\n");\r
+> -            return 1;\r
+> +\r
+> +    for (i = opt_index; i < argc; i++) {\r
+> +        if (strcmp (argv[i], "--") == 0) {\r
+> +            i++;\r
+> +            break;\r
+>          }\r
+> -        if (argv[i][0] == '+' && argv[i][1] == '-') {\r
+> -            /* This disallows adding the non-removable tag "-" and\r
+> -             * enables notmuch tag to take long options in the\r
+> -             * future. */\r
+> -            fprintf (stderr, "Error: tag names must not start with '-'.\n");\r
+> -            return 1;\r
+> +        if (argv[i][0] == '+' || argv[i][0] == '-') {\r
+> +            /* FIXME: checks should be consistent with those in batch tagging */\r
+> +            if (argv[i][0] == '+' && argv[i][1] == '\0') {\r
+> +                fprintf (stderr, "Error: tag names cannot be empty.\n");\r
+> +                return 1;\r
+> +            }\r
+> +            if (argv[i][0] == '+' && argv[i][1] == '-') {\r
+> +                /* This disallows adding the non-removable tag "-" and\r
+> +                 * enables notmuch tag to take long options in the\r
+> +                 * future. */\r
+> +                fprintf (stderr, "Error: tag names must not start with '-'.\n");\r
+> +                return 1;\r
+> +            }\r
+> +            tag_op_list_append (ctx, tag_ops,\r
+> +                                argv[i] + 1, (argv[i][0] == '-'));\r
+> +        } else {\r
+> +            break;\r
+>          }\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
+> -    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
+> +    if (tag_op_list_size (tag_ops) == 0) {\r
+> +        fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");\r
+> +        return 1;\r
+> +    }\r
+>  \r
+> -    query_string = query_string_from_args (ctx, argc - i, &argv[i]);\r
+> +    query_string = query_string_from_args (ctx, argc - i, &argv[i]);\r
+>  \r
+> -    if (*query_string == '\0') {\r
+> -    fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");\r
+> -    return 1;\r
+> +    if (*query_string == '\0') {\r
+> +        fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");\r
+> +        return 1;\r
+> +    }\r
+>      }\r
+>  \r
+>      config = notmuch_config_open (ctx, NULL, NULL);\r
+> @@ -244,11 +251,46 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+>                             NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
+>      return 1;\r
+>  \r
+> -    synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+> +    if (notmuch_config_get_maildir_synchronize_flags (config))\r
+> +    synchronize_flags = TAG_FLAG_MAILDIR_SYNC;\r
+\r
+Use |= to be future compatible?\r
+\r
+> +\r
+> +    if (batch) {\r
+> +\r
+> +    char *line = NULL;\r
+> +    size_t line_size = 0;\r
+> +    ssize_t line_len;\r
+> +    int ret = 0;\r
+> +    tag_op_list_t *tag_ops;\r
+\r
+The two above shadow the function level variables.\r
+\r
+> +\r
+> +    tag_ops = tag_op_list_create (ctx);\r
+> +    if (tag_ops == NULL) {\r
+> +        fprintf (stderr, "Out of memory.\n");\r
+> +        return 1;\r
+> +    }\r
+>  \r
+> -    ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);\r
+> +    while ((line_len = getline (&line, &line_size, input)) != -1 &&\r
+> +           ! interrupted) {\r
+> +\r
+> +        char *query_string;\r
+> +\r
+> +        ret =  parse_tag_line (ctx, line, TAG_FLAG_NONE,\r
+> +                               &query_string, tag_ops);\r
+\r
+Extra space after =.\r
+\r
+> +\r
+> +        if (ret > 0)\r
+> +            continue;\r
+> +\r
+> +        if (ret < 0 || tag_query (ctx, notmuch, query_string,\r
+> +                                  tag_ops, synchronize_flags))\r
+> +            break;\r
+> +    }\r
+> +\r
+> +    if (line)\r
+> +        free (line);\r
+> +    } else\r
+> +    ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);\r
+\r
+I'd like braces around the else block too.\r
+\r
+>  \r
+>      notmuch_database_destroy (notmuch);\r
+>  \r
+> -    return ret;\r
+> +    return ret || interrupted;\r
+> +\r
+>  }\r
+> -- \r
+> 1.7.10.4\r