Code

Don't access line[-1] for a zero-length "line" from fgets.
[git.git] / diff.c
diff --git a/diff.c b/diff.c
index 08ec66c7945fb330d289f9e579a162e9217a16eb..5bdc111378d61e5bdb13d1908217ab44bfc7967d 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -20,6 +20,7 @@
 static int diff_detect_rename_default;
 static int diff_rename_limit_default = 100;
 static int diff_use_color_default;
+static const char *external_diff_cmd_cfg;
 int diff_auto_refresh_index = 1;
 
 static char diff_colors[][COLOR_MAXLEN] = {
@@ -163,6 +164,10 @@ int git_diff_ui_config(const char *var, const char *value)
                diff_auto_refresh_index = git_config_bool(var, value);
                return 0;
        }
+       if (!strcmp(var, "diff.external")) {
+               external_diff_cmd_cfg = xstrdup(value);
+               return 0;
+       }
        if (!prefixcmp(var, "diff.")) {
                const char *ep = strrchr(var, '.');
 
@@ -209,6 +214,8 @@ static const char *external_diff(void)
        if (done_preparing)
                return external_diff_cmd;
        external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
+       if (!external_diff_cmd)
+               external_diff_cmd = external_diff_cmd_cfg;
        done_preparing = 1;
        return external_diff_cmd;
 }
@@ -283,28 +290,35 @@ static void emit_rewrite_diff(const char *name_a,
                              const char *name_b,
                              struct diff_filespec *one,
                              struct diff_filespec *two,
-                             int color_diff)
+                             struct diff_options *o)
 {
        int lc_a, lc_b;
+       int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
        const char *name_a_tab, *name_b_tab;
        const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
        const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
        const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
        const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
        const char *reset = diff_get_color(color_diff, DIFF_RESET);
+       static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
 
        name_a += (*name_a == '/');
        name_b += (*name_b == '/');
        name_a_tab = strchr(name_a, ' ') ? "\t" : "";
        name_b_tab = strchr(name_b, ' ') ? "\t" : "";
 
+       strbuf_reset(&a_name);
+       strbuf_reset(&b_name);
+       quote_two_c_style(&a_name, o->a_prefix, name_a, 0);
+       quote_two_c_style(&b_name, o->b_prefix, name_b, 0);
+
        diff_populate_filespec(one, 0);
        diff_populate_filespec(two, 0);
        lc_a = count_lines(one->data, one->size);
        lc_b = count_lines(two->data, two->size);
-       printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
-              metainfo, name_a, name_a_tab, reset,
-              metainfo, name_b, name_b_tab, reset, fraginfo);
+       printf("%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
+              metainfo, a_name.buf, name_a_tab, reset,
+              metainfo, b_name.buf, name_b_tab, reset, fraginfo);
        print_line_count(lc_a);
        printf(" +");
        print_line_count(lc_b);
@@ -1205,8 +1219,8 @@ static void builtin_diff(const char *name_a,
        const char *set = diff_get_color_opt(o, DIFF_METAINFO);
        const char *reset = diff_get_color_opt(o, DIFF_RESET);
 
-       a_one = quote_two("a/", name_a + (*name_a == '/'));
-       b_two = quote_two("b/", name_b + (*name_b == '/'));
+       a_one = quote_two(o->a_prefix, name_a + (*name_a == '/'));
+       b_two = quote_two(o->b_prefix, name_b + (*name_b == '/'));
        lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
        lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
        printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
@@ -1235,8 +1249,7 @@ static void builtin_diff(const char *name_a,
                if ((one->mode ^ two->mode) & S_IFMT)
                        goto free_ab_and_return;
                if (complete_rewrite) {
-                       emit_rewrite_diff(name_a, name_b, one, two,
-                                       DIFF_OPT_TST(o, COLOR_DIFF));
+                       emit_rewrite_diff(name_a, name_b, one, two, o);
                        o->found_changes = 1;
                        goto free_ab_and_return;
                }
@@ -2013,6 +2026,9 @@ void diff_setup(struct diff_options *options)
        else
                DIFF_OPT_CLR(options, COLOR_DIFF);
        options->detect_rename = diff_detect_rename_default;
+
+       options->a_prefix = "a/";
+       options->b_prefix = "b/";
 }
 
 int diff_setup_done(struct diff_options *options)
@@ -2284,6 +2300,12 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
                else if (40 < options->abbrev)
                        options->abbrev = 40;
        }
+       else if (!prefixcmp(arg, "--src-prefix="))
+               options->a_prefix = arg + 13;
+       else if (!prefixcmp(arg, "--dst-prefix="))
+               options->b_prefix = arg + 13;
+       else if (!strcmp(arg, "--no-prefix"))
+               options->a_prefix = options->b_prefix = "";
        else
                return 0;
        return 1;