[libcode-tidyall-perl] 292/374: Make git prereceive hook more modular, so they can be combined with custom checks

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:26:37 UTC 2013


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libcode-tidyall-perl.

commit f2e3b870192ca4ceeb68fd72e4ffa9aa4f921e2c
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Tue Oct 2 13:53:47 2012 -0700

    Make git prereceive hook more modular, so they can be combined with custom checks
---
 Changes                            |    1 +
 lib/Code/TidyAll/Git/Prereceive.pm |   98 +++++++++++++++++++++++++-----------
 2 files changed, 70 insertions(+), 29 deletions(-)

diff --git a/Changes b/Changes
index 1852dc5..da3e34e 100644
--- a/Changes
+++ b/Changes
@@ -7,6 +7,7 @@ Revision history for Code-TidyAll
 * Improvements
   - Add -r/--recursive flag to process directories recursively
   - Add --version
+  - Make git prereceive hook more modular, so they can be combined with custom checks
 
 0.13  Sep 30, 2012
 
diff --git a/lib/Code/TidyAll/Git/Prereceive.pm b/lib/Code/TidyAll/Git/Prereceive.pm
index 2e65d55..8454035 100644
--- a/lib/Code/TidyAll/Git/Prereceive.pm
+++ b/lib/Code/TidyAll/Git/Prereceive.pm
@@ -20,39 +20,14 @@ sub check {
     my ( $class, %params ) = @_;
 
     my $fail_msg;
-
     try {
         my $self = $class->new(%params);
 
         my $root_dir = realpath();
         local $ENV{GIT_DIR} = $root_dir;
 
-        my ( @results, $tidyall );
         my $input = do { local $/; <STDIN> };
-        my @lines = split( "\n", $input );
-        foreach my $line (@lines) {
-            chomp($line);
-            my ( $base, $commit, $ref ) = split( /\s+/, $line );
-            next unless $ref eq 'refs/heads/master';
-
-            # Create tidyall using configuration found in first commit
-            #
-            $tidyall ||= $self->create_tidyall($commit);
-
-            my @files = $self->get_changed_files( $base, $commit );
-            foreach my $file (@files) {
-                my $contents = $self->get_file_contents( $file, $commit );
-                push( @results, $tidyall->process_source( $contents, $file ) );
-            }
-        }
-
-        if ( my @error_results = grep { $_->error } @results ) {
-            unless ( $self->check_repeated_push($input) ) {
-                my $error_count = scalar(@error_results);
-                $fail_msg = sprintf( "%d file%s did not pass tidyall check",
-                    $error_count, $error_count > 1 ? "s" : "" );
-            }
-        }
+        $fail_msg = $self->check_input($input);
     }
     catch {
         my $error = $_;
@@ -66,6 +41,38 @@ sub check {
     die "$fail_msg\n" if $fail_msg;
 }
 
+sub check_input {
+    my ( $self, $input ) = @_;
+
+    my @lines = split( "\n", $input );
+    my ( @results, $tidyall );
+    foreach my $line (@lines) {
+        chomp($line);
+        my ( $base, $commit, $ref ) = split( /\s+/, $line );
+        next unless $ref eq 'refs/heads/master';
+
+        # Create tidyall using configuration found in first commit
+        #
+        $tidyall ||= $self->create_tidyall($commit);
+
+        my @files = $self->get_changed_files( $base, $commit );
+        foreach my $file (@files) {
+            my $contents = $self->get_file_contents( $file, $commit );
+            push( @results, $tidyall->process_source( $contents, $file ) );
+        }
+    }
+
+    my $fail_msg;
+    if ( my @error_results = grep { $_->error } @results ) {
+        unless ( $self->check_repeated_push($input) ) {
+            my $error_count = scalar(@error_results);
+            $fail_msg = sprintf( "%d file%s did not pass tidyall check",
+                $error_count, $error_count > 1 ? "s" : "" );
+        }
+    }
+    return $fail_msg;
+}
+
 sub create_tidyall {
     my ( $self, $commit ) = @_;
 
@@ -152,6 +159,19 @@ tidyall'd
     
     Code::TidyAll::Git::Prereceive->check();
 
+
+    # or
+
+    my $input = do { local $/; <STDIN> };
+
+    # Do other things with $input here
+
+    my $prereceive = Code::TidyAll::Git::Prereceive->new();
+    if (my $error = $prereceive->check_input($input)) {
+        die $error;
+    }
+
+
 =head1 DESCRIPTION
 
 This module implements a L<Git pre-receive
@@ -171,9 +191,10 @@ operates locally.
 
 =item check (key/value params...)
 
-Class method. Check that all files being added or modified in this push are
-tidied and valid according to L<tidyall|tidyall>. If not, then the entire push
-is rejected and the reason(s) are output to the client. e.g.
+An all-in-one class method. Reads commit info from standard input, then checks
+that all files being added or modified in this push are tidied and valid
+according to L<tidyall|tidyall>. If not, then the entire push is rejected and
+the reason(s) are output to the client. e.g.
 
     % git push
     Counting objects: 9, done.
@@ -208,6 +229,9 @@ commits 3 consecutive times (configurable via L</allow_repeated_push>):
 Or you can disable the hook in the repo being pushed to, e.g. by renaming
 .git/hooks/pre-receive.
 
+If an unexpected runtime error occurs, it is reported but by default the commit
+will be allowed through (see L</reject_on_error>).
+
 Passes mode = "commit" by default; see L<modes|tidyall/MODES>.
 
 Key/value parameters:
@@ -240,6 +264,11 @@ perlcriticrc' when the hook runs.
 Path to git to use in commands, e.g. '/usr/bin/git' or '/usr/local/bin/git'. By
 default, just uses 'git', which will search the user's PATH.
 
+=item reject_on_error
+
+Whether C<check()> should reject the commit when an unexpected runtime error
+occurs. By default, the error will be reported but the commit will be allowed.
+
 =item tidyall_class
 
 Subclass to use instead of L<Code::TidyAll|Code::TidyAll>
@@ -256,6 +285,17 @@ or pass additional options.
 
 =back
 
+=item new (key/value params...)
+
+Constructor. Takes the same parameters documented in check(), above, and
+returns a new object which you can then call L</check_input> on.
+
+=item check_input (input)
+
+Run a check on I<input>, the text block of lines that came from standard input.
+You can call this manually before or after you do other processing on the
+input. Returns an error string if there was a problem, undef if no problems.
+
 =back
 
 =cut

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcode-tidyall-perl.git



More information about the Pkg-perl-cvs-commits mailing list