--- /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 B28BF6DE1749\r
+ for <notmuch@notmuchmail.org>; Fri, 5 Jun 2015 10:30:26 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.241\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.241 tagged_above=-999 required=5 tests=[AWL=0.231, \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 eU4SwRI5xFYP for <notmuch@notmuchmail.org>;\r
+ Fri, 5 Jun 2015 10:30:20 -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 0DA6E6DE1617\r
+ for <notmuch@notmuchmail.org>; Fri, 5 Jun 2015 10:30:20 -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 1Z0vR3-0002ng-RE; Fri, 05 Jun 2015 17:29:37 +0000\r
+Received: (nullmailer pid 24252 invoked by uid 1000); Fri, 05 Jun 2015\r
+ 17:28:42 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 3/6] lib: API to retrieve database revision and UUID\r
+Date: Fri, 5 Jun 2015 19:28:35 +0200\r
+Message-Id: <1433525318-23756-4-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <1433525318-23756-1-git-send-email-david@tethera.net>\r
+References: <1432936375-astroid-4-0i1n6yczs2-1520@strange>\r
+ <1433525318-23756-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: Fri, 05 Jun 2015 17:30:26 -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 a68a487..ba8b8d9 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..74a7c49\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
++ 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 23085e7..1ec6c5a 100644\r
+--- a/test/test-lib.sh\r
++++ b/test/test-lib.sh\r
+@@ -719,6 +719,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/^[a-f0-9][a-f0-9-]*/UUID/'\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