[Bps-public-commit] App-Changeloggr branch, master, updated. 31f036518f28dbf040a04f74ad4b49468aaf984c

Alex M Vandiver alexmv at bestpractical.com
Mon Mar 30 15:29:15 EDT 2009


The branch, master has been updated
       via  31f036518f28dbf040a04f74ad4b49468aaf984c (commit)
      from  5a0fa5ad6a44c5fb51dd1dda222adda8ad3c7aa2 (commit)

Summary of changes:
 lib/App/Changeloggr.pm                        |   14 +++
 lib/App/Changeloggr/Action/AddChanges.pm      |   12 +++
 lib/App/Changeloggr/LogFormat.pm              |   32 +++++++
 lib/App/Changeloggr/LogFormat/Git.pm          |   90 ++++++++++++++++++++
 lib/App/Changeloggr/Model/ChangeCollection.pm |  112 +------------------------
 5 files changed, 150 insertions(+), 110 deletions(-)
 create mode 100644 lib/App/Changeloggr/LogFormat.pm
 create mode 100644 lib/App/Changeloggr/LogFormat/Git.pm

- Log -----------------------------------------------------------------
commit 31f036518f28dbf040a04f74ad4b49468aaf984c
Author: Alex Vandiver <alexmv at mit.edu>
Date:   Mon Mar 30 15:25:34 2009 -0400

    Move to an iterator for extracting log entries, for extensibility

diff --git a/lib/App/Changeloggr.pm b/lib/App/Changeloggr.pm
index 58e22ba..fd60632 100644
--- a/lib/App/Changeloggr.pm
+++ b/lib/App/Changeloggr.pm
@@ -2,5 +2,19 @@ package App::Changeloggr;
 use strict;
 use warnings;
 
+use App::Changeloggr::LogFormat;
+
+sub start {
+    my $class = shift;
+
+    # Find all log format parsers
+    Jifty::Module::Pluggable->import(
+        search_path => 'App::Changeloggr::LogFormat',
+        require     => 1,
+        inner       => 0,
+        sub_name    => 'log_formats',
+    );
+}
+
 1;
 
diff --git a/lib/App/Changeloggr/Action/AddChanges.pm b/lib/App/Changeloggr/Action/AddChanges.pm
index 3900d06..123b6b6 100644
--- a/lib/App/Changeloggr/Action/AddChanges.pm
+++ b/lib/App/Changeloggr/Action/AddChanges.pm
@@ -41,6 +41,18 @@ sub validate_admin_token {
     }
 }
 
