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
)
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
);
20 ptr
->size
= st
.st_size
;
24 int cmd_merge_file(int argc
, char **argv
, char **envp
)
28 mmbuffer_t result
= {NULL
, 0};
29 xpparam_t xpp
= {XDF_NEED_MINIMAL
};
30 int ret
= 0, i
= 0, to_stdout
= 0;
33 if (!strcmp(argv
[1], "-L") && i
< 3) {
37 } else if (!strcmp(argv
[1], "-p") ||
38 !strcmp(argv
[1], "--stdout"))
40 else if (!strcmp(argv
[1], "-q") ||
41 !strcmp(argv
[1], "--quiet"))
42 freopen("/dev/null", "w", stderr
);
44 usage(merge_file_usage
);
50 usage(merge_file_usage
);
53 names
[i
] = argv
[i
+ 1];
55 for (i
= 0; i
< 3; i
++)
56 if (read_file(mmfs
+ i
, argv
[i
+ 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
++)
66 char *filename
= argv
[1];
67 FILE *f
= to_stdout
? stdout
: fopen(filename
, "wb");
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
);
74 ret
= error("Could not close %s", filename
);