1

I'd like to search all git tracked files that 1. has an .hs file extension and 2. contain the word import in any lines.

I've tried to use git ls-files -z | xargs -0 rg -g '*.hs' "import" however unfortunately if you pass an explicit list of files to rg it seems to ignore the -g glob flag.

I could potentially use grep to use some regex lookbehind to extract files with a specific file extension, or potentially filter the output from git ls-files -z (ensuring we keep the null separated filenames aspect, both approaches seem a bit clunky though...

If anyone has any ideas.

Chris Stryczynski
  • 5,178
  • 5
  • 40
  • 80

2 Answers2

2

git grep can also do this for you:

git grep import -- '*.hs'

This will search for “import” in all git-tracked files matching *.hs, starting from the current directory.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
1

I hope I'm not misreading the question. If you want " to search all git tracked files that contain the word import in any lines", that should be:

git ls-files -z | xargs -0 grep -l import

If you only want to search in *.hs files, then:

git ls-files -z '*.hs' | xargs -0 grep -l import
larsks
  • 32,449
  • 5
  • 54
  • 70