Raw
1 /*
2 * Example implementation for the Git filter protocol version 2
3 * See Documentation/gitattributes.adoc, section "Filter Protocol"
4 *
5 * Usage: test-tool rot13-filter [--always-delay] --log=<path> <capabilities>
6 *
7 * Log path defines a debug log file that the script writes to. The
8 * subsequent arguments define a list of supported protocol capabilities
9 * ("clean", "smudge", etc).
10 *
11 * When --always-delay is given all pathnames with the "can-delay" flag
12 * that don't appear on the list below are delayed with a count of 1
13 * (see more below).
14 *
15 * This implementation supports special test cases:
16 * (1) If data with the pathname "clean-write-fail.r" is processed with
17 * a "clean" operation then the write operation will die.
18 * (2) If data with the pathname "smudge-write-fail.r" is processed with
19 * a "smudge" operation then the write operation will die.
20 * (3) If data with the pathname "error.r" is processed with any
21 * operation then the filter signals that it cannot or does not want
22 * to process the file.
23 * (4) If data with the pathname "abort.r" is processed with any
24 * operation then the filter signals that it cannot or does not want
25 * to process the file and any file after that is processed with the
26 * same command.
27 * (5) If data with a pathname that is a key in the delay hash is
28 * requested (e.g. "test-delay10.a") then the filter responds with
29 * a "delay" status and sets the "requested" field in the delay hash.
30 * The filter will signal the availability of this object after
31 * "count" (field in delay hash) "list_available_blobs" commands.
32 * (6) If data with the pathname "missing-delay.a" is processed that the
33 * filter will drop the path from the "list_available_blobs" response.
34 * (7) If data with the pathname "invalid-delay.a" is processed that the
35 * filter will add the path "unfiltered" which was not delayed before
36 * to the "list_available_blobs" response.
37 */
38
39 #include "test-tool.h"
40 #include "pkt-line.h"
41 #include "string-list.h"
42 #include "strmap.h"
43 #include "parse-options.h"
44
45 static FILE *logfile;
46 static int always_delay, has_clean_cap, has_smudge_cap;
47 static struct strmap delay = STRMAP_INIT;
48
49 static inline const char *str_or_null(const char *str)
50 {
51 return str ? str : "(null)";
52 }
53
54 static char *rot13(char *str)
55 {
56 char *c;
57 for (c = str; *c; c++)
58 if (isalpha(*c))
59 *c += tolower(*c) < 'n' ? 13 : -13;
60 return str;
61 }
62
63 static char *get_value(char *buf, const char *key)
64 {
65 const char *orig_buf = buf;
66 if (!buf ||
67 !skip_prefix((const char *)buf, key, (const char **)&buf) ||
68 !skip_prefix((const char *)buf, "=", (const char **)&buf) ||
69 !*buf)
70 die("expected key '%s', got '%s'", key, str_or_null(orig_buf));
71 return buf;
72 }
73
74 /*
75 * Read a text packet, expecting that it is in the form "key=value" for
76 * the given key. An EOF does not trigger any error and is reported
77 * back to the caller with NULL. Die if the "key" part of "key=value" does
78 * not match the given key, or the value part is empty.
79 */
80 static char *packet_key_val_read(const char *key)
81 {
82 char *buf;
83 if (packet_read_line_gently(0, NULL, &buf) < 0)
84 return NULL;
85 return xstrdup(get_value(buf, key));
86 }
87
88 static inline void assert_remote_capability(struct strset *caps, const char *cap)
89 {
90 if (!strset_contains(caps, cap))
91 die("required '%s' capability not available from remote", cap);
92 }
93
94 static void read_capabilities(struct strset *remote_caps)
95 {
96 for (;;) {
97 char *buf = packet_read_line(0, NULL);
98 if (!buf)
99 break;
100 strset_add(remote_caps, get_value(buf, "capability"));
101 }
102
103 assert_remote_capability(remote_caps, "clean");
104 assert_remote_capability(remote_caps, "smudge");
105 assert_remote_capability(remote_caps, "delay");
106 }
107
108 static void check_and_write_capabilities(struct strset *remote_caps,
109 const char **caps, int nr_caps)
110 {
111 int i;
112 for (i = 0; i < nr_caps; i++) {
113 if (!strset_contains(remote_caps, caps[i]))
114 die("our capability '%s' is not available from remote",
115 caps[i]);
116 packet_write_fmt(1, "capability=%s\n", caps[i]);
117 }
118 packet_flush(1);
119 }
120
121 struct delay_entry {
122 int requested, count;
123 char *output;
124 };
125
126 static void free_delay_entries(void)
127 {
128 struct hashmap_iter iter;
129 struct strmap_entry *ent;
130
131 strmap_for_each_entry(&delay, &iter, ent) {
132 struct delay_entry *delay_entry = ent->value;
133 free(delay_entry->output);
134 free(delay_entry);
135 }
136 strmap_clear(&delay, 0);
137 }
138
139 static void add_delay_entry(const char *pathname, int count, int requested)
140 {
141 struct delay_entry *entry = xcalloc(1, sizeof(*entry));
142 entry->count = count;
143 entry->requested = requested;
144 if (strmap_put(&delay, pathname, entry))
145 BUG("adding the same path twice to delay hash?");
146 }
147
148 static void reply_list_available_blobs_cmd(void)
149 {
150 struct hashmap_iter iter;
151 struct strmap_entry *ent;
152 struct string_list_item *str_item;
153 struct string_list paths = STRING_LIST_INIT_NODUP;
154
155 /* flush */
156 if (packet_read_line(0, NULL))
157 die("bad list_available_blobs end");
158
159 strmap_for_each_entry(&delay, &iter, ent) {
160 struct delay_entry *delay_entry = ent->value;
161 if (!delay_entry->requested)
162 continue;
163 delay_entry->count--;
164 if (!strcmp(ent->key, "invalid-delay.a")) {
165 /* Send Git a pathname that was not delayed earlier */
166 packet_write_fmt(1, "pathname=unfiltered");
167 }
168 if (!strcmp(ent->key, "missing-delay.a")) {
169 /* Do not signal Git that this file is available */
170 } else if (!delay_entry->count) {
171 string_list_append(&paths, ent->key);
172 packet_write_fmt(1, "pathname=%s", ent->key);
173 }
174 }
175
176 /* Print paths in sorted order. */
177 string_list_sort(&paths);
178 for_each_string_list_item(str_item, &paths)
179 fprintf(logfile, " %s", str_item->string);
180 string_list_clear(&paths, 0);
181
182 packet_flush(1);
183
184 fprintf(logfile, " [OK]\n");
185 packet_write_fmt(1, "status=success");
186 packet_flush(1);
187 }
188
189 static void command_loop(void)
190 {
191 for (;;) {
192 char *buf;
193 const char *output;
194 char *pathname;
195 struct delay_entry *entry;
196 struct strbuf input = STRBUF_INIT;
197 char *command = packet_key_val_read("command");
198
199 if (!command) {
200 fprintf(logfile, "STOP\n");
201 break;
202 }
203 fprintf(logfile, "IN: %s", command);
204
205 if (!strcmp(command, "list_available_blobs")) {
206 reply_list_available_blobs_cmd();
207 free(command);
208 continue;
209 }
210
211 pathname = packet_key_val_read("pathname");
212 if (!pathname)
213 die("unexpected EOF while expecting pathname");
214 fprintf(logfile, " %s", pathname);
215
216 /* Read until flush */
217 while ((buf = packet_read_line(0, NULL))) {
218 if (!strcmp(buf, "can-delay=1")) {
219 entry = strmap_get(&delay, pathname);
220 if (entry && !entry->requested)
221 entry->requested = 1;
222 else if (!entry && always_delay)
223 add_delay_entry(pathname, 1, 1);
224 } else if (starts_with(buf, "ref=") ||
225 starts_with(buf, "treeish=") ||
226 starts_with(buf, "blob=")) {
227 fprintf(logfile, " %s", buf);
228 } else {
229 /*
230 * In general, filters need to be graceful about
231 * new metadata, since it's documented that we
232 * can pass any key-value pairs, but for tests,
233 * let's be a little stricter.
234 */
235 die("Unknown message '%s'", buf);
236 }
237 }
238
239 read_packetized_to_strbuf(0, &input, 0);
240 fprintf(logfile, " %"PRIuMAX" [OK] -- ", (uintmax_t)input.len);
241
242 entry = strmap_get(&delay, pathname);
243 if (entry && entry->output) {
244 output = entry->output;
245 } else if (!strcmp(pathname, "error.r") || !strcmp(pathname, "abort.r")) {
246 output = "";
247 } else if (!strcmp(command, "clean") && has_clean_cap) {
248 output = rot13(input.buf);
249 } else if (!strcmp(command, "smudge") && has_smudge_cap) {
250 output = rot13(input.buf);
251 } else {
252 die("bad command '%s'", command);
253 }
254
255 if (!strcmp(pathname, "error.r")) {
256 fprintf(logfile, "[ERROR]\n");
257 packet_write_fmt(1, "status=error");
258 packet_flush(1);
259 } else if (!strcmp(pathname, "abort.r")) {
260 fprintf(logfile, "[ABORT]\n");
261 packet_write_fmt(1, "status=abort");
262 packet_flush(1);
263 } else if (!strcmp(command, "smudge") &&
264 (entry = strmap_get(&delay, pathname)) &&
265 entry->requested == 1) {
266 fprintf(logfile, "[DELAYED]\n");
267 packet_write_fmt(1, "status=delayed");
268 packet_flush(1);
269 entry->requested = 2;
270 if (entry->output != output) {
271 free(entry->output);
272 entry->output = xstrdup(output);
273 }
274 } else {
275 int i, nr_packets = 0;
276 size_t output_len;
277 const char *p;
278 packet_write_fmt(1, "status=success");
279 packet_flush(1);
280
281 if (skip_prefix(pathname, command, &p) &&
282 !strcmp(p, "-write-fail.r")) {
283 fprintf(logfile, "[WRITE FAIL]\n");
284 die("%s write error", command);
285 }
286
287 output_len = strlen(output);
288 fprintf(logfile, "OUT: %"PRIuMAX" ", (uintmax_t)output_len);
289
290 if (write_packetized_from_buf_no_flush_count(output,
291 output_len, 1, &nr_packets))
292 die("failed to write buffer to stdout");
293 packet_flush(1);
294
295 for (i = 0; i < nr_packets; i++)
296 fprintf(logfile, ".");
297 fprintf(logfile, " [OK]\n");
298
299 packet_flush(1);
300 }
301 free(pathname);
302 strbuf_release(&input);
303 free(command);
304 }
305 }
306
307 static void packet_initialize(void)
308 {
309 char *pkt_buf = packet_read_line(0, NULL);
310
311 if (!pkt_buf || strcmp(pkt_buf, "git-filter-client"))
312 die("bad initialize: '%s'", str_or_null(pkt_buf));
313
314 pkt_buf = packet_read_line(0, NULL);
315 if (!pkt_buf || strcmp(pkt_buf, "version=2"))
316 die("bad version: '%s'", str_or_null(pkt_buf));
317
318 pkt_buf = packet_read_line(0, NULL);
319 if (pkt_buf)
320 die("bad version end: '%s'", pkt_buf);
321
322 packet_write_fmt(1, "git-filter-server");
323 packet_write_fmt(1, "version=2");
324 packet_flush(1);
325 }
326
327 static const char *const rot13_usage[] = {
328 "test-tool rot13-filter [--always-delay] --log=<path> <capabilities>",
329 NULL
330 };
331
332 int cmd__rot13_filter(int argc, const char **argv)
333 {
334 int i, nr_caps;
335 struct strset remote_caps = STRSET_INIT;
336 const char *log_path = NULL;
337
338 struct option options[] = {
339 OPT_BOOL(0, "always-delay", &always_delay,
340 "delay all paths with the can-delay flag"),
341 OPT_STRING(0, "log", &log_path, "path",
342 "path to the debug log file"),
343 OPT_END()
344 };
345 nr_caps = parse_options(argc, argv, NULL, options, rot13_usage,
346 PARSE_OPT_STOP_AT_NON_OPTION);
347
348 if (!log_path || !nr_caps)
349 usage_with_options(rot13_usage, options);
350
351 logfile = fopen(log_path, "a");
352 if (!logfile)
353 die_errno("failed to open log file");
354
355 for (i = 0; i < nr_caps; i++) {
356 if (!strcmp(argv[i], "smudge"))
357 has_smudge_cap = 1;
358 if (!strcmp(argv[i], "clean"))
359 has_clean_cap = 1;
360 }
361
362 add_delay_entry("test-delay10.a", 1, 0);
363 add_delay_entry("test-delay11.a", 1, 0);
364 add_delay_entry("test-delay20.a", 2, 0);
365 add_delay_entry("test-delay10.b", 1, 0);
366 add_delay_entry("missing-delay.a", 1, 0);
367 add_delay_entry("invalid-delay.a", 1, 0);
368
369 fprintf(logfile, "START\n");
370 packet_initialize();
371
372 read_capabilities(&remote_caps);
373 check_and_write_capabilities(&remote_caps, argv, nr_caps);
374 fprintf(logfile, "init handshake complete\n");
375 strset_clear(&remote_caps);
376
377 command_loop();
378
379 if (fclose(logfile))
380 die_errno("error closing logfile");
381 free_delay_entries();
382 return 0;
383 }