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.
This commit is contained in:
Tom Willemse 2014-04-09 00:53:03 +02:00
parent efee9b6ce0
commit 74763c295b

View file

@ -24,6 +24,33 @@ first command-line argument. The style that's checked is based on
tpope's L<A Note About Git Commit tpope's L<A Note About Git Commit
Messages|http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html>. Messages|http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html>.
=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<commit-msg> 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. Currently there are 4 style errors that are checked for.
=over =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 All comments are skipped and so is all whitespace at the beginning of
the file. the file.
If any of the error conditions are encountered the program will exit If any of the error conditions are encountered, and the C<-0> option
with a non-C<0> status. This has the effect of stopping git from has not been given, the program will exit with a non-C<0> status. This
committing the message when this program is used as a C<commit-msg> has the effect of stopping git from committing the message when this
hook. Each error condition encountered also prints a message to the program is used as a C<commit-msg> hook. Each error condition
standard error stream. encountered also prints a message to the standard error stream.
=cut =cut
use strict; use strict;
use warnings; use warnings;
use Getopt::Std;
my $lineno = 0; my $lineno = 0;
my $status = 0; my $status = 0;
my %arguments = ();
sub err { sub err {
my ($msg) = @_; my ($msg) = @_;
@ -72,6 +101,21 @@ sub err {
$status = 1; $status = 1;
} }
sub usage {
my ($status) = @_;
print "Usage: commit-check [-0|-h] <file>\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]"; open(my $commitfile, "<", $ARGV[0]) or die "Couldn't open $ARGV[0]";
while (<$commitfile>) { while (<$commitfile>) {
@ -90,4 +134,5 @@ while (<$commitfile>) {
} }
close $commitfile; close $commitfile;
exit $status; exit $status if !$arguments{0};
exit 0;