5

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.

valk
  • 9,363
  • 12
  • 59
  • 79

4 Answers4

6

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

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

Benni
  • 69
  • 1
  • 4
  • That's a great way to achieve the goal *assuming* file naming is adequate. – valk Aug 12 '16 at 08:10
  • 1
    @valk 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. – hek2mgl Aug 12 '16 at 14:01
4

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
sparrowt
  • 2,641
  • 25
  • 24
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.
abhiarora
  • 9,743
  • 5
  • 32
  • 57
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).

joao
  • 3,517
  • 1
  • 31
  • 43