How to omit very long lines from `git grep`?
Asked Answered
T

4

6

I'm searching through my repository (with vim-fugitive's :Ggrep), I have different .js files - the minified ones and the regular ones.

I would like to omit from git grep the minified files (in other words - the very long lines that match the query). I looked into git help grep and googled but couldn't find anything. All ideas are welcomed.

Tricolor answered 27/3, 2016 at 20:55 Comment(4)
Is piping git grep output an option?Sible
Why do you store minified js in the repo?Spinnaker
@Spinnaker Sometimes you just get the code and have to deal with it ;-) If you know any solution to this except changing the structure, but purely solvable in vim, I'll be glad to have itTricolor
@Tricolor I like Benjamin Kotte's solutionSpinnaker
A
5

This doesn't actually omit long lines as requested, but to avoid your terminal filling up when a very long line matches (e.g. minified javascript) you can configure git to page the output through less with the -S option (short for --chop-long-lines):

# For current repo only
git config --local core.pager "less -S"

# For all git repos
git config --global core.pager "less -S"

or equivalently add this to the relevant .gitconfig or .git/config

[core]
    pager = less -S
Alumnus answered 6/7, 2021 at 10:58 Comment(0)
A
7

for me, the best way was to create a file with name .gitattributes and this content:

*.min.js binary *.min.css binary

Ania answered 11/8, 2016 at 7:24 Comment(2)
That's a great way to achieve the goal assuming file naming is adequate.Tricolor
@Tricolor You can choose the name, isn't it? If you for whatever reason cannot change the name, have a file like foo.bar.js which is a minified one, add foo.bar.js to gitattributes.Spinnaker
A
5

This doesn't actually omit long lines as requested, but to avoid your terminal filling up when a very long line matches (e.g. minified javascript) you can configure git to page the output through less with the -S option (short for --chop-long-lines):

# For current repo only
git config --local core.pager "less -S"

# For all git repos
git config --global core.pager "less -S"

or equivalently add this to the relevant .gitconfig or .git/config

[core]
    pager = less -S
Alumnus answered 6/7, 2021 at 10:58 Comment(0)
R
1

grep is a command-line utility for searching plain-text data sets for lines matching a regular expression. you can use a regular expression to limit the search.

For example, You can use curly braces to control the number of occurrences. For example, this means 0 to 10:

/^[a-z]{0,10}$/

The options are:

{3} Exactly 3 occurrences;
{6,} At least 6 occurrences;
{2,5} 2 to 5 occurrences.
Randers answered 27/3, 2016 at 21:52 Comment(0)
T
1

Benni's solution is pretty decent. Building on that, if you want a slightly more generic solution, use git grepto look for text files in the repo that have lines longer than, say, 1500 characters. Then mark these files binaryin the .git/info/attributes. Here's a one-liner to do that:

git grep -lI '.\{1500\}' | sed 's/$/ binary/' >> .git/info/attributes

You can also record this in .gitattributes, of course, if you want to version this (and your team agrees to it).

Tullius answered 24/11, 2020 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.