@matheusbsbs / matheus-bagliano / commits / e0e324a4dc
Fork 0

Fix configuration syntax to specify customized hunk header patterns.

This updates the hunk header customization syntax. The special case 'funcname' attribute is gone. You assign the name of the type of contents to path's "diff" attribute as a string value in .gitattributes like this: *.java diff=java *.perl diff=perl *.doc diff=doc If you supply "diff.<name>.funcname" variable via the configuration mechanism (e.g. in $HOME/.gitconfig), the value is used as the regexp set to find the line to use for the hunk header (the variable is called "funcname" because such a line typically is the one that has the name of the function in programming language source text). If there is no such configuration, built-in default is used, if any. Currently there are two default patterns: default and java. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 7, 2007 at 01:49 UTC e0e324a4dc18a4341e1320a7cfac9733d81f8b0b
3 files changed +84 -71
diff.c
+80 -67
@@ -56,6 +56,14 @@ static struct ll_diff_driver {
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
@@ -93,6 +101,45 @@ static int parse_lldiff_command(const char *var, const char *ep, const char *val
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
@@ -122,8 +169,12 @@ int git_diff_ui_config(const char *var, const char *value)
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);
@@ -1101,43 +1152,39 @@ static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
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))
@@ -1154,54 +1201,23 @@ int diff_filespec_is_binary(struct diff_filespec *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 /*
@@ -1209,12 +1225,12 @@ static const char *diff_hunk_header_regexp(struct diff_filespec *one)
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
@@ -1304,11 +1320,11 @@ static void builtin_diff(const char *name_a,
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));
@@ -1318,8 +1334,8 @@ static void builtin_diff(const char *name_a,
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="))
@@ -1862,10 +1878,7 @@ static const char *external_diff_attr(const char *name)
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;
diffcore.h
+1 -1
@@ -27,7 +27,7 @@ struct diff_filespec {
27 char *path;
28 void *data;
29 void *cnt_data;
30 - const void *hunk_header_ident;
30 + const char *funcname_pattern_ident;
31 unsigned long size;
32 int xfrm_flags; /* for use by the xfrm */
33 unsigned short mode; /* file mode */
t/t4018-diff-funcname.sh
+3 -3
@@ -38,12 +38,12 @@ test_expect_success 'default behaviour' '
38 '
39
40 test_expect_success 'preset java pattern' '
41 - echo "*.java funcname=java" >.gitattributes &&
41 + echo "*.java diff=java" >.gitattributes &&
42 git diff Beer.java Beer-correct.java |
43 grep "^@@.*@@ public static void main("
44 '
45
46 -git config funcname.java '!static
46 +git config diff.java.funcname '!static
47 !String
48 [^ ].*s.*'
49
@@ -53,7 +53,7 @@ test_expect_success 'custom pattern' '
53 '
54
55 test_expect_success 'last regexp must not be negated' '
56 - git config diff.functionnameregexp "!static" &&
56 + git config diff.java.funcname "!static" &&
57 ! git diff Beer.java Beer-correct.java
58 '
59