builtin/grep.c: add '--column' option to 'git-grep(1)'

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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Taylor Blau
2018-06-22 10:49:45 -05:00
committed by Junio C Hamano
parent 89252cd069
commit a449f27ffa
3 changed files with 101 additions and 1 deletions

View File

@ -99,6 +99,101 @@ 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 OR)" '
{
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 OR)" '
{
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, --invert, extended AND)" '
{
echo ${HC}hello_world:3:Hello world
echo ${HC}hello_world:3:Hello_world
echo ${HC}hello_world:6:HeLLo_world
} >expected &&
git grep --column --invert --not -e _ --and --not -e ll $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