Merge branch 'master' of git://repo.or.cz/git-gui
[git/jnareb-git/bp-gitweb.git] / builtin-merge-file.c
blob913577390862a847857a650516740059072b60ad
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;
43 ret = xdl_merge(mmfs + 1, mmfs + 0, names[0], mmfs + 2, names[2],
44 &xpp, XDL_MERGE_ZEALOUS, &result);
46 for (i = 0; i < 3; i++)
47 free(mmfs[i].ptr);
49 if (ret >= 0) {
50 char *filename = argv[1];
51 FILE *f = to_stdout ? stdout : fopen(filename, "wb");
53 if (!f)
54 ret = error("Could not open %s for writing", filename);
55 else if (fwrite(result.ptr, result.size, 1, f) != 1)
56 ret = error("Could not write to %s", filename);
57 else if (fclose(f))
58 ret = error("Could not close %s", filename);
59 free(result.ptr);
62 return ret;