Raw
1 /*
2 * test-delta.c: test code to exercise diff-delta.c and patch-delta.c
3 *
4 * (C) 2005 Nicolas Pitre <nico@fluxnic.net>
5 *
6 * This code is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include "test-tool.h"
12 #include "git-compat-util.h"
13 #include "delta.h"
14 #include "strbuf.h"
15
16 static const char usage_str[] =
17 "test-tool delta (-d|-p) <from_file> <data_file> <out_file>";
18
19 int cmd__delta(int argc, const char **argv)
20 {
21 int fd;
22 struct strbuf from = STRBUF_INIT, data = STRBUF_INIT;
23 char *out_buf;
24 unsigned long out_size;
25
26 if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p")))
27 usage(usage_str);
28
29 if (strbuf_read_file(&from, argv[2], 0) < 0)
30 die_errno("unable to read '%s'", argv[2]);
31 if (strbuf_read_file(&data, argv[3], 0) < 0)
32 die_errno("unable to read '%s'", argv[3]);
33
34 if (argv[1][1] == 'd')
35 out_buf = diff_delta(from.buf, from.len,
36 data.buf, data.len,
37 &out_size, 0);
38 else
39 out_buf = patch_delta(from.buf, from.len,
40 data.buf, data.len,
41 &out_size);
42 if (!out_buf)
43 die("delta operation failed (returned NULL)");
44
45 fd = xopen(argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
46 if (write_in_full(fd, out_buf, out_size) < 0)
47 die_errno("write(%s)", argv[4]);
48 if (close(fd) < 0)
49 die_errno("close(%s)", argv[4]);
50
51 strbuf_release(&from);
52 strbuf_release(&data);
53 free(out_buf);
54
55 return 0;
56 }