* rz/grepz:
  git grep: Add "-z/--null" option as in GNU's grep.
This commit is contained in:
Shawn O. Pearce 2008-10-09 10:18:59 -07:00
Родитель e4fff5ce65 83caecca2f
Коммит 86193807cf
4 изменённых файлов: 26 добавлений и 3 удалений

Просмотреть файл

@ -15,6 +15,7 @@ SYNOPSIS
[-E | --extended-regexp] [-G | --basic-regexp]
[-F | --fixed-strings] [-n]
[-l | --files-with-matches] [-L | --files-without-match]
[-z | --null]
[-c | --count] [--all-match]
[-A <post-context>] [-B <pre-context>] [-C <context>]
[-f <file>] [-e] <pattern>
@ -94,6 +95,11 @@ OPTIONS
For better compatibility with 'git-diff', --name-only is a
synonym for --files-with-matches.
-z::
--null::
Output \0 instead of the character that normally follows a
file name.
-c::
--count::
Instead of showing every matched line, show the number of

Просмотреть файл

@ -295,6 +295,9 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
push_arg("-l");
if (opt->unmatch_name_only)
push_arg("-L");
if (opt->null_following_name)
/* in GNU grep git's "-z" translates to "-Z" */
push_arg("-Z");
if (opt->count)
push_arg("-c");
if (opt->post_context || opt->pre_context) {
@ -599,6 +602,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
opt.unmatch_name_only = 1;
continue;
}
if (!strcmp("-z", arg) ||
!strcmp("--null", arg)) {
opt.null_following_name = 1;
continue;
}
if (!strcmp("-c", arg) ||
!strcmp("--count", arg)) {
opt.count = 1;

14
grep.c
Просмотреть файл

@ -239,6 +239,8 @@ static int word_char(char ch)
static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
const char *name, unsigned lno, char sign)
{
if (opt->null_following_name)
sign = '\0';
if (opt->pathname)
printf("%s%c", name, sign);
if (opt->linenum)
@ -246,6 +248,11 @@ static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
printf("%.*s\n", (int)(eol-bol), bol);
}
static void show_name(struct grep_opt *opt, const char *name)
{
printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
}
static int fixmatch(const char *pattern, char *line, regmatch_t *match)
{
char *hit = strstr(line, pattern);
@ -489,7 +496,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
return 1;
}
if (opt->name_only) {
printf("%s\n", name);
show_name(opt, name);
return 1;
}
/* Hit at this line. If we haven't shown the
@ -555,7 +562,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
return 0;
if (opt->unmatch_name_only) {
/* We did not see any hit, so we want to show this */
printf("%s\n", name);
show_name(opt, name);
return 1;
}
@ -565,7 +572,8 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
* make it another option? For now suppress them.
*/
if (opt->count && count)
printf("%s:%u\n", name, count);
printf("%s%c%u\n", name,
opt->null_following_name ? '\0' : ':', count);
return !!last_hit;
}

1
grep.h
Просмотреть файл

@ -74,6 +74,7 @@ struct grep_opt {
unsigned extended:1;
unsigned relative:1;
unsigned pathname:1;
unsigned null_following_name:1;
int regflags;
unsigned pre_context;
unsigned post_context;