2 #include "xdiff/xdiff.h"
4 static const char merge_file_usage
[] =
5 "git merge-file [-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
};
33 if (!strcmp(argv
[1], "-L")) {
39 usage(merge_file_usage
);
43 usage(merge_file_usage
);
46 names
[i
] = argv
[i
+ 1];
48 for (i
= 0; i
< 3; i
++)
49 if (read_file(mmfs
+ i
, argv
[i
+ 1]))
52 ret
= xdl_merge(mmfs
+ 1, mmfs
+ 0, names
[0], mmfs
+ 2, names
[2],
53 &xpp
, XDL_MERGE_ZEALOUS
, &result
);
55 for (i
= 0; i
< 3; i
++)
59 char *filename
= argv
[1];
60 FILE *f
= fopen(filename
, "wb");
63 ret
= error("Could not open %s for writing", filename
);
64 else if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
65 ret
= error("Could not write to %s", filename
);
67 ret
= error("Could not close %s", filename
);