[PATCH 4/5] CLI: add optional config data to dump output.
[notmuch-archives.git] / b7 / 88d3c89dd67fcabfdb451e82674eb5da55cd1b
1 Return-Path: <bremner@tethera.net>\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 arlo.cworth.org (Postfix) with ESMTP id 3789B6DE0B25\r
6  for <notmuch@notmuchmail.org>; Sat, 23 Jan 2016 07:00:33 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.31\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.31 tagged_above=-999 required=5 tests=[AWL=0.241, \r
12  RP_MATCHES_RCVD=-0.55, SPF_PASS=-0.001] autolearn=disabled\r
13 Received: from arlo.cworth.org ([127.0.0.1])\r
14  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
15  with ESMTP id hCtCYVI7q4OJ for <notmuch@notmuchmail.org>;\r
16  Sat, 23 Jan 2016 07:00:31 -0800 (PST)\r
17 Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
18  by arlo.cworth.org (Postfix) with ESMTPS id 9C5CE6DE1343\r
19  for <notmuch@notmuchmail.org>; Sat, 23 Jan 2016 07:00:14 -0800 (PST)\r
20 Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
21  (envelope-from <bremner@tethera.net>)\r
22  id 1aMzf5-00016d-GD; Sat, 23 Jan 2016 09:59:35 -0500\r
23 Received: (nullmailer pid 4047 invoked by uid 1000);\r
24  Sat, 23 Jan 2016 15:00:06 -0000\r
25 From: David Bremner <david@tethera.net>\r
26 To: notmuch@notmuchmail.org\r
27 Subject: [PATCH 4/5] CLI: add optional config data to dump output.\r
28 Date: Sat, 23 Jan 2016 10:59:57 -0400\r
29 Message-Id: <1453561198-2893-5-git-send-email-david@tethera.net>\r
30 X-Mailer: git-send-email 2.6.4\r
31 In-Reply-To: <1453561198-2893-1-git-send-email-david@tethera.net>\r
32 References: <1453561198-2893-1-git-send-email-david@tethera.net>\r
33 X-BeenThere: notmuch@notmuchmail.org\r
34 X-Mailman-Version: 2.1.20\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: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
44  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
45 X-List-Received-Date: Sat, 23 Jan 2016 15:00:33 -0000\r
46 \r
47 This lacks at least documentation. Note that it changes the default dump\r
48 output format, but doesn't break existing notmuch-restore. It might\r
49 break user scripts though.\r
50 ---\r
51  notmuch-client.h          |  8 +++++\r
52  notmuch-dump.c            | 80 +++++++++++++++++++++++++++++++++++++++++++++--\r
53  notmuch-new.c             |  2 +-\r
54  test/T150-tagging.sh      |  8 ++---\r
55  test/T240-dump-restore.sh | 14 ++++-----\r
56  test/T590-libconfig.sh    | 17 ++++++++++\r
57  test/test-lib.sh          |  6 ++++\r
58  7 files changed, 120 insertions(+), 15 deletions(-)\r
59 \r
60 diff --git a/notmuch-client.h b/notmuch-client.h\r
61 index 7c9a1ea..7588734 100644\r
62 --- a/notmuch-client.h\r
63 +++ b/notmuch-client.h\r
64 @@ -443,11 +443,19 @@ typedef enum dump_formats {\r
65      DUMP_FORMAT_SUP\r
66  } dump_format_t;\r
67  \r
68 +typedef enum dump_includes {\r
69 +    DUMP_INCLUDE_TAGS=1,\r
70 +    DUMP_INCLUDE_CONFIG=2,\r
71 +} dump_include_t;\r
72 +\r
73 +#define NOTMUCH_DUMP_VERSION 2\r
74 +\r
75  int\r
76  notmuch_database_dump (notmuch_database_t *notmuch,\r
77                        const char *output_file_name,\r
78                        const char *query_str,\r
79                        dump_format_t output_format,\r
80 +                      dump_include_t include,\r
81                        notmuch_bool_t gzip_output);\r
82  \r
83  /* If status is non-zero (i.e. error) print appropriate\r
84 diff --git a/notmuch-dump.c b/notmuch-dump.c\r
85 index 829781f..a6cf810 100644\r
86 --- a/notmuch-dump.c\r
87 +++ b/notmuch-dump.c\r
88 @@ -23,16 +23,82 @@\r
89  #include "string-util.h"\r
90  #include <zlib.h>\r
91  \r
92 +static int\r
93 +database_dump_config (notmuch_database_t *notmuch, gzFile output)\r
94 +{\r
95 +    notmuch_config_list_t *list;\r
96 +    int ret = EXIT_FAILURE;\r
97 +    char *buffer = NULL;\r
98 +    size_t buffer_size = 0;\r
99 +\r
100 +    if (print_status_database ("notmuch dump", notmuch,\r
101 +                              notmuch_database_get_config_list (notmuch, NULL, &list)))\r
102 +       goto DONE;\r
103 +\r
104 +    for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {\r
105 +       if (hex_encode (notmuch, notmuch_config_list_key (list),\r
106 +                       &buffer, &buffer_size) != HEX_SUCCESS) {\r
107 +           fprintf (stderr, "Error: failed to hex-encode config key %s\n",\r
108 +                    notmuch_config_list_key (list));\r
109 +           goto DONE;\r
110 +       }\r
111 +       gzprintf (output, "#@ %s", buffer);\r
112 +\r
113 +       if (hex_encode (notmuch, notmuch_config_list_value (list),\r
114 +                       &buffer, &buffer_size) != HEX_SUCCESS) {\r
115 +           fprintf (stderr, "Error: failed to hex-encode config value %s\n",\r
116 +                    notmuch_config_list_value (list) );\r
117 +           goto DONE;\r
118 +       }\r
119 +\r
120 +       gzprintf (output, " %s\n", buffer);\r
121 +    }\r
122 +\r
123 +    ret = EXIT_SUCCESS;\r
124 +\r
125 + DONE:\r
126 +    if (list)\r
127 +       notmuch_config_list_destroy (list);\r
128 +\r
129 +    if (buffer)\r
130 +       talloc_free (buffer);\r
131 +\r
132 +    return ret;\r
133 +}\r
134 +\r
135 +static void\r
136 +print_dump_header (gzFile output, int output_format, int include)\r
137 +{\r
138 +    gzprintf (output, "#notmuch-dump %s:%d %s%s%s\n",\r
139 +             (output_format == DUMP_FORMAT_SUP) ? "sup" : "batch-tag",\r
140 +             NOTMUCH_DUMP_VERSION,\r
141 +             (include & DUMP_INCLUDE_CONFIG) ? "config" : "",\r
142 +             (include & DUMP_INCLUDE_TAGS) && (include & DUMP_INCLUDE_CONFIG) ? "," : "",\r
143 +             (include & DUMP_INCLUDE_TAGS) ? "tags" : "");\r
144 +\r
145 +\r
146 +}\r
147  \r
148  static int\r
149  database_dump_file (notmuch_database_t *notmuch, gzFile output,\r
150 -                   const char *query_str, int output_format)\r
151 +                   const char *query_str, int output_format, int include)\r
152  {\r
153      notmuch_query_t *query;\r
154      notmuch_messages_t *messages;\r
155      notmuch_message_t *message;\r
156      notmuch_tags_t *tags;\r
157  \r
158 +    print_dump_header (output, output_format, include);\r
159 +\r
160 +    if (include & DUMP_INCLUDE_CONFIG) {\r
161 +       if (print_status_database ("notmuch dump", notmuch,\r
162 +                                  database_dump_config(notmuch,output)))\r
163 +           return EXIT_FAILURE;\r
164 +    }\r
165 +\r
166 +    if (! (include & DUMP_INCLUDE_TAGS))\r
167 +       return EXIT_SUCCESS;\r
168 +\r
169      if (! query_str)\r
170         query_str = "";\r
171  \r
172 @@ -130,6 +196,7 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
173                        const char *output_file_name,\r
174                        const char *query_str,\r
175                        dump_format_t output_format,\r
176 +                      dump_include_t include,\r
177                        notmuch_bool_t gzip_output)\r
178  {\r
179      gzFile output = NULL;\r
180 @@ -164,7 +231,7 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
181         goto DONE;\r
182      }\r
183  \r
184 -    ret = database_dump_file (notmuch, output, query_str, output_format);\r
185 +    ret = database_dump_file (notmuch, output, query_str, output_format, include);\r
186      if (ret) goto DONE;\r
187  \r
188      ret = gzflush (output, Z_FINISH);\r
189 @@ -226,6 +293,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
190      int opt_index;\r
191  \r
192      int output_format = DUMP_FORMAT_BATCH_TAG;\r
193 +    int include = 0;\r
194      notmuch_bool_t gzip_output = 0;\r
195  \r
196      notmuch_opt_desc_t options[] = {\r
197 @@ -233,6 +301,9 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
198           (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },\r
199                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
200                                   { 0, 0 } } },\r
201 +       { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',\r
202 +         (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },\r
203 +                                 { "tags", DUMP_INCLUDE_TAGS} } },\r
204         { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },\r
205         { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },\r
206         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
207 @@ -245,6 +316,9 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
208  \r
209      notmuch_process_shared_options (argv[0]);\r
210  \r
211 +    if (include == 0)\r
212 +       include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS;\r
213 +\r
214      if (opt_index < argc) {\r
215         query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);\r
216         if (query_str == NULL) {\r
217 @@ -254,7 +328,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
218      }\r
219  \r
220      ret = notmuch_database_dump (notmuch, output_file_name, query_str,\r
221 -                                output_format, gzip_output);\r
222 +                                output_format, include, gzip_output);\r
223  \r
224      notmuch_database_destroy (notmuch);\r
225  \r
226 diff --git a/notmuch-new.c b/notmuch-new.c\r
227 index e503776..fd2ff82 100644\r
228 --- a/notmuch-new.c\r
229 +++ b/notmuch-new.c\r
230 @@ -1041,7 +1041,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
231             }\r
232  \r
233             if (notmuch_database_dump (notmuch, backup_name, "",\r
234 -                                      DUMP_FORMAT_BATCH_TAG, TRUE)) {\r
235 +                                      DUMP_FORMAT_BATCH_TAG, DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS, TRUE)) {\r
236                 fprintf (stderr, "Backup failed. Aborting upgrade.");\r
237                 return EXIT_FAILURE;\r
238             }\r
239 diff --git a/test/T150-tagging.sh b/test/T150-tagging.sh\r
240 index 8adcabc..f1a6df0 100755\r
241 --- a/test/T150-tagging.sh\r
242 +++ b/test/T150-tagging.sh\r
243 @@ -188,7 +188,7 @@ cat <<EOF > EXPECTED\r
244  +%22%27%22%27%22%22%27%27 +inbox +tag4 +tag5 +unread -- id:msg-002@notmuch-test-suite\r
245  EOF\r
246  \r
247 -notmuch dump --format=batch-tag | sort > OUTPUT\r
248 +NOTMUCH_DUMP_TAGS > OUTPUT\r
249  notmuch restore --format=batch-tag < BACKUP\r
250  test_expect_equal_file EXPECTED OUTPUT\r
251  \r
252 @@ -209,7 +209,7 @@ cat <<EOF > EXPECTED\r
253  +%21@%23%20%24%25%5e%26%2a%29-_=+%5b%7b%5c%20%7c%3b%3a%27%20%22,.%3c%60%7e +inbox +tag5 +unread -- id:msg-001@notmuch-test-suite\r
254  EOF\r
255  \r
256 -notmuch dump --format=batch-tag | sort > OUTPUT\r
257 +NOTMUCH_DUMP_TAGS > OUTPUT\r
258  notmuch restore --format=batch-tag < BACKUP\r
259  test_expect_equal_file EXPECTED OUTPUT\r
260  \r
261 @@ -235,7 +235,7 @@ cat <<EOF > EXPECTED\r
262  +%2a@%7d%cf%b5%f4%85%80%adO3%da%a7 +=%e0%ac%95%c8%b3+%ef%aa%95%c8%a64w%c7%9d%c9%a2%cf%b3%d6%82%24B%c4%a9%c5%a1UX%ee%99%b0%27E7%ca%a4%d0%8b%5d +A%e1%a0%bc%de%8b%d5%b2V%d9%9b%f3%b5%a2%a3M%d8%a1u@%f0%a0%ac%948%7e%f0%ab%86%af%27 +L%df%85%ef%a1%a5m@%d3%96%c2%ab%d4%9f%ca%b8%f3%b3%a2%bf%c7%b1_u%d7%b4%c7%b1 +P%c4%98%2f +R +inbox +tag5 +unread +%7e%d1%8b%25%ec%a0%ae%d1%a0M%3b%e3%b6%b7%e9%a4%87%3c%db%9a%cc%a8%e1%96%9d +%c4%bf7%c7%ab9H%c4%99k%ea%91%bd%c3%8ck%e2%b3%8dk%c5%952V%e4%99%b2%d9%b3%e4%8b%bda%5b%24%c7%9b +%da%88=f%cc%b9I%ce%af%7b%c9%97%e3%b9%8bH%cb%92X%d2%8c6 +%dc%9crh%d2%86B%e5%97%a2%22t%ed%99%82d -- id:msg-001@notmuch-test-suite\r
263  EOF\r
264  \r
265 -notmuch dump --format=batch-tag | sort > OUTPUT\r
266 +NOTMUCH_DUMP_TAGS > OUTPUT\r
267  notmuch restore --format=batch-tag < BACKUP\r
268  test_expect_equal_file EXPECTED OUTPUT\r
269  \r
270 @@ -260,7 +260,7 @@ cat <<EOF > EXPECTED\r
271  +foo%3a%3abar%25 +found%3a%3ait +inbox +tag5 +unread +winner -- id:msg-001@notmuch-test-suite\r
272  EOF\r
273  \r
274 -notmuch dump --format=batch-tag | sort > OUTPUT\r
275 +NOTMUCH_DUMP_TAGS > OUTPUT\r
276  notmuch restore --format=batch-tag < BACKUP\r
277  test_expect_equal_file EXPECTED OUTPUT\r
278  \r
279 diff --git a/test/T240-dump-restore.sh b/test/T240-dump-restore.sh\r
280 index e6976ff..758d355 100755\r
281 --- a/test/T240-dump-restore.sh\r
282 +++ b/test/T240-dump-restore.sh\r
283 @@ -97,7 +97,7 @@ test_expect_equal_file dump.expected dump.actual\r
284  # Note, we assume all messages from cworth have a message-id\r
285  # containing cworth.org\r
286  \r
287 -grep 'cworth[.]org' dump.expected > dump-cworth.expected\r
288 +(head -1 dump.expected ; grep 'cworth[.]org' dump.expected) > dump-cworth.expected\r
289  \r
290  test_begin_subtest "dump -- from:cworth"\r
291  notmuch dump -- from:cworth > dump-dash-cworth.actual\r
292 @@ -118,16 +118,16 @@ notmuch search --output=messages from:cworth | sed s/^id:// |\\r
293  test_expect_equal_file OUTPUT EXPECTED\r
294  \r
295  test_begin_subtest "format=batch-tag, dump sanity check."\r
296 -notmuch dump --format=sup from:cworth | cut -f1 -d' ' | \\r
297 +NOTMUCH_DUMP_TAGS --format=sup from:cworth | cut -f1 -d' ' | \\r
298      sort > EXPECTED.$test_count\r
299 -notmuch dump --format=batch-tag from:cworth | sed 's/^.*-- id://' | \\r
300 +NOTMUCH_DUMP_TAGS --format=batch-tag from:cworth | sed 's/^.*-- id://' | \\r
301      sort > OUTPUT.$test_count\r
302  test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count\r
303  \r
304  test_begin_subtest "format=batch-tag, missing newline"\r
305  printf "+a_tag_without_newline -- id:20091117232137.GA7669@griffis1.net" > IN\r
306  notmuch restore --accumulate < IN\r
307 -notmuch dump id:20091117232137.GA7669@griffis1.net > OUT\r
308 +NOTMUCH_DUMP_TAGS id:20091117232137.GA7669@griffis1.net > OUT\r
309  cat <<EOF > EXPECTED\r
310  +a_tag_without_newline +inbox +unread -- id:20091117232137.GA7669@griffis1.net\r
311  EOF\r
312 @@ -155,7 +155,7 @@ cat <<EOF >EXPECTED.$test_count\r
313  + -- id:20091117232137.GA7669@griffis1.net\r
314  EOF\r
315  notmuch restore --format=batch-tag < EXPECTED.$test_count\r
316 -notmuch dump --format=batch-tag id:20091117232137.GA7669@griffis1.net > OUTPUT.$test_count\r
317 +NOTMUCH_DUMP_TAGS --format=batch-tag id:20091117232137.GA7669@griffis1.net > OUTPUT.$test_count\r
318  test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count\r
319  \r
320  tag1='comic_swear=$&^%$^%\\//-+$^%$'\r
321 @@ -217,9 +217,9 @@ notmuch dump --format=batch-tag > OUTPUT.$test_count\r
322  test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count\r
323  \r
324  test_begin_subtest 'format=batch-tag, checking encoded output'\r
325 -notmuch dump --format=batch-tag -- from:cworth |\\r
326 +NOTMUCH_DUMP_TAGS --format=batch-tag -- from:cworth |\\r
327          awk "{ print \"+$enc1 +$enc2 +$enc3 -- \" \$5 }" > EXPECTED.$test_count\r
328 -notmuch dump --format=batch-tag -- from:cworth  > OUTPUT.$test_count\r
329 +NOTMUCH_DUMP_TAGS --format=batch-tag -- from:cworth  > OUTPUT.$test_count\r
330  test_expect_equal_file EXPECTED.$test_count OUTPUT.$test_count\r
331  \r
332  test_begin_subtest 'restoring sane tags'\r
333 diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
334 index 8ca6883..5ea5300 100755\r
335 --- a/test/T590-libconfig.sh\r
336 +++ b/test/T590-libconfig.sh\r
337 @@ -115,4 +115,21 @@ testkey2 testvalue2\r
338  EOF\r
339  test_expect_equal_file EXPECTED OUTPUT\r
340  \r
341 +test_begin_subtest "dump config"\r
342 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
343 +{\r
344 +    RUN(notmuch_database_set_config (db, "key with spaces", "value, with, spaces!"));\r
345 +}\r
346 +EOF\r
347 +notmuch dump --include=config >OUTPUT\r
348 +cat <<'EOF' >EXPECTED\r
349 +#notmuch-dump batch-tag:2 config\r
350 +#@ aaabefore beforeval\r
351 +#@ key%20with%20spaces value,%20with,%20spaces%21\r
352 +#@ testkey1 testvalue1\r
353 +#@ testkey2 testvalue2\r
354 +#@ zzzafter afterval\r
355 +EOF\r
356 +test_expect_equal_file EXPECTED OUTPUT\r
357 +\r
358  test_done\r
359 diff --git a/test/test-lib.sh b/test/test-lib.sh\r
360 index 270c718..0c9b366 100644\r
361 --- a/test/test-lib.sh\r
362 +++ b/test/test-lib.sh\r
363 @@ -673,6 +673,12 @@ NOTMUCH_NEW ()\r
364      notmuch new "${@}" | grep -v -E -e '^Processed [0-9]*( total)? file|Found [0-9]* total file'\r
365  }\r
366  \r
367 +NOTMUCH_DUMP_TAGS ()\r
368 +{\r
369 +    # this relies on the default format being batch-tag, otherwise some tests will break\r
370 +    notmuch dump --include=tags "${@}" | sed '/^#/d' | sort\r
371 +}\r
372 +\r
373  notmuch_search_sanitize ()\r
374  {\r
375      perl -pe 's/("?thread"?: ?)("?)................("?)/\1\2XXX\3/'\r
376 -- \r
377 2.6.4\r
378 \r