Advertisement
Guest User

pre-commit

a guest
Mar 12th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.78 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.10;
  4. use strict;
  5. use warnings;
  6.  
  7. use File::Basename;
  8. use Term::ANSIColor qw(colored);
  9.  
  10. $ENV{LOG} = "test";
  11.  
  12. my $nb_errors = 0;
  13. for my $filepath (`git diff --cached --name-only`) {
  14.     chomp $filepath;
  15.     next if not -f $filepath;
  16.  
  17.     my @file_infos = fileparse( $filepath, qr/\.[^.]*/ );
  18.  
  19.     if ( $file_infos[2] =~ /^.pl|^.pm$/ ) {
  20.         system(qq{/usr/bin/perl -wc $filepath}) == 0
  21.           or say "\n" and $nb_errors++;
  22.     }
  23.     elsif ( $file_infos[2] =~ /^.tt$/ ) {
  24.  
  25.         #TODO
  26.     }
  27.     elsif ( $file_infos[2] =~ /^.js$/ ) {
  28.  
  29.         #TODO
  30.     }
  31. }
  32.  
  33. my $filepath;
  34. for my $l ( split '\n', `git diff-index -p -M --cached HEAD` ) {
  35.     if ( $l =~ /^diff --git a\/([^ ]*) .*$/ ) {
  36.         $filepath = $1;
  37.     }
  38.     if ( $l =~ /console.log/ ) {
  39.         say colored( "$filepath contains console.log ($l)", 'red' );
  40.         $nb_errors++;
  41.     }
  42.     elsif ( $l =~ /^\+ *warn Data::Dumper::Dumper / ) {
  43.         say colored( "$filepath contains warn Data::Dumper::Dumper ($l)",
  44.             'red' );
  45.         $nb_errors++;
  46.     }
  47.  
  48.     # This one could be uncommented when Koha will have the Logger module
  49.     elsif ( $l =~ /^\+ *warn / ) {
  50.         say "$filepath contains warn ($l)";
  51.         $nb_errors++;
  52.     }
  53.     elsif ( $l =~ m/^<<<<<<</ or $l =~ m/^>>>>>>>/ or $l =~ m/^=======/ ) {
  54.         say colored( "$filepath contains $& ($l)", 'red' );
  55.     }
  56.     elsif ( $l =~ /(\+|-)\s*use/ ) {
  57.         say colored( "Patch add or remove a use in $filepath: $l", 'red' );
  58.         say
  59. "You should run xt/find-undefined-subroutines.pl to check if nothing is broken";
  60.         $nb_errors++;
  61.     }
  62. }
  63.  
  64. if ($nb_errors) {
  65.     say "\nAre you sure you want to commit ?";
  66.     say "You can commit with the --no-verify argument";
  67.     exit 1;
  68. }
  69. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement