Parse SVN Log Changed Path for Package Names

2019-09-20 04:53发布

问题:

I am running the following command in linux to generate a verbose log for a particular revision.

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

The output is:

------------------------------------------------------------------------
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 
------------------------------------------------------------------------

I want to parse this log output the package names for each of those files. Eg:

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

How to write the perl or python or a shell script to do that so that the usage can be like this for example:

$ perl svnlog.pl 12345 > log.txt

回答1:

This is a version that will run svn log and parse the output.

Of course I don't have your repo so I had to fake the command and output by just cating your output but it works.

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";
    }
}

My test output.

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

A maven repo would have a src/test/java directory too :D, I presume you're not interested in them.


EDIT Running the body of the Perl against your data basically adding my $cmd = "sh fake.sh"; from the other question produces:

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

Which seems to be what the OP wants in the other question.

So the issue would be looping around the body of the Perl once for each $revno.

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

EDIT changing the body of the code group by module to be...

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";
    }
}

produces this on the data previously given.

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

Which should go someway to helping the OP achieve what they want.



回答2:

I think your java project is maven project:

 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

test with your example:

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