Browse Source

Add structure for check opt

This commit adds internal logic to handle 2 different check modes,
commit and (eventually) message.  For now, the check commit mode
is assumed, without the option to define message.
add-jsconsole-commit-check
Blaine Motsinger 1 year ago
parent
commit
2fd1e5adfd
  1. 2
      .gitconfig.example
  2. 23
      bin/git-lint
  3. 23
      lib/Git/Lint.pm
  4. 28
      lib/Git/Lint/Config.pm

2
.gitconfig.example

@ -2,7 +2,7 @@
# the following can be added to ~/.gitconfig or /repo/.git/config
# settings here will override those set in Git::Lint::Config
[lint "profiles"]
[lint "profiles.commit"]
# create a whitespace profile with specific modules
whitespace = Whitespace, OtherModule

23
bin/git-lint

@ -13,6 +13,10 @@ use Git::Lint;
my $VERSION = '0.001';
my %opt = (
# TODO: until the 'message' check mode is implemented, default to 'commit.'
# until then, we're silently running check => commit and don't open that
# option to the user.
check => 'commit',
profile => 'default',
);
Getopt::Long::GetOptions(
@ -31,15 +35,20 @@ my $lint = Git::Lint->new();
$lint->run(\%opt);
if ( keys %{ $lint->{issues} } ) {
print STDERR "the following issues were found:\n\n";
print STDERR "the following issues were found in your ";
foreach my $filename ( keys %{ $lint->{issues} } ) {
print STDERR $filename . "\n";
if ($opt{check} eq 'commit') {
print STDERR "commit:\n\n";
foreach my $issue ( @{ $lint->{issues}{$filename} } ) {
print STDERR $issue . "\n";
foreach my $filename ( keys %{ $lint->{issues} } ) {
print STDERR $filename . "\n";
print STDERR $_ . "\n" foreach @{ $lint->{issues}{$filename} };
print STDERR "\n";
}
}
else {
print STDERR "message:\n\n";
print STDERR $_ . "\n" foreach @{ $lint->{issues} };
print STDERR "\n";
}
@ -70,7 +79,7 @@ C<git-lint> is a program to lint git commits.
=item --profile
Run a specific profile of checks.
Run a specific profile of check modules.
Defaults to the 'default' profile.

23
lib/Git/Lint.pm

@ -32,11 +32,14 @@ sub run {
my $opt = shift;
die 'git-lint: profile ' . $opt->{profile} . ' was not found' . "\n"
unless exists $self->config->{profiles}{ $opt->{profile} };
unless exists $self->config->{profiles}{ $opt->{check} }{ $opt->{profile} };
my $check = lc $opt->{check};
$check = ucfirst $check;
my @issues;
foreach my $check ( @{ $self->config->{profiles}{ $opt->{profile} } } ) {
my $class = 'Git::Lint::Check::Commit::' . $check;
foreach my $module ( @{ $self->config->{profiles}{ $opt->{check} }{ $opt->{profile} } } ) {
my $class = q{Git::Lint::Check::} . $check . q{::} . $module;
try {
$self->{_loader}->load($class);
}
@ -46,14 +49,24 @@ sub run {
};
my $plugin = $class->new();
# ensure the plugins don't manipulate the original input
# TODO: implement 'message' check mode
my $input = $class->diff();
# ensure the plugins don't manipulate the original input
my @lines = @{$input};
push @issues, $plugin->check( \@lines );
}
foreach my $issue (@issues) {
push @{ $self->{issues}{ $issue->{filename} } }, $issue->{message};
if ( $opt->{check} eq 'commit' ) {
push @{ $self->{issues}{ $issue->{filename} } }, $issue->{message};
}
else {
# TODO: the 'message' check mode isn't implemented yet, so the 'issue'
# data structure may not be accurate.
# until then, this is just a stub which won't be run.
push @{ $self->{issues} }, $issue->{message};
}
}
return;

28
lib/Git/Lint/Config.pm

@ -17,16 +17,20 @@ sub new {
my $namespace = 'Git::Lint::Check::Commit';
my @checks = List::MoreUtils::apply {s/$namespace\:\://g} $loader->find_modules( $namespace, { max_depth => 1 } );
my $self = { profiles => { default => \@checks } };
# TODO: load Message namespace
my $self = { profiles => { commit => { default => \@checks } } };
bless $self, $class;
my $user_config = $self->user_config();
# user defined profiles override default profiles
# user defined profiles override internally defined profiles
foreach my $cat ( keys %{$user_config} ) {
foreach my $key ( keys %{ $user_config->{$cat} } ) {
$self->{$cat}{$key} = $user_config->{$cat}{$key};
foreach my $check ( keys %{ $user_config->{$cat} } ) {
foreach my $profile ( keys %{ $user_config->{$cat}{$check} } ) {
$self->{$cat}{$check}{$profile} = $user_config->{$cat}{$check}{$profile};
}
}
}
@ -42,11 +46,11 @@ sub user_config {
my %parsed_config = ();
foreach my $line ( split( /\n/, $config_raw ) ) {
next unless $line =~ /^lint\.(\w+).(\w+)\s+(.+)$/;
my ( $cat, $key, $value ) = ( $1, $2, $3 );
next unless $line =~ /^lint\.(\w+).(\w+).(\w+)\s+(.+)$/;
my ( $cat, $check, $profile, $value ) = ( $1, $2, $3, $4 );
my @values = List::MoreUtils::apply {s/^\s+|\s+$//g} split( /,/, $value );
push @{ $parsed_config{$cat}{$key} }, @values;
push @{ $parsed_config{$cat}{$check}{$profile} }, @values;
}
return \%parsed_config;
@ -103,18 +107,18 @@ The C<Git::Lint::Config> object will contain the following keys:
=item profiles
The C<profiles> key by default contains one profile, C<default>, which contains all check modules.
The C<profiles> key by default contains one profile per C<check> mode, C<default>, which contains all check modules for that mode.
The C<default> profile can be overridden through C<git config>.
To set the default profile to only run the C<Whitespace> check:
To set the default profile for the commit check mode to only run the C<Whitespace> check:
[lint "profiles"]
[lint "profiles.commit"]
default = Whitespace
Or set the default profile to C<Whitespace> and the fictional check, C<Flipdoozler>:
Or set the default profile for the commit check mode to C<Whitespace> and the fictional check, C<Flipdoozler>:
[lint "profiles"]
[lint "profiles.commit"]
default = Whitespace, Flipdoozler
=back

Loading…
Cancel
Save