56
char *cmd;
57
} *user_diff, **user_diff_tail;
58
59
+static void read_config_if_needed(void)
60
+{
61
+ if (!user_diff_tail) {
62
+ user_diff_tail = &user_diff;
63
+ git_config(git_diff_ui_config);
64
+ }
65
+}
66
+
67
/*
68
* Currently there is only "diff.<drivername>.command" variable;
69
* because there are "diff.color.<slot>" variables, we are parsing
101
return 0;
102
}
103
104
+/*
105
+ * 'diff.<what>.funcname' attribute can be specified in the configuration
106
+ * to define a customized regexp to find the beginning of a function to
107
+ * be used for hunk header lines of "diff -p" style output.
108
+ */
109
+static struct funcname_pattern {
110
+ char *name;
111
+ char *pattern;
112
+ struct funcname_pattern *next;
113
+} *funcname_pattern_list;
114
+
115
+static int parse_funcname_pattern(const char *var, const char *ep, const char *value)
116
+{
117
+ const char *name;
118
+ int namelen;
119
+ struct funcname_pattern *pp;
120
+
121
+ name = var + 5; /* "diff." */
122
+ namelen = ep - name;
123
+
124
+ for (pp = funcname_pattern_list; pp; pp = pp->next)
125
+ if (!strncmp(pp->name, name, namelen) && !pp->name[namelen])
126
+ break;
127
+ if (!pp) {
128
+ char *namebuf;
129
+ pp = xcalloc(1, sizeof(*pp));
130
+ namebuf = xmalloc(namelen + 1);
131
+ memcpy(namebuf, name, namelen);
132
+ namebuf[namelen] = 0;
133
+ pp->name = namebuf;
134
+ pp->next = funcname_pattern_list;
135
+ funcname_pattern_list = pp;
136
+ }
137
+ if (pp->pattern)
138
+ free(pp->pattern);
139
+ pp->pattern = xstrdup(value);
140
+ return 0;
141
+}
142
+
143
/*
144
* These are to give UI layer defaults.
145
* The core-level commands such as git-diff-files should
169
if (!prefixcmp(var, "diff.")) {
170
const char *ep = strrchr(var, '.');
171
125
- if (ep != var + 4 && !strcmp(ep, ".command"))
126
- return parse_lldiff_command(var, ep, value);
172
+ if (ep != var + 4) {
173
+ if (!strcmp(ep, ".command"))
174
+ return parse_lldiff_command(var, ep, value);
175
+ if (!strcmp(ep, ".funcname"))
176
+ return parse_funcname_pattern(var, ep, value);
177
+ }
178
}
179
if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
180
int slot = parse_diff_color_slot(var, 11);
1152
static void setup_diff_attr_check(struct git_attr_check *check)
1153
{
1154
static struct git_attr *attr_diff;
1104
- static struct git_attr *attr_diff_func_name;
1155
1156
if (!attr_diff) {
1157
attr_diff = git_attr("diff", 4);
1108
- attr_diff_func_name = git_attr("funcname", 8);
1158
}
1159
check[0].attr = attr_diff;
1111
- check[1].attr = attr_diff_func_name;
1160
}
1161
1162
static void diff_filespec_check_attr(struct diff_filespec *one)
1163
{
1116
- struct git_attr_check attr_diff_check[2];
1164
+ struct git_attr_check attr_diff_check;
1165
1166
if (one->checked_attr)
1167
return;
1168
1121
- setup_diff_attr_check(attr_diff_check);
1169
+ setup_diff_attr_check(&attr_diff_check);
1170
one->is_binary = 0;
1123
- one->hunk_header_ident = NULL;
1171
+ one->funcname_pattern_ident = NULL;
1172
1125
- if (!git_checkattr(one->path, ARRAY_SIZE(attr_diff_check), attr_diff_check)) {
1173
+ if (!git_checkattr(one->path, 1, &attr_diff_check)) {
1174
const char *value;
1175
1176
/* binaryness */
1129
- value = attr_diff_check[0].value;
1177
+ value = attr_diff_check.value;
1178
if (ATTR_TRUE(value))
1179
;
1180
else if (ATTR_FALSE(value))
1181
one->is_binary = 1;
1182
1135
- /* hunk header ident */
1136
- value = attr_diff_check[1].value;
1183
+ /* funcname pattern ident */
1184
if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
1185
;
1186
else
1140
- one->hunk_header_ident = value;
1187
+ one->funcname_pattern_ident = value;
1188
}
1189
1190
if (!one->data && DIFF_FILE_VALID(one))
1201
return one->is_binary;
1202
}
1203
1157
-static struct hunk_header_regexp {
1158
- char *name;
1159
- char *regexp;
1160
- struct hunk_header_regexp *next;
1161
-} *hunk_header_regexp_list, **hunk_header_regexp_tail;
1162
-
1163
-static int hunk_header_config(const char *var, const char *value)
1204
+static const char *funcname_pattern(const char *ident)
1205
{
1165
- static const char funcname[] = "funcname.";
1166
- struct hunk_header_regexp *hh;
1206
+ struct funcname_pattern *pp;
1207
1168
- if (prefixcmp(var, funcname))
1169
- return 0;
1170
- var += strlen(funcname);
1171
- for (hh = hunk_header_regexp_list; hh; hh = hh->next)
1172
- if (!strcmp(var, hh->name)) {
1173
- free(hh->regexp);
1174
- hh->regexp = xstrdup(value);
1175
- return 0;
1176
- }
1177
- hh = xcalloc(1, sizeof(*hh));
1178
- hh->name = xstrdup(var);
1179
- hh->regexp = xstrdup(value);
1180
- hh->next = NULL;
1181
- *hunk_header_regexp_tail = hh;
1182
- return 0;
1183
-}
1184
-
1185
-static const char *hunk_header_regexp(const char *ident)
1186
-{
1187
- struct hunk_header_regexp *hh;
1188
-
1189
- if (!hunk_header_regexp_tail) {
1190
- hunk_header_regexp_tail = &hunk_header_regexp_list;
1191
- git_config(hunk_header_config);
1192
- }
1193
- for (hh = hunk_header_regexp_list; hh; hh = hh->next)
1194
- if (!strcmp(ident, hh->name))
1195
- return hh->regexp;
1208
+ read_config_if_needed();
1209
+ for (pp = funcname_pattern_list; pp; pp = pp->next)
1210
+ if (!strcmp(ident, pp->name))
1211
+ return pp->pattern;
1212
return NULL;
1213
}
1214
1199
-static const char *diff_hunk_header_regexp(struct diff_filespec *one)
1215
+static const char *diff_funcname_pattern(struct diff_filespec *one)
1216
{
1201
- const char *ident, *regexp;
1217
+ const char *ident, *pattern;
1218
1219
diff_filespec_check_attr(one);
1204
- ident = one->hunk_header_ident;
1220
+ ident = one->funcname_pattern_ident;
1221
1222
if (!ident)
1223
/*
1225
* regexp is used; otherwise NULL is returned and xemit uses
1226
* the built-in default.
1227
*/
1212
- return hunk_header_regexp("default");
1228
+ return funcname_pattern("default");
1229
1230
/* Look up custom "funcname.$ident" regexp from config. */
1215
- regexp = hunk_header_regexp(ident);
1216
- if (regexp)
1217
- return regexp;
1231
+ pattern = funcname_pattern(ident);
1232
+ if (pattern)
1233
+ return pattern;
1234
1235
/*
1236
* And define built-in fallback patterns here. Note that
1320
xdemitconf_t xecfg;
1321
xdemitcb_t ecb;
1322
struct emit_callback ecbdata;
1307
- const char *hunk_header_regexp;
1323
+ const char *funcname_pattern;
1324
1309
- hunk_header_regexp = diff_hunk_header_regexp(one);
1310
- if (!hunk_header_regexp)
1311
- hunk_header_regexp = diff_hunk_header_regexp(two);
1325
+ funcname_pattern = diff_funcname_pattern(one);
1326
+ if (!funcname_pattern)
1327
+ funcname_pattern = diff_funcname_pattern(two);
1328
1329
memset(&xecfg, 0, sizeof(xecfg));
1330
memset(&ecbdata, 0, sizeof(ecbdata));
1334
xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1335
xecfg.ctxlen = o->context;
1336
xecfg.flags = XDL_EMIT_FUNCNAMES;
1321
- if (hunk_header_regexp)
1322
- xdiff_set_find_func(&xecfg, hunk_header_regexp);
1337
+ if (funcname_pattern)
1338
+ xdiff_set_find_func(&xecfg, funcname_pattern);
1339
if (!diffopts)
1340
;
1341
else if (!prefixcmp(diffopts, "--unified="))
1878
!ATTR_UNSET(value)) {
1879
struct ll_diff_driver *drv;
1880
1865
- if (!user_diff_tail) {
1866
- user_diff_tail = &user_diff;
1867
- git_config(git_diff_ui_config);
1868
- }
1881
+ read_config_if_needed();
1882
for (drv = user_diff; drv; drv = drv->next)
1883
if (!strcmp(drv->name, value))
1884
return drv->cmd;