+sub validate_changes {
+    my $self = shift;
+    my $text = shift;
+
+    my $parser = App::Changeloggr::LogFormat->new( text => $text );
+    if ( $parser ) {
+        return $self->validation_ok( 'changes' );
+    } else {
+        return $self->validation_error( changes => "That doesn't look like a log format we recognize." );
+    }
+}
+
 sub take_action {
     my $self = shift;
     my $changelog = $self->get_changelog;
diff --git a/lib/App/Changeloggr/LogFormat.pm b/lib/App/Changeloggr/LogFormat.pm
new file mode 100644
index 0000000..3feefab
--- /dev/null
+++ b/lib/App/Changeloggr/LogFormat.pm
@@ -0,0 +1,32 @@
+package App::Changeloggr::LogFormat;
+use strict;
+use warnings;
+
+sub new {
+    my $class = shift;
+    my %args = @_;
+    return unless $args{text};
+
+    $args{text} =~ s/^\s+//;
+    $args{text} =~ s/\r\n/\n/g;
+
+    if ($class eq "App::Changeloggr::LogFormat") {
+        for my $format (App::Changeloggr->log_formats) {
+            return $format->new( @_ ) if $format->matches( @_ );
+        }
+        return undef;
+    }
+
+    return bless \%args, $class;
+}
+
+sub matches {
+    my $class = shift;
+    return 0;
+}
+
+sub next_match {
+    return undef;
+}
+
+1;
diff --git a/lib/App/Changeloggr/LogFormat/Git.pm b/lib/App/Changeloggr/LogFormat/Git.pm
new file mode 100644
index 0000000..1d0d40d
--- /dev/null
+++ b/lib/App/Changeloggr/LogFormat/Git.pm
@@ -0,0 +1,90 @@
+package App::Changeloggr::LogFormat::Git;
+use base qw/App::Changeloggr::LogFormat/;
+use strict;
+use warnings;
+
+use DateTime::Format::Strptime;
+use constant DATE_PARSER => DateTime::Format::Strptime->new(
+    pattern => '%a %b %d %T %Y %z',
+);
+
+sub matches {
+    my $self = shift;
+    my %args = @_;
+
+    return $args{text} =~ /^commit \w+\r?\n/;
+}
+    
+sub next_match {
+    my $self = shift;
+
+    # git log --format=fuller --stat
+    $self->{text} =~ s{
+        \A
+        (
+            ^ commit \  \w+ \n
+            .*?
+        )
+        (?=
+            \Z
+            |
+            ^ commit \  \w+ \n
+        )
+    }{}xms;
+
+    my $entry = $1
+        or return;
+    my %fields;
+
+    $fields{raw} = $entry;
+
+    if ($entry =~ /^commit (.*)$/im) {
+        $fields{identifier} = $1;
+    }
+    if ($entry =~ /^Author:\s*(.*)$/im) {
+        $fields{author} = $1;
+    }
+    if ($entry =~ /^(?:Author)?Date:\s*(.*)$/im) {
+        $fields{date} = DATE_PARSER->parse_datetime($1);
+    }
+    # We don't have these columns in the database yet
+#    if ($entry =~ /^Commit:\s*(.*)$/im) {
+#        $fields{commit} = $1;
+#    }
+#    if ($entry =~ /^CommitDate:\s*(.*)$/im) {
+#        $fields{commit_date} = $1;
+#    }
+
+    if ($entry =~ /.*?^(\s{4}.*?)(^\s{1,2}\S+\s+\|\s+\d+|\z)/ims) {
+        $fields{message} = $1;
+    }
+    if ($entry =~ /\n(\s{1,2}\S+\s+\|\s+\d+.*)$/ims) {
+        $fields{diff} = $1;
+    }
+
+    return \%fields;
+}
+
+=begin git-sample
+
+(this is produced by git log --format=fuller --stat)
+
+commit 8837a66df7e8959d3101a5227d7b3c597990c0d0
+Author:     Nicholas Clark <nick at ccl4.org>
+AuthorDate: Tue Dec 2 20:16:33 2008 +0000
+Commit:     David Mitchell <davem at iabyn.com>
+CommitDate: Wed Jan 28 00:05:55 2009 +0000
+
+    Codify the current behaviour of evals which define subroutines before
+    failing (due to syntax errors).
+    
+    p4raw-id: //depot/perl at 34984
+    
+    (cherry picked from commit 99d3381e871dbd1d94b47516b4475d85b3935ac6)
+
+ t/comp/retainedlines.t |   23 ++++++++++++++++++++++-
+ 1 files changed, 22 insertions(+), 1 deletions(-)
+
+=cut
+
+1;
diff --git a/lib/App/Changeloggr/Model/ChangeCollection.pm b/lib/App/Changeloggr/Model/ChangeCollection.pm
index cbec04f..81fff28 100644
--- a/lib/App/Changeloggr/Model/ChangeCollection.pm
+++ b/lib/App/Changeloggr/Model/ChangeCollection.pm
@@ -5,10 +5,6 @@ use base 'App::Changeloggr::Collection';
 use Params::Validate qw(validate SCALAR);
 use DateTime::Format::Strptime;
 
-my $gitdate = DateTime::Format::Strptime->new(
-    pattern => '%a %b %d %T %Y %z',
-);
-
 sub create_from_text {
     my $self = shift;
     my %args = validate(@_, {
@@ -19,10 +15,9 @@ sub create_from_text {
     my $text      = $args{text};
     my $changelog = $args{changelog};
 
-    while ($text =~ /\S/) {
-        my ($fields, $newtext) = $self->extract_change_data_from_text($text);
-        last if !defined($newtext);
+    my $parser = App::Changeloggr::LogFormat->new( text => $text );
 
+    while (my $fields = $parser->next_match) {
         my $change = App::Changeloggr::Model::Change->new;
 
         my ($ok, $msg) = $change->create(
@@ -36,111 +31,8 @@ sub create_from_text {
         else {
             warn "Unable to create Change: $msg";
         }
-
-        $text = $newtext;
     }
-
-    return $text;
-}
-
-sub identify_format {
-    my $self = shift;
-    my $text = shift;
-
-    if ($text =~ /^commit \w+\r?\n/) {
-        return 'git';
-    }
-
-    return;
 }
 
-sub extract_change_data_from_text {
-    my $self = shift;
-    my $text = shift;
-
-    $text =~ s/^\s+//;
-    $text =~ s/\r\n/\n/g;
-
-    my $format = $self->identify_format($text);
-    die "I'm unable to handle the change text format: " . substr($text, 0, 30) . '...'
-        if !defined($format);
-
-    my $extract_method = "extract_change_data_from_$format";
-    return $self->$extract_method($text);
-}
-
-sub extract_change_data_from_git {
-    my $self = shift;
-    my $text = shift;
-
-    # git log --format=fuller --stat
-    $text =~ s{
-        \A
-        (
-            ^ commit \  \w+ \n
-            .*?
-        )
-        (?=
-            \Z
-            |
-            ^ commit \  \w+ \n
-        )
-    }{}xms;
-
-    my $entry = $1
-        or return;
-    my %fields;
-
-    $fields{raw} = $entry;
-
-    if ($entry =~ /^commit (.*)$/im) {
-        $fields{identifier} = $1;
-    }
-    if ($entry =~ /^Author:\s*(.*)$/im) {
-        $fields{author} = $1;
-    }
-    if ($entry =~ /^(?:Author)?Date:\s*(.*)$/im) {
-        $fields{date} = $gitdate->parse_datetime($1);
-    }
-    # We don't have these columns in the database yet
-#    if ($entry =~ /^Commit:\s*(.*)$/im) {
-#        $fields{commit} = $1;
-#    }
-#    if ($entry =~ /^CommitDate:\s*(.*)$/im) {
-#        $fields{commit_date} = $1;
-#    }
-
-    if ($entry =~ /.*?^(\s{4}.*?)(^\s{1,2}\S+\s+\|\s+\d+|\z)/ims) {
-        $fields{message} = $1;
-    }
-    if ($entry =~ /\n(\s{1,2}\S+\s+\|\s+\d+.*)$/ims) {
-        $fields{diff} = $1;
-    }
-
-    return (\%fields, $text);
-}
-
-=begin git-sample
-
-(this is produced by git log --format=fuller --stat)
-
-commit 8837a66df7e8959d3101a5227d7b3c597990c0d0
-Author:     Nicholas Clark <nick at ccl4.org>
-AuthorDate: Tue Dec 2 20:16:33 2008 +0000
-Commit:     David Mitchell <davem at iabyn.com>
-CommitDate: Wed Jan 28 00:05:55 2009 +0000
-
-    Codify the current behaviour of evals which define subroutines before
-    failing (due to syntax errors).
-    
-    p4raw-id: //depot/perl at 34984
-    
-    (cherry picked from commit 99d3381e871dbd1d94b47516b4475d85b3935ac6)
-
- t/comp/retainedlines.t |   23 ++++++++++++++++++++++-
- 1 files changed, 22 insertions(+), 1 deletions(-)
-
-=cut
-
 1;
 

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list