144

Is there a way to perform a full text search of a subversion repository, including all the history?

For example, I've written a feature that I used somewhere, but then it wasn't needed, so I svn rm'd the files, but now I need to find it again to use it for something else. The svn log probably says something like "removed unused stuff", and there's loads of checkins like that.

Edit 2016-04-15: Please note that what is asked here by the term "full text search", is to search the actual diffs of the commit history, and not filenames and/or commit messages. I'm pointing this out because the author's phrasing above does not reflect that very well - since in his example he might as well be only looking for a filename and/or commit message. Hence a lot of the svn log answers and comments.

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
rjmunro
  • 27,203
  • 20
  • 110
  • 132
  • 6
    Apache Subversion 1.8 accepts `--search` argument for `svn log` command. See my answer at http://stackoverflow.com/a/17473516/761095 – bahrep Jul 04 '13 at 15:06
  • 3
    `svn log --search` does not perform a full text search as required by @rjmunro, but only searches author, date, log message and list of changed paths. – zb226 Apr 15 '16 at 10:25

16 Answers16

72
git svn clone <svn url>
git log -G<some regex>
rjmunro
  • 27,203
  • 20
  • 110
  • 132
luis gutierrez
  • 896
  • 7
  • 2
  • 1
    Good point. The gitk GUI does this kind of search very well, and it's easy to use the git svn tools to access an svn repository. Admitedly, I've moved entirely to using git since the time I asked this question, so accepting this answer might be a bit specific to me. – rjmunro Sep 29 '10 at 14:08
  • If you want to just search the commit messages, use `git log --grep regex`. – lmat - Reinstate Monica Oct 17 '13 at 19:26
  • 4
    Be advised, this can take a while depending on the size of your repository. For me it took more than an hour. – user247702 Aug 07 '15 at 16:31
  • [More git grep examples in this answer](http://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history) – Chomeh Sep 10 '15 at 22:52
  • 1
    Does this really search through the whole history? – Jonny Nov 24 '15 at 09:08
  • @rjmunro: A simple `git grep < some regex >` as given in the answer does not search the history at all. And thus this doesn't answer your question at all. – zb226 Mar 03 '16 at 09:33
  • 1
    @zb226 Converting to git and searching with the gitk gui worked for me. I assumed this answer was the equivalent command line way to do it. Actually it looks like the equivalent command is `git log -S` (from [this answer](http://stackoverflow.com/a/2928721/3408)), but `git log -G` might be even better. I'm editing this answer. – rjmunro Mar 03 '16 at 11:26
  • 33
    I downvoted this solution since converting a big SVN repository to GIT is often not feasible or would take much too long. It's like recommending Java when having a question about a C# language construct. – ooxi Mar 11 '16 at 07:31
  • 3
    You may need to install an additional package for this command. On Ubuntu, you want to `apt-get install git-svn`. – nedned Mar 23 '16 at 23:53
  • 7
    Doesn't "svn log --verbose --diff | grep ..." buy your roughly the same functionality without having to use git? – lyte Jul 06 '16 at 05:33
  • 3
    @lyte It roughly is the same functionality. It is very good in so far you work with svn alone. I find this idea to utilize git to search for commits ridiculous. The trouble starts when you want to see more than just the line containing the term you searched for. `grep` can print a number of lines before the finding and after but you never know how many lines you need (to find the revision number at the top or the whole comment to the bottom). The output is hard to read. – Bernhard Döbler Aug 23 '17 at 10:42
  • 3
    This is searching with git, not searching with SVN as per the question. This is the equivalent of providing a Python script to answer a C question - it works, but it doesn't answer the original question. – Tydaeus Mar 19 '19 at 15:58
45

Update April, 2022

VisualSVN Server 5.0 comes with a new full-text search feature that allows you to search through the contents and history of your repositories in the web interface. Try out the feature on the demo server.


Old answer

svn log in Apache Subversion 1.8 supports a new --search option. So you can search Subversion repository history log messages without using 3'rd party tools and scripts.

svn log --search searches in author, date, log message text and list of changed paths.

See SVNBook | svn log command-line reference.

bahrep
  • 29,961
  • 12
  • 103
  • 150
  • 7
    Handy, but not full-text search. I'm sticking with the git-svn answer :-) – rjmunro Jul 04 '13 at 19:10
  • 4
    Not that at the moment svn repos on googlecode are still running on svn 1.6... see: https://code.google.com/p/support/wiki/SubversionFAQ#What_version_of_Subversion_do_you_use? But, updating your client to 1,8 (and svn upgrade of any checked out repo) will allow you to use svn log --search on the repo... – Mario Ruggier Aug 15 '14 at 11:05
  • The working copy needs all the updates but this command lists the whole change including revision number, changed files and comment. How is it not full-text? – Bernhard Döbler Aug 23 '17 at 10:50
24

If you are running Windows have a look at SvnQuery. It maintains a full text index of local or remote repositories. Every document ever committed to a repository gets indexed. You can do google-like queries from a simple web interface.

Christian Rodemeyer
  • 2,001
  • 1
  • 19
  • 22
21

I'm using a small shellscript, but this only works for a single file. You can ofcourse combine this with find to include more files.

#!/bin/bash
for REV in `svn log $1 | grep ^r[0-9] | awk '{print $1}'`; do 
  svn cat $1 -r $REV | grep -q $2
  if [ $? -eq 0 ]; then 
    echo "$REV"
  fi 
done

If you really want to search everything, use the svnadmin dump command and grep through that.

Bas Grolleman
  • 319
  • 2
  • 3
13

The best way that I've found to do this is with less:

svn log --verbose | less

Once less comes up with output, you can hit / to search, like VIM.

Edit:

According to the author, he wants to search more than just the messages and the file names. In which case you will be required to ghetto-hack it together with something like:

svn diff -r0:HEAD | less

You can also substitute grep or something else to do the searching for you. If you want to use this on a sub-directory of the repository, you will need to use svn log to discern the first revision in which that directory existed, and use that revision instead of 0.

Jack M.
  • 30,350
  • 7
  • 55
  • 67
  • 6
    That's not full text searching, it's searching the logs and filenames. – rjmunro Sep 07 '08 at 14:09
  • If that is the case, then you need to use more expressive commit logs. If you want to grep the difference between revisions, that is whole other ball of wax. And I personally do not know a way to do that. – Jack M. Mar 23 '09 at 18:11
  • > svn diff -r0:HEAD > log > less log is my choice on windows. Thanks – kingoleg Apr 11 '16 at 14:57
10
svn log -v [repository] > somefile.log

for diff you can use the --diff option

svn log -v --diff [repository] > somefile.log

then use vim or nano or whatever you like using, and do a search for what you're looking for. You'll find it pretty quickly.

It's not a fancy script or anything automated. But it works.

JREN
  • 3,572
  • 3
  • 27
  • 45
9

I have been looking for something similar. The best I have come up with is OpenGrok. I have not tried to implement it yet, but sounds promising.

alanc
  • 4,102
  • 21
  • 24
Mike Schall
  • 5,829
  • 4
  • 41
  • 47
6

While not free, you might take a look at Fisheye from Atlassian, the same folks that bring you JIRA. It does full text search against SVN with many other useful features.

http://www.atlassian.com/software/fisheye/

mrjabba
  • 503
  • 1
  • 5
  • 8
5

I just ran into this problem and

svnadmin dump <repo location> |grep -i <search term>

did the job for me. Returned the revision of the first occurrence and quoted the line I was looking for.

pfyon
  • 332
  • 1
  • 6
  • 10
4

I was looking for the same thing and found this:

http://svn-search.sourceforge.net/

4

Use unix utility like grep:

svn log -l <commit limit> --diff | grep -C <5 or more lines> <search message>

or you can save the result of the svn log somewhere, then search through it

Abilogos
  • 4,777
  • 2
  • 19
  • 39
pinkRhino
  • 1,164
  • 9
  • 11
2

I don't have any experience with it, but SupoSE (open source, written in Java) is a tool designed to do exactly this.

dF.
  • 74,139
  • 30
  • 130
  • 136
1

I usually do what Jack M says (use svn log --verbose) but I pipe to grep instead of less.

Community
  • 1
  • 1
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
  • 6
    That's not full text searching, it's searching the logs and filenames. – rjmunro Sep 07 '08 at 14:07
  • That is what I usually end up doing, but I've found that with `less` you can actually see the revision, date, etc instead of just the line in the comment. That is usually what I'm looking for anyway. – Jack M. Mar 23 '09 at 18:10
1

I wrote this as a cygwin bash script to solve this problem.

However it requires that the search term is currently within the filesystem file. For all the files that match the filesystem grep, an grep of all the svn diffs for that file are then performed. Not perfect, but should be good enough for most usage. Hope this helps.

/usr/local/bin/svngrep

#!/bin/bash
# Usage: svngrep $regex @grep_args

regex="$@"
pattern=`echo $regex | perl -p -e 's/--?\S+//g; s/^\\s+//;'` # strip --args
if [[ ! $regex ]]; then
    echo "Usage: svngrep \$regex @grep_args"
else 
    for file in `grep -irl --no-messages --exclude=\*.tmp --exclude=\.svn $regex ./`;     do 
        revs="`svnrevisions $file`";
        for rev in $revs; do
            diff=`svn diff $file -r$[rev-1]:$rev \
                 --diff-cmd /usr/bin/diff -x "-Ew -U5 --strip-trailing-cr" 2> /dev/null`
            context=`echo "$diff" \
                 | grep -i --color=none   -U5 "^\(+\|-\).*$pattern" \
                 | grep -i --color=always -U5             $pattern  \
                 | grep -v '^+++\|^---\|^===\|^Index: ' \
                 `
            if [[ $context ]]; then
                info=`echo "$diff" | grep '^+++\|^---'`
                log=`svn log $file -r$rev`
                #author=`svn info -r$rev | awk '/Last Changed Author:/ { print $4 }'`; 

                echo "========================================================================"
                echo "========================================================================"
                echo "$log"
                echo "$info"
                echo "$context"
                echo
            fi;
        done;
    done;
fi

/usr/local/bin/svnrevisions

#!/bin/sh
# Usage:  svnrevisions $file
# Output: list of fully numeric svn revisions (without the r), one per line

file="$@"
    svn log "$file" 2> /dev/null | awk '/^r[[:digit:]]+ \|/ { sub(/^r/,"",$1); print  $1 }'
James McGuigan
  • 7,542
  • 4
  • 26
  • 29
-2

In case you are trying to determine which revision is responsible for a specific line of code, you are probably looking for:

svn blame

Credit: original answer

Community
  • 1
  • 1
DustWolf
  • 516
  • 7
  • 10
-3

I came across this bash script, but I have not tried it.

Stephan
  • 41,764
  • 65
  • 238
  • 329