| 1 | package VCI::VCS::Git::Commit; | ||
| 2 | use Moose; | ||
| 3 | |||
| 4 | extends 'VCI::Abstract::Commit'; | ||
| 5 | |||
| 6 | has 'x_changes' => (is => 'ro', lazy_build => 1); | ||
| 7 | # Moose doesn't let me do lazy_build here, as of Moose 0.33. | ||
| 8 | has '+message' => (lazy => 1, default => sub { shift->_build_message }); | ||
| 9 | |||
| 10 | sub _build_message { | ||
| 11 | my $self = shift; | ||
| 12 | my $text = $self->project->x_do('log', ['-1', '--pretty=medium', | ||
| 13 | $self->revision], 1); | ||
| 14 | # Everything before the message ends in two newlines. | ||
| 15 | $text =~ s/^.+\n\n//s; | ||
| 16 | # Also, every line is idented by four spaces. | ||
| 17 | $text =~ s/^\s{4}//mg; | ||
| 18 | # If Git's "subject" or "body" are empty, it prints "<unknown>" | ||
| 19 | $text =~ s/^<unknown>\n//s; | ||
| 20 | $text =~ s/\n<unknown>$//s; | ||
| 21 | chomp($text); | ||
| 22 | return $text; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub _build_added { return shift->_x_files_from_changes('A') } | ||
| 26 | sub _build_removed { return shift->_x_files_from_changes('D') } | ||
| 27 | sub _build_modified { return shift->_x_files_from_changes('M') } | ||
| 28 | sub _build_moved { return shift->_x_hash_from_changes('R') } | ||
| 29 | sub _build_copied { return shift->_x_hash_from_changes('C') } | ||
| 30 | |||
| 31 | sub _build_as_diff { | ||
| 32 | my $self = shift; | ||
| 33 | my $diff = $self->project->x_do('whatchanged', | ||
| 34 | ['-m', '-p', '-1', '-C', '--pretty=format:', $self->revision], 1); | ||
| 35 | return $self->diff_class->new(raw => $diff, project => $self->project); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _x_files_from_changes { | ||
| 39 | my ($self, $type) = @_; | ||
| 40 | my $files = $self->x_changes->{$type}; | ||
| 41 | (print STDERR "Creating " . scalar @$files . " $type file objects...\n") | ||
| 42 | if $self->vci->debug; | ||
| 43 | return [map { $self->file_class->new(path => $_, project => $self->project, | ||
| 44 | revision => $self->revision) } | ||
| 45 | @$files]; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _x_hash_from_changes { | ||
| 49 | my ($self, $type) = @_; | ||
| 50 | my $details = $self->x_changes->{$type}; | ||
| 51 | if ($self->vci->debug) { | ||
| 52 | print STDERR "Creating " . scalar(keys %$details) | ||
| 53 | . " $type file objects...\n"; | ||
| 54 | } | ||
| 55 | my %result; | ||
| 56 | foreach my $new_name (keys %$details) { | ||
| 57 | my %params = %{ $details->{$new_name} }; | ||
| 58 | $result{$new_name} = | ||
| 59 | $self->file_class->new(%params, project => $self->project); | ||
| 60 | } | ||
| 61 | return \%result; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub _build_x_changes { | ||
| 66 | my $self = shift; | ||
| 67 | my $output = $self->project->x_do('whatchanged', | ||
| 68 | ['-m', '-1', '-C', '--pretty=format:%P', $self->revision]); | ||
| 69 | |||
| 70 | my (%moved, %copied); | ||
| 71 | my %actions = ('A' => [], 'M' => [], 'D' => [], | ||
| 72 | 'C' => \%copied, 'R' => \%moved); | ||
| 73 | |||
| 74 | my @parents = split(' ', shift @$output) if @$output; | ||
| 75 | foreach my $line (@$output) { | ||
| 76 | # The format of this line is described in the git-diff-tree manpage. | ||
| 77 | $line =~ /^:\d+ \d+ (\w+)\.* (\w+)\.* (\w)(\d+)?\t([^\t]+)(\t(.*))?$/o; | ||
| 78 | my ($sha1, $sha2, $type, $file, $new_name) = ($1, $2, $3, $5, $7); | ||
| 79 | if ($type eq 'C') { | ||
| 80 | $copied{$new_name} = { path => $file, x_one_of => \@parents }; | ||
| 81 | if ($sha1 eq $sha2) { | ||
| 82 | push(@{ $actions{'A'} }, $new_name); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | push(@{ $actions{'M'} }, $new_name); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | elsif ($type eq 'R') { | ||
| 89 | $moved{$new_name} = { path => $file, x_one_of => \@parents }; | ||
| 90 | if ($sha1 ne $sha2) { | ||
| 91 | push(@{ $actions{'M'} }, $new_name); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | push(@{ $actions{$type} }, $file); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return \%actions; | ||
| 100 | } | ||
| 101 | |||
| 102 | __PACKAGE__->meta->make_immutable; | ||
| 103 | |||
| 104 | 1; |