Git fork
at reftables-rust 589 lines 19 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2#define DISABLE_SIGN_COMPARE_WARNINGS 3 4#include "git-compat-util.h" 5#include "config.h" 6#include "userdiff.h" 7#include "attr.h" 8#include "strbuf.h" 9#include "environment.h" 10 11static struct userdiff_driver *drivers; 12static int ndrivers; 13static int drivers_alloc; 14 15#define PATTERNS(lang, rx, wrx) { \ 16 .name = lang, \ 17 .binary = -1, \ 18 .funcname = { \ 19 .pattern = rx, \ 20 .cflags = REG_EXTENDED, \ 21 }, \ 22 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \ 23 .word_regex_multi_byte = wrx "|[^[:space:]]", \ 24} 25#define IPATTERN(lang, rx, wrx) { \ 26 .name = lang, \ 27 .binary = -1, \ 28 .funcname = { \ 29 .pattern = rx, \ 30 .cflags = REG_EXTENDED | REG_ICASE, \ 31 }, \ 32 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \ 33 .word_regex_multi_byte = wrx "|[^[:space:]]", \ 34} 35 36/* 37 * Built-in drivers for various languages, sorted by their names 38 * (except that the "default" is left at the end). 39 * 40 * When writing or updating patterns, assume that the contents these 41 * patterns are applied to are syntactically correct. The patterns 42 * can be simple without implementing all syntactical corner cases, as 43 * long as they are sufficiently permissive. 44 */ 45static struct userdiff_driver builtin_drivers[] = { 46IPATTERN("ada", 47 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n" 48 "!^[ \t]*with[ \t].*$\n" 49 "^[ \t]*((procedure|function)[ \t]+.*)$\n" 50 "^[ \t]*((package|protected|task)[ \t]+.*)$", 51 /* -- */ 52 "[a-zA-Z][a-zA-Z0-9_]*" 53 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?" 54 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"), 55PATTERNS("bash", 56 /* Optional leading indentation */ 57 "^[ \t]*" 58 /* Start of captured text */ 59 "(" 60 "(" 61 /* POSIX identifier with mandatory parentheses */ 62 "([a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))" 63 "|" 64 /* Bashism identifier with optional parentheses */ 65 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+)))" 66 ")" 67 /* Everything after the function header is captured */ 68 ".*$" 69 /* End of captured text */ 70 ")", 71 /* -- */ 72 /* Identifiers: variable and function names */ 73 "[a-zA-Z_][a-zA-Z0-9_]*" 74 /* Shell variables: $VAR, ${VAR} */ 75 "|\\$[a-zA-Z0-9_]+|\\$\\{" 76 /*Command list separators and redirection operators */ 77 "|\\|\\||&&|<<|>>" 78 /* Operators ending in '=' (comparison + compound assignment) */ 79 "|==|!=|<=|>=|[-+*/%&|^]=" 80 /* Additional parameter expansion operators */ 81 "|:=|:-|:\\+|:\\?|##|%%|\\^\\^|,," 82 /* Command-line options (to avoid splitting -option) */ 83 "|[-a-zA-Z0-9_]+" 84 /* Brackets and grouping symbols */ 85 "|\\(|\\)|\\{|\\}|\\[|\\]"), 86PATTERNS("bibtex", 87 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", 88 /* -- */ 89 "[={}\"]|[^={}\" \t]+"), 90PATTERNS("cpp", 91 /* Jump targets or access declarations */ 92 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n" 93 /* functions/methods, variables, and compounds at top level */ 94 "^((::[[:space:]]*)?[A-Za-z_].*)$", 95 /* -- */ 96 /* identifiers and keywords */ 97 "[a-zA-Z_][a-zA-Z0-9_]*" 98 /* decimal and octal integers as well as floatingpoint numbers */ 99 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*" 100 /* hexadecimal and binary integers */ 101 "|0[xXbB][0-9a-fA-F]+[lLuU]*" 102 /* floatingpoint numbers that begin with a decimal point */ 103 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?" 104 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"), 105PATTERNS("csharp", 106 /* 107 * Jump over reserved keywords which are illegal method names, but which 108 * can be followed by parentheses without special characters in between, 109 * making them look like methods. 110 */ 111 "!(^|[ \t]+)" /* Start of line or whitespace. */ 112 "(do|while|for|foreach|if|else|new|default|return|switch|case|throw" 113 "|catch|using|lock|fixed)" 114 "([ \t(]+|$)\n" /* Whitespace, "(", or end of line. */ 115 /* 116 * Methods/constructors: 117 * The strategy is to identify a minimum of two groups (any combination 118 * of keywords/type/name) before the opening parenthesis, and without 119 * final unexpected characters, normally only used in ordinary statements. 120 */ 121 "^[ \t]*" /* Remove leading whitespace. */ 122 "(" /* Start chunk header capture. */ 123 "(" /* First group. */ 124 "[][[:alnum:]@_.]" /* Name. */ 125 "(<[][[:alnum:]@_, \t<>]+>)?" /* Optional generic parameters. */ 126 ")+" 127 "([ \t]+" /* Subsequent groups, prepended with space. */ 128 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+" 129 ")+" 130 "[ \t]*" /* Optional space before parameters start. */ 131 "\\(" /* Start of method parameters. */ 132 "[^;]*" /* Allow complex parameters, but exclude statements (;). */ 133 ")$\n" /* Close chunk header capture. */ 134 /* 135 * Properties: 136 * As with methods, expect a minimum of two groups. But, more trivial than 137 * methods, the vast majority of properties long enough to be worth 138 * showing a chunk header for don't include "=:;,()" on the line they are 139 * defined, since they don't have a parameter list. 140 */ 141 "^[ \t]*(" 142 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+" 143 "([ \t]+" 144 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+" 145 ")+" /* Up to here, same as methods regex. */ 146 "[^;=:,()]*" /* Compared to methods, no parameter list allowed. */ 147 ")$\n" 148 /* Type definitions */ 149 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n" 150 /* Namespace */ 151 "^[ \t]*(namespace[ \t]+.*)$", 152 /* -- */ 153 "[a-zA-Z_][a-zA-Z0-9_]*" 154 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" 155 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), 156IPATTERN("css", 157 "![:;][[:space:]]*$\n" 158 "^[:[@.#]?[_a-z0-9].*$", 159 /* -- */ 160 /* 161 * This regex comes from W3C CSS specs. Should theoretically also 162 * allow ISO 10646 characters U+00A0 and higher, 163 * but they are not handled in this regex. 164 */ 165 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */ 166 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */ 167), 168PATTERNS("dts", 169 "!;\n" 170 "!=\n" 171 /* lines beginning with a word optionally preceded by '&' or the root */ 172 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)", 173 /* -- */ 174 /* Property names and math operators */ 175 "[a-zA-Z0-9,._+?#-]+" 176 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"), 177PATTERNS("elixir", 178 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$", 179 /* -- */ 180 /* Atoms, names, and module attributes */ 181 "[@:]?[a-zA-Z0-9@_?!]+" 182 /* Numbers with specific base */ 183 "|[-+]?0[xob][0-9a-fA-F]+" 184 /* Numbers */ 185 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?" 186 /* Operators and atoms that represent them */ 187 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)" 188 /* Not real operators, but should be grouped */ 189 "|:?%[A-Za-z0-9_.]\\{\\}?"), 190IPATTERN("fortran", 191 /* Don't match comment lines */ 192 "!^([C*]|[ \t]*!)\n" 193 /* Don't match 'module procedure' lines */ 194 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n" 195 /* Program, module, block data */ 196 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA" 197 /* Subroutines and functions */ 198 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$", 199 /* -- */ 200 "[a-zA-Z][a-zA-Z0-9_]*" 201 "|\\.([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])\\." 202 /* numbers and format statements like 2E14.4, or ES12.6, 9X. 203 * Don't worry about format statements without leading digits since 204 * they would have been matched above as a variable anyway. */ 205 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?" 206 "|//|\\*\\*|::|[/<>=]="), 207IPATTERN("fountain", 208 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$", 209 /* -- */ 210 "[^ \t-]+"), 211PATTERNS("golang", 212 /* Functions */ 213 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n" 214 /* Structs and interfaces */ 215 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)", 216 /* -- */ 217 "[a-zA-Z_][a-zA-Z0-9_]*" 218 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?" 219 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"), 220PATTERNS("html", 221 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$", 222 /* -- */ 223 "[^<>= \t]+"), 224PATTERNS("ini", 225 "^[ \t]*\\[[^]]+\\]", 226 /* -- */ 227 "[^ \t]+"), 228PATTERNS("java", 229 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" 230 /* Class, enum, interface, and record declarations */ 231 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n" 232 /* Method definitions; note that constructor signatures are not */ 233 /* matched because they are indistinguishable from method calls. */ 234 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", 235 /* -- */ 236 "[a-zA-Z_][a-zA-Z0-9_]*" 237 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" 238 "|[-+*/<>%&^|=!]=" 239 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"), 240PATTERNS("kotlin", 241 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$", 242 /* -- */ 243 "[a-zA-Z_][a-zA-Z0-9_]*" 244 /* hexadecimal and binary numbers */ 245 "|0[xXbB][0-9a-fA-F_]+[lLuU]*" 246 /* integers and floats */ 247 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*" 248 /* floating point numbers beginning with decimal point */ 249 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?" 250 /* unary and binary operators */ 251 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"), 252PATTERNS("markdown", 253 "^ {0,3}#{1,6}[ \t].*", 254 /* -- */ 255 "[^<>= \t]+"), 256PATTERNS("matlab", 257 /* 258 * Octave pattern is mostly the same as matlab, except that '%%%' and 259 * '##' can also be used to begin code sections, in addition to '%%' 260 * that is understood by both. 261 */ 262 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$", 263 /* -- */ 264 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"), 265PATTERNS("objc", 266 /* Negate C statements that can look like functions */ 267 "!^[ \t]*(do|for|if|else|return|switch|while)\n" 268 /* Objective-C methods */ 269 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n" 270 /* C functions */ 271 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n" 272 /* Objective-C class/protocol definitions */ 273 "^(@(implementation|interface|protocol)[ \t].*)$", 274 /* -- */ 275 "[a-zA-Z_][a-zA-Z0-9_]*" 276 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" 277 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), 278PATTERNS("pascal", 279 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface" 280 "|implementation|initialization|finalization)[ \t]*.*)$\n" 281 "^(.*=[ \t]*(class|record).*)$", 282 /* -- */ 283 "[a-zA-Z_][a-zA-Z0-9_]*" 284 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" 285 "|<>|<=|>=|:=|\\.\\."), 286PATTERNS("perl", 287 "^package .*\n" 288 "^sub [[:alnum:]_':]+[ \t]*" 289 "(\\([^)]*\\)[ \t]*)?" /* prototype */ 290 /* 291 * Attributes. A regex can't count nested parentheses, 292 * so just slurp up whatever we see, taking care not 293 * to accept lines like "sub foo; # defined elsewhere". 294 * 295 * An attribute could contain a semicolon, but at that 296 * point it seems reasonable enough to give up. 297 */ 298 "(:[^;#]*)?" 299 "(\\{[ \t]*)?" /* brace can come here or on the next line */ 300 "(#.*)?$\n" /* comment */ 301 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*" 302 "(\\{[ \t]*)?" /* brace can come here or on the next line */ 303 "(#.*)?$\n" 304 "^=head[0-9] .*", /* POD */ 305 /* -- */ 306 "[[:alpha:]_'][[:alnum:]_']*" 307 "|0[xb]?[0-9a-fA-F_]*" 308 /* taking care not to interpret 3..5 as (3.)(.5) */ 309 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?" 310 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::" 311 "|&&=|\\|\\|=|//=|\\*\\*=" 312 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?" 313 "|[-+*/%.^&<>=!|]=" 314 "|=~|!~" 315 "|<<|<>|<=>|>>"), 316PATTERNS("php", 317 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n" 318 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$", 319 /* -- */ 320 "[a-zA-Z_][a-zA-Z0-9_]*" 321 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" 322 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"), 323PATTERNS("python", 324 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$", 325 /* -- */ 326 "[a-zA-Z_][a-zA-Z0-9_]*" 327 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?" 328 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"), 329 /* -- */ 330PATTERNS("r", 331 "^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*(<-|=)[ \t]*function.*)$", 332 /* -- */ 333 "[^ \t]+"), 334PATTERNS("ruby", 335 "^[ \t]*((class|module|def)[ \t].*)$", 336 /* -- */ 337 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" 338 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." 339 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), 340PATTERNS("rust", 341 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$", 342 /* -- */ 343 "[a-zA-Z_][a-zA-Z0-9_]*" 344 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?" 345 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"), 346PATTERNS("scheme", 347 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$", 348 /* 349 * R7RS valid identifiers include any sequence enclosed 350 * within vertical lines having no backslashes 351 */ 352 "\\|([^\\\\]*)\\|" 353 /* All other words should be delimited by spaces or parentheses */ 354 "|([^][)(}{[ \t])+"), 355PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", 356 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"), 357{ .name = "default", .binary = -1 }, 358}; 359#undef PATTERNS 360#undef IPATTERN 361 362static struct userdiff_driver driver_true = { 363 .name = "diff=true", 364 .binary = 0, 365}; 366 367static struct userdiff_driver driver_false = { 368 .name = "!diff", 369 .binary = 1, 370}; 371 372struct find_by_namelen_data { 373 const char *name; 374 size_t len; 375 struct userdiff_driver *driver; 376}; 377 378static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver, 379 enum userdiff_driver_type type UNUSED, 380 void *priv) 381{ 382 struct find_by_namelen_data *cb_data = priv; 383 384 if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) { 385 cb_data->driver = driver; 386 return 1; /* tell the caller to stop iterating */ 387 } 388 return 0; 389} 390 391static int regexec_supports_multi_byte_chars(void) 392{ 393 static const char not_space[] = "[^[:space:]]"; 394 static const char utf8_multi_byte_char[] = "\xc2\xa3"; 395 regex_t re; 396 regmatch_t match; 397 static int result = -1; 398 399 if (result != -1) 400 return result; 401 if (regcomp(&re, not_space, REG_EXTENDED)) 402 BUG("invalid regular expression: %s", not_space); 403 result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) && 404 match.rm_so == 0 && 405 match.rm_eo == strlen(utf8_multi_byte_char); 406 regfree(&re); 407 return result; 408} 409 410static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len) 411{ 412 struct find_by_namelen_data udcbdata = { 413 .name = name, 414 .len = len, 415 }; 416 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata); 417 return udcbdata.driver; 418} 419 420static int parse_funcname(struct userdiff_funcname *f, const char *k, 421 const char *v, int cflags) 422{ 423 f->pattern = NULL; 424 FREE_AND_NULL(f->pattern_owned); 425 if (git_config_string(&f->pattern_owned, k, v) < 0) 426 return -1; 427 f->pattern = f->pattern_owned; 428 f->cflags = cflags; 429 return 0; 430} 431 432static int parse_tristate(int *b, const char *k, const char *v) 433{ 434 if (v && !strcasecmp(v, "auto")) 435 *b = -1; 436 else 437 *b = git_config_bool(k, v); 438 return 0; 439} 440 441static int parse_bool(int *b, const char *k, const char *v) 442{ 443 *b = git_config_bool(k, v); 444 return 0; 445} 446 447int userdiff_config(const char *k, const char *v) 448{ 449 struct userdiff_driver *drv; 450 const char *name, *type; 451 size_t namelen; 452 453 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name) 454 return 0; 455 456 drv = userdiff_find_by_namelen(name, namelen); 457 if (!drv) { 458 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc); 459 drv = &drivers[ndrivers++]; 460 memset(drv, 0, sizeof(*drv)); 461 drv->name = xmemdupz(name, namelen); 462 drv->binary = -1; 463 } 464 465 if (!strcmp(type, "funcname")) 466 return parse_funcname(&drv->funcname, k, v, 0); 467 if (!strcmp(type, "xfuncname")) 468 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED); 469 if (!strcmp(type, "binary")) 470 return parse_tristate(&drv->binary, k, v); 471 if (!strcmp(type, "command")) { 472 FREE_AND_NULL(drv->external.cmd); 473 return git_config_string(&drv->external.cmd, k, v); 474 } 475 if (!strcmp(type, "trustexitcode")) { 476 drv->external.trust_exit_code = git_config_bool(k, v); 477 return 0; 478 } 479 if (!strcmp(type, "textconv")) { 480 int ret; 481 FREE_AND_NULL(drv->textconv_owned); 482 ret = git_config_string(&drv->textconv_owned, k, v); 483 drv->textconv = drv->textconv_owned; 484 return ret; 485 } 486 if (!strcmp(type, "cachetextconv")) 487 return parse_bool(&drv->textconv_want_cache, k, v); 488 if (!strcmp(type, "wordregex")) { 489 int ret; 490 FREE_AND_NULL(drv->word_regex_owned); 491 ret = git_config_string(&drv->word_regex_owned, k, v); 492 drv->word_regex = drv->word_regex_owned; 493 return ret; 494 } 495 if (!strcmp(type, "algorithm")) { 496 int ret; 497 FREE_AND_NULL(drv->algorithm_owned); 498 ret = git_config_string(&drv->algorithm_owned, k, v); 499 drv->algorithm = drv->algorithm_owned; 500 return ret; 501 } 502 503 return 0; 504} 505 506struct userdiff_driver *userdiff_find_by_name(const char *name) 507{ 508 int len = strlen(name); 509 struct userdiff_driver *driver = userdiff_find_by_namelen(name, len); 510 if (driver && driver->word_regex_multi_byte) { 511 if (regexec_supports_multi_byte_chars()) 512 driver->word_regex = driver->word_regex_multi_byte; 513 driver->word_regex_multi_byte = NULL; 514 } 515 return driver; 516} 517 518struct userdiff_driver *userdiff_find_by_path(struct index_state *istate, 519 const char *path) 520{ 521 static struct attr_check *check; 522 523 if (!check) 524 check = attr_check_initl("diff", NULL); 525 if (!path) 526 return NULL; 527 git_check_attr(istate, path, check); 528 529 if (ATTR_TRUE(check->items[0].value)) 530 return &driver_true; 531 if (ATTR_FALSE(check->items[0].value)) 532 return &driver_false; 533 if (ATTR_UNSET(check->items[0].value)) 534 return NULL; 535 return userdiff_find_by_name(check->items[0].value); 536} 537 538struct userdiff_driver *userdiff_get_textconv(struct repository *r, 539 struct userdiff_driver *driver) 540{ 541 if (!driver->textconv) 542 return NULL; 543 544 if (driver->textconv_want_cache && !driver->textconv_cache && 545 have_git_dir()) { 546 struct notes_cache *c = xmalloc(sizeof(*c)); 547 struct strbuf name = STRBUF_INIT; 548 549 strbuf_addf(&name, "textconv/%s", driver->name); 550 notes_cache_init(r, c, name.buf, driver->textconv); 551 driver->textconv_cache = c; 552 strbuf_release(&name); 553 } 554 555 return driver; 556} 557 558static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn, 559 enum userdiff_driver_type type, void *cb_data, 560 struct userdiff_driver *drv, 561 int drv_size) 562{ 563 int i; 564 int ret; 565 for (i = 0; i < drv_size; i++) { 566 struct userdiff_driver *item = drv + i; 567 if ((ret = fn(item, type, cb_data))) 568 return ret; 569 } 570 return 0; 571} 572 573int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data) 574{ 575 int ret; 576 577 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM, 578 cb_data, drivers, ndrivers); 579 if (ret) 580 return ret; 581 582 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN, 583 cb_data, builtin_drivers, 584 ARRAY_SIZE(builtin_drivers)); 585 if (ret) 586 return ret; 587 588 return 0; 589}