Skip to content

Commit

Permalink
inflate git-log
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Dieter Pearcey committed Jun 19, 2008
1 parent ce29189 commit eed528b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
70 changes: 65 additions & 5 deletions lib/Git/Wrapper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ my $GIT = 'git';

sub _opt {
my $name = shift;
$name =~ tr/_/-/;
return length($name) == 1
? "-$name"
: "--$name"
;
}

sub AUTOLOAD {
sub _cmd {
my $self = shift;
(my $meth = our $AUTOLOAD) =~ s/.+:://;
return if $meth eq 'DESTROY';
$meth =~ tr/_/-/;

my $cmd = shift;

my $opt = ref $_[0] eq 'HASH' ? shift : {};

Expand All @@ -43,7 +43,7 @@ sub AUTOLOAD {
next if $val eq '0';
push @cmd, _opt($name) . ($val eq '1' ? "" : "=$val");
}
push @cmd, $meth;
push @cmd, $cmd;
for my $name (keys %$opt) {
my $val = delete $opt->{$name};
next if $val eq '0';
Expand Down Expand Up @@ -78,6 +78,45 @@ sub AUTOLOAD {
return @out;
}

sub AUTOLOAD {
my $self = shift;
(my $meth = our $AUTOLOAD) =~ s/.+:://;
return if $meth eq 'DESTROY';
$meth =~ tr/_/-/;

return $self->_cmd($meth, @_);
}

sub log {
my $self = shift;
my $opt = ref $_[0] eq 'HASH' ? shift : {};
$opt->{no_color} = 1;
my @out = $self->_cmd(log => $opt, @_);

my @logs;
while (@out) {
local $_ = shift @out;
die "unhandled: $_" unless /^commit (\S+)/;
my $current = Git::Wrapper::Log->new($1);
$_ = shift @out;

while (/^(\S+):\s+(.+)$/) {
$current->attr->{lc $1} = $2;
$_ = shift @out;
}
die "no blank line separating head from body" if $_;
my $body = '';
while (@out and length($_ = shift @out)) {
s/^\s+//;
$body .= "$_\n";
}
$current->body($body);
push @logs, $current;
}

return @logs;
}

package Git::Wrapper::Exception;

sub new { my $class = shift; bless { @_ } => $class }
Expand All @@ -91,6 +130,27 @@ sub output { join "", map { "$_\n" } @{ shift->{output} } }
sub error { join "", map { "$_\n" } @{ shift->{error} } }
sub status { shift->{status} }

package Git::Wrapper::Log;

sub new {
my ($class, $id, %arg) = @_;
return bless {
id => $id,
attr => {},
%arg,
} => $class;
}

sub id { shift->{id} }

sub attr { shift->{attr} }

sub body { @_ > 1 ? ($_[0]->{body} = $_[1]) : $_[0]->{body} }

sub date { shift->attr->{date} }

sub author { shift->attr->{author} }

1;
__END__
Expand Down
10 changes: 10 additions & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use IO::File;
use Git::Wrapper;
use File::Spec;
use File::Path qw(mkpath);
use POSIX qw(strftime);
use Test::Deep;

my $dir = tempdir(CLEANUP => 1);

Expand All @@ -30,6 +32,7 @@ is_deeply(
);

$git->commit({ message => "FIRST" });
my $date = strftime("%a %b %d %H:%M:%S %Y %z", localtime);

my @rev_list =
$git->rev_list({ all => 1, quiet => 1, pretty => 'oneline' });
Expand All @@ -39,3 +42,10 @@ like($rev_list[0], qr/^[a-f\d]{40} FIRST$/);
eval { $git->no_such_command };
ok(my $e = $@, "got an error");
like($e, qr/'no-such-command' is not a git-command/);

my @log = $git->log;
is(@log, 1, 'one log entry');
my $log = $log[0];
is($log->id, (split /\s/, $rev_list[0])[0], 'id');
is($log->body, "FIRST\n", "body");
is($log->date, $date, "date");

0 comments on commit eed528b

Please sign in to comment.