git-branch: rename config vars branch.<branch>.*, too
[git/mjg.git] / builtin-merge-file.c
blob6c4c3a351333b94ab62b77793b540af5c9bcaa3a
1 #include "cache.h"
2 #include "xdiff/xdiff.h"
4 static const char merge_file_usage[] =
5 "git merge-file [-p | --stdout] [-q | --quiet] [-L name1 [-L orig [-L name2]]] file1 orig_file file2";
7 static int read_file(mmfile_t *ptr, const char *filename)
9 struct stat st;
10 FILE *f;
12 if (stat(filename, &st))
13 return error("Could not stat %s", filename);
14 if ((f = fopen(filename, "rb")) == NULL)
15 return error("Could not open %s", filename);
16 ptr->ptr = xmalloc(st.st_size);
17 if (fread(ptr->ptr, st.st_size, 1, f) != 1)
18 return error("Could not read %s", filename);
19 fclose(f);
20 ptr->size = st.st_size;
21 return 0;
24 int cmd_merge_file(int argc, char **argv, char **envp)
26 char *names[3];
27 mmfile_t mmfs[3];
28 mmbuffer_t result = {NULL, 0};
29 xpparam_t xpp = {XDF_NEED_MINIMAL};
30 int ret = 0, i = 0, to_stdout = 0;
32 while (argc > 4) {
33 if (!strcmp(argv[1], "-L") && i < 3) {
34 names[i++] = argv[2];
35 argc--;
36 argv++;
37 } else if (!strcmp(argv[1], "-p") ||
38 !strcmp(argv[1], "--stdout"))
39 to_stdout = 1;
40 else if (!strcmp(argv[1], "-q") ||
41 !strcmp(argv[1], "--quiet"))
42 freopen("/dev/null", "w", stderr);
43 else
44 usage(merge_file_usage);
45 argc--;
46 argv++;
49 if (argc != 4)
50 usage(merge_file_usage);
52 for (; i < 3; i++)
53 names[i] = argv[i + 1];
55 for (i = 0; i < 3; i++)
56 if (read_file(mmfs + i, argv[i + 1]))
57 return -1;
59 ret = xdl_merge(mmfs + 1, mmfs + 0, names[0], mmfs + 2, names[2],
60 &xpp, XDL_MERGE_ZEALOUS, &result);
62 for (i = 0; i < 3; i++)
63 free(mmfs[i].ptr);
65 if (ret >= 0) {
66 char *filename = argv[1];
67 FILE *f = to_stdout ? stdout : fopen(filename, "wb");
69 if (!f)
70 ret = error("Could not open %s for writing", filename);
71 else if (fwrite(result.ptr, result.size, 1, f) != 1)
72 ret = error("Could not write to %s", filename);
73 else if (fclose(f))
74 ret = error("Could not close %s", filename);
75 free(result.ptr);
78 return ret;