--- /dev/null
+Return-Path: <bremner@tesseract.cs.unb.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 2A8E96DE1654\r
+ for <notmuch@notmuchmail.org>; Sun, 9 Aug 2015 02:26:00 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.141\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.141 tagged_above=-999 required=5 tests=[AWL=0.131, \r
+ T_HEADER_FROM_DIFFERENT_DOMAINS=0.01] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id CWoOsZkh87qr for <notmuch@notmuchmail.org>;\r
+ Sun, 9 Aug 2015 02:25:58 -0700 (PDT)\r
+Received: from gitolite.debian.net (gitolite.debian.net [87.98.215.224])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 38D896DE02D2\r
+ for <notmuch@notmuchmail.org>; Sun, 9 Aug 2015 02:25:58 -0700 (PDT)\r
+Received: from remotemail by gitolite.debian.net with local (Exim 4.80)\r
+ (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1ZOMqy-0005W1-7R; Sun, 09 Aug 2015 09:25:16 +0000\r
+Received: (nullmailer pid 7063 invoked by uid 1000); Sun, 09 Aug 2015\r
+ 09:24:47 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 2/5] lib: API to retrieve database revision and UUID\r
+Date: Sun, 9 Aug 2015 11:24:42 +0200\r
+Message-Id: <1439112285-6681-3-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <1439112285-6681-1-git-send-email-david@tethera.net>\r
+References: <1439112285-6681-1-git-send-email-david@tethera.net>\r
+Cc: Austin Clements <aclements@csail.mit.edu>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.18\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: Sun, 09 Aug 2015 09:26:00 -0000\r
+\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+\r
+This exposes the committed database revision to library users along\r
+with a UUID that can be used to detect when revision numbers are no\r
+longer comparable (e.g., because the database has been replaced).\r
+---\r
+ lib/database-private.h | 1 +\r
+ lib/database.cc | 11 +++++++++++\r
+ lib/notmuch.h | 18 ++++++++++++++++++\r
+ test/T570-revision-tracking.sh | 37 +++++++++++++++++++++++++++++++++++++\r
+ test/test-lib.sh | 5 +++++\r
+ 5 files changed, 72 insertions(+)\r
+ create mode 100755 test/T570-revision-tracking.sh\r
+\r
+diff --git a/lib/database-private.h b/lib/database-private.h\r
+index 5c5a2bb..4e93257 100644\r
+--- a/lib/database-private.h\r
++++ b/lib/database-private.h\r
+@@ -170,6 +170,7 @@ struct _notmuch_database {\r
+ * under a higher revision number, which can be generated with\r
+ * notmuch_database_new_revision. */\r
+ unsigned long revision;\r
++ const char *uuid;\r
+ \r
+ Xapian::QueryParser *query_parser;\r
+ Xapian::TermGenerator *term_gen;\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 52e2e8f..fc78769 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -992,6 +992,8 @@ notmuch_database_open_verbose (const char *path,\r
+ notmuch->revision = 0;\r
+ else\r
+ notmuch->revision = Xapian::sortable_unserialise (last_mod);\r
++ notmuch->uuid = talloc_strdup (\r
++ notmuch, notmuch->xapian_db->get_uuid ().c_str ());\r
+ \r
+ notmuch->query_parser = new Xapian::QueryParser;\r
+ notmuch->term_gen = new Xapian::TermGenerator;\r
+@@ -1666,6 +1668,15 @@ DONE:\r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
+ \r
++unsigned long\r
++notmuch_database_get_revision (notmuch_database_t *notmuch,\r
++ const char **uuid)\r
++{\r
++ if (uuid)\r
++ *uuid = notmuch->uuid;\r
++ return notmuch->revision;\r
++}\r
++\r
+ /* We allow the user to use arbitrarily long paths for directories. But\r
+ * we have a term-length limit. So if we exceed that, we'll use the\r
+ * SHA-1 of the path for the database term.\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 421c19d..a7ac012 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -468,6 +468,24 @@ notmuch_status_t\r
+ notmuch_database_end_atomic (notmuch_database_t *notmuch);\r
+ \r
+ /**\r
++ * Return the committed database revision and UUID.\r
++ *\r
++ * The database revision number increases monotonically with each\r
++ * commit to the database. Hence, all messages and message changes\r
++ * committed to the database (that is, visible to readers) have a last\r
++ * modification revision <= the committed database revision. Any\r
++ * messages committed in the future will be assigned a modification\r
++ * revision > the committed database revision.\r
++ *\r
++ * The UUID is a NUL-terminated opaque string that uniquely identifies\r
++ * this database. Two revision numbers are only comparable if they\r
++ * have the same database UUID.\r
++ */\r
++unsigned long\r
++notmuch_database_get_revision (notmuch_database_t *notmuch,\r
++ const char **uuid);\r
++\r
++/**\r
+ * Retrieve a directory object from the database for 'path'.\r
+ *\r
+ * Here, 'path' should be a path relative to the path of 'database'\r
+diff --git a/test/T570-revision-tracking.sh b/test/T570-revision-tracking.sh\r
+new file mode 100755\r
+index 0000000..e0a5703\r
+--- /dev/null\r
++++ b/test/T570-revision-tracking.sh\r
+@@ -0,0 +1,37 @@\r
++#!/usr/bin/env bash\r
++test_description="database revision tracking"\r
++\r
++. ./test-lib.sh || exit 1\r
++\r
++add_email_corpus\r
++\r
++test_begin_subtest "notmuch_database_get_revision"\r
++test_C ${MAIL_DIR} <<'EOF'\r
++#include <stdio.h>\r
++#include <string.h>\r
++#include <notmuch.h>\r
++int main (int argc, char** argv)\r
++{\r
++ notmuch_database_t *db;\r
++ notmuch_status_t stat;\r
++ unsigned long revision;\r
++ const char *uuid;\r
++\r
++ unsigned long rev;\r
++\r
++ stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_ONLY, &db);\r
++ if (stat)\r
++ fputs ("open failed\n", stderr);\r
++ revision = notmuch_database_get_revision (db, &uuid);\r
++ printf("%s\t%lu\n", uuid, revision);\r
++}\r
++EOF\r
++notmuch_uuid_sanitize < OUTPUT > CLEAN\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++UUID 53\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED CLEAN\r
++\r
++test_done\r
+diff --git a/test/test-lib.sh b/test/test-lib.sh\r
+index 0bf7163..126911f 100644\r
+--- a/test/test-lib.sh\r
++++ b/test/test-lib.sh\r
+@@ -720,6 +720,11 @@ notmuch_date_sanitize ()\r
+ sed \\r
+ -e 's/^Date: Fri, 05 Jan 2001 .*0000/Date: GENERATED_DATE/'\r
+ }\r
++\r
++notmuch_uuid_sanitize ()\r
++{\r
++ sed 's/[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}/UUID/g'\r
++}\r
+ # End of notmuch helper functions\r
+ \r
+ # Use test_set_prereq to tell that a particular prerequisite is available.\r
+-- \r
+2.1.4\r
+\r