| 1 |
package Analizo::Batch::Git; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
use parent qw( |
| 6 |
Analizo::Batch |
| 7 |
Class::Accessor::Fast |
| 8 |
); |
| 9 |
|
| 10 |
use Analizo::Batch::Job::Git; |
| 11 |
use Cwd 'abs_path'; |
| 12 |
use YAML::XS; |
| 13 |
|
| 14 |
__PACKAGE__->mk_ro_accessors(qw( directory )); |
| 15 |
|
| 16 |
sub new { |
| 17 |
my ($class, $directory) = @_; |
| 18 |
$directory ||= '.'; |
| 19 |
$directory = abs_path($directory); |
| 20 |
$class->SUPER::new(directory => $directory); |
| 21 |
} |
| 22 |
|
| 23 |
sub fetch_next { |
| 24 |
my ($self) = @_; |
| 25 |
$self->initialize(); |
| 26 |
my $nextcommit = $self->{relevant}->[$self->{index}]; |
| 27 |
if ($nextcommit) { |
| 28 |
$self->{index}++; |
| 29 |
return $nextcommit; |
| 30 |
} else { |
| 31 |
return undef; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
sub initialize { |
| 36 |
my ($self) = @_; |
| 37 |
unless(defined($self->{index})) { |
| 38 |
# initialize filter: by default look only for files in known languages |
| 39 |
unless ($self->has_filters) { |
| 40 |
$self->filters(Analizo::LanguageFilter->new('all')); |
| 41 |
} |
| 42 |
|
| 43 |
# read in list of commits |
| 44 |
my $data = `(cd $self->{directory} && git log --name-status --format='---%nid: %H%nparents: %P%nauthor_date: %at%nauthor_name: %aN%nauthor_email: %aE%n--- |')`; |
| 45 |
$data =~ s/^([[:upper:]])+\d*\t/ $1 /sgm; |
| 46 |
my @data = (); |
| 47 |
eval { @data = Load($data) }; |
| 48 |
if ($@) { |
| 49 |
die $!; |
| 50 |
} |
| 51 |
my @jobs = (); |
| 52 |
while($#data > 0) { |
| 53 |
|
| 54 |
my $commit_data = shift(@data); |
| 55 |
|
| 56 |
my @parents = (); |
| 57 |
@parents = split(/\s+/, $commit_data->{parents}) if defined($commit_data->{parents}); |
| 58 |
$commit_data->{parents} = \@parents; |
| 59 |
|
| 60 |
my $changed_files = shift(@data); |
| 61 |
if(scalar(@parents) > 1) { |
| 62 |
# merge commits do not have their changed files listed in `git log`, no |
| 63 |
# matter what. This way we *need* to do a `git show` here. |
| 64 |
$changed_files = `(cd $self->{directory} && git show --name-status --format='%n' $commit_data->{id})`; |
| 65 |
} |
| 66 |
chomp($changed_files); |
| 67 |
$changed_files =~ s/^\s*//; # remove leading whitespace |
| 68 |
my %changed_files = map { my ($status, $file) = split(/\s+/, $_); $file => $status } (split("\n", $changed_files)); |
| 69 |
for my $file (keys(%changed_files)) { |
| 70 |
if (!$self->filename_matches_filters($file)) { |
| 71 |
delete $changed_files{$file}; |
| 72 |
} |
| 73 |
} |
| 74 |
$commit_data->{changed_files} = \%changed_files; |
| 75 |
|
| 76 |
my $job = Analizo::Batch::Job::Git->new($self->{directory}, $commit_data->{id}, $commit_data); |
| 77 |
$job->batch($self); |
| 78 |
push @jobs, $job; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
my %jobs = map { $_->id => $_ } @jobs; |
| 83 |
my @relevant = grep { $_->relevant } @jobs; |
| 84 |
$self->{jobs} = \%jobs; |
| 85 |
$self->{relevant} = \@relevant; |
| 86 |
$self->{count} = scalar(@relevant); |
| 87 |
$self->{index} = 0; |
| 88 |
for my $job (@relevant) { |
| 89 |
# force calculating previous relevant right now |
| 90 |
$job->previous_relevant(); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
sub count { |
| 96 |
my ($self) = @_; |
| 97 |
return $self->{count}; |
| 98 |
} |
| 99 |
|
| 100 |
sub find { |
| 101 |
my ($self, $id) = @_; |
| 102 |
return $self->{jobs}->{$id}; |
| 103 |
} |
| 104 |
|
| 105 |
1; |