1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
| | /* notmuch - Not much of an email program, (just index and search)
*
* Copyright © 2023 Felipe Contreras
* Copyright © 2024 David Bremner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/ .
*
* Authors: Felipe Contreras
* David Bremner <david@tethera.net>
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <notmuch.h>
#include "notmuch-client.h"
#include "path-util.h"
#include "hex-escape.h"
#include "string-util.h"
#define ASSERT(x) assert((x))
/* File scope globals */
const char *alias = NULL;
const char *nm_dir = NULL;
const char *url = NULL;
const char* debug_flags = NULL;
unsigned long lastmod;
notmuch_database_t *db;
FILE *log_file = NULL;
static void
flog (const char *format, ...) {
va_list va_args;
if (log_file) {
va_start (va_args, format);
vfprintf (log_file, format, va_args);
fflush (log_file);
va_end (va_args);
}
}
static unsigned long read_lastmod (const void *ctx, const char *dir) {
char *filename = NULL;
unsigned long num = 0;
FILE *in;
ASSERT(filename = talloc_asprintf (ctx, "%s/lastmod", dir));
in = fopen (filename, "r");
if (in) {
ASSERT(fscanf (in, "%zu", &num) == 1);
} else {
if (errno != ENOENT) {
fprintf (stderr, "error opening lastmod file");
exit(EXIT_FAILURE);
}
}
flog ("loaded lastmod = %zu\n", num);
return num;
}
static void
store_lastmod (notmuch_database_t *notmuch, const char *dir) {
char *filename = NULL;
FILE *out;
ASSERT(filename = talloc_asprintf (notmuch, "%s/lastmod", dir));
ASSERT(out = fopen (filename, "w"));
ASSERT(fprintf (out, "%zu\n", notmuch_database_get_revision (notmuch, NULL)) > 0);
}
static void
read_one_line (FILE *stream, char **line_p, size_t *len_p) {
ssize_t nread;
nread = getline(line_p, len_p, stream);
if (nread < 0) {
perror ("getline");
exit (EXIT_FAILURE);
}
ASSERT(line_p);
chomp_newline(*line_p);
}
static const char*
shell2string (const char* command) {
FILE *out;
char *line = NULL;
size_t len;
out = popen (command,"r");
if (out == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
read_one_line (out, &line, &len);
return line;
}
static void
wr_data(const char *data) {
printf ("data %zu\n", strlen(data));
fputs (data, stdout);
}
static void
cmd_capabilities () {
fputs("import\nexport\nrefspec refs/heads/*:refs/notmuch/*\n\n", stdout);
fflush (stdout);
}
static void
cmd_list () {
unsigned long current_lastmod;
current_lastmod = notmuch_database_get_revision (db, NULL);
printf("? refs/heads/master%s\n\n",
lastmod == current_lastmod ? " unchanged" : "");
fflush (stdout);
}
static void
usage() {
fprintf (stderr, "usage: git-remote-nm ALIAS URL\n");
exit(EXIT_FAILURE);
}
static void
cmd_import (notmuch_database_t *notmuch) {
const char* ident = NULL;
const char* lastmod_str = NULL;
notmuch_messages_t *messages;
notmuch_status_t status;
notmuch_query_t *query;
char *mid_buf = NULL;
size_t mid_buf_len = 0;
ident = shell2string("git var GIT_COMMITTER_IDENT");
printf ("feature done\ncommit refs/notmuch/master\nmark :1\ncommitter %s\n", ident);
ASSERT(lastmod_str = talloc_asprintf (notmuch, "lastmod: %zu\n", lastmod));
wr_data (lastmod_str);
if (lastmod > 0)
puts("from refs/notmuch/master^0");
puts("deleteall");
status = notmuch_query_create_with_syntax (notmuch,
"",
NOTMUCH_QUERY_SYNTAX_XAPIAN,
&query);
if (print_status_database ("git-remote-nm", notmuch, status))
exit(EXIT_FAILURE);
if (debug_flags && strchr (debug_flags, 's'))
notmuch_query_set_sort (query, NOTMUCH_SORT_NEWEST_FIRST);
else
notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
status = notmuch_query_search_messages (query, &messages);
if (print_status_query ("git-remote-nm", query, status))
exit(EXIT_FAILURE);
for (;
notmuch_messages_valid (messages);
notmuch_messages_move_to_next (messages)) {
const char* tag_buf = "";
const char* mid;
const char* hash;
notmuch_message_t *message = notmuch_messages_get (messages);
mid = notmuch_message_get_message_id (message);
if (hex_encode (notmuch, mid, &mid_buf, &mid_buf_len) != HEX_SUCCESS) {
fprintf (stderr, "Error: failed to hex-encode message-id %s\n", mid);
exit(EXIT_FAILURE);
}
/* we can't use _notmuch_sha1_from_string because we don't want
* to include the null terminator */
GChecksum *sha1;
sha1 = g_checksum_new (G_CHECKSUM_SHA1);
g_checksum_update (sha1, (const guchar *) mid, strlen (mid));
hash = g_checksum_get_string (sha1);
printf ("M 644 inline %2.2s/%2.2s/%s/tags\n", hash, hash+2, mid_buf);
g_checksum_free (sha1);
for (notmuch_tags_t *tags = notmuch_message_get_tags (message);
notmuch_tags_valid (tags);
notmuch_tags_move_to_next (tags)) {
const char *tag_str = notmuch_tags_get (tags);
ASSERT(tag_buf=talloc_asprintf (message, "%s%s\n", tag_buf, tag_str));
}
wr_data (tag_buf);
notmuch_message_destroy (message);
}
puts("");
puts("done");
fflush (stdout);
store_lastmod(notmuch, nm_dir);
}
int
main (int argc, char *argv[])
{
notmuch_status_t status;
char *status_string = NULL;
const char* git_dir;
ssize_t nread;
size_t len = 0;
const char *log_file_name;
char *line = NULL;
debug_flags = getenv ("GIT_REMOTE_NM_DEBUG");
log_file_name = getenv ("GIT_REMOTE_NM_LOG");
if (log_file_name)
log_file = fopen (log_file_name, "w");
if (argc != 3)
usage();
/* setup globals */
alias = argv[1];
url = argv[2];
status = notmuch_database_open_with_config (NULL,
NOTMUCH_DATABASE_MODE_READ_WRITE,
NULL,
NULL,
&db,
&status_string);
if (status) {
if (status_string) {
fputs (status_string, stderr);
free (status_string);
status_string = NULL;
}
return EXIT_FAILURE;
}
git_dir = getenv ("GIT_DIR");
if (! git_dir) {
fprintf (stderr, "GIT_DIR not set\n");
exit(EXIT_FAILURE);
}
flog ("GIT_DIR=%s\n", git_dir);
ASSERT(nm_dir = talloc_asprintf(db, "%s/%s", git_dir, "notmuch"));
status = mkdir_recursive (db, nm_dir, 0700, &status_string);
if (status) {
if (status_string)
fputs(status_string, stderr);
exit (EXIT_FAILURE);
}
lastmod = read_lastmod (db, nm_dir);
while ((nread = getline(&line, &len, stdin)) != -1) {
size_t count=0;
char *s = line;
flog ("command = %s\n", line);
/* skip leading space */
while (*s && isspace(*s)) s++;
while (s[count] && ! isspace(s[count])) count++;
if (count == 0)
break;
if (STRNCMP_LITERAL (s, "capabilities")== 0)
cmd_capabilities ();
else if (STRNCMP_LITERAL (s, "import") == 0)
cmd_import (db);
else if (STRNCMP_LITERAL (s, "list") == 0)
cmd_list ();
fflush(stdout);
flog ("finished command = %s\n", s);
}
flog ("finished loop\n");
notmuch_database_destroy (db);
}
|