[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug-diffutils] bug#18641: [PATCH] diff: Add --only=REGEX option to pars
From: |
Daniel Hilst Selli |
Subject: |
[bug-diffutils] bug#18641: [PATCH] diff: Add --only=REGEX option to parse only files that match REGEX |
Date: |
Sun, 5 Oct 2014 20:05:37 +0000 |
I run to this problem some time ago. I need to parse to trees, but
only the source files (*.[hcS]). I see some find solutions, but that
don't work for me since I have new files created on modified
tree. Best solution would be patch diff. So here I am.
This patches add a new option (--only=REGEX) that parse only files
that match such regexp. Multiple regexps can be used and they will be
OR'ed. Also, --exclude is processed before --only, so will can still
exclude files before they match REGEX.
This commit don't update man pages and info docs, I looking for it.
Cheers,
>8---------------------8<
* src/diff.h: XTERN only_regexp added.
* src/diff.c: global only_regexp_list, and ONLY_OPTION added.
"only-that-matches" element added to longopts.
Help string added to global option_help_msgid array.
* src/diff.c: (main) only_regexp_list.buf initalized.
ONLY_OPITON processed on options loop
* src/dir.c: (dir_read) Added an `if' to process --only option. Was
added after --exclude processing. This way user can
exclude files that still match --only REGEX.
---
src/diff.c | 12 ++++++++++++
src/diff.h | 3 +++
src/dir.c | 10 ++++++++++
3 files changed, 25 insertions(+)
diff --git a/src/diff.c b/src/diff.c
index 397815e..003eb59 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -84,6 +84,9 @@ static struct regexp_list function_regexp_list;
/* Ignore changes affecting only lines that match these regexps. */
static struct regexp_list ignore_regexp_list;
+/* Parse only files that match this regexp. */
+static struct regexp_list only_regexp_list;
+
#if O_BINARY
/* Use binary I/O when reading and writing data (--binary).
On POSIX hosts, this has no effect. */
@@ -126,6 +129,7 @@ enum
SUPPRESS_COMMON_LINES_OPTION,
TABSIZE_OPTION,
TO_FILE_OPTION,
+ ONLY_OPTION,
/* These options must be in sequence. */
UNCHANGED_LINE_FORMAT_OPTION,
@@ -191,6 +195,7 @@ static struct option const longopts[] =
{"normal", 0, 0, NORMAL_OPTION},
{"old-group-format", 1, 0, OLD_GROUP_FORMAT_OPTION},
{"old-line-format", 1, 0, OLD_LINE_FORMAT_OPTION},
+ {"only-that-matches", 1, 0, ONLY_OPTION},
{"paginate", 0, 0, 'l'},
{"rcs", 0, 0, 'n'},
{"recursive", 0, 0, 'r'},
@@ -282,6 +287,7 @@ main (int argc, char **argv)
c_stack_action (0);
function_regexp_list.buf = &function_regexp;
ignore_regexp_list.buf = &ignore_regexp;
+ only_regexp_list.buf = &only_regexp;
re_set_syntax (RE_SYNTAX_GREP | RE_NO_POSIX_BACKTRACKING);
excluded = new_exclude ();
@@ -609,6 +615,10 @@ main (int argc, char **argv)
specify_value (&to_file, optarg, "--to-file");
break;
+ case ONLY_OPTION:
+ add_regexp (&only_regexp_list, optarg);
+ break;
+
case UNCHANGED_LINE_FORMAT_OPTION:
case OLD_LINE_FORMAT_OPTION:
case NEW_LINE_FORMAT_OPTION:
@@ -696,6 +706,7 @@ main (int argc, char **argv)
summarize_regexp_list (&function_regexp_list);
summarize_regexp_list (&ignore_regexp_list);
+ summarize_regexp_list (&only_regexp_list);
if (output_style == OUTPUT_IFDEF)
{
@@ -883,6 +894,7 @@ static char const * const option_help_msgid[] = {
N_(" --no-ignore-file-name-case consider case when comparing file
names"),
N_("-x, --exclude=PAT exclude files that match PAT"),
N_("-X, --exclude-from=FILE exclude files that match any pattern in
FILE"),
+ N_(" --only=REGEX only parse files that match REGEX"),
N_("-S, --starting-file=FILE start with FILE when comparing
directories"),
N_(" --from-file=FILE1 compare FILE1 to all operands;\n"
" FILE1 can be a directory"),
diff --git a/src/diff.h b/src/diff.h
index e9f0471..1df36f9 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -145,6 +145,9 @@ XTERN char *file_label[2];
/* Regexp to identify function-header lines (-F). */
XTERN struct re_pattern_buffer function_regexp;
+/* Parse only file that match this regexp (--only) */
+XTERN struct re_pattern_buffer only_regexp;
+
/* Ignore changes that affect only lines matching this regexp (-I). */
XTERN struct re_pattern_buffer ignore_regexp;
diff --git a/src/dir.c b/src/dir.c
index d3b0a2d..5fcd06f 100644
--- a/src/dir.c
+++ b/src/dir.c
@@ -99,6 +99,16 @@ dir_read (struct file_data const *dir, struct dirdata
*dirdata)
if (excluded_file_name (excluded, d_name))
continue;
+ if (only_regexp.fastmap)
+ {
+ int only_match = re_search (&only_regexp, d_name, d_size, 0,
d_size, NULL);
+
+ if (-1 == only_match)
+ continue;
+ else if (-2 == only_match)
+ fatal("Error while matching --only option");
+ }
+
while (data_alloc < data_used + d_size)
{
if (PTRDIFF_MAX / 2 <= data_alloc)
--
2.1.2
- [bug-diffutils] bug#18641: [PATCH] diff: Add --only=REGEX option to parse only files that match REGEX,
Daniel Hilst Selli <=