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