解析包名称的SVN日志更改路径

时间:2013-02-14 10:46:55

标签: python perl svn shell

我在linux中运行以下命令,为特定版本生成详细日志。

svn log -v -r12345 http://svn-remote.com/path

输出结果为:

------------------------------------------------------------------------
r12345 | debajyoti.das@email.com | 2013-02-07 01:27:08 -0800 (Thu, 07 Feb 2013) | 1 line
Changed paths:
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.agent/src/main/java/com/companyname/cycle/agent/Main.java
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.agent/src/main/java/com/companyname/cycle/agent/bll/tasks/CycleGroupTask.java
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.agent/src/main/java/com/companyname/cycle/agent/bll/tasks/CycleProcessTaskBll.java
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.agent/src/main/java/com/companyname/cycle/agent/bll/tasks/CycleSequenceProcessTaskBll.java
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.test/src/main/java/com/companyname/cycle/test/bll/MockCycleBll.java
   M /PROJECT/CYCLE/branches/DEV_Branch/pas.cycle/src/main/java/com/companyname/pas/cycle/bll/DasCycleBll.java
   M /PROJECT/CYCLE/branches/DEV_Branch/pas.cycle/src/main/java/com/companyname/pas/cycle/dal/DasCycleDal.java

BUG ID 12345678 - BLAH IMPLEMENTATION IN PROJECT.CYCLE 
------------------------------------------------------------------------

我想解析每个文件的日志输出包名称。例如:

PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleGroupTask.java

如何编写perl或python或shell脚本来执行此操作,以便使用例如:

$ perl svnlog.pl 12345 > log.txt

2 个答案:

答案 0 :(得分:1)

我认为你的java项目是maven项目:

 awk -F'src/main/java/' 'NF==2{split($1,a,"/");j=p=$2;sub(/.*\//,"",j);sub("/[^/]*$","",p);gsub("/",".",p);print a[2]"."a[3],p,j}' yourlog

用你的例子测试:

kent$  cat f1
------------------------------------------------------------------------
r12345 | debajyoti.das@email.com | 2013-02-07 01:27:08 -0800 (Thu, 07 Feb 2013) | 1 line
Changed paths:
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.agent/src/main/java/com/companyname/cycle/agent/Main.java
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.agent/src/main/java/com/companyname/cycle/agent/bll/tasks/CycleGroupTask.java
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.agent/src/main/java/com/companyname/cycle/agent/bll/tasks/CycleProcessTaskBll.java
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.agent/src/main/java/com/companyname/cycle/agent/bll/tasks/CycleSequenceProcessTaskBll.java
   M /PROJECT/CYCLE/branches/DEV_Branch/cycle.test/src/main/java/com/companyname/cycle/test/bll/MockCycleBll.java
   M /PROJECT/CYCLE/branches/DEV_Branch/pas.cycle/src/main/java/com/companyname/pas/cycle/bll/DasCycleBll.java
   M /PROJECT/CYCLE/branches/DEV_Branch/pas.cycle/src/main/java/com/companyname/pas/cycle/dal/DasCycleDal.java

BUG ID 12345678 - BLAH IMPLEMENTATION IN PROJECT.CYCLE 
------------------------------------------------------------------------

kent$  awk -F'src/main/java/' 'NF==2{split($1,a,"/");j=p=$2;sub(/.*\//,"",j);sub("/[^/]*$","",p);gsub("/",".",p);print a[2]"."a[3],p,j}' f1
PROJECT.CYCLE com.companyname.cycle.agent Main.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleGroupTask.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleProcessTaskBll.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleSequenceProcessTaskBll.java
PROJECT.CYCLE com.companyname.cycle.test.bll MockCycleBll.java
PROJECT.CYCLE com.companyname.pas.cycle.bll DasCycleBll.java
PROJECT.CYCLE com.companyname.pas.cycle.dal DasCycleDal.java

答案 1 :(得分:1)

这是一个运行svn log并解析输出的版本。

当然我没有你的回购,所以我不得不假装命令和输出只是cat你的输出,但它的工作原理。

use strict;

my $revno = shift @ARGV;

die "Usage: $0: svn-revision\n" unless $revno;

my $cmd = "svn log -v -r$revno 'http://svn-remote.com/path'";

open(SVN,"$cmd |") or die "Command '$cmd' Failed : $!\n";

