3 #include "xdiff/xdiff.h"
4 #include "xdiff-interface.h"
6 static const char merge_file_usage
[] =
7 "git merge-file [-p | --stdout] [--diff3] [-q | --quiet] [-L name1 [-L orig [-L name2]]] file1 orig_file file2";
9 int cmd_merge_file(int argc
, const char **argv
, const char *prefix
)
13 mmbuffer_t result
= {NULL
, 0};
14 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
15 int ret
= 0, i
= 0, to_stdout
= 0;
16 int merge_level
= XDL_MERGE_ZEALOUS_ALNUM
;
20 prefix
= setup_git_directory_gently(&nongit
);
22 /* Read the configuration file */
23 git_config(git_xmerge_config
, NULL
);
24 if (0 <= git_xmerge_style
)
25 merge_style
= git_xmerge_style
;
29 if (!strcmp(argv
[1], "-L") && i
< 3) {
33 } else if (!strcmp(argv
[1], "-p") ||
34 !strcmp(argv
[1], "--stdout"))
36 else if (!strcmp(argv
[1], "-q") ||
37 !strcmp(argv
[1], "--quiet"))
38 freopen("/dev/null", "w", stderr
);
39 else if (!strcmp(argv
[1], "--diff3"))
40 merge_style
= XDL_MERGE_DIFF3
;
42 usage(merge_file_usage
);
48 usage(merge_file_usage
);
51 names
[i
] = argv
[i
+ 1];
53 for (i
= 0; i
< 3; i
++) {
54 if (read_mmfile(mmfs
+ i
, argv
[i
+ 1]))
56 if (buffer_is_binary(mmfs
[i
].ptr
, mmfs
[i
].size
))
57 return error("Cannot merge binary files: %s\n",
61 ret
= xdl_merge(mmfs
+ 1, mmfs
+ 0, names
[0], mmfs
+ 2, names
[2],
62 &xpp
, merge_level
| merge_style
, &result
);
64 for (i
= 0; i
< 3; i
++)
68 const char *filename
= argv
[1];
69 FILE *f
= to_stdout
? stdout
: fopen(filename
, "wb");
72 ret
= error("Could not open %s for writing", filename
);
73 else if (result
.size
&&
74 fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
75 ret
= error("Could not write to %s", filename
);
77 ret
= error("Could not close %s", filename
);