Skip to content

Commit 3d5b23a

Browse files
pcloudsgitster
authored andcommitted
diffcore-pickaxe: Add regcomp_or_die()
There's another regcomp code block coming in this function that needs the same error handling. This function can help avoid duplicating error handling code. Helped-by: Jeff King <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 18547aa commit 3d5b23a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

diffcore-pickaxe.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
198198
*q = outq;
199199
}
200200

201+
static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
202+
{
203+
int err = regcomp(regex, needle, cflags);
204+
if (err) {
205+
/* The POSIX.2 people are surely sick */
206+
char errbuf[1024];
207+
regerror(err, regex, errbuf, 1024);
208+
regfree(regex);
209+
die("invalid regex: %s", errbuf);
210+
}
211+
}
212+
201213
void diffcore_pickaxe(struct diff_options *o)
202214
{
203215
const char *needle = o->pickaxe;
@@ -206,18 +218,10 @@ void diffcore_pickaxe(struct diff_options *o)
206218
kwset_t kws = NULL;
207219

208220
if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
209-
int err;
210221
int cflags = REG_EXTENDED | REG_NEWLINE;
211222
if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
212223
cflags |= REG_ICASE;
213-
err = regcomp(&regex, needle, cflags);
214-
if (err) {
215-
/* The POSIX.2 people are surely sick */
216-
char errbuf[1024];
217-
regerror(err, &regex, errbuf, 1024);
218-
regfree(&regex);
219-
die("invalid regex: %s", errbuf);
220-
}
224+
regcomp_or_die(&regex, needle, cflags);
221225
regexp = &regex;
222226
} else {
223227
kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)

0 commit comments

Comments
 (0)