Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

svn - How do I search all revisions of a file in a SubVersion repository?

I'd like to grep all revisions of a file for a string. e.g. to find when a function was added or removed.

Is there a "simple" way to do this? (i.e. a single bash command line would be nice.) Doing a manual binary search by checking out revisions and testing individually seems too tedious and error prone.

If I was smart enough to commit the change with a useful description then I can grep the log with something like:

svn log myfile.c | grep my_func

This doesn't provide a revision number though, so I suspect there's a better way to do that too.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I wrote a script to do it

TYpical usage:

perl searchrev.pl Import.php setImportStatus

----------------------------------------------------------------------
r19565 | johnf | 2009-06-24 14:33:00 +0100 (Wed, 24 Jun 2009) | 1 line
----------------------------------------------------------------------
line 60 $this->setImportStatus($entity_id, $entity_attr_id);
---------------------------------------------------------------------
r13722 | john | 2008-03-10 17:06:14 +0000 (Mon, 10 Mar 2008) | 1 line
---------------------------------------------------------------------
line 70 $this->setImportStatus($entity_id, $entity_attr_id);
---------------------------------------------------------------------
r11692 | paul | 2007-05-23 10:55:45 +0100 (Wed, 23 May 2007) | 1 line
---------------------------------------------------------------------
Not found
---------------------------------------------------------------------
r11691 | paul | 2007-05-23 10:36:26 +0100 (Wed, 23 May 2007) | 1 line
---------------------------------------------------------------------
Not found
---------------------------------------------------------------------
r11683 | paul | 2007-05-23 09:04:29 +0100 (Wed, 23 May 2007) | 1 line
---------------------------------------------------------------------
Not found

Here's the script, easy to hack for your own purposes

#!/usr/bin/perl -w

my $file=$ARGV[0];
my $pattern=$ARGV[1];

my @history=`svn log "$file"`;
foreach (@history)
{
    chomp;
    if (m/^r(d+)/)
    {
        my $revision=$1;
        my $sep='-' x length($_);

        print "$sep
$_
$sep
";

        my @code=`svn cat -r $revision "$file"`;
        my $lineno=0;
        my $found=0;
        foreach my $line (@code)
        {
            $lineno++;
            if ($line=~m/$pattern/)
            {
                $line=~s/^s+//;
                print "line $lineno $line";
                 $found=1;
            }
        }

        print "Not found
" unless ($found);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...