2 #include "xdiff/xdiff.h"
3 #include "xdiff-interface.h"
5 static const char merge_file_usage[] =
6 "git merge-file [-p | --stdout] [-q | --quiet] [-L name1 [-L orig [-L name2]]] file1 orig_file file2";
8 int cmd_merge_file(int argc, char **argv, char **envp)
12 mmbuffer_t result = {NULL, 0};
13 xpparam_t xpp = {XDF_NEED_MINIMAL};
14 int ret = 0, i = 0, to_stdout = 0;
17 if (!strcmp(argv[1], "-L") && i < 3) {
21 } else if (!strcmp(argv[1], "-p") ||
22 !strcmp(argv[1], "--stdout"))
24 else if (!strcmp(argv[1], "-q") ||
25 !strcmp(argv[1], "--quiet"))
26 freopen("/dev/null", "w", stderr);
28 usage(merge_file_usage);
34 usage(merge_file_usage);
37 names[i] = argv[i + 1];
39 for (i = 0; i < 3; i++) {
40 if (read_mmfile(mmfs + i, argv[i + 1]))
42 if (buffer_is_binary(mmfs[i].ptr, mmfs[i].size))
43 return error("Cannot merge binary files: %s\n",
47 ret = xdl_merge(mmfs + 1, mmfs + 0, names[0], mmfs + 2, names[2],
48 &xpp, XDL_MERGE_ZEALOUS, &result);
50 for (i = 0; i < 3; i++)
54 char *filename = argv[1];
55 FILE *f = to_stdout ? stdout : fopen(filename, "wb");
58 ret = error("Could not open %s for writing", filename);
59 else if (fwrite(result.ptr, result.size, 1, f) != 1)
60 ret = error("Could not write to %s", filename);
62 ret = error("Could not close %s", filename);