Re: Applying patches directly from emails?
[notmuch-archives.git] / 3d / 11da19f266af337639e3db06f32c7f8d5aaeab
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 13B0F6DE0362\r
6  for <notmuch@notmuchmail.org>; Sat, 30 Apr 2016 18:24:56 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.015\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.015 tagged_above=-999 required=5\r
12  tests=[AWL=-0.004, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
13  autolearn=disabled\r
14 Received: from arlo.cworth.org ([127.0.0.1])\r
15  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
16  with ESMTP id kCCEivaTvHoA for <notmuch@notmuchmail.org>;\r
17  Sat, 30 Apr 2016 18:24:48 -0700 (PDT)\r
18 Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
19  by arlo.cworth.org (Postfix) with ESMTPS id EE39E6DE026C\r
20  for <notmuch@notmuchmail.org>; Sat, 30 Apr 2016 18:24:47 -0700 (PDT)\r
21 Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
22  (envelope-from <bremner@tethera.net>)\r
23  id 1awg7m-0006bY-PT; Sat, 30 Apr 2016 21:24:42 -0400\r
24 Received: (nullmailer pid 29955 invoked by uid 1000);\r
25  Sun, 01 May 2016 01:24:43 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [Patch v3 08/11] CLI: optionally restore config data.\r
29 Date: Sat, 30 Apr 2016 22:24:36 -0300\r
30 Message-Id: <1462065879-29860-9-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.8.0.rc3\r
32 In-Reply-To: <1462065879-29860-1-git-send-email-david@tethera.net>\r
33 References: <1462065879-29860-1-git-send-email-david@tethera.net>\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.20\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38  <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
40  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
45  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Sun, 01 May 2016 01:24:56 -0000\r
47 \r
48 The default to restore config data seems safest, especially since\r
49 currently we have no config data to mess up.\r
50 ---\r
51  doc/man1/notmuch-restore.rst | 18 +++++++++++++++\r
52  notmuch-restore.c            | 53 ++++++++++++++++++++++++++++++++++++++++++++\r
53  test/T590-libconfig.sh       | 11 +++++++++\r
54  3 files changed, 82 insertions(+)\r
55 \r
56 diff --git a/doc/man1/notmuch-restore.rst b/doc/man1/notmuch-restore.rst\r
57 index 362e262..87fa22e 100644\r
58 --- a/doc/man1/notmuch-restore.rst\r
59 +++ b/doc/man1/notmuch-restore.rst\r
60 @@ -50,6 +50,24 @@ Supported options for **restore** include\r
61              format, this heuristic, based the fact that batch-tag format\r
62              contains no parentheses, should be accurate.\r
63  \r
64 +    ``--include=(config|tags)``\r
65 +\r
66 +      Control what kind of metadata is restored.\r
67 +\r
68 +       **config**\r
69 +\r
70 +         Restore configuration data to the database. Each configuration line starts\r
71 +         with "#@ ", followed by a space seperated key-value pair.\r
72 +         Both key and value are hex encoded if needed.\r
73 +\r
74 +       **tags**\r
75 +\r
76 +         Output per-message metadata, namely tags. See *format* above\r
77 +         for more details.\r
78 +\r
79 +      The default is to restore both tags and configuration\r
80 +      information\r
81 +\r
82      ``--input=``\ <filename>\r
83          Read input from given file instead of stdin.\r
84  \r
85 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
86 index 9abc64f..e06fbde 100644\r
87 --- a/notmuch-restore.c\r
88 +++ b/notmuch-restore.c\r
89 @@ -24,6 +24,38 @@\r
90  #include "string-util.h"\r
91  #include "zlib-extra.h"\r
92  \r
93 +static int\r
94 +process_config_line(notmuch_database_t *notmuch, const char* line){\r
95 +    const char *key_p, *val_p;\r
96 +    char *key, *val;\r
97 +    size_t key_len,val_len;\r
98 +    const char *delim=" \t\n";\r
99 +    int ret = EXIT_FAILURE;\r
100 +\r
101 +    void *local = talloc_new(NULL);\r
102 +\r
103 +    key_p = strtok_len_c(line, delim, &key_len);\r
104 +    val_p = strtok_len_c(key_p+key_len, delim, &val_len);\r
105 +\r
106 +    key = talloc_strndup(local, key_p, key_len);\r
107 +    val = talloc_strndup(local, val_p, val_len);\r
108 +    if (hex_decode_inplace (key) != HEX_SUCCESS ||\r
109 +       hex_decode_inplace (val) != HEX_SUCCESS ) {\r
110 +       fprintf (stderr, "hex decoding failure on line %s\n", line);\r
111 +       goto DONE;\r
112 +    }\r
113 +\r
114 +    if (print_status_database ("notmuch restore", notmuch,\r
115 +                              notmuch_database_set_config (notmuch, key, val)))\r
116 +       goto DONE;\r
117 +\r
118 +    ret = EXIT_SUCCESS;\r
119 +\r
120 + DONE:\r
121 +    talloc_free (local);\r
122 +    return ret;\r
123 +}\r
124 +\r
125  static regex_t regex;\r
126  \r
127  /* Non-zero return indicates an error in retrieving the message,\r
128 @@ -137,6 +169,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
129  \r
130      int ret = 0;\r
131      int opt_index;\r
132 +    int include=0;\r
133      int input_format = DUMP_FORMAT_AUTO;\r
134  \r
135      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
136 @@ -152,6 +185,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
137                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
138                                   { "sup", DUMP_FORMAT_SUP },\r
139                                   { 0, 0 } } },\r
140 +       { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',\r
141 +         (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },\r
142 +                                 { "tags", DUMP_INCLUDE_TAGS} } },\r
143 +\r
144         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },\r
145         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },\r
146         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },\r
147 @@ -167,6 +204,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
148      notmuch_process_shared_options (argv[0]);\r
149      notmuch_exit_if_unmatched_db_uuid (notmuch);\r
150  \r
151 +    if (include == 0) {\r
152 +       include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS;\r
153 +    }\r
154 +\r
155      name_for_error = input_file_name ? input_file_name : "stdin";\r
156  \r
157      if (! accumulate)\r
158 @@ -225,11 +266,23 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
159             ret = EXIT_FAILURE;\r
160             goto DONE;\r
161         }\r
162 +\r
163 +       if ((include & DUMP_INCLUDE_CONFIG) && line_len >= 2 && line[0] == '#' && line[1] == '@') {\r
164 +           ret = process_config_line(notmuch, line+2);\r
165 +           if (ret)\r
166 +               goto DONE;\r
167 +       }\r
168 +\r
169      } while ((line_len == 0) ||\r
170              (line[0] == '#') ||\r
171              /* the cast is safe because we checked about for line_len < 0 */\r
172              (strspn (line, " \t\n") == (unsigned)line_len));\r
173  \r
174 +    if (! (include & DUMP_INCLUDE_TAGS)) {\r
175 +       ret = EXIT_SUCCESS;\r
176 +       goto DONE;\r
177 +    }\r
178 +\r
179      char *p;\r
180      for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {\r
181         if (*p == '(')\r
182 diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
183 index 5ea5300..9c1e566 100755\r
184 --- a/test/T590-libconfig.sh\r
185 +++ b/test/T590-libconfig.sh\r
186 @@ -132,4 +132,15 @@ cat <<'EOF' >EXPECTED\r
187  EOF\r
188  test_expect_equal_file EXPECTED OUTPUT\r
189  \r
190 +test_begin_subtest "restore config"\r
191 +notmuch dump --include=config >EXPECTED\r
192 +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
193 +{\r
194 +    RUN(notmuch_database_set_config (db, "testkey1", "mutatedvalue"));\r
195 +}\r
196 +EOF\r
197 +notmuch restore --include=config <EXPECTED\r
198 +notmuch dump --include=config >OUTPUT\r
199 +test_expect_equal_file EXPECTED OUTPUT\r
200 +\r
201  test_done\r
202 -- \r
203 2.8.0.rc3\r
204 \r