5 static struct userdiff_driver
*drivers
;
7 static int drivers_alloc
;
9 #define PATTERNS(name, pattern, word_regex) \
10 { name, NULL, -1, { pattern, REG_EXTENDED }, \
11 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
12 #define IPATTERN(name, pattern, word_regex) \
13 { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
14 word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
15 static struct userdiff_driver builtin_drivers
[] = {
17 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
18 "!^[ \t]*with[ \t].*$\n"
19 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
20 "^[ \t]*((package|protected|task)[ \t]+.*)$",
22 "[a-zA-Z][a-zA-Z0-9_]*"
23 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
24 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
27 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
28 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
29 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
31 "[a-zA-Z][a-zA-Z0-9_]*"
32 "|\\.([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])\\."
33 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
34 * Don't worry about format statements without leading digits since
35 * they would have been matched above as a variable anyway. */
36 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
37 "|//|\\*\\*|::|[/<>=]="),
38 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
40 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
43 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
44 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
46 "[a-zA-Z_][a-zA-Z0-9_]*"
47 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
49 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
51 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
52 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
54 /* Negate C statements that can look like functions */
55 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
56 /* Objective-C methods */
57 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
59 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
60 /* Objective-C class/protocol definitions */
61 "^(@(implementation|interface|protocol)[ \t].*)$",
63 "[a-zA-Z_][a-zA-Z0-9_]*"
64 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
65 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
67 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
68 "implementation|initialization|finalization)[ \t]*.*)$"
70 "^(.*=[ \t]*(class|record).*)$",
72 "[a-zA-Z_][a-zA-Z0-9_]*"
73 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
74 "|<>|<=|>=|:=|\\.\\."),
77 "^sub [[:alnum:]_':]+[ \t]*"
78 "(\\([^)]*\\)[ \t]*)?" /* prototype */
80 * Attributes. A regex can't count nested parentheses,
81 * so just slurp up whatever we see, taking care not
82 * to accept lines like "sub foo; # defined elsewhere".
84 * An attribute could contain a semicolon, but at that
85 * point it seems reasonable enough to give up.
88 "(\\{[ \t]*)?" /* brace can come here or on the next line */
89 "(#.*)?$\n" /* comment */
90 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
91 "(\\{[ \t]*)?" /* brace can come here or on the next line */
93 "^=head[0-9] .*", /* POD */
95 "[[:alpha:]_'][[:alnum:]_']*"
96 "|0[xb]?[0-9a-fA-F_]*"
97 /* taking care not to interpret 3..5 as (3.)(.5) */
98 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
99 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
100 "|&&=|\\|\\|=|//=|\\*\\*="
101 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
106 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
109 "[a-zA-Z_][a-zA-Z0-9_]*"
110 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
111 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
112 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
114 "[a-zA-Z_][a-zA-Z0-9_]*"
115 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
116 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
118 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
120 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
121 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
122 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
123 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
124 "[={}\"]|[^={}\" \t]+"),
125 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
126 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
128 /* Jump targets or access declarations */
129 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
130 /* functions/methods, variables, and compounds at top level */
131 "^((::[[:space:]]*)?[A-Za-z_].*)$",
133 "[a-zA-Z_][a-zA-Z0-9_]*"
134 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
135 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
138 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
139 /* Methods and constructors */
140 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
142 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
143 /* Type definitions */
144 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
146 "^[ \t]*(namespace[ \t]+.*)$",
148 "[a-zA-Z_][a-zA-Z0-9_]*"
149 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
150 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
152 "![:;][[:space:]]*$\n"
156 * This regex comes from W3C CSS specs. Should theoretically also
157 * allow ISO 10646 characters U+00A0 and higher,
158 * but they are not handled in this regex.
160 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
161 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
163 { "default", NULL
, -1, { NULL
, 0 } },
168 static struct userdiff_driver driver_true
= {
175 static struct userdiff_driver driver_false
= {
182 static struct userdiff_driver
*userdiff_find_by_namelen(const char *k
, int len
)
185 for (i
= 0; i
< ndrivers
; i
++) {
186 struct userdiff_driver
*drv
= drivers
+ i
;
187 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
190 for (i
= 0; i
< ARRAY_SIZE(builtin_drivers
); i
++) {
191 struct userdiff_driver
*drv
= builtin_drivers
+ i
;
192 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
198 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
199 const char *v
, int cflags
)
201 if (git_config_string(&f
->pattern
, k
, v
) < 0)
207 static int parse_tristate(int *b
, const char *k
, const char *v
)
209 if (v
&& !strcasecmp(v
, "auto"))
212 *b
= git_config_bool(k
, v
);
216 static int parse_bool(int *b
, const char *k
, const char *v
)
218 *b
= git_config_bool(k
, v
);
222 int userdiff_config(const char *k
, const char *v
)
224 struct userdiff_driver
*drv
;
225 const char *name
, *type
;
228 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
231 drv
= userdiff_find_by_namelen(name
, namelen
);
233 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
234 drv
= &drivers
[ndrivers
++];
235 memset(drv
, 0, sizeof(*drv
));
236 drv
->name
= xmemdupz(name
, namelen
);
240 if (!strcmp(type
, "funcname"))
241 return parse_funcname(&drv
->funcname
, k
, v
, 0);
242 if (!strcmp(type
, "xfuncname"))
243 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
244 if (!strcmp(type
, "binary"))
245 return parse_tristate(&drv
->binary
, k
, v
);
246 if (!strcmp(type
, "command"))
247 return git_config_string(&drv
->external
, k
, v
);
248 if (!strcmp(type
, "textconv"))
249 return git_config_string(&drv
->textconv
, k
, v
);
250 if (!strcmp(type
, "cachetextconv"))
251 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
252 if (!strcmp(type
, "wordregex"))
253 return git_config_string(&drv
->word_regex
, k
, v
);
258 struct userdiff_driver
*userdiff_find_by_name(const char *name
) {
259 int len
= strlen(name
);
260 return userdiff_find_by_namelen(name
, len
);
263 struct userdiff_driver
*userdiff_find_by_path(const char *path
)
265 static struct attr_check
*check
;
268 check
= attr_check_initl("diff", NULL
);
271 if (git_check_attr(path
, check
))
274 if (ATTR_TRUE(check
->items
[0].value
))
276 if (ATTR_FALSE(check
->items
[0].value
))
277 return &driver_false
;
278 if (ATTR_UNSET(check
->items
[0].value
))
280 return userdiff_find_by_name(check
->items
[0].value
);
283 struct userdiff_driver
*userdiff_get_textconv(struct userdiff_driver
*driver
)
285 if (!driver
->textconv
)
288 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
) {
289 struct notes_cache
*c
= xmalloc(sizeof(*c
));
290 struct strbuf name
= STRBUF_INIT
;
292 strbuf_addf(&name
, "textconv/%s", driver
->name
);
293 notes_cache_init(c
, name
.buf
, driver
->textconv
);
294 driver
->textconv_cache
= c
;