[PATCH] cli: add support for notmuch search --duplicate=N with --output=messages

David Edmondson dme at dme.org
Fri Oct 31 00:14:19 PDT 2014


On Thu, Oct 30 2014, Jani Nikula wrote:
> Print the message IDs of all messages matching the search terms that
> have at least N files associated with them.

Briefly tested as working. I commend this patch to the masters of push.

Thanks Jani!

> ---
>  doc/man1/notmuch-search.rst | 12 ++++++++----
>  notmuch-search.c            | 34 ++++++++++++++++++++++++++++++----
>  2 files changed, 38 insertions(+), 8 deletions(-)
>
> diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst
> index 90160f21e23c..aeba4bf604f6 100644
> --- a/doc/man1/notmuch-search.rst
> +++ b/doc/man1/notmuch-search.rst
> @@ -122,10 +122,14 @@ Supported options for **search** include
>          rather than the number of matching messages.
>  
>      ``--duplicate=N``
> -        Effective with ``--output=files``, output the Nth filename
> -        associated with each message matching the query (N is 1-based).
> -        If N is greater than the number of files associated with the
> -        message, don't print anything.
> +        For ``--output=files``, output the Nth filename associated
> +        with each message matching the query (N is 1-based). If N is
> +        greater than the number of files associated with the message,
> +        don't print anything.
> +
> +        For ``--output=messages``, only output message IDs of messages
> +        matching the search terms that have at least N filenames
> +        associated with them.
>  
>          Note that this option is orthogonal with the **folder:** search
>          prefix. The prefix matches messages based on filenames. This
> diff --git a/notmuch-search.c b/notmuch-search.c
> index bc9be4593ecc..2bf876fd5abf 100644
> --- a/notmuch-search.c
> +++ b/notmuch-search.c
> @@ -215,6 +215,24 @@ do_search_threads (sprinter_t *format,
>  }
>  
>  static int
> +_count_filenames (notmuch_message_t *message)
> +{
> +    notmuch_filenames_t *filenames;
> +    int i = 0;
> +
> +    filenames = notmuch_message_get_filenames (message);
> +
> +    while (notmuch_filenames_valid (filenames)) {
> +	notmuch_filenames_move_to_next (filenames);
> +	i++;
> +    }
> +
> +    notmuch_filenames_destroy (filenames);
> +
> +    return i;
> +}
> +
> +static int
>  do_search_messages (sprinter_t *format,
>  		    notmuch_query_t *query,
>  		    output_t output,
> @@ -265,10 +283,13 @@ do_search_messages (sprinter_t *format,
>  	    notmuch_filenames_destroy( filenames );
>  
>  	} else { /* output == OUTPUT_MESSAGES */
> -	    format->set_prefix (format, "id");
> -	    format->string (format,
> -			    notmuch_message_get_message_id (message));
> -	    format->separator (format);
> +	    /* special case 1 for speed */
> +	    if (dupe <= 1 || dupe <= _count_filenames (message)) {
> +		format->set_prefix (format, "id");
> +		format->string (format,
> +				notmuch_message_get_message_id (message));
> +		format->separator (format);
> +	    }
>  	}
>  
>  	notmuch_message_destroy (message);
> @@ -387,6 +408,11 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
>      if (opt_index < 0)
>  	return EXIT_FAILURE;
>  
> +    if (output != OUTPUT_FILES && output != OUTPUT_MESSAGES && dupe != -1) {
> +	fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
> +	return EXIT_FAILURE;
> +    }
> +
>      switch (format_sel) {
>      case NOTMUCH_FORMAT_TEXT:
>  	format = sprinter_text_create (config, stdout);
> -- 
> 2.1.1


More information about the notmuch mailing list