From: Taylor Blau <me@ttaylorr.com>
To: git@vger.kernel.org
Cc: avarab@gmail.com, peff@peff.net, gitster@pobox.com
Subject: [PATCH v2 5/7] builtin/grep.c: add '--column' option to 'git-grep(1)'
Date: Wed, 20 Jun 2018 15:05:44 -0500 [thread overview]
Message-ID: <ed1c197cc2cdbcfe7fce14747b8601d1488f89a9.1529524852.git.me@ttaylorr.com> (raw)
In-Reply-To: <cover.1529524852.git.me@ttaylorr.com>
Teach 'git-grep(1)' a new option, '--column', to show the column
number of the first match on a non-context line. This makes it possible
to teach 'contrib/git-jump/git-jump' how to seek to the first matching
position of a grep match in your editor, and allows similar additional
scripting capabilities.
For example:
$ git grep -n --column foo | head -n3
.clang-format:51:14:# myFunction(foo, bar, baz);
.clang-format:64:7:# int foo();
.clang-format:75:8:# void foo()
Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
Documentation/git-grep.txt | 6 ++-
builtin/grep.c | 1 +
t/t7810-grep.sh | 84 ++++++++++++++++++++++++++++++++++++++
3 files changed, 90 insertions(+), 1 deletion(-)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index 312409a607..31dc0392a6 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -13,7 +13,7 @@ SYNOPSIS
[-v | --invert-match] [-h|-H] [--full-name]
[-E | --extended-regexp] [-G | --basic-regexp]
[-P | --perl-regexp]
- [-F | --fixed-strings] [-n | --line-number]
+ [-F | --fixed-strings] [-n | --line-number] [--column]
[-l | --files-with-matches] [-L | --files-without-match]
[(-O | --open-files-in-pager) [<pager>]]
[-z | --null]
@@ -169,6 +169,10 @@ providing this option will cause it to die.
--line-number::
Prefix the line number to matching lines.
+--column::
+ Prefix the 1-indexed byte-offset of the first match from the start of the
+ matching line.
+
-l::
--files-with-matches::
--name-only::
diff --git a/builtin/grep.c b/builtin/grep.c
index ee753a403e..61bcaf6e58 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -828,6 +828,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
GREP_PATTERN_TYPE_PCRE),
OPT_GROUP(""),
OPT_BOOL('n', "line-number", &opt.linenum, N_("show line numbers")),
+ OPT_BOOL(0, "column", &opt.columnnum, N_("show column number of first match")),
OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1),
OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1),
OPT_NEGBIT(0, "full-name", &opt.relative,
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 1797f632a3..bf0b572dab 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -99,6 +99,90 @@ do
test_cmp expected actual
'
+ test_expect_success "grep -w $L (with --column)" '
+ {
+ echo ${HC}file:5:foo mmap bar
+ echo ${HC}file:14:foo_mmap bar mmap
+ echo ${HC}file:5:foo mmap bar_mmap
+ echo ${HC}file:14:foo_mmap bar mmap baz
+ } >expected &&
+ git grep --column -w -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --column, extended)" '
+ {
+ echo ${HC}file:14:foo_mmap bar mmap
+ echo ${HC}file:19:foo_mmap bar mmap baz
+ } >expected &&
+ git grep --column -w -e mmap$ --or -e baz $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --column, --invert)" '
+ {
+ echo ${HC}file:1:foo mmap bar
+ echo ${HC}file:1:foo_mmap bar
+ echo ${HC}file:1:foo_mmap bar mmap
+ echo ${HC}file:1:foo mmap bar_mmap
+ } >expected &&
+ git grep --column --invert -w -e baz $H -- file >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep $L (with --column, --invert, extended)" '
+ {
+ echo ${HC}hello_world:6:HeLLo_world
+ } >expected &&
+ git grep --column --invert -e ll --or --not -e _ $H -- hello_world \
+ >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep $L (with --column, double-negation)" '
+ {
+ echo ${HC}file:1:foo_mmap bar mmap baz
+ } >expected &&
+ git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
+ >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --column, -C)" '
+ {
+ echo ${HC}file:5:foo mmap bar
+ echo ${HC}file-foo_mmap bar
+ echo ${HC}file:14:foo_mmap bar mmap
+ echo ${HC}file:5:foo mmap bar_mmap
+ echo ${HC}file:14:foo_mmap bar mmap baz
+ } >expected &&
+ git grep --column -w -C1 -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --line-number, --column)" '
+ {
+ echo ${HC}file:1:5:foo mmap bar
+ echo ${HC}file:3:14:foo_mmap bar mmap
+ echo ${HC}file:4:5:foo mmap bar_mmap
+ echo ${HC}file:5:14:foo_mmap bar mmap baz
+ } >expected &&
+ git grep -n --column -w -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with non-extended patterns, --column)" '
+ {
+ echo ${HC}file:5:foo mmap bar
+ echo ${HC}file:10:foo_mmap bar
+ echo ${HC}file:10:foo_mmap bar mmap
+ echo ${HC}file:5:foo mmap bar_mmap
+ echo ${HC}file:10:foo_mmap bar mmap baz
+ } >expected &&
+ git grep --column -w -e bar -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
test_expect_success "grep -w $L" '
{
echo ${HC}file:1:foo mmap bar
--
2.17.0.582.gccdcbd54c
next prev parent reply other threads:[~2018-06-20 20:05 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-18 23:43 [PATCH 0/7] grep.c: teach --column to 'git-grep(1)' Taylor Blau
2018-06-18 23:43 ` [PATCH 1/7] Documentation/config.txt: camel-case lineNumber for consistency Taylor Blau
2018-06-18 23:43 ` [PATCH 2/7] grep.c: expose {,inverted} match column in match_line() Taylor Blau
2018-06-19 16:49 ` Junio C Hamano
2018-06-19 17:02 ` Taylor Blau
2018-06-18 23:43 ` [PATCH 3/7] grep.[ch]: extend grep_opt to allow showing matched column Taylor Blau
2018-06-18 23:43 ` [PATCH 4/7] grep.c: display column number of first match Taylor Blau
2018-06-19 16:28 ` Jeff King
2018-06-19 16:34 ` Taylor Blau
2018-06-18 23:43 ` [PATCH 5/7] builtin/grep.c: add '--column' option to 'git-grep(1)' Taylor Blau
2018-06-18 23:43 ` [PATCH 6/7] grep.c: add configuration variables to show matched option Taylor Blau
2018-06-18 23:43 ` [PATCH 7/7] contrib/git-jump/git-jump: jump to exact location Taylor Blau
2018-06-19 16:35 ` [PATCH 0/7] grep.c: teach --column to 'git-grep(1)' Jeff King
2018-06-19 17:33 ` René Scharfe
2018-06-19 17:44 ` Taylor Blau
2018-06-19 17:50 ` René Scharfe
2018-06-19 20:26 ` René Scharfe
2018-06-19 17:48 ` Jeff King
2018-06-19 17:54 ` Taylor Blau
2018-06-19 17:58 ` Junio C Hamano
2018-06-19 18:02 ` Taylor Blau
2018-06-19 18:05 ` Jeff King
2018-06-19 18:09 ` Junio C Hamano
2018-06-19 18:50 ` René Scharfe
2018-06-19 19:11 ` Jeff King
2018-06-19 20:34 ` René Scharfe
2018-06-19 20:51 ` Junio C Hamano
2018-06-19 16:46 ` Junio C Hamano
2018-06-19 17:02 ` Taylor Blau
2018-06-19 22:51 ` Taylor Blau
2018-06-20 20:05 ` [PATCH v2 " Taylor Blau
2018-06-20 20:05 ` [PATCH v2 1/7] Documentation/config.txt: camel-case lineNumber for consistency Taylor Blau
2018-06-20 20:05 ` [PATCH v2 2/7] grep.c: expose {,inverted} match column in match_line() Taylor Blau
2018-06-20 20:05 ` [PATCH v2 3/7] grep.[ch]: extend grep_opt to allow showing matched column Taylor Blau
2018-06-20 20:05 ` [PATCH v2 4/7] grep.c: display column number of first match Taylor Blau
2018-06-20 20:05 ` Taylor Blau [this message]
2018-06-20 20:05 ` [PATCH v2 6/7] grep.c: add configuration variables to show matched option Taylor Blau
2018-06-20 20:05 ` [PATCH v2 7/7] contrib/git-jump/git-jump: jump to exact location Taylor Blau
2018-06-21 11:53 ` [PATCH v2 0/7] grep.c: teach --column to 'git-grep(1)' Jeff King
2018-06-21 12:01 ` Jeff King
2018-06-22 21:45 ` Johannes Schindelin
2018-06-22 22:26 ` Jeff King
2018-06-21 20:52 ` Junio C Hamano
2018-06-21 21:45 ` Taylor Blau
2018-06-22 7:22 ` Jeff King
2018-06-22 15:49 ` [PATCH v3 " Taylor Blau
2018-06-22 15:49 ` [PATCH v3 1/7] Documentation/config.txt: camel-case lineNumber for consistency Taylor Blau
2018-06-22 15:49 ` [PATCH v3 2/7] grep.c: expose {,inverted} match column in match_line() Taylor Blau
2018-06-22 15:49 ` [PATCH v3 3/7] grep.[ch]: extend grep_opt to allow showing matched column Taylor Blau
2018-06-22 15:49 ` [PATCH v3 4/7] grep.c: display column number of first match Taylor Blau
2018-06-22 15:49 ` [PATCH v3 5/7] builtin/grep.c: add '--column' option to 'git-grep(1)' Taylor Blau
2018-06-22 15:49 ` [PATCH v3 6/7] grep.c: add configuration variables to show matched option Taylor Blau
2018-06-22 15:49 ` [PATCH v3 7/7] contrib/git-jump/git-jump: jump to exact location Taylor Blau
2018-06-25 18:43 ` [PATCH v3 0/7] grep.c: teach --column to 'git-grep(1)' Jeff King
2018-06-25 18:47 ` Taylor Blau
2018-06-26 16:45 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ed1c197cc2cdbcfe7fce14747b8601d1488f89a9.1529524852.git.me@ttaylorr.com \
--to=me@ttaylorr.com \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).