5 static struct userdiff_driver
*drivers
;
7 static int drivers_alloc
;
9 #define FUNCNAME(name, pattern) \
10 { name, NULL, { pattern, REG_EXTENDED } }
11 static struct userdiff_driver builtin_drivers
[] = {
12 FUNCNAME("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$"),
14 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
15 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$"),
17 /* Negate C statements that can look like functions */
18 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
19 /* Objective-C methods */
20 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
22 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$\n"
23 /* Objective-C class/protocol definitions */
24 "^(@(implementation|interface|protocol)[ \t].*)$"),
26 "^((procedure|function|constructor|destructor|interface|"
27 "implementation|initialization|finalization)[ \t]*.*)$"
29 "^(.*=[ \t]*(class|record).*)$"),
30 FUNCNAME("php", "^[\t ]*((function|class).*)"),
31 FUNCNAME("python", "^[ \t]*((class|def)[ \t].*)$"),
32 FUNCNAME("ruby", "^[ \t]*((class|module|def)[ \t].*)$"),
33 FUNCNAME("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$"),
34 FUNCNAME("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$"),
38 static struct userdiff_driver driver_true
= {
43 struct userdiff_driver
*USERDIFF_ATTR_TRUE
= &driver_true
;
45 static struct userdiff_driver driver_false
= {
50 struct userdiff_driver
*USERDIFF_ATTR_FALSE
= &driver_false
;
52 static struct userdiff_driver
*userdiff_find_by_namelen(const char *k
, int len
)
55 for (i
= 0; i
< ndrivers
; i
++) {
56 struct userdiff_driver
*drv
= drivers
+ i
;
57 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
60 for (i
= 0; i
< ARRAY_SIZE(builtin_drivers
); i
++) {
61 struct userdiff_driver
*drv
= builtin_drivers
+ i
;
62 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
68 static struct userdiff_driver
*parse_driver(const char *var
,
69 const char *value
, const char *type
)
71 struct userdiff_driver
*drv
;
76 if (prefixcmp(var
, "diff."))
78 dot
= strrchr(var
, '.');
81 if (strcmp(type
, dot
+1))
86 drv
= userdiff_find_by_namelen(name
, namelen
);
88 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
89 drv
= &drivers
[ndrivers
++];
90 memset(drv
, 0, sizeof(*drv
));
91 drv
->name
= xmemdupz(name
, namelen
);
96 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
97 const char *v
, int cflags
)
99 if (git_config_string(&f
->pattern
, k
, v
) < 0)
105 static int parse_string(const char **d
, const char *k
, const char *v
)
107 if (git_config_string(d
, k
, v
) < 0)
112 int userdiff_config_basic(const char *k
, const char *v
)
114 struct userdiff_driver
*drv
;
116 if ((drv
= parse_driver(k
, v
, "funcname")))
117 return parse_funcname(&drv
->funcname
, k
, v
, 0);
118 if ((drv
= parse_driver(k
, v
, "xfuncname")))
119 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
124 int userdiff_config_porcelain(const char *k
, const char *v
)
126 struct userdiff_driver
*drv
;
128 if ((drv
= parse_driver(k
, v
, "command")))
129 return parse_string(&drv
->external
, k
, v
);
134 struct userdiff_driver
*userdiff_find_by_name(const char *name
) {
135 int len
= strlen(name
);
136 return userdiff_find_by_namelen(name
, len
);
139 struct userdiff_driver
*userdiff_find_by_path(const char *path
)
141 static struct git_attr
*attr
;
142 struct git_attr_check check
;
145 attr
= git_attr("diff", 4);
150 if (git_checkattr(path
, 1, &check
))
153 if (ATTR_TRUE(check
.value
))
155 if (ATTR_FALSE(check
.value
))
156 return &driver_false
;
157 if (ATTR_UNSET(check
.value
))
159 return userdiff_find_by_name(check
.value
);