Coverage Report

Created: 2024-09-08 06:23

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