2009-08-27 68 views
3

如果我希望基本上grep知识库中的每一行,有没有办法做到这一点?我知道这对于大型项目来说需要很长时间。如何搜索整个CVS存储库(所有分支机构/历史/评论)?

如果不是全部包容,至少只是当前分支及其整个源历史?

编辑:我应该更加明确。 如果我没有直接访问CVS存储库所在的服务器,该怎么办?所以我不能直接 grep具有CVS存储库的文件系统。

+0

无法访问存储库是一个相当大的事情,省略,你不觉得吗?但是,除非您使用pserver,否则您的IT部门经历了很长时间,您可能有权访问存储库 - 只是访问不方便。请编辑您的问题并添加CVS/Root文件的内容(您可以在工作树的任何目录中找到它)。如果您不想让人们知道您的网络,请将所有主机名引用替换为foo.example.com,并根据需要替换目录名称。但别改变别的。 – kdgregory 2009-08-28 01:37:49

+0

对不起,我不知道标准的CVS设置是什么。我一直在使用Subversion,而且我最后一次使用CVS的时间已经超过8年。我正在使用pserver。我试着在主机上闪烁,但没有运气。我会询问有关建立一个帐户,因为这似乎是最好的方式。 – 2009-08-28 11:06:41

回答

0

这取决于你在找什么。 CVS版本文件以明文形式包含了文件发生过的所有编辑。因此,如果您只是在查找包含特定单词的所有文件,请对存储库执行递归grep。

如果您正在寻找包含这些单词的特定版本,那么您将不得不从版本库中提取版本,这很昂贵。但是,如果您可以通过刷新存储库来限制文件集,那么它并不是那么糟糕。

1

没有办法使用标准CVS工具来访问存储库。一个第三方工具可以做到这一点(我不知道其中一个,尽管CS-CVS似乎是claim to),但是要以编程方式进行,您必须在所有相关文件上执行CVS日志,然后检索并在日志中搜索由CVS报告的每个版本(CVS日志是CVS中的命令行选项,显示任何文件的修订历史记录,但不会显示内容)。

+0

+1,thx。很高兴知道。 – 2009-08-28 15:32:59

1

这是我最近使用的情况下,我没有访问服务器的情况。它似乎在那个时候工作。从工作副本中调用它,在PATH中使用CVS。请注意,这不会搜索提交消息,但您可以简单地grep'cvs log'。

#!/usr/bin/perl 

# Searches CVS diffs and first revisions behind the current working 
# directory for an expression (perlre syntax). 

# Synopsis: cvsgrep [-n] <search-expression> [<file_1> ... <file_n>] 

# -n means that contents of matching files should not be printed to stdout. 

use Getopt::Std; 

my %options=(); 
getopts("n",\%options); 
my $no_content_dump=$options{"n"}; 

my $search_term=shift 
    or die "Error: usage is: cvsgrep [-n] <search-expression>". 
    " [<file_1> ... <file_n>]"; 

sub quote_fn 
{ 
    my $fn=shift; 
    $fn =~ s/\'/\'\"\'\"\'/g; 
    "'".$fn."'"; 
} 

my $args_str; 
while(@ARGV) 
{ 
    my $arg=shift; 
    $args_str.=' ' if $args_str; 
    $args_str.=&quote_fn($arg); 
} 

print 
    "Searching for term: $search_term", 
    ($args_str?" in: $args_str":""), 
    "\n"; 

open CVSLOGH,"cvs log -N $args_str|" or die "Cannot execute cvs log: $!"; 

my @files_revisions=(); 

my $cur_file; 
my $cur_revision; 

while(<CVSLOGH>) 
{ 
    chop; 
    if(/^Working file\:\s*(.*)$/) 
    { 
     $cur_file=$1; 
     $cur_revision=''; 
    } 
    elsif(/^revision\s+(.*)$/) 
    { 
     $cur_revision=$1; 
    } 
    elsif((/^\=\=\=\=/ || /^\-\-\-\-/) && $cur_revision) 
    { 
     push @files_revisions,{file=>$cur_file,rev=>$cur_revision}; 
    } 
} 

close CVSLOGH; 

my $matchcount=0; 
my $count=0; 
my $progress_msg="Scanned %d out of %d commit(s)\r"; 
my $erase_ln=(" " x (length($progress_msg)+20)) . "\r"; 

foreach my $file_revision(@files_revisions) 
{ 
    printf($progress_msg,$count++,scalar(@files_revisions)); 

    my($file,$rev) = ($file_revision->{file},$file_revision->{rev}); 

    $rev =~ /^(.*\.)([0-9]+)/; 
    my $revbase=$1; 
    my $revlastdigit=$2; 
    my $rev1=$revbase.($revlastdigit - 1); 
    my $diffcommand = "cvs diff -N -r $rev1 -r $rev ".&quote_fn($file); 
    open CVSDIFFH,"$diffcommand|" or die "Cannot execute cvs diff: $!"; 

    my $diffresult; 
    while(<CVSDIFFH>) 
    { 
     if(/^[\<\>]/) 
     { 
      s/^.//; 
      $diffresult.=$_; 
     } 
    } 
    close CVSDIFFH; 

    if($diffresult =~ /$search_term/s) 
    { 
     print "${erase_ln}FOUND: in diff for $file $rev1:$rev\n"; 
     $matchcount++; 
     system($diffcommand) unless $no_content_dump; 
    } 
} 

print "${erase_ln}Done ($matchcount match(es)).\n";