[PATCH v2] nmbug: Add 'clone' and replace FETCH_HEAD with @{upstream}
[notmuch-archives.git] / 91 / 31e628b9ff840131159fe202584f15715934e2
1 Return-Path: <chris@chris-wilson.co.uk>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 7203A431FBC\r
6         for <notmuch@notmuchmail.org>; Fri, 15 Jan 2010 02:14:00 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.825\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.825 tagged_above=-999 required=5 tests=[AWL=1.174,\r
12         BAYES_50=0.001, RCVD_IN_DNSWL_MED=-4] autolearn=ham\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id rBMqHZMtURkN for <notmuch@notmuchmail.org>;\r
16         Fri, 15 Jan 2010 02:13:59 -0800 (PST)\r
17 Received: from fmsmga101.fm.intel.com (mga05.intel.com [192.55.52.89])\r
18         by olra.theworths.org (Postfix) with ESMTP id 3F05F431FAE\r
19         for <notmuch@notmuchmail.org>; Fri, 15 Jan 2010 02:13:59 -0800 (PST)\r
20 Received: from fmsmga001.fm.intel.com ([10.253.24.23])\r
21         by fmsmga101.fm.intel.com with ESMTP; 15 Jan 2010 02:13:37 -0800\r
22 X-ExtLoop1: 1\r
23 X-IronPort-AV: E=Sophos;i="4.49,281,1262592000"; d="scan'208";a="764481457"\r
24 Received: from unknown (HELO localhost.localdomain) ([10.255.17.127])\r
25         by fmsmga001.fm.intel.com with ESMTP; 15 Jan 2010 02:13:55 -0800\r
26 From: Chris Wilson <chris@chris-wilson.co.uk>\r
27 To: notmuch@notmuchmail.org\r
28 Date: Fri, 15 Jan 2010 10:13:50 +0000\r
29 Message-Id: <1263550430-14223-1-git-send-email-chris@chris-wilson.co.uk>\r
30 X-Mailer: git-send-email 1.6.6\r
31 Subject: [notmuch] [PATCH] [RFC] notmuch search: Return a non-zero exitcode\r
32         if the search returns no hits.\r
33 X-BeenThere: notmuch@notmuchmail.org\r
34 X-Mailman-Version: 2.1.13\r
35 Precedence: list\r
36 List-Id: "Use and development of the notmuch mail system."\r
37         <notmuch.notmuchmail.org>\r
38 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
39         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
40 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
41 List-Post: <mailto:notmuch@notmuchmail.org>\r
42 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
43 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
45 X-List-Received-Date: Fri, 15 Jan 2010 10:14:00 -0000\r
46 \r
47 If the user is explicitly searching for a message, then if notmuch\r
48 fails to find it, it is useful to set a failure exit code. This makes it\r
49 easier for scripting. However, we will want to then distinguish between\r
50 fatal errors (such as out-of-memory, invalid arguments, corrupt db, etc)\r
51 from the simple no message. To that end, we introduce a set of\r
52 enumerated exit-codes which may prove useful to use consistently across\r
53 notmuch. (I'm not that convinced highly differentiated errors is\r
54 particularly useful, though separate CONFIG_ERROR and DATABASE_ERROR\r
55 would seem to have value for scripts.)\r
56 ---\r
57  notmuch-search.c |   34 ++++++++++++++++++++++++----------\r
58  1 files changed, 24 insertions(+), 10 deletions(-)\r
59 \r
60 diff --git a/notmuch-search.c b/notmuch-search.c\r
61 index dc44eb6..6ca903b 100644\r
62 --- a/notmuch-search.c\r
63 +++ b/notmuch-search.c\r
64 @@ -20,7 +20,17 @@\r
65  \r
66  #include "notmuch-client.h"\r
67  \r
68 -static void\r
69 +/* XXX After some review, make these universal? */\r
70 +enum {\r
71 +    NOTMUCH_EXIT_SUCCESS = 0,\r
72 +    NOTMUCH_EXIT_NOT_FOUND,\r
73 +    NOTMUCH_EXIT_INVALID_ARGUMENT,\r
74 +    NOTMUCH_EXIT_SYSTEM_ERROR, /* XXX just out-of-memory? */\r
75 +    NOTMUCH_EXIT_CONFIG_ERROR,\r
76 +    NOTMUCH_EXIT_DATABASE_ERROR,\r
77 +};\r
78 +\r
79 +static int\r
80  do_search_threads (const void *ctx,\r
81                    notmuch_query_t *query,\r
82                    notmuch_sort_t sort)\r
83 @@ -30,6 +40,7 @@ do_search_threads (const void *ctx,\r
84      notmuch_tags_t *tags;\r
85      time_t date;\r
86      const char *relative_date;\r
87 +    int count = 0;\r
88  \r
89      for (threads = notmuch_query_search_threads (query);\r
90          notmuch_threads_has_more (threads);\r
91 @@ -67,7 +78,10 @@ do_search_threads (const void *ctx,\r
92         printf (")\n");\r
93  \r
94         notmuch_thread_destroy (thread);\r
95 +       count++;\r
96      }\r
97 +\r
98 +    return count;\r
99  }\r
100  \r
101  int\r
102 @@ -94,11 +108,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
103                 sort = NOTMUCH_SORT_NEWEST_FIRST;\r
104             } else {\r
105                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
106 -               return 1;\r
107 +               return NOTMUCH_EXIT_INVALID_ARGUMENT;\r
108             }\r
109         } else {\r
110             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
111 -           return 1;\r
112 +           return NOTMUCH_EXIT_INVALID_ARGUMENT;\r
113         }\r
114      }\r
115  \r
116 @@ -107,35 +121,35 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
117  \r
118      config = notmuch_config_open (ctx, NULL, NULL);\r
119      if (config == NULL)\r
120 -       return 1;\r
121 +       return NOTMUCH_EXIT_CONFIG_ERROR;\r
122  \r
123      notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
124                                      NOTMUCH_DATABASE_MODE_READ_ONLY);\r
125      if (notmuch == NULL)\r
126 -       return 1;\r
127 +       return NOTMUCH_EXIT_DATABASE_ERROR;\r
128  \r
129      query_str = query_string_from_args (ctx, argc, argv);\r
130      if (query_str == NULL) {\r
131         fprintf (stderr, "Out of memory.\n");\r
132 -       return 1;\r
133 +       return NOTMUCH_EXIT_SYSTEM_ERROR;\r
134      }\r
135      if (*query_str == '\0') {\r
136         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");\r
137 -       return 1;\r
138 +       return NOTMUCH_EXIT_INVALID_ARGUMENT;\r
139      }\r
140  \r
141      query = notmuch_query_create (notmuch, query_str);\r
142      if (query == NULL) {\r
143         fprintf (stderr, "Out of memory\n");\r
144 -       return 1;\r
145 +       return NOTMUCH_EXIT_SYSTEM_ERROR;\r
146      }\r
147  \r
148      notmuch_query_set_sort (query, sort);\r
149  \r
150 -    do_search_threads (ctx, query, sort);\r
151 +    i = do_search_threads (ctx, query, sort);\r
152  \r
153      notmuch_query_destroy (query);\r
154      notmuch_database_close (notmuch);\r
155  \r
156 -    return 0;\r
157 +    return i ? NOTMUCH_EXIT_SUCCESS : NOTMUCH_EXIT_NOT_FOUND;\r
158  }\r
159 -- \r
160 1.6.6\r
161 \r