Searching through all revisions

A colleague was asking:

I’m trying to work out a technique for searching for an occurrence of a phrase in _all_ revisions of a specific file in a subversion repository. How can I do this?

Of course, in subversion, the answer is slow and complicated. But, you can use git (and git svn in particular) to achieve the answer fairly simply (and much more quickly).

First, you just clone the subversion repository into git. This takes a while, mostly because subversion isn’t that quick.

  git clone -s https://svn.example.com/proj

Then, you can wrap a little bit of shell scripting around git to get what you need.

  cd proj
  git branch -a | grep tags/ | while read tag
  do
    git --no-pager grep 'something' $tag -- some/file.txt
  done

So, we pull out a list of tags, and run git grep over each one.

There may well be a more effective way to do this, but hey, it took seconds to come up with. And it shows off the reason I like git — it’s so scriptable.

Comments 4

  1. Aristotle Pagaltzis wrote:
    git branch -a | grep tags/

    You mean git tag.

    while read tag ; do git grep foo $tag ; done

    git grep takes any number of treeishs, not just one.

    So that makes it:

    git grep 'something' `git tag` -- some/file.txt

    You might also want to try this:

    git log -S 'something' --tags

    (Your comment textarea could really stand to be bigger.)

    Posted 05 Feb 2009 at 12:08
  2. dom wrote:

    No, I mean git branch -a because git svn clone turns svn tags into git branches. Which is yucky, but hasn’t bothered me too much so far.

    I did play (briefly) with the treeish notation, but couldn’t quite get it to work, so I went for the simpler solution.

    Posted 05 Feb 2009 at 17:43
  3. Aristotle Pagaltzis wrote:

    I mean git branch -a because git svn clone turns svn tags into git branches.

    For the record (although it might not be much help if you need to go two-way with git svn), Converting git-svn tag branches to real tags.

    Posted 25 Feb 2009 at 03:53
  4. dom wrote:

    Thanks for that — I did need to do that when I converted my personal repo’s to git. I think I hacked up something similar by hand… Sadly the work stuff is in svn and likely to stay that way for a while. 🙁

    Posted 25 Feb 2009 at 07:56