]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-delta.c
Merge branch 'js/empty-index-fixes'
[thirdparty/git.git] / t / helper / test-delta.c
CommitLineData
a310d434
NP
1/*
2 * test-delta.c: test code to exercise diff-delta.c and patch-delta.c
3 *
03aa8ff3 4 * (C) 2005 Nicolas Pitre <nico@fluxnic.net>
a310d434
NP
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
9153dde5 11#include "test-tool.h"
85023577 12#include "git-compat-util.h"
a310d434 13#include "delta.h"
d5ebb50d 14#include "wrapper.h"
a310d434 15
adb7b5fc 16static const char usage_str[] =
9153dde5 17 "test-tool delta (-d|-p) <from_file> <data_file> <out_file>";
a310d434 18
9153dde5 19int cmd__delta(int argc, const char **argv)
a310d434
NP
20{
21 int fd;
22 struct stat st;
f40a6934 23 void *from_buf = NULL, *data_buf = NULL, *out_buf = NULL;
a310d434 24 unsigned long from_size, data_size, out_size;
f40a6934 25 int ret = 1;
a310d434
NP
26
27 if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) {
b978403a 28 fprintf(stderr, "usage: %s\n", usage_str);
a310d434
NP
29 return 1;
30 }
31
32 fd = open(argv[2], O_RDONLY);
33 if (fd < 0 || fstat(fd, &st)) {
34 perror(argv[2]);
35 return 1;
36 }
37 from_size = st.st_size;
d65930c5
JK
38 from_buf = xmalloc(from_size);
39 if (read_in_full(fd, from_buf, from_size) < 0) {
a310d434 40 perror(argv[2]);
e35f9824 41 close(fd);
f40a6934 42 goto cleanup;
a310d434
NP
43 }
44 close(fd);
45
46 fd = open(argv[3], O_RDONLY);
47 if (fd < 0 || fstat(fd, &st)) {
48 perror(argv[3]);
f40a6934 49 goto cleanup;
a310d434
NP
50 }
51 data_size = st.st_size;
d65930c5
JK
52 data_buf = xmalloc(data_size);
53 if (read_in_full(fd, data_buf, data_size) < 0) {
a310d434 54 perror(argv[3]);
e35f9824 55 close(fd);
f40a6934 56 goto cleanup;
a310d434
NP
57 }
58 close(fd);
59
60 if (argv[1][1] == 'd')
61 out_buf = diff_delta(from_buf, from_size,
75c42d8c 62 data_buf, data_size,
3c849742 63 &out_size, 0);
a310d434
NP
64 else
65 out_buf = patch_delta(from_buf, from_size,
75c42d8c
LT
66 data_buf, data_size,
67 &out_size);
a310d434
NP
68 if (!out_buf) {
69 fprintf(stderr, "delta operation failed (returned NULL)\n");
f40a6934 70 goto cleanup;
a310d434
NP
71 }
72
73 fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
06f46f23 74 if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) {
a310d434 75 perror(argv[4]);
f40a6934 76 goto cleanup;
a310d434
NP
77 }
78
f40a6934
ÆAB
79 ret = 0;
80cleanup:
81 free(from_buf);
82 free(data_buf);
83 free(out_buf);
84
85 return ret;
a310d434 86}