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