From 74763c295b5f8cc4f9b5864bb2bba832bc040e74 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Wed, 9 Apr 2014 00:53:03 +0200 Subject: Add -h and -0 command-line options Add the `-0' option so that a flycheck checker can be written. These don't seem to like it when the checker exits with a non-zero status, whereas for the purposes of being a `commit-msg' hook for git it is important that it does exit with a non-zero status to stop git from committing. --- commit-check | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/commit-check b/commit-check index c788036..ae80804 100755 --- a/commit-check +++ b/commit-check @@ -24,6 +24,33 @@ first command-line argument. The style that's checked is based on tpope's L. +=head2 Command-line options + +There are 2 command line options available for use with this program: + +=over + +=item -0 + +Don't return a non-zero status when errors have been found. For use as +a git C hook it is important that it does exit with a +non-zero status. But if you are using it for other tools, such as for +a flycheck checker, exiting with a non-zero status might confuse the +tools calling it. + +=item -h + +Show a little help text that describes the command line options and +usage. + +=back + +When no arguments are passed (not even the file name) the help text is +also shown and the exit status is non-0 (no matter if C<-0> has been +given). + +=head2 Errors + Currently there are 4 style errors that are checked for. =over @@ -51,19 +78,21 @@ No line should be longer than 72 characters. All comments are skipped and so is all whitespace at the beginning of the file. -If any of the error conditions are encountered the program will exit -with a non-C<0> status. This has the effect of stopping git from -committing the message when this program is used as a C -hook. Each error condition encountered also prints a message to the -standard error stream. +If any of the error conditions are encountered, and the C<-0> option +has not been given, the program will exit with a non-C<0> status. This +has the effect of stopping git from committing the message when this +program is used as a C hook. Each error condition +encountered also prints a message to the standard error stream. =cut use strict; use warnings; +use Getopt::Std; my $lineno = 0; my $status = 0; +my %arguments = (); sub err { my ($msg) = @_; @@ -72,6 +101,21 @@ sub err { $status = 1; } +sub usage { + my ($status) = @_; + print "Usage: commit-check [-0|-h] \n" + . "\n" + . "Accepted arguments:\n" + . " -0 Always return 0 exit-status, regardless of errors found.\n" + . " -h Show this help text.\n"; + exit $status; +} + +getopts("0h", \%arguments); + +usage 0 if $arguments{h}; +usage 1 if !$ARGV[0]; + open(my $commitfile, "<", $ARGV[0]) or die "Couldn't open $ARGV[0]"; while (<$commitfile>) { @@ -90,4 +134,5 @@ while (<$commitfile>) { } close $commitfile; -exit $status; +exit $status if !$arguments{0}; +exit 0; -- cgit v1.2.3-54-g00ecf