6 static struct userdiff_driver
*drivers
;
8 static int drivers_alloc
;
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]+" }
16 static struct userdiff_driver builtin_drivers
[] = {
18 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
19 "!^[ \t]*with[ \t].*$\n"
20 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
21 "^[ \t]*((package|protected|task)[ \t]+.*)$",
23 "[a-zA-Z][a-zA-Z0-9_]*"
24 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
25 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
29 /* lines beginning with a word optionally preceded by '&' or the root */
30 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
32 /* Property names and math operators */
34 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
36 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
38 /* Atoms, names, and module attributes */
39 "[@:]?[a-zA-Z0-9@_?!]+"
40 /* Numbers with specific base */
41 "|[-+]?0[xob][0-9a-fA-F]+"
43 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
44 /* Operators and atoms that represent them */
45 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
46 /* Not real operators, but should be grouped */
47 "|:?%[A-Za-z0-9_.]\\{\\}?"),
50 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
51 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
52 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
54 "[a-zA-Z][a-zA-Z0-9_]*"
55 "|\\.([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])\\."
56 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
57 * Don't worry about format statements without leading digits since
58 * they would have been matched above as a variable anyway. */
59 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
60 "|//|\\*\\*|::|[/<>=]="),
61 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
65 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
66 /* Structs and interfaces */
67 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
69 "[a-zA-Z_][a-zA-Z0-9_]*"
70 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
71 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
72 PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
75 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
76 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
78 "[a-zA-Z_][a-zA-Z0-9_]*"
79 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
81 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
84 * Octave pattern is mostly the same as matlab, except that '%%%' and
85 * '##' can also be used to begin code sections, in addition to '%%'
86 * that is understood by both.
88 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
89 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
91 /* Negate C statements that can look like functions */
92 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
93 /* Objective-C methods */
94 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
96 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
97 /* Objective-C class/protocol definitions */
98 "^(@(implementation|interface|protocol)[ \t].*)$",
100 "[a-zA-Z_][a-zA-Z0-9_]*"
101 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
102 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
104 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
105 "implementation|initialization|finalization)[ \t]*.*)$"
107 "^(.*=[ \t]*(class|record).*)$",
109 "[a-zA-Z_][a-zA-Z0-9_]*"
110 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
111 "|<>|<=|>=|:=|\\.\\."),
114 "^sub [[:alnum:]_':]+[ \t]*"
115 "(\\([^)]*\\)[ \t]*)?" /* prototype */
117 * Attributes. A regex can't count nested parentheses,
118 * so just slurp up whatever we see, taking care not
119 * to accept lines like "sub foo; # defined elsewhere".
121 * An attribute could contain a semicolon, but at that
122 * point it seems reasonable enough to give up.
125 "(\\{[ \t]*)?" /* brace can come here or on the next line */
126 "(#.*)?$\n" /* comment */
127 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
128 "(\\{[ \t]*)?" /* brace can come here or on the next line */
130 "^=head[0-9] .*", /* POD */
132 "[[:alpha:]_'][[:alnum:]_']*"
133 "|0[xb]?[0-9a-fA-F_]*"
134 /* taking care not to interpret 3..5 as (3.)(.5) */
135 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
136 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
137 "|&&=|\\|\\|=|//=|\\*\\*="
138 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
143 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
144 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
146 "[a-zA-Z_][a-zA-Z0-9_]*"
147 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
148 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
149 PATTERNS("python", "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
151 "[a-zA-Z_][a-zA-Z0-9_]*"
152 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
153 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
155 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
157 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
158 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
159 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
161 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$",
163 "[a-zA-Z_][a-zA-Z0-9_]*"
164 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
165 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
166 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
167 "[={}\"]|[^={}\" \t]+"),
168 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
169 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
171 /* Jump targets or access declarations */
172 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
173 /* functions/methods, variables, and compounds at top level */
174 "^((::[[:space:]]*)?[A-Za-z_].*)$",
176 "[a-zA-Z_][a-zA-Z0-9_]*"
177 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
178 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
181 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
182 /* Methods and constructors */
183 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
185 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
186 /* Type definitions */
187 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
189 "^[ \t]*(namespace[ \t]+.*)$",
191 "[a-zA-Z_][a-zA-Z0-9_]*"
192 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
193 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
195 "![:;][[:space:]]*$\n"
199 * This regex comes from W3C CSS specs. Should theoretically also
200 * allow ISO 10646 characters U+00A0 and higher,
201 * but they are not handled in this regex.
203 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
204 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
206 { "default", NULL
, -1, { NULL
, 0 } },
211 static struct userdiff_driver driver_true
= {
218 static struct userdiff_driver driver_false
= {
225 static struct userdiff_driver
*userdiff_find_by_namelen(const char *k
, size_t len
)
228 for (i
= 0; i
< ndrivers
; i
++) {
229 struct userdiff_driver
*drv
= drivers
+ i
;
230 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
233 for (i
= 0; i
< ARRAY_SIZE(builtin_drivers
); i
++) {
234 struct userdiff_driver
*drv
= builtin_drivers
+ i
;
235 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
241 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
242 const char *v
, int cflags
)
244 if (git_config_string(&f
->pattern
, k
, v
) < 0)
250 static int parse_tristate(int *b
, const char *k
, const char *v
)
252 if (v
&& !strcasecmp(v
, "auto"))
255 *b
= git_config_bool(k
, v
);
259 static int parse_bool(int *b
, const char *k
, const char *v
)
261 *b
= git_config_bool(k
, v
);
265 int userdiff_config(const char *k
, const char *v
)
267 struct userdiff_driver
*drv
;
268 const char *name
, *type
;
271 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
274 drv
= userdiff_find_by_namelen(name
, namelen
);
276 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
277 drv
= &drivers
[ndrivers
++];
278 memset(drv
, 0, sizeof(*drv
));
279 drv
->name
= xmemdupz(name
, namelen
);
283 if (!strcmp(type
, "funcname"))
284 return parse_funcname(&drv
->funcname
, k
, v
, 0);
285 if (!strcmp(type
, "xfuncname"))
286 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
287 if (!strcmp(type
, "binary"))
288 return parse_tristate(&drv
->binary
, k
, v
);
289 if (!strcmp(type
, "command"))
290 return git_config_string(&drv
->external
, k
, v
);
291 if (!strcmp(type
, "textconv"))
292 return git_config_string(&drv
->textconv
, k
, v
);
293 if (!strcmp(type
, "cachetextconv"))
294 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
295 if (!strcmp(type
, "wordregex"))
296 return git_config_string(&drv
->word_regex
, k
, v
);
301 struct userdiff_driver
*userdiff_find_by_name(const char *name
)
303 int len
= strlen(name
);
304 return userdiff_find_by_namelen(name
, len
);
307 struct userdiff_driver
*userdiff_find_by_path(struct index_state
*istate
,
310 static struct attr_check
*check
;
313 check
= attr_check_initl("diff", NULL
);
316 git_check_attr(istate
, path
, check
);
318 if (ATTR_TRUE(check
->items
[0].value
))
320 if (ATTR_FALSE(check
->items
[0].value
))
321 return &driver_false
;
322 if (ATTR_UNSET(check
->items
[0].value
))
324 return userdiff_find_by_name(check
->items
[0].value
);
327 struct userdiff_driver
*userdiff_get_textconv(struct repository
*r
,
328 struct userdiff_driver
*driver
)
330 if (!driver
->textconv
)
333 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
) {
334 struct notes_cache
*c
= xmalloc(sizeof(*c
));
335 struct strbuf name
= STRBUF_INIT
;
337 strbuf_addf(&name
, "textconv/%s", driver
->name
);
338 notes_cache_init(r
, c
, name
.buf
, driver
->textconv
);
339 driver
->textconv_cache
= c
;
340 strbuf_release(&name
);