| 1 | package Carmel::Difftool; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Carton::Snapshot; | ||
| 6 | use CPAN::DistnameInfo; | ||
| 7 | use version; | ||
| 8 | use Class::Tiny; | ||
| 9 | use Capture::Tiny qw(capture); | ||
| 10 | use Path::Tiny; | ||
| 11 | |||
| 12 | use constant RED => 31; | ||
| 13 | use constant GREEN => 32; | ||
| 14 | use constant YELLOW => 33; | ||
| 15 | use constant PURPLE => 35; | ||
| 16 | |||
| 17 | sub should_color { | ||
| 18 | -t STDOUT && !$ENV{NO_COLOR}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub color { | ||
| 22 | my($code, $text) = @_; | ||
| 23 | return $text unless should_color(); | ||
| 24 | return "\e[${code}m${text}\e[0m"; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub load_snapshot { | ||
| 28 | my($self, $file, $dists, $index) = @_; | ||
| 29 | |||
| 30 | defined $file | ||
| 31 | or die "Usage: carmel difftool LOCAL REMOTE\n"; | ||
| 32 | |||
| 33 | my $snapshot = Carton::Snapshot->new(path => $file); | ||
| 34 | $snapshot->load; | ||
| 35 | |||
| 36 | for my $dist ($snapshot->distributions) { | ||
| 37 | (my $path = $dist->pathname) =~ s!^[A-Z]/[A-Z]{2}/!!; | ||
| 38 | my $info = CPAN::DistnameInfo->new($path); | ||
| 39 | $dists->{$info->dist}[$index] = $info; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | sub diff { | ||
| 44 | my($self, @args) = @_; | ||
| 45 | |||
| 46 | my %dists; | ||
| 47 | $self->load_snapshot($args[0], \%dists, 0); | ||
| 48 | $self->load_snapshot($args[1], \%dists, 1); | ||
| 49 | |||
| 50 | if ($Carmel::DEBUG) { | ||
| 51 | $self->text_diff(\%dists); | ||
| 52 | } else { | ||
| 53 | $self->simple_diff(\%dists); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub text_diff { | ||
| 58 | my($self, $dists) = @_; | ||
| 59 | |||
| 60 | my @text; | ||
| 61 | for my $dist (sort { lc($a) cmp lc($b) } keys %$dists) { | ||
| 62 | for my $idx (0, 1) { | ||
| 63 | if ($dists->{$dist}[$idx]) { | ||
| 64 | $text[$idx] .= "$dist\n " . $dists->{$dist}[$idx]->pathname . "\n"; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->git_diff(@text); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub git_diff { | ||
| 73 | my($self, @text) = @_; | ||
| 74 | |||
| 75 | my @files = map { | ||
| 76 | my $temp = Path::Tiny->tempfile; | ||
| 77 | $temp->spew($_); | ||
| 78 | $temp; | ||
| 79 | } @text; | ||
| 80 | |||
| 81 | my @options; | ||
| 82 | @options = ("--color") if should_color(); | ||
| 83 | |||
| 84 | my($stdout, $stderr, $code) = capture { system("git", "diff", @options, @files) }; | ||
| 85 | print $stdout; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub simple_diff { | ||
| 89 | my($self, $dists) = @_; | ||
| 90 | |||
| 91 | for my $dist (sort { lc($a) cmp lc($b) } keys %$dists) { | ||
| 92 | $self->dist_diff($dist, @{$dists->{$dist}}); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub dist_diff { | ||
| 97 | my($self, $dist, $old, $new) = @_; | ||
| 98 | |||
| 99 | # unchanged | ||
| 100 | return if $old && $new && $old->distvname eq $new->distvname; | ||
| 101 | |||
| 102 | # added | ||
| 103 | if (!$old && $new) { | ||
| 104 | printf "%s %s (%s)\n", | ||
| 105 | color(YELLOW, 'A'), | ||
| 106 | $dist, color(GREEN, $new->version); | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | # deleted | ||
| 111 | if ($old && !$new) { | ||
| 112 | printf "%s %s (%s)\n", | ||
| 113 | color(YELLOW, 'D'), | ||
| 114 | $dist, color(RED, $old->version); | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | printf "%s %s (%s -> %s)\n", | ||
| 119 | color(GREEN, 'M'), | ||
| 120 | $dist, color(RED, $old->version), color(GREEN, $new->version); | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; |