0

When needing to find revisions that have a code change, in a Mercurial repository, one can use the command hg grep --all '<pattern>' and get a pretty, colored output in the following format:

# filename : revision : +/- : line in file
filename.ext:100:+:The quick brown fox jumped over the lazy dog.
filename.ext:100:-:Teh quick brown fox jumped over the lazy dog.
filename.ext:95:+:Teh quick brown fox jumped over the lazy dog.
filename.ext:95:-:Teh quick brown fox jumped over the lazy doge.

I was wondering if there was a way to get output similar to this in a Git repository.

Edit: The closest I've been able to get to is the following:

> git rev-list --all | xargs git grep <pattern>

deadbe..ef6543:filename.ext:The quick brown fox jumped over the lazy dog.
deadbe..ef1324:filename.ext:Teh quick brown fox jumped over the lazy dog.
deadbe..ef243a:filename.ext:Teh quick brown fox jumped over the lazy doge.

Which I found from this answer: https://stackoverflow.com/a/39701177

Edit: I have been continuing to work and research this. As it stands right now, I think there is no "out-of-the-box" solution to be able to accomplish this. I now think the only way to achieve this is to build a script, or string together some complicated commands, and maybe store them as an alias.

Damian T.
  • 321
  • 1
  • 10
  • Sounds like you want the "pickaxe". See the docs for `git log`. – matt Dec 07 '20 at 20:07
  • @matt I've tried the pickaxe method, with both `-G` and `-S` and such, but all it does is return the log, not the actual code changes. – Damian T. Dec 07 '20 at 21:06
  • @mkrieger1 I did find one command on that page that produces something similar to what I am looking for. I updated my post to show reflect that. – Damian T. Dec 07 '20 at 21:07
  • 1
    See [alias `grep-all`](https://github.com/GitAlias/gitalias/blob/bf490080764f84d54dbd237920902d27c4b13e8f/gitalias.txt#L457) from [Git Alias](http://www.gitalias.com/); full disclosure: I am a contributor. – phd Dec 07 '20 at 21:20
  • 1
    The pickaxe is what you want, ask log to show you the patch. `git log --all -pS pattern` (or `-pG` if you're after any change that mentions that at all). – jthill Dec 07 '20 at 22:24
  • 2
    So, you want some tool that will scrape just the specific text you want from pickaxe's patch output? I don't know of one, just `/pattern/` in the pager works for me, but a diff scanner's not that hard to write. – jthill Dec 08 '20 at 00:02
  • https://stackoverflow.com/search?q=%5Bgit%5D+grep+all+revisions – phd Dec 08 '20 at 17:09
  • 1
    You weren't the only person to ask about this today, so I wrote [this](https://stackoverflow.com/a/65204346/1290731). Does that help? – jthill Dec 08 '20 at 18:53

0 Answers0