--- /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 235EA6DE1778\r
+ for <notmuch@notmuchmail.org>; Fri, 5 Jun 2015 10:30:59 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.239\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.239 tagged_above=-999 required=5 tests=[AWL=0.229, \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 YKAsOXzskGOw for <notmuch@notmuchmail.org>;\r
+ Fri, 5 Jun 2015 10:30:57 -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 284546DE179F\r
+ for <notmuch@notmuchmail.org>; Fri, 5 Jun 2015 10:30:57 -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 1Z0vRe-0002nz-Th; Fri, 05 Jun 2015 17:30:14 +0000\r
+Received: (nullmailer pid 24256 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 5/6] cli: add global option "--db-revision"\r
+Date: Fri, 5 Jun 2015 19:28:37 +0200\r
+Message-Id: <1433525318-23756-6-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
+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:59 -0000\r
+\r
+The function notmuch_exit_if_unmatched_db_revision is split from\r
+notmuch_process_shared_options because it needs an open notmuch\r
+database.\r
+---\r
+ doc/man1/notmuch.rst | 10 ++++++++--\r
+ notmuch-client.h | 4 ++++\r
+ notmuch-compact.c | 4 ++++\r
+ notmuch-config.c | 4 ++++\r
+ notmuch-count.c | 2 ++\r
+ notmuch-dump.c | 2 ++\r
+ notmuch-insert.c | 2 ++\r
+ notmuch-new.c | 3 ++-\r
+ notmuch-reply.c | 2 ++\r
+ notmuch-restore.c | 2 ++\r
+ notmuch-search.c | 2 ++\r
+ notmuch-setup.c | 4 ++++\r
+ notmuch-show.c | 2 ++\r
+ notmuch-tag.c | 2 ++\r
+ notmuch.c | 18 ++++++++++++++++++\r
+ test/T570-revision-tracking.sh | 26 ++++++++++++++++++++++++++\r
+ test/random-corpus.c | 2 ++\r
+ 17 files changed, 88 insertions(+), 3 deletions(-)\r
+\r
+diff --git a/doc/man1/notmuch.rst b/doc/man1/notmuch.rst\r
+index ae0461a..b3f75a4 100644\r
+--- a/doc/man1/notmuch.rst\r
++++ b/doc/man1/notmuch.rst\r
+@@ -49,9 +49,15 @@ Supported global options for ``notmuch`` include\r
+ Specify the configuration file to use. This overrides any\r
+ configuration file specified by ${NOTMUCH\_CONFIG}.\r
+ \r
++ ``--db-revision=UUID``\r
++ Enforce that the database revision is UUID. This is useful to\r
++ detect rollover in modification counts on messages. You can\r
++ find this UUID in the first column of output from\r
++ ``notmuch count --output=modifications``\r
++\r
+ All global options except ``--config`` can also be specified after the\r
+-command. For example, ``notmuch subcommand --version`` is equivalent to\r
+-``notmuch --version subcommand``.\r
++command. For example, ``notmuch subcommand --db-revision`` is\r
++equivalent to ``notmuch --db-revision subcommand``.\r
+ \r
+ COMMANDS\r
+ ========\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 78680aa..d3fab21 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -466,7 +466,11 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
+ notmuch_bool_t gzip_output);\r
+ \r
+ #include "command-line-arguments.h"\r
++\r
++extern char *notmuch_requested_db_revision;\r
+ extern const notmuch_opt_desc_t notmuch_shared_options [];\r
++void notmuch_exit_if_unmatched_db_revision (notmuch_database_t *notmuch);\r
++\r
+ void notmuch_process_shared_options (const char* subcommand_name);\r
+ int notmuch_minimal_options (const char* subcommand_name,\r
+ int argc, char **argv);\r
+diff --git a/notmuch-compact.c b/notmuch-compact.c\r
+index 5be551d..3da4bfb 100644\r
+--- a/notmuch-compact.c\r
++++ b/notmuch-compact.c\r
+@@ -46,6 +46,10 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])\r
+ if (opt_index < 0)\r
+ return EXIT_FAILURE;\r
+ \r
++ if (notmuch_requested_db_revision)\r
++ fprintf (stderr, "Warning: ignoring --db-revision=%s\n",\r
++ notmuch_requested_db_revision);\r
++\r
+ notmuch_process_shared_options (argv[0]);\r
+ \r
+ if (! quiet)\r
+diff --git a/notmuch-config.c b/notmuch-config.c\r
+index 9348278..f1826e4 100644\r
+--- a/notmuch-config.c\r
++++ b/notmuch-config.c\r
+@@ -878,6 +878,10 @@ notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])\r
+ if (opt_index < 0)\r
+ return EXIT_FAILURE;\r
+ \r
++ if (notmuch_requested_db_revision)\r
++ fprintf (stderr, "Warning: ignoring --db-revision=%s\n",\r
++ notmuch_requested_db_revision);\r
++\r
+ /* skip at least subcommand argument */\r
+ argc-= opt_index;\r
+ argv+= opt_index;\r
+diff --git a/notmuch-count.c b/notmuch-count.c\r
+index 7c61ccb..dda85b5 100644\r
+--- a/notmuch-count.c\r
++++ b/notmuch-count.c\r
+@@ -191,6 +191,8 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])\r
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))\r
+ return EXIT_FAILURE;\r
+ \r
++ notmuch_exit_if_unmatched_db_revision (notmuch);\r
++\r
+ query_str = query_string_from_args (config, argc-opt_index, argv+opt_index);\r
+ if (query_str == NULL) {\r
+ fprintf (stderr, "Out of memory.\n");\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index fab22bd..9c0b82d 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -215,6 +215,8 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))\r
+ return EXIT_FAILURE;\r
+ \r
++ notmuch_exit_if_unmatched_db_revision (notmuch);\r
++\r
+ char *output_file_name = NULL;\r
+ int opt_index;\r
+ \r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index c277d62..42d346f 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -536,6 +536,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))\r
+ return EXIT_FAILURE;\r
+ \r
++ notmuch_exit_if_unmatched_db_revision (notmuch);\r
++\r
+ /* Write the message to the Maildir new directory. */\r
+ newpath = maildir_write_new (config, STDIN_FILENO, maildir);\r
+ if (! newpath) {\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 8ff1ade..2064a86 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -995,10 +995,11 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ fputs (status_string, stderr);\r
+ free (status_string);\r
+ }\r
+-\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
++ notmuch_exit_if_unmatched_db_revision (notmuch);\r
++\r
+ if (notmuch_database_needs_upgrade (notmuch)) {\r
+ time_t now = time (NULL);\r
+ struct tm *gm_time = gmtime (&now);\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 4464741..52e0d9b 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -831,6 +831,8 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))\r
+ return EXIT_FAILURE;\r
+ \r
++ notmuch_exit_if_unmatched_db_revision (notmuch);\r
++\r
+ query = notmuch_query_create (notmuch, query_string);\r
+ if (query == NULL) {\r
+ fprintf (stderr, "Out of memory\n");\r
+diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+index 2a534dc..3e96aa8 100644\r
+--- a/notmuch-restore.c\r
++++ b/notmuch-restore.c\r
+@@ -165,6 +165,8 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+ }\r
+ \r
+ notmuch_process_shared_options (argv[0]);\r
++ notmuch_exit_if_unmatched_db_revision (notmuch);\r
++\r
+ name_for_error = input_file_name ? input_file_name : "stdin";\r
+ \r
+ if (! accumulate)\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index b89a17e..919837c 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -583,6 +583,8 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
++ notmuch_exit_if_unmatched_db_revision (ctx->notmuch);\r
++\r
+ query_str = query_string_from_args (ctx->notmuch, argc, argv);\r
+ if (query_str == NULL) {\r
+ fprintf (stderr, "Out of memory.\n");\r
+diff --git a/notmuch-setup.c b/notmuch-setup.c\r
+index 7dd5822..dffda81 100644\r
+--- a/notmuch-setup.c\r
++++ b/notmuch-setup.c\r
+@@ -148,6 +148,10 @@ notmuch_setup_command (notmuch_config_t *config,\r
+ if (notmuch_minimal_options ("setup", argc, argv) < 0)\r
+ return EXIT_FAILURE;\r
+ \r
++ if (notmuch_requested_db_revision)\r
++ fprintf (stderr, "Warning: ignoring --db-revision=%s\n",\r
++ notmuch_requested_db_revision);\r
++\r
+ if (notmuch_config_is_new (config))\r
+ welcome_message_pre_setup ();\r
+ \r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index b80933a..2535c8c 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -1213,6 +1213,8 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))\r
+ return EXIT_FAILURE;\r
+ \r
++ notmuch_exit_if_unmatched_db_revision (notmuch);\r
++\r
+ query = notmuch_query_create (notmuch, query_string);\r
+ if (query == NULL) {\r
+ fprintf (stderr, "Out of memory\n");\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 38d99aa..22446fe 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -261,6 +261,8 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])\r
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))\r
+ return EXIT_FAILURE;\r
+ \r
++ notmuch_exit_if_unmatched_db_revision (notmuch);\r
++\r
+ if (notmuch_config_get_maildir_synchronize_flags (config))\r
+ tag_flags |= TAG_FLAG_MAILDIR_SYNC;\r
+ \r
+diff --git a/notmuch.c b/notmuch.c\r
+index c528dce..fab737c 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -47,10 +47,12 @@ static int\r
+ _help_for (const char *topic);\r
+ \r
+ static notmuch_bool_t print_version = FALSE, print_help = FALSE;\r
++char *notmuch_requested_db_revision = NULL;\r
+ \r
+ const notmuch_opt_desc_t notmuch_shared_options [] = {\r
+ { NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },\r
++ { NOTMUCH_OPT_STRING, ¬much_requested_db_revision, "db-revision", 'd', 0 },\r
+ {0, 0, 0, 0, 0}\r
+ };\r
+ \r
+@@ -218,6 +220,22 @@ be supported in the future.\n", notmuch_format_version);\r
+ }\r
+ }\r
+ \r
++void\r
++notmuch_exit_if_unmatched_db_revision (notmuch_database_t *notmuch)\r
++{\r
++ const char *uuid = NULL;\r
++\r
++ if (!notmuch_requested_db_revision)\r
++ return;\r
++ IGNORE_RESULT (notmuch_database_get_revision (notmuch, &uuid));\r
++\r
++ if (strcmp (notmuch_requested_db_revision, uuid) != 0){\r
++ fprintf (stderr, "Error: requested database revision %s does not match %s\n",\r
++ notmuch_requested_db_revision, uuid);\r
++ exit (1);\r
++ }\r
++}\r
++\r
+ static void\r
+ exec_man (const char *page)\r
+ {\r
+diff --git a/test/T570-revision-tracking.sh b/test/T570-revision-tracking.sh\r
+index 062fd59..bb09bf6 100755\r
+--- a/test/T570-revision-tracking.sh\r
++++ b/test/T570-revision-tracking.sh\r
+@@ -46,4 +46,30 @@ notmuch tag +a-random-tag-8743632 '*'\r
+ after=$(notmuch count --output=modifications '*' | cut -f2)\r
+ result=$(($before < $after))\r
+ test_expect_equal 1 ${result}\r
++\r
++notmuch count --output=modifications '*' | cut -f1 > UUID\r
++\r
++test_expect_success 'search succeeds with correct db revision' \\r
++ "notmuch search --db-revision=$(cat UUID) '*'"\r
++\r
++test_expect_success 'db-revision works as global option ' \\r
++ "notmuch --db-revision=$(cat UUID) search '*'"\r
++\r
++test_expect_code 1 'db-revision works as global option II' \\r
++ "notmuch --db-revision=this-is-no-uuid search '*'"\r
++\r
++test_expect_code 1 'search fails with incorrect db revision' \\r
++ "notmuch search --db-revision=this-is-no-uuid '*'"\r
++\r
++test_expect_success 'show succeeds with correct db revision' \\r
++ "notmuch show --db-revision=$(cat UUID) '*'"\r
++\r
++test_expect_code 1 'show fails with incorrect db revision' \\r
++ "notmuch show --db-revision=this-is-no-uuid '*'"\r
++\r
++test_expect_success 'tag succeeds with correct db revision' \\r
++ "notmuch tag --db-revision=$(cat UUID) +test '*'"\r
++\r
++test_expect_code 1 'tag fails with incorrect db revision' \\r
++ "notmuch show --db-revision=this-is-no-uuid '*' +test2"\r
+ test_done\r
+diff --git a/test/random-corpus.c b/test/random-corpus.c\r
+index b377eb4..542b453 100644\r
+--- a/test/random-corpus.c\r
++++ b/test/random-corpus.c\r
+@@ -119,6 +119,8 @@ const notmuch_opt_desc_t notmuch_shared_options[] = {\r
+ { 0, 0, 0, 0, 0 }\r
+ };\r
+ \r
++char *notmuch_requested_db_revision = NULL;\r
++\r
+ void\r
+ notmuch_process_shared_options (unused (const char *dummy))\r
+ {\r
+-- \r
+2.1.4\r
+\r