]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-search.c
test: Test upgrade to ghost messages feature
[notmuch] / notmuch-search.c
index 621862289f043ba4e77b488a176e34937aaa3e3d..bc9be4593ecc0a06b34e82bab62b9a034ffb9f40 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "notmuch-client.h"
 #include "sprinter.h"
+#include "string-util.h"
 
 typedef enum {
     OUTPUT_SUMMARY,
@@ -29,21 +30,43 @@ typedef enum {
     OUTPUT_TAGS
 } output_t;
 
-static char *
-sanitize_string (const void *ctx, const char *str)
+/* Return two stable query strings that identify exactly the matched
+ * and unmatched messages currently in thread.  If there are no
+ * matched or unmatched messages, the returned buffers will be
+ * NULL. */
+static int
+get_thread_query (notmuch_thread_t *thread,
+                 char **matched_out, char **unmatched_out)
 {
-    char *out, *loop;
-
-    if (NULL == str)
-       return NULL;
+    notmuch_messages_t *messages;
+    char *escaped = NULL;
+    size_t escaped_len = 0;
 
-    loop = out = talloc_strdup (ctx, str);
+    *matched_out = *unmatched_out = NULL;
 
-    for (; *loop; loop++) {
-       if ((unsigned char)(*loop) < 32)
-           *loop = '?';
+    for (messages = notmuch_thread_get_messages (thread);
+        notmuch_messages_valid (messages);
+        notmuch_messages_move_to_next (messages))
+    {
+       notmuch_message_t *message = notmuch_messages_get (messages);
+       const char *mid = notmuch_message_get_message_id (message);
+       /* Determine which query buffer to extend */
+       char **buf = notmuch_message_get_flag (
+           message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
+       /* Add this message's id: query.  Since "id" is an exclusive
+        * prefix, it is implicitly 'or'd together, so we only need to
+        * join queries with a space. */
+       if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
+           return -1;
+       if (*buf)
+           *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
+       else
+           *buf = talloc_strdup (thread, escaped);
+       if (!*buf)
+           return -1;
     }
-    return out;
+    talloc_free (escaped);
+    return 0;
 }
 
 static int
@@ -131,6 +154,25 @@ do_search_threads (sprinter_t *format,
                format->string (format, authors);
                format->map_key (format, "subject");
                format->string (format, subject);
+               if (notmuch_format_version >= 2) {
+                   char *matched_query, *unmatched_query;
+                   if (get_thread_query (thread, &matched_query,
+                                         &unmatched_query) < 0) {
+                       fprintf (stderr, "Out of memory\n");
+                       return 1;
+                   }
+                   format->map_key (format, "query");
+                   format->begin_list (format);
+                   if (matched_query)
+                       format->string (format, matched_query);
+                   else
+                       format->null (format);
+                   if (unmatched_query)
+                       format->string (format, unmatched_query);
+                   else
+                       format->null (format);
+                   format->end (format);
+               }
            }
 
            talloc_free (ctx_quote);
@@ -177,7 +219,8 @@ do_search_messages (sprinter_t *format,
                    notmuch_query_t *query,
                    output_t output,
                    int offset,
-                   int limit)
+                   int limit,
+                   int dupe)
 {
     notmuch_message_t *message;
     notmuch_messages_t *messages;
@@ -206,14 +249,17 @@ do_search_messages (sprinter_t *format,
        message = notmuch_messages_get (messages);
 
        if (output == OUTPUT_FILES) {
+           int j;
            filenames = notmuch_message_get_filenames (message);
 
-           for (;
+           for (j = 1;
                 notmuch_filenames_valid (filenames);
-                notmuch_filenames_move_to_next (filenames))
+                notmuch_filenames_move_to_next (filenames), j++)
            {
-               format->string (format, notmuch_filenames_get (filenames));
-               format->separator (format);
+               if (dupe < 0 || dupe == j) {
+                   format->string (format, notmuch_filenames_get (filenames));
+                   format->separator (format);
+               }
            }
            
            notmuch_filenames_destroy( filenames );
@@ -283,16 +329,9 @@ do_search_tags (notmuch_database_t *notmuch,
     return 0;
 }
 
-enum {
-    EXCLUDE_TRUE,
-    EXCLUDE_FALSE,
-    EXCLUDE_FLAG,
-};
-
 int
-notmuch_search_command (void *ctx, int argc, char *argv[])
+notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 {
-    notmuch_config_t *config;
     notmuch_database_t *notmuch;
     notmuch_query_t *query;
     char *query_str;
@@ -302,11 +341,16 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     output_t output = OUTPUT_SUMMARY;
     int offset = 0;
     int limit = -1; /* unlimited */
-    int exclude = EXCLUDE_TRUE;
+    notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
+    int dupe = -1;
     unsigned int i;
 
-    enum { NOTMUCH_FORMAT_JSON, NOTMUCH_FORMAT_TEXT, NOTMUCH_FORMAT_SEXP }
-       format_sel = NOTMUCH_FORMAT_TEXT;
+    enum {
+       NOTMUCH_FORMAT_JSON,
+       NOTMUCH_FORMAT_TEXT,
+       NOTMUCH_FORMAT_TEXT0,
+       NOTMUCH_FORMAT_SEXP
+    } format_sel = NOTMUCH_FORMAT_TEXT;
 
     notmuch_opt_desc_t options[] = {
        { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
@@ -317,7 +361,9 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
          (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
                                  { "sexp", NOTMUCH_FORMAT_SEXP },
                                  { "text", NOTMUCH_FORMAT_TEXT },
+                                 { "text0", NOTMUCH_FORMAT_TEXT0 },
                                  { 0, 0 } } },
+       { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
        { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
          (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
                                  { "threads", OUTPUT_THREADS },
@@ -326,71 +372,76 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
                                  { "tags", OUTPUT_TAGS },
                                  { 0, 0 } } },
         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
-          (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
-                                  { "false", EXCLUDE_FALSE },
-                                  { "flag", EXCLUDE_FLAG },
+          (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
+                                  { "false", NOTMUCH_EXCLUDE_FALSE },
+                                  { "flag", NOTMUCH_EXCLUDE_FLAG },
+                                  { "all", NOTMUCH_EXCLUDE_ALL },
                                   { 0, 0 } } },
        { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
        { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0  },
+       { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0  },
        { 0, 0, 0, 0, 0 }
     };
 
     opt_index = parse_arguments (argc, argv, options, 1);
-
-    if (opt_index < 0) {
-       return 1;
-    }
+    if (opt_index < 0)
+       return EXIT_FAILURE;
 
     switch (format_sel) {
     case NOTMUCH_FORMAT_TEXT:
-       format = sprinter_text_create (ctx, stdout);
+       format = sprinter_text_create (config, stdout);
+       break;
+    case NOTMUCH_FORMAT_TEXT0:
+       if (output == OUTPUT_SUMMARY) {
+           fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
+           return EXIT_FAILURE;
+       }
+       format = sprinter_text0_create (config, stdout);
        break;
     case NOTMUCH_FORMAT_JSON:
-       format = sprinter_json_create (ctx, stdout);
+       format = sprinter_json_create (config, stdout);
        break;
     case NOTMUCH_FORMAT_SEXP:
-       format = sprinter_sexp_create (ctx, stdout);
+       format = sprinter_sexp_create (config, stdout);
        break;
     default:
        /* this should never happen */
        INTERNAL_ERROR("no output format selected");
     }
 
-    config = notmuch_config_open (ctx, NULL, NULL);
-    if (config == NULL)
-       return 1;
+    notmuch_exit_if_unsupported_format ();
 
     if (notmuch_database_open (notmuch_config_get_database_path (config),
                               NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
-       return 1;
+       return EXIT_FAILURE;
 
     query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
     if (query_str == NULL) {
        fprintf (stderr, "Out of memory.\n");
-       return 1;
+       return EXIT_FAILURE;
     }
     if (*query_str == '\0') {
        fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
-       return 1;
+       return EXIT_FAILURE;
     }
 
     query = notmuch_query_create (notmuch, query_str);
     if (query == NULL) {
        fprintf (stderr, "Out of memory\n");
-       return 1;
+       return EXIT_FAILURE;
     }
 
     notmuch_query_set_sort (query, sort);
 
-    if (exclude == EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {
+    if (exclude == NOTMUCH_EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {
        /* If we are not doing summary output there is nowhere to
         * print the excluded flag so fall back on including the
         * excluded messages. */
        fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
-       exclude = EXCLUDE_FALSE;
+       exclude = NOTMUCH_EXCLUDE_FALSE;
     }
 
-    if (exclude == EXCLUDE_TRUE || exclude == EXCLUDE_FLAG) {
+    if (exclude != NOTMUCH_EXCLUDE_FALSE) {
        const char **search_exclude_tags;
        size_t search_exclude_tags_length;
 
@@ -398,8 +449,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
            (config, &search_exclude_tags_length);
        for (i = 0; i < search_exclude_tags_length; i++)
            notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
-       if (exclude == EXCLUDE_FLAG)
-           notmuch_query_set_omit_excluded (query, FALSE);
+       notmuch_query_set_omit_excluded (query, exclude);
     }
 
     switch (output) {
@@ -410,7 +460,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
        break;
     case OUTPUT_MESSAGES:
     case OUTPUT_FILES:
-       ret = do_search_messages (format, query, output, offset, limit);
+       ret = do_search_messages (format, query, output, offset, limit, dupe);
        break;
     case OUTPUT_TAGS:
        ret = do_search_tags (notmuch, format, query);
@@ -422,5 +472,5 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 
     talloc_free (format);
 
-    return ret;
+    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
 }