while(<SVN>) {
    chomp;
    if ( /^\s+\S\s\/([^\/]+\/[^\/]+).*[^\/]$/ ) {
        (my $tag = $1) =~ s!/!.!g;
        s!.*/src/main/java/!!;
        my @a =  split(/\//);
        my $file = pop @a;
        print $tag, " ",  join(".", @a), " ",  $file, "\n";
    }
}

我的测试输出。

PROJECT.CYCLE com.companyname.cycle.agent Main.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleGroupTask.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleProcessTaskBll.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleSequenceProcessTaskBll.java
PROJECT.CYCLE com.companyname.cycle.test.bll MockCycleBll.java
PROJECT.CYCLE com.companyname.pas.cycle.bll DasCycleBll.java
PROJECT.CYCLE com.companyname.pas.cycle.dal DasCycleDal.java

maven repo也有一个src/test/java目录:D,我认为你对它们不感兴趣。


修改 根据您的数据运行Perl的主体,基本上从另一个问题中添加my $cmd = "sh fake.sh";会产生:

PROJECT.CYCLE com.companyname.cycle.agent Main.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleGroupTask.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleProcessTaskBll.java
PROJECT.CYCLE com.companyname.cycle.agent.bll.tasks CycleSequenceProcessTaskBll.java
PROJECT.CYCLE com.companyname.cycle.test.bll MockCycleBll.java
PROJECT.CYCLE com.companyname.pas.cycle.bll DasCycleBll.java
PROJECT.CYCLE com.companyname.pas.cycle.dal DasCycleDal.java
PROJECT.SHARED com.companyname.extensibility NamedExtensionPoint.java
PROJECT.SHARED com.companyname.extensibility PrePostExtensionPoint.java
PROJECT.SHARED com.companyname.extensibility.helper ExtensionConfigurationHelper.java
PROJECT.SRE com.companyname.ext DocumentGeneratorExt.java
PROJECT.SRE com.companyname.ext NamedExtensionPoint.java
PROJECT.SRE com.companyname.ext SystemDateBll.java
MODULE.CYCLE com.companyname.extensibility FileCycle1.java
MODULE.CYCLE com.companyname.extensibility FileCycle2.java

这似乎是OP在另一个问题上想要的。

因此,每个$revno问题都会围绕Perl的主体循环。

foreach my $revno (@ARGV)   {
        print $revno, "\n";
}

编辑按模块更改代码组的正文...

my %info;
while(<SVN>) {
    chomp;
    if ( /^\s+\S\s\/([^\/]+\/[^\/]+).*[^\/]$/ ) {
        (my $tag = $1) =~ s!/!.!g;
        s!.*/src/main/java/!!;
        my @a =  split(/\//);
        my $file = pop @a;
        print $tag, " ",  join(".", @a), " ",  $file, "\n";
        push @{$info{$tag}}, join(" ", join(".", @a), $file);
        #push @{$info{$tag}}, $file;
    }
}

while ( my ($key, $value) = each %info )
{
    print "$key\n";
    for my $line ( @{$info{$key}} ) {
        print "\t", $line, "\n";
    }
}

根据先前给出的数据生成此内容。

PROJECT.SHARED
    com.companyname.extensibility NamedExtensionPoint.java
    com.companyname.extensibility PrePostExtensionPoint.java
    com.companyname.extensibility.helper ExtensionConfigurationHelper.java
PROJECT.CYCLE
    com.companyname.cycle.agent Main.java
    com.companyname.cycle.agent.bll.tasks CycleGroupTask.java
    com.companyname.cycle.agent.bll.tasks CycleProcessTaskBll.java
    com.companyname.cycle.agent.bll.tasks CycleSequenceProcessTaskBll.java
    com.companyname.cycle.test.bll MockCycleBll.java
    com.companyname.pas.cycle.bll DasCycleBll.java
    com.companyname.pas.cycle.dal DasCycleDal.java
MODULE.CYCLE
    com.companyname.extensibility FileCycle1.java
    com.companyname.extensibility FileCycle2.java
PROJECT.SRE
    com.companyname.ext DocumentGeneratorExt.java
    com.companyname.ext NamedExtensionPoint.java
    com.companyname.ext SystemDateBll.java

应该在某种程度上帮助OP实现他们想要的目标。