--- /dev/null
+Return-Path: <bremner@tethera.net>\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 3EFD6429E44\r
+ for <notmuch@notmuchmail.org>; Tue, 11 Mar 2014 16:03:32 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ 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 fdI+qZwqSgsx for <notmuch@notmuchmail.org>;\r
+ Tue, 11 Mar 2014 16:03:28 -0700 (PDT)\r
+Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
+ (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 59A42429E4C\r
+ for <notmuch@notmuchmail.org>; Tue, 11 Mar 2014 16:02:35 -0700 (PDT)\r
+Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1WNVgw-0001wH-NY; Tue, 11 Mar 2014 20:02:34 -0300\r
+Received: (nullmailer pid 25853 invoked by uid 1000); Tue, 11 Mar 2014\r
+ 23:01:46 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v6 07/14] lib: make folder: prefix literal\r
+Date: Tue, 11 Mar 2014 20:01:33 -0300\r
+Message-Id: <1394578900-25618-8-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.8.5.3\r
+In-Reply-To: <1394578900-25618-1-git-send-email-david@tethera.net>\r
+References: <1394578900-25618-1-git-send-email-david@tethera.net>\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: Tue, 11 Mar 2014 23:03:32 -0000\r
+\r
+From: Jani Nikula <jani@nikula.org>\r
+\r
+In xapian terms, convert folder: prefix from probabilistic to boolean\r
+prefix, matching the paths, relative from the maildir root, of the\r
+message files, ignoring the maildir new and cur leaf directories.\r
+\r
+folder:foo matches all message files in foo, foo/new, and foo/cur.\r
+\r
+folder:foo/new does *not* match message files in foo/new.\r
+\r
+folder:"" matches all message files in the top level maildir and its\r
+new and cur subdirectories.\r
+\r
+This change constitutes a database change: bump the database version\r
+and add database upgrade support for folder: terms. The upgrade also\r
+adds path: terms.\r
+\r
+Finally, fix the folder search test for literal folder: search, as\r
+some of the folder: matching capabilities are lost in the\r
+probabilistic to boolean prefix change.\r
+---\r
+ lib/database.cc | 44 ++++++++++++++++++++++--\r
+ lib/message.cc | 80 +++++++++++++++++++++++++++++++++++++------\r
+ lib/notmuch-private.h | 3 ++\r
+ test/T100-search-by-folder.sh | 24 +++++++++++--\r
+ 4 files changed, 135 insertions(+), 16 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 93cc7f5..aef748f 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -42,7 +42,7 @@ typedef struct {\r
+ const char *prefix;\r
+ } prefix_t;\r
+ \r
+-#define NOTMUCH_DATABASE_VERSION 1\r
++#define NOTMUCH_DATABASE_VERSION 2\r
+ \r
+ #define STRINGIFY(s) _SUB_STRINGIFY(s)\r
+ #define _SUB_STRINGIFY(s) #s\r
+@@ -210,6 +210,13 @@ static prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {\r
+ { "is", "K" },\r
+ { "id", "Q" },\r
+ { "path", "P" },\r
++ /*\r
++ * Without the ":", since this is a multi-letter prefix, Xapian\r
++ * will add a colon itself if the first letter of the path is\r
++ * upper-case ASCII. Including the ":" forces there to always be a\r
++ * colon, which keeps our own logic simpler.\r
++ */\r
++ { "folder", "XFOLDER:" },\r
+ };\r
+ \r
+ static prefix_t PROBABILISTIC_PREFIX[]= {\r
+@@ -217,7 +224,6 @@ static prefix_t PROBABILISTIC_PREFIX[]= {\r
+ { "to", "XTO" },\r
+ { "attachment", "XATTACHMENT" },\r
+ { "subject", "XSUBJECT"},\r
+- { "folder", "XFOLDER"}\r
+ };\r
+ \r
+ const char *\r
+@@ -1168,6 +1174,40 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ }\r
+ }\r
+ \r
++ /*\r
++ * Prior to version 2, the "folder:" prefix was probabilistic and\r
++ * stemmed. Change it to the current boolean prefix. Add "path:"\r
++ * prefixes while at it.\r
++ */\r
++ if (version < 2) {\r
++ notmuch_query_t *query = notmuch_query_create (notmuch, "");\r
++ notmuch_messages_t *messages;\r
++ notmuch_message_t *message;\r
++\r
++ count = 0;\r
++ total = notmuch_query_count_messages (query);\r
++\r
++ for (messages = notmuch_query_search_messages (query);\r
++ notmuch_messages_valid (messages);\r
++ notmuch_messages_move_to_next (messages)) {\r
++ if (do_progress_notify) {\r
++ progress_notify (closure, (double) count / total);\r
++ do_progress_notify = 0;\r
++ }\r
++\r
++ message = notmuch_messages_get (messages);\r
++\r
++ _notmuch_message_upgrade_folder (message);\r
++ _notmuch_message_sync (message);\r
++\r
++ notmuch_message_destroy (message);\r
++\r
++ count++;\r
++ }\r
++\r
++ notmuch_query_destroy (query);\r
++ }\r
++\r
+ db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));\r
+ db->flush ();\r
+ \r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 21abe8e..9243b76 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -504,6 +504,56 @@ _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)\r
+ }\r
+ }\r
+ \r
++/* Return true if p points at "new" or "cur". */\r
++static bool is_maildir (const char *p)\r
++{\r
++ return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;\r
++}\r
++\r
++/* Add "folder:" term for directory. */\r
++static notmuch_status_t\r
++_notmuch_message_add_folder_terms (notmuch_message_t *message,\r
++ const char *directory)\r
++{\r
++ char *folder, *last;\r
++\r
++ folder = talloc_strdup (NULL, directory);\r
++ if (! folder)\r
++ return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++\r
++ /*\r
++ * If the message file is in a leaf directory named "new" or\r
++ * "cur", presume maildir and index the parent directory. Thus a\r
++ * "folder:" prefix search matches messages in the specified\r
++ * maildir folder, i.e. in the specified directory and its "new"\r
++ * and "cur" subdirectories.\r
++ *\r
++ * Note that this means the "folder:" prefix can't be used for\r
++ * distinguishing between message files in "new" or "cur". The\r
++ * "path:" prefix needs to be used for that.\r
++ *\r
++ * Note the deliberate difference to _filename_is_in_maildir(). We\r
++ * don't want to index different things depending on the existence\r
++ * or non-existence of all maildir sibling directories "new",\r
++ * "cur", and "tmp". Doing so would be surprising, and difficult\r
++ * for the user to fix in case all subdirectories were not in\r
++ * place during indexing.\r
++ */\r
++ last = strrchr (folder, '/');\r
++ if (last) {\r
++ if (is_maildir (last + 1))\r
++ *last = '\0';\r
++ } else if (is_maildir (folder)) {\r
++ *folder = '\0';\r
++ }\r
++\r
++ _notmuch_message_add_term (message, "folder", folder);\r
++\r
++ talloc_free (folder);\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
+ #define RECURSIVE_SUFFIX "/**"\r
+ \r
+ /* Add "path:" terms for directory. */\r
+@@ -570,9 +620,8 @@ _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)\r
+ directory = _notmuch_database_get_directory_path (ctx,\r
+ message->notmuch,\r
+ directory_id);\r
+- if (strlen (directory))\r
+- _notmuch_message_gen_terms (message, "folder", directory);\r
+ \r
++ _notmuch_message_add_folder_terms (message, directory);\r
+ _notmuch_message_add_path_terms (message, directory);\r
+ }\r
+ \r
+@@ -610,9 +659,7 @@ _notmuch_message_add_filename (notmuch_message_t *message,\r
+ * notmuch_directory_get_child_files() . */\r
+ _notmuch_message_add_term (message, "file-direntry", direntry);\r
+ \r
+- /* New terms allow user to search with folder: specification. */\r
+- _notmuch_message_gen_terms (message, "folder", directory);\r
+-\r
++ _notmuch_message_add_folder_terms (message, directory);\r
+ _notmuch_message_add_path_terms (message, directory);\r
+ \r
+ talloc_free (local);\r
+@@ -637,8 +684,6 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+ const char *filename)\r
+ {\r
+ void *local = talloc_new (message);\r
+- const char *folder_prefix = _find_prefix ("folder");\r
+- char *zfolder_prefix = talloc_asprintf(local, "Z%s", folder_prefix);\r
+ char *direntry;\r
+ notmuch_private_status_t private_status;\r
+ notmuch_status_t status;\r
+@@ -659,10 +704,7 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+ /* Re-synchronize "folder:" and "path:" terms for this message. */\r
+ \r
+ /* Remove all "folder:" terms. */\r
+- _notmuch_message_remove_terms (message, folder_prefix);\r
+-\r
+- /* Remove all "folder:" stemmed terms. */\r
+- _notmuch_message_remove_terms (message, zfolder_prefix);\r
++ _notmuch_message_remove_terms (message, _find_prefix ("folder"));\r
+ \r
+ /* Remove all "path:" terms. */\r
+ _notmuch_message_remove_terms (message, _find_prefix ("path"));\r
+@@ -675,6 +717,22 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+ return status;\r
+ }\r
+ \r
++/* Upgrade the "folder:" prefix from V1 to V2. */\r
++#define FOLDER_PREFIX_V1 "XFOLDER"\r
++#define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1\r
++void\r
++_notmuch_message_upgrade_folder (notmuch_message_t *message)\r
++{\r
++ /* Remove all old "folder:" terms. */\r
++ _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);\r
++\r
++ /* Remove all old "folder:" stemmed terms. */\r
++ _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);\r
++\r
++ /* Add new boolean "folder:" and "path:" terms. */\r
++ _notmuch_message_add_directory_terms (message, message);\r
++}\r
++\r
+ char *\r
+ _notmuch_message_talloc_copy_data (notmuch_message_t *message)\r
+ {\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index af185c7..59eb2bc 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -263,6 +263,9 @@ _notmuch_message_gen_terms (notmuch_message_t *message,\r
+ void\r
+ _notmuch_message_upgrade_filename_storage (notmuch_message_t *message);\r
+ \r
++void\r
++_notmuch_message_upgrade_folder (notmuch_message_t *message);\r
++\r
+ notmuch_status_t\r
+ _notmuch_message_add_filename (notmuch_message_t *message,\r
+ const char *filename);\r
+diff --git a/test/T100-search-by-folder.sh b/test/T100-search-by-folder.sh\r
+index 5cc2ca8..a7f63dd 100755\r
+--- a/test/T100-search-by-folder.sh\r
++++ b/test/T100-search-by-folder.sh\r
+@@ -3,6 +3,7 @@ test_description='"notmuch search" by folder: (with variations)'\r
+ . ./test-lib.sh\r
+ \r
+ add_message '[dir]=bad' '[subject]="To the bone"'\r
++add_message '[dir]=.' '[subject]="Top level"'\r
+ add_message '[dir]=bad/news' '[subject]="Bears"'\r
+ mkdir -p "${MAIL_DIR}/duplicate/bad/news"\r
+ cp "$gen_msg_filename" "${MAIL_DIR}/duplicate/bad/news"\r
+@@ -12,29 +13,46 @@ add_message '[dir]=things/favorite' '[subject]="Raindrops, whiskers, kettles"'\r
+ add_message '[dir]=things/bad' '[subject]="Bites, stings, sad feelings"'\r
+ \r
+ test_begin_subtest "Single-world folder: specification (multiple results)"\r
+-output=$(notmuch search folder:bad | notmuch_search_sanitize)\r
++output=$(notmuch search folder:bad folder:bad/news folder:things/bad | notmuch_search_sanitize)\r
+ test_expect_equal "$output" "thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; To the bone (inbox unread)\r
+ thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Bears (inbox unread)\r
+ thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Bites, stings, sad feelings (inbox unread)"\r
+ \r
++test_begin_subtest "Top level folder"\r
++output=$(notmuch search folder:'""' | notmuch_search_sanitize)\r
++test_expect_equal "$output" "thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Top level (inbox unread)"\r
++\r
+ test_begin_subtest "Two-word path to narrow results to one"\r
+ output=$(notmuch search folder:bad/news | notmuch_search_sanitize)\r
+ test_expect_equal "$output" "thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Bears (inbox unread)"\r
+ \r
++test_begin_subtest "Folder search with --output=files"\r
++output=$(notmuch search --output=files folder:bad/news | notmuch_search_files_sanitize)\r
++test_expect_equal "$output" "MAIL_DIR/bad/news/msg-003\r
++MAIL_DIR/duplicate/bad/news/msg-003"\r
++\r
+ test_begin_subtest "After removing duplicate instance of matching path"\r
+ rm -r "${MAIL_DIR}/bad/news"\r
+ notmuch new\r
+ output=$(notmuch search folder:bad/news | notmuch_search_sanitize)\r
++test_expect_equal "$output" ""\r
++\r
++test_begin_subtest "Folder search with --output=files part #2"\r
++output=$(notmuch search --output=files folder:duplicate/bad/news | notmuch_search_files_sanitize)\r
++test_expect_equal "$output" "MAIL_DIR/duplicate/bad/news/msg-003"\r
++\r
++test_begin_subtest "After removing duplicate instance of matching path part #2"\r
++output=$(notmuch search folder:duplicate/bad/news | notmuch_search_sanitize)\r
+ test_expect_equal "$output" "thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Bears (inbox unread)"\r
+ \r
+ test_begin_subtest "After rename, old path returns nothing"\r
+ mv "${MAIL_DIR}/duplicate/bad/news" "${MAIL_DIR}/duplicate/bad/olds"\r
+ notmuch new\r
+-output=$(notmuch search folder:bad/news | notmuch_search_sanitize)\r
++output=$(notmuch search folder:duplicate/bad/news | notmuch_search_sanitize)\r
+ test_expect_equal "$output" ""\r
+ \r
+ test_begin_subtest "After rename, new path returns result"\r
+-output=$(notmuch search folder:bad/olds | notmuch_search_sanitize)\r
++output=$(notmuch search folder:duplicate/bad/olds | notmuch_search_sanitize)\r
+ test_expect_equal "$output" "thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Bears (inbox unread)"\r
+ \r
+ test_done\r
+-- \r
+1.8.5.3\r
+\r