]> git.notmuchmail.org Git - notmuch/commitdiff
cli: add support for notmuch search --duplicate=N with --output=messages
authorJani Nikula <jani@nikula.org>
Sat, 1 Nov 2014 09:31:06 +0000 (11:31 +0200)
committerDavid Bremner <david@tethera.net>
Sun, 2 Nov 2014 18:42:12 +0000 (19:42 +0100)
Print the message IDs of all messages matching the search terms that
have at least N files associated with them.

doc/man1/notmuch-search.rst
notmuch-search.c

index b6607c922cc083c35b9add629c771d6d3927da1b..8110086eff4fa29953c10d7e18f84bc3cecc4e28 100644 (file)
@@ -142,10 +142,14 @@ Supported options for **search** include
         rather than the number of matching messages.
 
     ``--duplicate=N``
         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
 
         Note that this option is orthogonal with the **folder:** search
         prefix. The prefix matches messages based on filenames. This
index 671fe4139981055e77e7f6c7c269a4bdb77ccb81..6345fb6280ac3a336ade0fa576722a8265c2cccf 100644 (file)
@@ -310,6 +310,24 @@ process_address_header (const search_options_t *opt, const char *value)
     g_object_unref (list);
 }
 
     g_object_unref (list);
 }
 
+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 (search_options_t *opt)
 {
 static int
 do_search_messages (search_options_t *opt)
 {
@@ -357,10 +375,13 @@ do_search_messages (search_options_t *opt)
            notmuch_filenames_destroy( filenames );
 
        } else if (opt->output == OUTPUT_MESSAGES) {
            notmuch_filenames_destroy( filenames );
 
        } else if (opt->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 (opt->dupe <= 1 || opt->dupe <= _count_filenames (message)) {
+                format->set_prefix (format, "id");
+                format->string (format,
+                                notmuch_message_get_message_id (message));
+                format->separator (format);
+            }
        } else {
            if (opt->output & OUTPUT_SENDER) {
                const char *addrs;
        } else {
            if (opt->output & OUTPUT_SENDER) {
                const char *addrs;
@@ -503,6 +524,12 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     if (! opt.output)
        opt.output = OUTPUT_SUMMARY;
 
     if (! opt.output)
        opt.output = OUTPUT_SUMMARY;
 
+    if (opt.output != OUTPUT_FILES && opt.output != OUTPUT_MESSAGES &&
+       opt.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:
        opt.format = sprinter_text_create (config, stdout);
     switch (format_sel) {
     case NOTMUCH_FORMAT_TEXT:
        opt.format = sprinter_text_create (config, stdout);