Merge branch 'ml/doc-merge-updates' into maint-2.43
[alt-git.git] / builtin / merge-file.c
blob832c93d8d54cc50b8581102be4db7c92d80e738a
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "hex.h"
4 #include "object-name.h"
5 #include "object-store.h"
6 #include "config.h"
7 #include "gettext.h"
8 #include "setup.h"
9 #include "xdiff/xdiff.h"
10 #include "xdiff-interface.h"
11 #include "parse-options.h"
13 static const char *const merge_file_usage[] = {
14 N_("git merge-file [<options>] [-L <name1> [-L <orig> [-L <name2>]]] <file1> <orig-file> <file2>"),
15 NULL
18 static int label_cb(const struct option *opt, const char *arg, int unset)
20 static int label_count = 0;
21 const char **names = (const char **)opt->value;
23 BUG_ON_OPT_NEG(unset);
25 if (label_count >= 3)
26 return error("too many labels on the command line");
27 names[label_count++] = arg;
28 return 0;
31 int cmd_merge_file(int argc, const char **argv, const char *prefix)
33 const char *names[3] = { 0 };
34 mmfile_t mmfs[3] = { 0 };
35 mmbuffer_t result = { 0 };
36 xmparam_t xmp = { 0 };
37 int ret = 0, i = 0, to_stdout = 0, object_id = 0;
38 int quiet = 0;
39 struct option options[] = {
40 OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
41 OPT_BOOL(0, "object-id", &object_id, N_("use object IDs instead of filenames")),
42 OPT_SET_INT(0, "diff3", &xmp.style, N_("use a diff3 based merge"), XDL_MERGE_DIFF3),
43 OPT_SET_INT(0, "zdiff3", &xmp.style, N_("use a zealous diff3 based merge"),
44 XDL_MERGE_ZEALOUS_DIFF3),
45 OPT_SET_INT(0, "ours", &xmp.favor, N_("for conflicts, use our version"),
46 XDL_MERGE_FAVOR_OURS),
47 OPT_SET_INT(0, "theirs", &xmp.favor, N_("for conflicts, use their version"),
48 XDL_MERGE_FAVOR_THEIRS),
49 OPT_SET_INT(0, "union", &xmp.favor, N_("for conflicts, use a union version"),
50 XDL_MERGE_FAVOR_UNION),
51 OPT_INTEGER(0, "marker-size", &xmp.marker_size,
52 N_("for conflicts, use this marker size")),
53 OPT__QUIET(&quiet, N_("do not warn about conflicts")),
54 OPT_CALLBACK('L', NULL, names, N_("name"),
55 N_("set labels for file1/orig-file/file2"), &label_cb),
56 OPT_END(),
59 xmp.level = XDL_MERGE_ZEALOUS_ALNUM;
60 xmp.style = 0;
61 xmp.favor = 0;
63 if (startup_info->have_repository) {
64 /* Read the configuration file */
65 git_config(git_xmerge_config, NULL);
66 if (0 <= git_xmerge_style)
67 xmp.style = git_xmerge_style;
70 argc = parse_options(argc, argv, prefix, options, merge_file_usage, 0);
71 if (argc != 3)
72 usage_with_options(merge_file_usage, options);
73 if (quiet) {
74 if (!freopen("/dev/null", "w", stderr))
75 return error_errno("failed to redirect stderr to /dev/null");
78 if (object_id)
79 setup_git_directory();
81 for (i = 0; i < 3; i++) {
82 char *fname;
83 struct object_id oid;
84 mmfile_t *mmf = mmfs + i;
86 if (!names[i])
87 names[i] = argv[i];
89 fname = prefix_filename(prefix, argv[i]);
91 if (object_id) {
92 if (repo_get_oid(the_repository, argv[i], &oid))
93 ret = error(_("object '%s' does not exist"),
94 argv[i]);
95 else if (!oideq(&oid, the_hash_algo->empty_blob))
96 read_mmblob(mmf, &oid);
97 else
98 read_mmfile(mmf, "/dev/null");
99 } else if (read_mmfile(mmf, fname)) {
100 ret = -1;
102 if (ret != -1 && (mmf->size > MAX_XDIFF_SIZE ||
103 buffer_is_binary(mmf->ptr, mmf->size))) {
104 ret = error("Cannot merge binary files: %s",
105 argv[i]);
108 free(fname);
109 if (ret)
110 goto cleanup;
114 xmp.ancestor = names[1];
115 xmp.file1 = names[0];
116 xmp.file2 = names[2];
117 ret = xdl_merge(mmfs + 1, mmfs + 0, mmfs + 2, &xmp, &result);
119 if (ret >= 0) {
120 if (object_id && !to_stdout) {
121 struct object_id oid;
122 if (result.size) {
123 if (write_object_file(result.ptr, result.size, OBJ_BLOB, &oid) < 0)
124 ret = error(_("Could not write object file"));
125 } else {
126 oidcpy(&oid, the_hash_algo->empty_blob);
128 if (ret >= 0)
129 printf("%s\n", oid_to_hex(&oid));
130 } else {
131 const char *filename = argv[0];
132 char *fpath = prefix_filename(prefix, argv[0]);
133 FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
135 if (!f)
136 ret = error_errno("Could not open %s for writing",
137 filename);
138 else if (result.size &&
139 fwrite(result.ptr, result.size, 1, f) != 1)
140 ret = error_errno("Could not write to %s", filename);
141 else if (fclose(f))
142 ret = error_errno("Could not close %s", filename);
143 free(fpath);
145 free(result.ptr);
148 if (ret > 127)
149 ret = 127;
151 cleanup:
152 for (i = 0; i < 3; i++)
153 free(mmfs[i].ptr);
155 return ret;