t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / builtin / merge-file.c
blobcb42865eb524bc9cd66f64fbe016c56df4869c87
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "abspath.h"
4 #include "diff.h"
5 #include "hex.h"
6 #include "object-name.h"
7 #include "object-store.h"
8 #include "config.h"
9 #include "gettext.h"
10 #include "setup.h"
11 #include "xdiff/xdiff.h"
12 #include "xdiff-interface.h"
13 #include "parse-options.h"
15 static const char *const merge_file_usage[] = {
16 N_("git merge-file [<options>] [-L <name1> [-L <orig> [-L <name2>]]] <file1> <orig-file> <file2>"),
17 NULL
20 static int label_cb(const struct option *opt, const char *arg, int unset)
22 static int label_count = 0;
23 const char **names = (const char **)opt->value;
25 BUG_ON_OPT_NEG(unset);
27 if (label_count >= 3)
28 return error("too many labels on the command line");
29 names[label_count++] = arg;
30 return 0;
33 static int set_diff_algorithm(xpparam_t *xpp,
34 const char *alg)
36 long diff_algorithm = parse_algorithm_value(alg);
37 if (diff_algorithm < 0)
38 return -1;
39 xpp->flags = (xpp->flags & ~XDF_DIFF_ALGORITHM_MASK) | diff_algorithm;
40 return 0;
43 static int diff_algorithm_cb(const struct option *opt,
44 const char *arg, int unset)
46 xpparam_t *xpp = opt->value;
48 BUG_ON_OPT_NEG(unset);
50 if (set_diff_algorithm(xpp, arg))
51 return error(_("option diff-algorithm accepts \"myers\", "
52 "\"minimal\", \"patience\" and \"histogram\""));
54 return 0;
57 int cmd_merge_file(int argc,
58 const char **argv,
59 const char *prefix,
60 struct repository *repo UNUSED)
62 const char *names[3] = { 0 };
63 mmfile_t mmfs[3] = { 0 };
64 mmbuffer_t result = { 0 };
65 xmparam_t xmp = { 0 };
66 int ret = 0, i = 0, to_stdout = 0, object_id = 0;
67 int quiet = 0;
68 struct option options[] = {
69 OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
70 OPT_BOOL(0, "object-id", &object_id, N_("use object IDs instead of filenames")),
71 OPT_SET_INT(0, "diff3", &xmp.style, N_("use a diff3 based merge"), XDL_MERGE_DIFF3),
72 OPT_SET_INT(0, "zdiff3", &xmp.style, N_("use a zealous diff3 based merge"),
73 XDL_MERGE_ZEALOUS_DIFF3),
74 OPT_SET_INT(0, "ours", &xmp.favor, N_("for conflicts, use our version"),
75 XDL_MERGE_FAVOR_OURS),
76 OPT_SET_INT(0, "theirs", &xmp.favor, N_("for conflicts, use their version"),
77 XDL_MERGE_FAVOR_THEIRS),
78 OPT_SET_INT(0, "union", &xmp.favor, N_("for conflicts, use a union version"),
79 XDL_MERGE_FAVOR_UNION),
80 OPT_CALLBACK_F(0, "diff-algorithm", &xmp.xpp, N_("<algorithm>"),
81 N_("choose a diff algorithm"),
82 PARSE_OPT_NONEG, diff_algorithm_cb),
83 OPT_INTEGER(0, "marker-size", &xmp.marker_size,
84 N_("for conflicts, use this marker size")),
85 OPT__QUIET(&quiet, N_("do not warn about conflicts")),
86 OPT_CALLBACK('L', NULL, names, N_("name"),
87 N_("set labels for file1/orig-file/file2"), &label_cb),
88 OPT_END(),
91 xmp.level = XDL_MERGE_ZEALOUS_ALNUM;
92 xmp.style = 0;
93 xmp.favor = 0;
95 if (startup_info->have_repository) {
96 /* Read the configuration file */
97 git_config(git_xmerge_config, NULL);
98 if (0 <= git_xmerge_style)
99 xmp.style = git_xmerge_style;
102 argc = parse_options(argc, argv, prefix, options, merge_file_usage, 0);
103 if (argc != 3)
104 usage_with_options(merge_file_usage, options);
105 if (quiet) {
106 if (!freopen("/dev/null", "w", stderr))
107 return error_errno("failed to redirect stderr to /dev/null");
110 if (object_id)
111 setup_git_directory();
113 for (i = 0; i < 3; i++) {
114 char *fname;
115 struct object_id oid;
116 mmfile_t *mmf = mmfs + i;
118 if (!names[i])
119 names[i] = argv[i];
121 fname = prefix_filename(prefix, argv[i]);
123 if (object_id) {
124 if (repo_get_oid(the_repository, argv[i], &oid))
125 ret = error(_("object '%s' does not exist"),
126 argv[i]);
127 else if (!oideq(&oid, the_hash_algo->empty_blob))
128 read_mmblob(mmf, &oid);
129 else
130 read_mmfile(mmf, "/dev/null");
131 } else if (read_mmfile(mmf, fname)) {
132 ret = -1;
134 if (ret != -1 && (mmf->size > MAX_XDIFF_SIZE ||
135 buffer_is_binary(mmf->ptr, mmf->size))) {
136 ret = error("Cannot merge binary files: %s",
137 argv[i]);
140 free(fname);
141 if (ret)
142 goto cleanup;
146 xmp.ancestor = names[1];
147 xmp.file1 = names[0];
148 xmp.file2 = names[2];
149 ret = xdl_merge(mmfs + 1, mmfs + 0, mmfs + 2, &xmp, &result);
151 if (ret >= 0) {
152 if (object_id && !to_stdout) {
153 struct object_id oid;
154 if (result.size) {
155 if (write_object_file(result.ptr, result.size, OBJ_BLOB, &oid) < 0)
156 ret = error(_("Could not write object file"));
157 } else {
158 oidcpy(&oid, the_hash_algo->empty_blob);
160 if (ret >= 0)
161 printf("%s\n", oid_to_hex(&oid));
162 } else {
163 const char *filename = argv[0];
164 char *fpath = prefix_filename(prefix, argv[0]);
165 FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
167 if (!f)
168 ret = error_errno("Could not open %s for writing",
169 filename);
170 else if (result.size &&
171 fwrite(result.ptr, result.size, 1, f) != 1)
172 ret = error_errno("Could not write to %s", filename);
173 else if (fclose(f))
174 ret = error_errno("Could not close %s", filename);
175 free(fpath);
177 free(result.ptr);
180 if (ret > 127)
181 ret = 127;
183 cleanup:
184 for (i = 0; i < 3; i++)
185 free(mmfs[i].ptr);
187 return ret;