Update git-merge documentation.
[git/git-p4.git] / builtin-merge-file.c
blob10ec63b17e6b16382f2b39e5c01cc6cf1dce448e
1 #include "cache.h"
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)
10 char *names[3];
11 mmfile_t mmfs[3];
12 mmbuffer_t result = {NULL, 0};
13 xpparam_t xpp = {XDF_NEED_MINIMAL};
14 int ret = 0, i = 0, to_stdout = 0;
16 while (argc > 4) {
17 if (!strcmp(argv[1], "-L") && i < 3) {
18 names[i++] = argv[2];
19 argc--;
20 argv++;
21 } else if (!strcmp(argv[1], "-p") ||
22 !strcmp(argv[1], "--stdout"))
23 to_stdout = 1;
24 else if (!strcmp(argv[1], "-q") ||
25 !strcmp(argv[1], "--quiet"))
26 freopen("/dev/null", "w", stderr);
27 else
28 usage(merge_file_usage);
29 argc--;
30 argv++;
33 if (argc != 4)
34 usage(merge_file_usage);
36 for (; i < 3; i++)
37 names[i] = argv[i + 1];
39 for (i = 0; i < 3; i++) {
40 if (read_mmfile(mmfs + i, argv[i + 1]))
41 return -1;
42 if (buffer_is_binary(mmfs[i].ptr, mmfs[i].size))
43 return error("Cannot merge binary files: %s\n",
44 argv[i + 1]);
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++)
51 free(mmfs[i].ptr);
53 if (ret >= 0) {
54 char *filename = argv[1];
55 FILE *f = to_stdout ? stdout : fopen(filename, "wb");
57 if (!f)
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);
61 else if (fclose(f))
62 ret = error("Could not close %s", filename);
63 free(result.ptr);
66 return ret;