--- /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 9B8B56DE1B72\r
+ for <notmuch@notmuchmail.org>; Sun, 5 Apr 2015 16:00:43 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.585\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.585 tagged_above=-999 required=5 tests=[AWL=0.575, \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 I7n22twWl2Oh for <notmuch@notmuchmail.org>;\r
+ Sun, 5 Apr 2015 16:00:41 -0700 (PDT)\r
+Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
+ [87.98.215.224])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 915B96DE1B6F\r
+ for <notmuch@notmuchmail.org>; Sun, 5 Apr 2015 16:00:41 -0700 (PDT)\r
+Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
+ 4.80) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1YetWJ-0002nb-U2; Sun, 05 Apr 2015 22:59:59 +0000\r
+Received: (nullmailer pid 2241 invoked by uid 1000); Sun, 05 Apr 2015\r
+ 22:59:25 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [WIP2 03/12] lib: API to retrieve database revision and UUID\r
+Date: Mon, 6 Apr 2015 07:59:05 +0900\r
+Message-Id: <1428274754-1698-4-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <1428274754-1698-1-git-send-email-david@tethera.net>\r
+References: <1428274754-1698-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, 05 Apr 2015 23:00:43 -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
+ 4 files changed, 67 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 4378979..f88bdb3 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -978,6 +978,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
+@@ -1651,6 +1653,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 20c4e01..b6be727 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -461,6 +461,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..8c12740\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\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
++ 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("revision=%d\nuuid_len=%d\n", revision, strlen (uuid));\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++revision=53\r
++uuid_len=36\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file OUTPUT EXPECTED\r
++\r
++test_done\r
+-- \r
+2.1.4\r
+\r