userdiff.con commit Merge branch 'maint' (6339f67)
   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 }, word_regex }
  12static struct userdiff_driver builtin_drivers[] = {
  13PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
  14         "[^<>= \t]+|[^[:space:]]|[\x80-\xff]+"),
  15PATTERNS("java",
  16         "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
  17         "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
  18         /* -- */
  19         "[a-zA-Z_][a-zA-Z0-9_]*"
  20         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
  21         "|[-+*/<>%&^|=!]="
  22         "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"
  23         "|[^[:space:]]|[\x80-\xff]+"),
  24PATTERNS("objc",
  25         /* Negate C statements that can look like functions */
  26         "!^[ \t]*(do|for|if|else|return|switch|while)\n"
  27         /* Objective-C methods */
  28         "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
  29         /* C functions */
  30         "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
  31         /* Objective-C class/protocol definitions */
  32         "^(@(implementation|interface|protocol)[ \t].*)$",
  33         /* -- */
  34         "[a-zA-Z_][a-zA-Z0-9_]*"
  35         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
  36         "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
  37         "|[^[:space:]]|[\x80-\xff]+"),
  38PATTERNS("pascal",
  39         "^((procedure|function|constructor|destructor|interface|"
  40                "implementation|initialization|finalization)[ \t]*.*)$"
  41         "\n"
  42         "^(.*=[ \t]*(class|record).*)$",
  43         /* -- */
  44         "[a-zA-Z_][a-zA-Z0-9_]*"
  45         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
  46         "|<>|<=|>=|:=|\\.\\."
  47         "|[^[:space:]]|[\x80-\xff]+"),
  48PATTERNS("php",
  49         "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
  50         "^[\t ]*(class.*)$",
  51         /* -- */
  52         "[a-zA-Z_][a-zA-Z0-9_]*"
  53         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
  54         "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"
  55         "|[^[:space:]]|[\x80-\xff]+"),
  56PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
  57         /* -- */
  58         "[a-zA-Z_][a-zA-Z0-9_]*"
  59         "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
  60         "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"
  61         "|[^[:space:]|[\x80-\xff]+"),
  62         /* -- */
  63PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
  64         /* -- */
  65         "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
  66         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
  67         "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"
  68         "|[^[:space:]|[\x80-\xff]+"),
  69PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
  70         "[={}\"]|[^={}\" \t]+"),
  71PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
  72         "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+|[^[:space:]]"),
  73PATTERNS("cpp",
  74         /* Jump targets or access declarations */
  75         "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
  76         /* C/++ functions/methods at top level */
  77         "^([A-Za-z_][A-Za-z_0-9]*([ \t]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
  78         /* compound type at top level */
  79         "^((struct|class|enum)[^;]*)$",
  80         /* -- */
  81         "[a-zA-Z_][a-zA-Z0-9_]*"
  82         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
  83         "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"
  84         "|[^[:space:]]|[\x80-\xff]+"),
  85{ "default", NULL, -1, { NULL, 0 } },
  86};
  87#undef PATTERNS
  88
  89static struct userdiff_driver driver_true = {
  90        "diff=true",
  91        NULL,
  92        0,
  93        { NULL, 0 }
  94};
  95
  96static struct userdiff_driver driver_false = {
  97        "!diff",
  98        NULL,
  99        1,
 100        { NULL, 0 }
 101};
 102
 103static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
 104{
 105        int i;
 106        for (i = 0; i < ndrivers; i++) {
 107                struct userdiff_driver *drv = drivers + i;
 108                if (!strncmp(drv->name, k, len) && !drv->name[len])
 109                        return drv;
 110        }
 111        for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
 112                struct userdiff_driver *drv = builtin_drivers + i;
 113                if (!strncmp(drv->name, k, len) && !drv->name[len])
 114                        return drv;
 115        }
 116        return NULL;
 117}
 118
 119static struct userdiff_driver *parse_driver(const char *var,
 120                const char *value, const char *type)
 121{
 122        struct userdiff_driver *drv;
 123        const char *dot;
 124        const char *name;
 125        int namelen;
 126
 127        if (prefixcmp(var, "diff."))
 128                return NULL;
 129        dot = strrchr(var, '.');
 130        if (dot == var + 4)
 131                return NULL;
 132        if (strcmp(type, dot+1))
 133                return NULL;
 134
 135        name = var + 5;
 136        namelen = dot - name;
 137        drv = userdiff_find_by_namelen(name, namelen);
 138        if (!drv) {
 139                ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
 140                drv = &drivers[ndrivers++];
 141                memset(drv, 0, sizeof(*drv));
 142                drv->name = xmemdupz(name, namelen);
 143                drv->binary = -1;
 144        }
 145        return drv;
 146}
 147
 148static int parse_funcname(struct userdiff_funcname *f, const char *k,
 149                const char *v, int cflags)
 150{
 151        if (git_config_string(&f->pattern, k, v) < 0)
 152                return -1;
 153        f->cflags = cflags;
 154        return 1;
 155}
 156
 157static int parse_string(const char **d, const char *k, const char *v)
 158{
 159        if (git_config_string(d, k, v) < 0)
 160                return -1;
 161        return 1;
 162}
 163
 164static int parse_tristate(int *b, const char *k, const char *v)
 165{
 166        if (v && !strcasecmp(v, "auto"))
 167                *b = -1;
 168        else
 169                *b = git_config_bool(k, v);
 170        return 1;
 171}
 172
 173static int parse_bool(int *b, const char *k, const char *v)
 174{
 175        *b = git_config_bool(k, v);
 176        return 1;
 177}
 178
 179int userdiff_config(const char *k, const char *v)
 180{
 181        struct userdiff_driver *drv;
 182
 183        if ((drv = parse_driver(k, v, "funcname")))
 184                return parse_funcname(&drv->funcname, k, v, 0);
 185        if ((drv = parse_driver(k, v, "xfuncname")))
 186                return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
 187        if ((drv = parse_driver(k, v, "binary")))
 188                return parse_tristate(&drv->binary, k, v);
 189        if ((drv = parse_driver(k, v, "command")))
 190                return parse_string(&drv->external, k, v);
 191        if ((drv = parse_driver(k, v, "textconv")))
 192                return parse_string(&drv->textconv, k, v);
 193        if ((drv = parse_driver(k, v, "cachetextconv")))
 194                return parse_bool(&drv->textconv_want_cache, k, v);
 195        if ((drv = parse_driver(k, v, "wordregex")))
 196                return parse_string(&drv->word_regex, k, v);
 197
 198        return 0;
 199}
 200
 201struct userdiff_driver *userdiff_find_by_name(const char *name) {
 202        int len = strlen(name);
 203        return userdiff_find_by_namelen(name, len);
 204}
 205
 206struct userdiff_driver *userdiff_find_by_path(const char *path)
 207{
 208        static struct git_attr *attr;
 209        struct git_attr_check check;
 210
 211        if (!attr)
 212                attr = git_attr("diff");
 213        check.attr = attr;
 214
 215        if (!path)
 216                return NULL;
 217        if (git_checkattr(path, 1, &check))
 218                return NULL;
 219
 220        if (ATTR_TRUE(check.value))
 221                return &driver_true;
 222        if (ATTR_FALSE(check.value))
 223                return &driver_false;
 224        if (ATTR_UNSET(check.value))
 225                return NULL;
 226        return userdiff_find_by_name(check.value);
 227}