X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=diff.c;h=6a713764831e7521958f4b280f2e1d3911fb3cea;hb=e79992abdb42f8c9e517c60d7e023894a83764a1;hp=aab246c97eb1660acfa231f7008180a12f6deb8d;hpb=a0c2089c1d48dc9969822126170d35c6e5aa141f;p=git.git diff --git a/diff.c b/diff.c index aab246c97..6a7137648 100644 --- a/diff.c +++ b/diff.c @@ -13,42 +13,19 @@ static int use_size_cache; +static int diff_detect_rename_default = 0; static int diff_rename_limit_default = -1; static int diff_use_color_default = 0; -enum color_diff { - DIFF_RESET = 0, - DIFF_PLAIN = 1, - DIFF_METAINFO = 2, - DIFF_FRAGINFO = 3, - DIFF_FILE_OLD = 4, - DIFF_FILE_NEW = 5, -}; - -#define COLOR_NORMAL "" -#define COLOR_BOLD "\033[1m" -#define COLOR_DIM "\033[2m" -#define COLOR_UL "\033[4m" -#define COLOR_BLINK "\033[5m" -#define COLOR_REVERSE "\033[7m" -#define COLOR_RESET "\033[m" - -#define COLOR_BLACK "\033[30m" -#define COLOR_RED "\033[31m" -#define COLOR_GREEN "\033[32m" -#define COLOR_YELLOW "\033[33m" -#define COLOR_BLUE "\033[34m" -#define COLOR_MAGENTA "\033[35m" -#define COLOR_CYAN "\033[36m" -#define COLOR_WHITE "\033[37m" - -static const char *diff_colors[] = { - [DIFF_RESET] = COLOR_RESET, - [DIFF_PLAIN] = COLOR_NORMAL, - [DIFF_METAINFO] = COLOR_BOLD, - [DIFF_FRAGINFO] = COLOR_CYAN, - [DIFF_FILE_OLD] = COLOR_RED, - [DIFF_FILE_NEW] = COLOR_GREEN, +/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */ +static char diff_colors[][24] = { + "\033[m", /* reset */ + "", /* normal */ + "\033[1m", /* bold */ + "\033[36m", /* cyan */ + "\033[31m", /* red */ + "\033[32m", /* green */ + "\033[33m" /* yellow */ }; static int parse_diff_color_slot(const char *var, int ofs) @@ -63,45 +40,131 @@ static int parse_diff_color_slot(const char *var, int ofs) return DIFF_FILE_OLD; if (!strcasecmp(var+ofs, "new")) return DIFF_FILE_NEW; + if (!strcasecmp(var+ofs, "commit")) + return DIFF_COMMIT; die("bad config variable '%s'", var); } -static const char *parse_diff_color_value(const char *value, const char *var) -{ - if (!strcasecmp(value, "normal")) - return COLOR_NORMAL; - if (!strcasecmp(value, "bold")) - return COLOR_BOLD; - if (!strcasecmp(value, "dim")) - return COLOR_DIM; - if (!strcasecmp(value, "ul")) - return COLOR_UL; - if (!strcasecmp(value, "blink")) - return COLOR_BLINK; - if (!strcasecmp(value, "reverse")) - return COLOR_REVERSE; - if (!strcasecmp(value, "reset")) - return COLOR_RESET; - if (!strcasecmp(value, "black")) - return COLOR_BLACK; - if (!strcasecmp(value, "red")) - return COLOR_RED; - if (!strcasecmp(value, "green")) - return COLOR_GREEN; - if (!strcasecmp(value, "yellow")) - return COLOR_YELLOW; - if (!strcasecmp(value, "blue")) - return COLOR_BLUE; - if (!strcasecmp(value, "magenta")) - return COLOR_MAGENTA; - if (!strcasecmp(value, "cyan")) - return COLOR_CYAN; - if (!strcasecmp(value, "white")) - return COLOR_WHITE; +static int parse_color(const char *name, int len) +{ + static const char * const color_names[] = { + "normal", "black", "red", "green", "yellow", + "blue", "magenta", "cyan", "white" + }; + char *end; + int i; + for (i = 0; i < ARRAY_SIZE(color_names); i++) { + const char *str = color_names[i]; + if (!strncasecmp(name, str, len) && !str[len]) + return i - 1; + } + i = strtol(name, &end, 10); + if (*name && !*end && i >= -1 && i <= 255) + return i; + return -2; +} + +static int parse_attr(const char *name, int len) +{ + static const int attr_values[] = { 1, 2, 4, 5, 7 }; + static const char * const attr_names[] = { + "bold", "dim", "ul", "blink", "reverse" + }; + int i; + for (i = 0; i < ARRAY_SIZE(attr_names); i++) { + const char *str = attr_names[i]; + if (!strncasecmp(name, str, len) && !str[len]) + return attr_values[i]; + } + return -1; +} + +static void parse_diff_color_value(const char *value, const char *var, char *dst) +{ + const char *ptr = value; + int attr = -1; + int fg = -2; + int bg = -2; + + if (!strcasecmp(value, "reset")) { + strcpy(dst, "\033[m"); + return; + } + + /* [fg [bg]] [attr] */ + while (*ptr) { + const char *word = ptr; + int val, len = 0; + + while (word[len] && !isspace(word[len])) + len++; + + ptr = word + len; + while (*ptr && isspace(*ptr)) + ptr++; + + val = parse_color(word, len); + if (val >= -1) { + if (fg == -2) { + fg = val; + continue; + } + if (bg == -2) { + bg = val; + continue; + } + goto bad; + } + val = parse_attr(word, len); + if (val < 0 || attr != -1) + goto bad; + attr = val; + } + + if (attr >= 0 || fg >= 0 || bg >= 0) { + int sep = 0; + + *dst++ = '\033'; + *dst++ = '['; + if (attr >= 0) { + *dst++ = '0' + attr; + sep++; + } + if (fg >= 0) { + if (sep++) + *dst++ = ';'; + if (fg < 8) { + *dst++ = '3'; + *dst++ = '0' + fg; + } else { + dst += sprintf(dst, "38;5;%d", fg); + } + } + if (bg >= 0) { + if (sep++) + *dst++ = ';'; + if (bg < 8) { + *dst++ = '4'; + *dst++ = '0' + bg; + } else { + dst += sprintf(dst, "48;5;%d", bg); + } + } + *dst++ = 'm'; + } + *dst = 0; + return; +bad: die("bad config value '%s' for variable '%s'", value, var); } -int git_diff_config(const char *var, const char *value) +/* + * These are to give UI layer defaults. + * The core-level commands such as git-diff-files should + * never be affected by the setting of diff.renames + * the user happens to have in the configuration file. + */ +int git_diff_ui_config(const char *var, const char *value) { if (!strcmp(var, "diff.renamelimit")) { diff_rename_limit_default = git_config_int(var, value); @@ -112,7 +175,7 @@ int git_diff_config(const char *var, const char *value) diff_use_color_default = 1; /* bool */ else if (!strcasecmp(value, "auto")) { diff_use_color_default = 0; - if (isatty(1)) { + if (isatty(1) || pager_in_use) { char *term = getenv("TERM"); if (term && strcmp(term, "dumb")) diff_use_color_default = 1; @@ -126,9 +189,19 @@ int git_diff_config(const char *var, const char *value) diff_use_color_default = git_config_bool(var, value); return 0; } + if (!strcmp(var, "diff.renames")) { + if (!value) + diff_detect_rename_default = DIFF_DETECT_RENAME; + else if (!strcasecmp(value, "copies") || + !strcasecmp(value, "copy")) + diff_detect_rename_default = DIFF_DETECT_COPY; + else if (git_config_bool(var,value)) + diff_detect_rename_default = DIFF_DETECT_RENAME; + return 0; + } if (!strncmp(var, "diff.color.", 11)) { int slot = parse_diff_color_slot(var, 11); - diff_colors[slot] = parse_diff_color_value(value, var); + parse_diff_color_value(value, var, diff_colors[slot]); return 0; } return git_default_config(var, value); @@ -291,7 +364,7 @@ struct emit_callback { const char **label_path; }; -static inline const char *get_color(int diff_use_color, enum color_diff ix) +const char *diff_get_color(int diff_use_color, enum color_diff ix) { if (diff_use_color) return diff_colors[ix]; @@ -302,8 +375,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) { int i; struct emit_callback *ecbdata = priv; - const char *set = get_color(ecbdata->color_diff, DIFF_METAINFO); - const char *reset = get_color(ecbdata->color_diff, DIFF_RESET); + const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO); + const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); if (ecbdata->label_path[0]) { printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset); @@ -318,7 +391,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) ; if (2 <= i && i < len && line[i] == ' ') { ecbdata->nparents = i - 1; - set = get_color(ecbdata->color_diff, DIFF_FRAGINFO); + set = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO); } else if (len < ecbdata->nparents) set = reset; @@ -331,7 +404,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) else if (line[i] == '+') color = DIFF_FILE_NEW; } - set = get_color(ecbdata->color_diff, color); + set = diff_get_color(ecbdata->color_diff, color); } if (len > 0 && line[len-1] == '\n') len--; @@ -688,8 +761,8 @@ static void builtin_diff(const char *name_a, mmfile_t mf1, mf2; const char *lbl[2]; char *a_one, *b_two; - const char *set = get_color(o->color_diff, DIFF_METAINFO); - const char *reset = get_color(o->color_diff, DIFF_RESET); + const char *set = diff_get_color(o->color_diff, DIFF_METAINFO); + const char *reset = diff_get_color(o->color_diff, DIFF_RESET); a_one = quote_two("a/", name_a); b_two = quote_two("b/", name_b); @@ -729,7 +802,7 @@ static void builtin_diff(const char *name_a, if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) die("unable to read files to diff"); - if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) { + if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) { /* Quite common confusing case */ if (mf1.size == mf2.size && !memcmp(mf1.ptr, mf2.ptr, mf1.size)) @@ -1437,6 +1510,7 @@ void diff_setup(struct diff_options *options) options->change = diff_change; options->add_remove = diff_addremove; options->color_diff = diff_use_color_default; + options->detect_rename = diff_detect_rename_default; } int diff_setup_done(struct diff_options *options) @@ -1567,6 +1641,9 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac) options->output_format |= DIFF_FORMAT_PATCH; options->full_index = options->binary = 1; } + else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) { + options->text = 1; + } else if (!strcmp(arg, "--name-only")) options->output_format |= DIFF_FORMAT_NAME; else if (!strcmp(arg, "--name-status")) @@ -1616,10 +1693,14 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac) } else if (!strcmp(arg, "--color")) options->color_diff = 1; + else if (!strcmp(arg, "--no-color")) + options->color_diff = 0; else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space")) options->xdl_opts |= XDF_IGNORE_WHITESPACE; else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change")) options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE; + else if (!strcmp(arg, "--no-renames")) + options->detect_rename = 0; else return 0; return 1;