Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2549ee

Browse files
committed
diff: don't error out on an invalid regex
When parsing user-provided regex patterns for functions, we must not fail to provide a diff just because a pattern is not well formed. Ignore it instead.
1 parent d994cfc commit c2549ee

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/diff_driver.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ static int diff_driver_add_patterns(
9797
for (scan = regex_str; scan; scan = end) {
9898
/* get pattern to fill in */
9999
if ((pat = git_array_alloc(drv->fn_patterns)) == NULL) {
100-
error = -1;
101-
break;
100+
return -1;
102101
}
103102

104103
pat->flags = regex_flags;
@@ -117,18 +116,18 @@ static int diff_driver_add_patterns(
117116
break;
118117

119118
if ((error = regcomp(&pat->re, buf.ptr, regex_flags)) != 0) {
120-
/* if regex fails to compile, warn? fail? */
121-
error = giterr_set_regex(&pat->re, error);
122-
regfree(&pat->re);
123-
break;
119+
/*
120+
* TODO: issue a warning
121+
*/
124122
}
125123
}
126124

127125
if (error && pat != NULL)
128126
(void)git_array_pop(drv->fn_patterns); /* release last item */
129127
git_buf_free(&buf);
130128

131-
return error;
129+
/* We want to ignore bad patterns, so return success regardless */
130+
return 0;
132131
}
133132

134133
static int diff_driver_xfuncname(const git_config_entry *entry, void *payload)

tests/diff/drivers.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,29 @@ void test_diff_drivers__builtins(void)
250250
git_buf_free(&expected);
251251
git_vector_free(&files);
252252
}
253+
254+
void test_diff_drivers__invalid_pattern(void)
255+
{
256+
git_config *cfg;
257+
git_index *idx;
258+
git_diff *diff;
259+
git_patch *patch;
260+
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
261+
262+
g_repo = cl_git_sandbox_init("userdiff");
263+
cl_git_mkfile("userdiff/.gitattributes", "*.storyboard diff=storyboard\n");
264+
265+
cl_git_pass(git_repository_config__weakptr(&cfg, g_repo));
266+
cl_git_pass(git_config_set_string(cfg, "diff.storyboard.xfuncname", "<!--(.*?)-->"));
267+
268+
cl_git_mkfile("userdiff/dummy.storyboard", "");
269+
cl_git_pass(git_repository_index__weakptr(&idx, g_repo));
270+
cl_git_pass(git_index_add_bypath(idx, "dummy.storyboard"));
271+
cl_git_mkfile("userdiff/dummy.storyboard", "some content\n");
272+
273+
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
274+
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
275+
276+
git_patch_free(patch);
277+
git_diff_free(diff);
278+
}

0 commit comments

Comments
 (0)