Sync with v2.13.3
[gitweb.git] / builtin / merge-file.c
index 55447053f2dde004ceffff9967b015b4a01c35d1..b08803e61119a569e2ca5a33402666d8978d9ff0 100644 (file)
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
 #include "xdiff/xdiff.h"
 #include "xdiff-interface.h"
 #include "parse-options.h"
@@ -28,7 +29,6 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
        xmparam_t xmp = {{0}};
        int ret = 0, i = 0, to_stdout = 0;
        int quiet = 0;
-       int prefixlen = 0;
        struct option options[] = {
                OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
                OPT_SET_INT(0, "diff3", &xmp.style, N_("use a diff3 based merge"), XDL_MERGE_DIFF3),
@@ -62,19 +62,22 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
                usage_with_options(merge_file_usage, options);
        if (quiet) {
                if (!freopen("/dev/null", "w", stderr))
-                       return error("failed to redirect stderr to /dev/null: "
-                                    "%s", strerror(errno));
+                       return error_errno("failed to redirect stderr to /dev/null");
        }
 
-       if (prefix)
-               prefixlen = strlen(prefix);
-
        for (i = 0; i < 3; i++) {
-               const char *fname = prefix_filename(prefix, prefixlen, argv[i]);
+               char *fname;
+               int ret;
+
                if (!names[i])
                        names[i] = argv[i];
-               if (read_mmfile(mmfs + i, fname))
+
+               fname = prefix_filename(prefix, argv[i]);
+               ret = read_mmfile(mmfs + i, fname);
+               free(fname);
+               if (ret)
                        return -1;
+
                if (mmfs[i].size > MAX_XDIFF_SIZE ||
                    buffer_is_binary(mmfs[i].ptr, mmfs[i].size))
                        return error("Cannot merge binary files: %s",
@@ -91,17 +94,19 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
 
        if (ret >= 0) {
                const char *filename = argv[0];
-               const char *fpath = prefix_filename(prefix, prefixlen, argv[0]);
+               char *fpath = prefix_filename(prefix, argv[0]);
                FILE *f = to_stdout ? stdout : fopen(fpath, "wb");
 
                if (!f)
-                       ret = error("Could not open %s for writing", filename);
+                       ret = error_errno("Could not open %s for writing",
+                                         filename);
                else if (result.size &&
                         fwrite(result.ptr, result.size, 1, f) != 1)
-                       ret = error("Could not write to %s", filename);
+                       ret = error_errno("Could not write to %s", filename);
                else if (fclose(f))
-                       ret = error("Could not close %s", filename);
+                       ret = error_errno("Could not close %s", filename);
                free(result.ptr);
+               free(fpath);
        }
 
        if (ret > 127)