--- /dev/null
+Return-Path: <chris@chris-wilson.co.uk>\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 7203A431FBC\r
+ for <notmuch@notmuchmail.org>; Fri, 15 Jan 2010 02:14:00 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.825\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.825 tagged_above=-999 required=5 tests=[AWL=1.174,\r
+ BAYES_50=0.001, RCVD_IN_DNSWL_MED=-4] autolearn=ham\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 rBMqHZMtURkN for <notmuch@notmuchmail.org>;\r
+ Fri, 15 Jan 2010 02:13:59 -0800 (PST)\r
+Received: from fmsmga101.fm.intel.com (mga05.intel.com [192.55.52.89])\r
+ by olra.theworths.org (Postfix) with ESMTP id 3F05F431FAE\r
+ for <notmuch@notmuchmail.org>; Fri, 15 Jan 2010 02:13:59 -0800 (PST)\r
+Received: from fmsmga001.fm.intel.com ([10.253.24.23])\r
+ by fmsmga101.fm.intel.com with ESMTP; 15 Jan 2010 02:13:37 -0800\r
+X-ExtLoop1: 1\r
+X-IronPort-AV: E=Sophos;i="4.49,281,1262592000"; d="scan'208";a="764481457"\r
+Received: from unknown (HELO localhost.localdomain) ([10.255.17.127])\r
+ by fmsmga001.fm.intel.com with ESMTP; 15 Jan 2010 02:13:55 -0800\r
+From: Chris Wilson <chris@chris-wilson.co.uk>\r
+To: notmuch@notmuchmail.org\r
+Date: Fri, 15 Jan 2010 10:13:50 +0000\r
+Message-Id: <1263550430-14223-1-git-send-email-chris@chris-wilson.co.uk>\r
+X-Mailer: git-send-email 1.6.6\r
+Subject: [notmuch] [PATCH] [RFC] notmuch search: Return a non-zero exitcode\r
+ if the search returns no hits.\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: Fri, 15 Jan 2010 10:14:00 -0000\r
+\r
+If the user is explicitly searching for a message, then if notmuch\r
+fails to find it, it is useful to set a failure exit code. This makes it\r
+easier for scripting. However, we will want to then distinguish between\r
+fatal errors (such as out-of-memory, invalid arguments, corrupt db, etc)\r
+from the simple no message. To that end, we introduce a set of\r
+enumerated exit-codes which may prove useful to use consistently across\r
+notmuch. (I'm not that convinced highly differentiated errors is\r
+particularly useful, though separate CONFIG_ERROR and DATABASE_ERROR\r
+would seem to have value for scripts.)\r
+---\r
+ notmuch-search.c | 34 ++++++++++++++++++++++++----------\r
+ 1 files changed, 24 insertions(+), 10 deletions(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index dc44eb6..6ca903b 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -20,7 +20,17 @@\r
+ \r
+ #include "notmuch-client.h"\r
+ \r
+-static void\r
++/* XXX After some review, make these universal? */\r
++enum {\r
++ NOTMUCH_EXIT_SUCCESS = 0,\r
++ NOTMUCH_EXIT_NOT_FOUND,\r
++ NOTMUCH_EXIT_INVALID_ARGUMENT,\r
++ NOTMUCH_EXIT_SYSTEM_ERROR, /* XXX just out-of-memory? */\r
++ NOTMUCH_EXIT_CONFIG_ERROR,\r
++ NOTMUCH_EXIT_DATABASE_ERROR,\r
++};\r
++\r
++static int\r
+ do_search_threads (const void *ctx,\r
+ notmuch_query_t *query,\r
+ notmuch_sort_t sort)\r
+@@ -30,6 +40,7 @@ do_search_threads (const void *ctx,\r
+ notmuch_tags_t *tags;\r
+ time_t date;\r
+ const char *relative_date;\r
++ int count = 0;\r
+ \r
+ for (threads = notmuch_query_search_threads (query);\r
+ notmuch_threads_has_more (threads);\r
+@@ -67,7 +78,10 @@ do_search_threads (const void *ctx,\r
+ printf (")\n");\r
+ \r
+ notmuch_thread_destroy (thread);\r
++ count++;\r
+ }\r
++\r
++ return count;\r
+ }\r
+ \r
+ int\r
+@@ -94,11 +108,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+ sort = NOTMUCH_SORT_NEWEST_FIRST;\r
+ } else {\r
+ fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
+- return 1;\r
++ return NOTMUCH_EXIT_INVALID_ARGUMENT;\r
+ }\r
+ } else {\r
+ fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
+- return 1;\r
++ return NOTMUCH_EXIT_INVALID_ARGUMENT;\r
+ }\r
+ }\r
+ \r
+@@ -107,35 +121,35 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+ \r
+ config = notmuch_config_open (ctx, NULL, NULL);\r
+ if (config == NULL)\r
+- return 1;\r
++ return NOTMUCH_EXIT_CONFIG_ERROR;\r
+ \r
+ notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
+ NOTMUCH_DATABASE_MODE_READ_ONLY);\r
+ if (notmuch == NULL)\r
+- return 1;\r
++ return NOTMUCH_EXIT_DATABASE_ERROR;\r
+ \r
+ query_str = query_string_from_args (ctx, argc, argv);\r
+ if (query_str == NULL) {\r
+ fprintf (stderr, "Out of memory.\n");\r
+- return 1;\r
++ return NOTMUCH_EXIT_SYSTEM_ERROR;\r
+ }\r
+ if (*query_str == '\0') {\r
+ fprintf (stderr, "Error: notmuch search requires at least one search term.\n");\r
+- return 1;\r
++ return NOTMUCH_EXIT_INVALID_ARGUMENT;\r
+ }\r
+ \r
+ query = notmuch_query_create (notmuch, query_str);\r
+ if (query == NULL) {\r
+ fprintf (stderr, "Out of memory\n");\r
+- return 1;\r
++ return NOTMUCH_EXIT_SYSTEM_ERROR;\r
+ }\r
+ \r
+ notmuch_query_set_sort (query, sort);\r
+ \r
+- do_search_threads (ctx, query, sort);\r
++ i = do_search_threads (ctx, query, sort);\r
+ \r
+ notmuch_query_destroy (query);\r
+ notmuch_database_close (notmuch);\r
+ \r
+- return 0;\r
++ return i ? NOTMUCH_EXIT_SUCCESS : NOTMUCH_EXIT_NOT_FOUND;\r
+ }\r
+-- \r
+1.6.6\r
+\r