bcae006abe70641fa732590eef0638c10de2b237
   1#include "cache.h"
   2#include "config.h"
   3#include "userdiff.h"
   4#include "attr.h"
   5
   6static struct userdiff_driver *drivers;
   7static int ndrivers;
   8static int drivers_alloc;
   9
  10#define PATTERNS(name, pattern, word_regex)                     \
  11        { name, NULL, -1, { pattern, REG_EXTENDED },            \
  12          word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
  13#define IPATTERN(name, pattern, word_regex)                     \
  14        { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
  15          word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
  16static struct userdiff_driver builtin_drivers[] = {
  17IPATTERN("ada",
  18         "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
  19         "!^[ \t]*with[ \t].*$\n"
  20         "^[ \t]*((procedure|function)[ \t]+.*)$\n"
  21         "^[ \t]*((package|protected|task)[ \t]+.*)$",
  22         /* -- */
  23         "[a-zA-Z][a-zA-Z0-9_]*"
  24         "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
  25         "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
  26IPATTERN("fortran",
  27         "!^([C*]|[ \t]*!)\n"
  28         "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
  29         "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
  30                "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
  31         /* -- */
  32         "[a-zA-Z][a-zA-Z0-9_]*"
  33         "|\\.([Ee][Qq]|[Nn][Ee]|[Gg][TtEe]|[Ll][TtEe]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|[Aa][Nn][Dd]|[Oo][Rr]|[Nn]?[Ee][Qq][Vv]|[Nn][Oo][Tt])\\."
  34         /* numbers and format statements like 2E14.4, or ES12.6, 9X.
  35          * Don't worry about format statements without leading digits since
  36          * they would have been matched above as a variable anyway. */
  37         "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
  38         "|//|\\*\\*|::|[/<>=]="),
  39IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
  40         "[^ \t-]+"),
  41PATTERNS("golang",
  42         /* Functions */
  43         "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
  44         /* Structs and interfaces */
  45         "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
  46         /* -- */
  47         "[a-zA-Z_][a-zA-Z0-9_]*"
  48         "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
  49         "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
  50PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
  51         "[^<>= \t]+"),
  52PATTERNS("java",
  53         "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
  54         "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
  55         /* -- */
  56         "[a-zA-Z_][a-zA-Z0-9_]*"
  57         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
  58         "|[-+*/<>%&^|=!]="
  59         "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
  60PATTERNS("matlab",
  61         /* Octave pattern is mostly the same as matlab, except that '%%%' and
  62          * '##' can also be used to begin code sections, in addition to '%%'
  63          * that is understood by both. */
  64         "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
  65         "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
  66PATTERNS("objc",
  67         /* Negate C statements that can look like functions */
  68         "!^[ \t]*(do|for|if|else|return|switch|while)\n"
  69         /* Objective-C methods */
  70         "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
  71         /* C functions */
  72         "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
  73         /* Objective-C class/protocol definitions */
  74         "^(@(implementation|interface|protocol)[ \t].*)$",
  75         /* -- */
  76         "[a-zA-Z_][a-zA-Z0-9_]*"
  77         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
  78         "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
  79PATTERNS("pascal",
  80         "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
  81                "implementation|initialization|finalization)[ \t]*.*)$"
  82         "\n"
  83         "^(.*=[ \t]*(class|record).*)$",
  84         /* -- */
  85         "[a-zA-Z_][a-zA-Z0-9_]*"
  86         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
  87         "|<>|<=|>=|:=|\\.\\."),
  88PATTERNS("perl",
  89         "^package .*\n"
  90         "^sub [[:alnum:]_':]+[ \t]*"
  91                "(\\([^)]*\\)[ \t]*)?" /* prototype */
  92                /*
  93                 * Attributes.  A regex can't count nested parentheses,
  94                 * so just slurp up whatever we see, taking care not
  95                 * to accept lines like "sub foo; # defined elsewhere".
  96                 *
  97                 * An attribute could contain a semicolon, but at that
  98                 * point it seems reasonable enough to give up.
  99                 */
 100                "(:[^;#]*)?"
 101                "(\\{[ \t]*)?" /* brace can come here or on the next line */
 102                "(#.*)?$\n" /* comment */
 103         "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
 104                "(\\{[ \t]*)?" /* brace can come here or on the next line */
 105                "(#.*)?$\n"
 106         "^=head[0-9] .*",      /* POD */
 107         /* -- */
 108         "[[:alpha:]_'][[:alnum:]_']*"
 109         "|0[xb]?[0-9a-fA-F_]*"
 110         /* taking care not to interpret 3..5 as (3.)(.5) */
 111         "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
 112         "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
 113         "|&&=|\\|\\|=|//=|\\*\\*="
 114         "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
 115         "|[-+*/%.^&<>=!|]="
 116         "|=~|!~"
 117         "|<<|<>|<=>|>>"),
 118PATTERNS("php",
 119         "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
 120         "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
 121         /* -- */
 122         "[a-zA-Z_][a-zA-Z0-9_]*"
 123         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
 124         "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
 125PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
 126         /* -- */
 127         "[a-zA-Z_][a-zA-Z0-9_]*"
 128         "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
 129         "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
 130         /* -- */
 131PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
 132         /* -- */
 133         "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
 134         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
 135         "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
 136PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
 137         "[={}\"]|[^={}\" \t]+"),
 138PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
 139         "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
 140PATTERNS("cpp",
 141         /* Jump targets or access declarations */
 142         "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
 143         /* functions/methods, variables, and compounds at top level */
 144         "^((::[[:space:]]*)?[A-Za-z_].*)$",
 145         /* -- */
 146         "[a-zA-Z_][a-zA-Z0-9_]*"
 147         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
 148         "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
 149PATTERNS("csharp",
 150         /* Keywords */
 151         "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
 152         /* Methods and constructors */
 153         "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
 154         /* Properties */
 155         "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
 156         /* Type definitions */
 157         "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
 158         /* Namespace */
 159         "^[ \t]*(namespace[ \t]+.*)$",
 160         /* -- */
 161         "[a-zA-Z_][a-zA-Z0-9_]*"
 162         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
 163         "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
 164IPATTERN("css",
 165         "![:;][[:space:]]*$\n"
 166         "^[_a-z0-9].*$",
 167         /* -- */
 168         /*
 169          * This regex comes from W3C CSS specs. Should theoretically also
 170          * allow ISO 10646 characters U+00A0 and higher,
 171          * but they are not handled in this regex.
 172          */
 173         "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
 174         "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
 175),
 176{ "default", NULL, -1, { NULL, 0 } },
 177};
 178#undef PATTERNS
 179#undef IPATTERN
 180
 181static struct userdiff_driver driver_true = {
 182        "diff=true",
 183        NULL,
 184        0,
 185        { NULL, 0 }
 186};
 187
 188static struct userdiff_driver driver_false = {
 189        "!diff",
 190        NULL,
 191        1,
 192        { NULL, 0 }
 193};
 194
 195static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
 196{
 197        int i;
 198        for (i = 0; i < ndrivers; i++) {
 199                struct userdiff_driver *drv = drivers + i;
 200                if (!strncmp(drv->name, k, len) && !drv->name[len])
 201                        return drv;
 202        }
 203        for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
 204                struct userdiff_driver *drv = builtin_drivers + i;
 205                if (!strncmp(drv->name, k, len) && !drv->name[len])
 206                        return drv;
 207        }
 208        return NULL;
 209}
 210
 211static int parse_funcname(struct userdiff_funcname *f, const char *k,
 212                const char *v, int cflags)
 213{
 214        if (git_config_string(&f->pattern, k, v) < 0)
 215                return -1;
 216        f->cflags = cflags;
 217        return 0;
 218}
 219
 220static int parse_tristate(int *b, const char *k, const char *v)
 221{
 222        if (v && !strcasecmp(v, "auto"))
 223                *b = -1;
 224        else
 225                *b = git_config_bool(k, v);
 226        return 0;
 227}
 228
 229static int parse_bool(int *b, const char *k, const char *v)
 230{
 231        *b = git_config_bool(k, v);
 232        return 0;
 233}
 234
 235int userdiff_config(const char *k, const char *v)
 236{
 237        struct userdiff_driver *drv;
 238        const char *name, *type;
 239        int namelen;
 240
 241        if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
 242                return 0;
 243
 244        drv = userdiff_find_by_namelen(name, namelen);
 245        if (!drv) {
 246                ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
 247                drv = &drivers[ndrivers++];
 248                memset(drv, 0, sizeof(*drv));
 249                drv->name = xmemdupz(name, namelen);
 250                drv->binary = -1;
 251        }
 252
 253        if (!strcmp(type, "funcname"))
 254                return parse_funcname(&drv->funcname, k, v, 0);
 255        if (!strcmp(type, "xfuncname"))
 256                return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
 257        if (!strcmp(type, "binary"))
 258                return parse_tristate(&drv->binary, k, v);
 259        if (!strcmp(type, "command"))
 260                return git_config_string(&drv->external, k, v);
 261        if (!strcmp(type, "textconv"))
 262                return git_config_string(&drv->textconv, k, v);
 263        if (!strcmp(type, "cachetextconv"))
 264                return parse_bool(&drv->textconv_want_cache, k, v);
 265        if (!strcmp(type, "wordregex"))
 266                return git_config_string(&drv->word_regex, k, v);
 267
 268        return 0;
 269}
 270
 271struct userdiff_driver *userdiff_find_by_name(const char *name)
 272{
 273        int len = strlen(name);
 274        return userdiff_find_by_namelen(name, len);
 275}
 276
 277struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
 278                                              const char *path)
 279{
 280        static struct attr_check *check;
 281
 282        if (!check)
 283                check = attr_check_initl("diff", NULL);
 284        if (!path)
 285                return NULL;
 286        git_check_attr(istate, path, check);
 287
 288        if (ATTR_TRUE(check->items[0].value))
 289                return &driver_true;
 290        if (ATTR_FALSE(check->items[0].value))
 291                return &driver_false;
 292        if (ATTR_UNSET(check->items[0].value))
 293                return NULL;
 294        return userdiff_find_by_name(check->items[0].value);
 295}
 296
 297struct userdiff_driver *userdiff_get_textconv(struct repository *r,
 298                                              struct userdiff_driver *driver)
 299{
 300        if (!driver->textconv)
 301                return NULL;
 302
 303        if (driver->textconv_want_cache && !driver->textconv_cache) {
 304                struct notes_cache *c = xmalloc(sizeof(*c));
 305                struct strbuf name = STRBUF_INIT;
 306
 307                strbuf_addf(&name, "textconv/%s", driver->name);
 308                notes_cache_init(r, c, name.buf, driver->textconv);
 309                driver->textconv_cache = c;
 310                strbuf_release(&name);
 311        }
 312
 313        return driver;
 314}