--- /dev/null
+Return-Path: <aclements@csail.mit.edu>\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 1B883431FBF\r
+ for <notmuch@notmuchmail.org>; Mon, 6 Oct 2014 16:17:30 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] 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 kqGYkVidWxFZ for <notmuch@notmuchmail.org>;\r
+ Mon, 6 Oct 2014 16:17:26 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-3.mit.edu (dmz-mailsec-scanner-3.mit.edu\r
+ [18.9.25.14])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id B04A2431FAE\r
+ for <notmuch@notmuchmail.org>; Mon, 6 Oct 2014 16:17:26 -0700 (PDT)\r
+X-AuditID: 1209190e-f79d46d000003643-42-5433230508c4\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 99.5D.13891.50323345; Mon, 6 Oct 2014 19:17:25 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s96NHPVd025203; \r
+ Mon, 6 Oct 2014 19:17:25 -0400\r
+Received: from drake.dyndns.org ([12.185.136.2]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s96NHMsj014457\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Mon, 6 Oct 2014 19:17:24 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <aclements@csail.mit.edu>)\r
+ id 1XbHWr-0001JF-ED; Mon, 06 Oct 2014 17:17:21 -0600\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 00/12] Add ghost messages and fix thread linking\r
+Date: Mon, 6 Oct 2014 17:17:06 -0600\r
+Message-Id: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFlrKIsWRmVeSWpSXmKPExsUixG6nosuqbBxi0DXT0OJGazejxfWbM5kd\r
+ mDyerbrF7LHl0HvmAKYoLpuU1JzMstQifbsErowzy9+xFmxSqJg57R9jA+M8qS5GTg4JAROJ\r
+ yce3MkLYYhIX7q1n62Lk4hASmM0ksfvqXEYIZwOjRMOpJlYIZymTxM3Xe9hBWoQEljBK3LkI\r
+ ZrMJ6EusWDuJFcQWEZCW2Hl3NpjNLCAqMef/PTBbWMBB4lVjIxOIzSKgKvHk6x8wm1fATWL9\r
+ /MUsEGfISWzY/Z9xAiPvAkaGVYyyKblVurmJmTnFqcm6xcmJeXmpRbrGermZJXqpKaWbGEGh\r
+ wSnJt4Px60GlQ4wCHIxKPLwROwxDhFgTy4orcw8xSnIwKYnyzmE3DhHiS8pPqcxILM6ILyrN\r
+ SS0+xCjBwawkwsv12yhEiDclsbIqtSgfJiXNwaIkzrvpB1+IkEB6YklqdmpqQWoRTFaGg0NJ\r
+ gveQItBQwaLU9NSKtMycEoQ0EwcnyHAeoOH/QGp4iwsSc4sz0yHypxgVpcR5k5WAEgIgiYzS\r
+ PLheWOy+YhQHekWY9yZIOw8w7uG6XwENZgIabDpHH2RwSSJCSqqBcVHIKUb/T+UXP7Rm+Ean\r
+ 6l73bGH8b3fhJvei3Gbe/u+b6vwP3j/KlrL8jVGpWGHBOsX604eOxdhJfpzcs3n2omTO6ZwM\r
+ s77rL39lNmF+bENgaurtEmX/+KX7sm/ve7PpJnNMq/7TX1Wt+lt92R7lpFmK3wn6rDWt5mJZ\r
+ vsGunSnTguVi7hpPVGIpzkg01GIuKk4EAJjvj4S4AgAA\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: Mon, 06 Oct 2014 23:17:30 -0000\r
+\r
+This is v2 of the\r
+id:1412345958-8278-1-git-send-email-aclements@csail.mit.edu. This\r
+adds some comments and clarifies some code as suggested by David.\r
+Patch 6 is new in v2 and adds some bit-twiddling macros for clarity\r
+and robustness in later patches.\r
+\r
+The diff from v1 is below.\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 4655f59..6e51a72 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -1277,6 +1277,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ ++total;\r
+ }\r
+ if (new_features & NOTMUCH_FEATURE_GHOSTS) {\r
++ /* The ghost message upgrade converts all thread_id_*\r
++ * metadata values into ghost message documents. */\r
+ t_end = db->metadata_keys_end ("thread_id_");\r
+ for (t = db->metadata_keys_begin ("thread_id_"); t != t_end; ++t)\r
+ ++total;\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index ad832cf..a7a13cc 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -344,15 +344,17 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message)\r
+ \r
+ /* Get document type */\r
+ assert (strcmp (id_prefix, type_prefix) < 0);\r
+- if (! (message->lazy_flags & (1 << NOTMUCH_MESSAGE_FLAG_GHOST))) {\r
++ if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {\r
+ i.skip_to (type_prefix);\r
++ /* "T" is the prefix "type" fields. See\r
++ * BOOLEAN_PREFIX_INTERNAL. */\r
+ if (*i == "Tmail")\r
+- message->flags &= ~(1 << NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
+ else if (*i == "Tghost")\r
+- message->flags |= (1 << NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
+ else\r
+ INTERNAL_ERROR ("Message without type term");\r
+- message->lazy_flags |= (1 << NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
+ }\r
+ \r
+ /* Get filename list. Here we get only the terms. We lazily\r
+@@ -390,8 +392,8 @@ _notmuch_message_invalidate_metadata (notmuch_message_t *message,\r
+ }\r
+ \r
+ if (strcmp ("type", prefix_name) == 0) {\r
+- message->flags &= ~(1 << NOTMUCH_MESSAGE_FLAG_GHOST);\r
+- message->lazy_flags &= ~(1 << NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
++ NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);\r
+ }\r
+ \r
+ if (strcmp ("file-direntry", prefix_name) == 0) {\r
+@@ -893,10 +895,10 @@ notmuch_message_get_flag (notmuch_message_t *message,\r
+ notmuch_message_flag_t flag)\r
+ {\r
+ if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&\r
+- ! (message->lazy_flags & (1 << flag)))\r
++ ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))\r
+ _notmuch_message_ensure_metadata (message);\r
+ \r
+- return message->flags & (1 << flag);\r
++ return NOTMUCH_TEST_BIT (message->flags, flag);\r
+ }\r
+ \r
+ void\r
+@@ -904,10 +906,10 @@ notmuch_message_set_flag (notmuch_message_t *message,\r
+ notmuch_message_flag_t flag, notmuch_bool_t enable)\r
+ {\r
+ if (enable)\r
+- message->flags |= (1 << flag);\r
++ NOTMUCH_SET_BIT (&message->flags, flag);\r
+ else\r
+- message->flags &= ~(1 << flag);\r
+- message->lazy_flags |= (1 << flag);\r
++ NOTMUCH_CLEAR_BIT (&message->flags, flag);\r
++ NOTMUCH_SET_BIT (&message->lazy_flags, flag);\r
+ }\r
+ \r
+ time_t\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 2fbd38e..2f43c1d 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -63,6 +63,17 @@ NOTMUCH_BEGIN_DECLS\r
+ #define STRNCMP_LITERAL(var, literal) \\r
+ strncmp ((var), (literal), sizeof (literal) - 1)\r
+ \r
++/* Robust bit test/set/reset macros */\r
++#define NOTMUCH_TEST_BIT(val, bit) \\r
++ ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? 0 \\r
++ : !!((val) & (1ull << bit)))\r
++#define NOTMUCH_SET_BIT(valp, bit) \\r
++ ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? *(valp) \\r
++ : (*(valp) |= (1ull << bit)))\r
++#define NOTMUCH_CLEAR_BIT(valp, bit) \\r
++ ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? *(valp) \\r
++ : (*(valp) &= ~(1ull << bit)))\r
++\r
+ #define unused(x) x __attribute__ ((unused))\r
+ \r
+ #ifdef __cplusplus\r
+\r
+\r