App-GitHooks

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

Prevent committing code with #NOCOMMIT mentions.

 * [App::GitHooks::Plugin::BlockProductionCommits]
   (https://metacpan.org/pod/App::GitHooks::Plugin::BlockProductionCommits)

Prevent commits in a production environment.

 * [App::GitHooks::Plugin::DetectCommitNoVerify]
   (https://metacpan.org/pod/App::GitHooks::Plugin::DetectCommitNoVerify)

Find out when someone uses --no-verify and append the pre-commit checks to the
commit message.

 * [App::GitHooks::Plugin::ForceRegularUpdate]
   (https://metacpan.org/pod/App::GitHooks::Plugin::ForceRegularUpdate)

Force running a specific tool at regular intervals.

 * [App::GitHooks::Plugin::MatchBranchTicketID]
   (https://metacpan.org/pod/App::GitHooks::Plugin::MatchBranchTicketID)

lib/App/GitHooks.pm  view on Meta::CPAN

=item * L<App::GitHooks::Plugin::BlockNOCOMMIT>

Prevent committing code with #NOCOMMIT mentions.

=item * L<App::GitHooks::Plugin::BlockProductionCommits>

Prevent commits in a production environment.

=item * L<App::GitHooks::Plugin::DetectCommitNoVerify>

Find out when someone uses --no-verify and append the pre-commit checks to the
commit message.

=item * L<App::GitHooks::Plugin::ForceRegularUpdate>

Force running a specific tool at regular intervals.

=item * L<App::GitHooks::Plugin::MatchBranchTicketID>

Detect discrepancies between the ticket ID specified by the branch name and the
one in the commit message.

lib/App/GitHooks/Hook/PreCommit.pm  view on Meta::CPAN

	my $config = $app->get_config();
	my $force_interactive = $config->get( 'testing', 'force_interactive' );
	return $HOOK_EXIT_SUCCESS
		if !$app->get_terminal()->is_interactive() && !$force_interactive;

	# Run the checks on the staged changes.
	my $checks_pass = run_all_tests( $app );

	# If the checks passed, write a file for the prepare-commit-msg hook to know
	# that we've already run the checks and there's no need to do it a second time.
	# This is what allows it to detect when --no-verify was used.
	if ( $checks_pass )
	{
		Path::Tiny::path( '.git', 'COMMIT-MSG-CHECKS' )
			->spew( $checks_pass );
	}

	# Indicate if we should allow continuing to the commit message or not.
	return $checks_pass
		? $HOOK_EXIT_SUCCESS
		: $HOOK_EXIT_FAILURE;

lib/App/GitHooks/Hook/PreCommit.pm  view on Meta::CPAN


		$has_warnings = 1
			if $check_result == $PLUGIN_RETURN_WARNED;
	}

	# Check the changed files individually with plugins that support
	# "pre-commit-file".
	{
		my ( $file_checks, $file_warnings ) = $app
			->get_staged_changes()
			->verify();
		$tests_success = 0
			if !$file_checks;
		$has_warnings = 1
			if $file_warnings;
	}

	# If warnings were found, notify users.
	if ( $has_warnings )
	{
		# If we have a user, stop and ask if we should continue with the commit.

lib/App/GitHooks/Hook/PrepareCommitMsg.pm  view on Meta::CPAN

	{
		my $terminal = $app->get_terminal();
		my $message = $commit_message->get_message();
		my $file = Path::Tiny::path( $commit_message_file );
		$terminal->is_utf8()
			? $file->spew_utf8( $message )
			: $file->spew( $message );
	}

	# .git/COMMIT-MSG-CHECKS is a file we use to track if the pre-commit hook has
	# run, as opposed to being skipped with --no-verify. Since pre-commit can be
	# skipped, but prepare-commit-msg cannot, plugins can use the presence of
	# that file to determine if some optional processing should be performed in
	# the prepare-commit-msg phase. For example, you may want to add a warning
	# indicating that --no-verify was used. Note however that the githooks man
	# page says "it should not be used as replacement for pre-commit hook".
	#
	# And since we're done with prepare-commit-msg checks now, we can safely
	# remove the file.
	unlink( '.git/COMMIT-MSG-CHECKS' );

	return $tests_success
		? $HOOK_EXIT_SUCCESS
		: $HOOK_EXIT_FAILURE;
}

lib/App/GitHooks/StagedChanges.pm  view on Meta::CPAN

=cut

sub get_app
{
	my ( $self ) = @_;

	return $self->{'app'};
}


=head2 verify()

Verify the changes that are being committed.

This method returns an array composed of:

=over 4

=item * A boolean to indicate whether the checks passed or failed.

=item * A boolean to indicate whether any warnings were displayed.

=back

	( $allow_commit, $has_warnings ) = $staged_changes->verify();

=cut

sub verify
{
	my ( $self, %args ) = @_;
	croak 'Invalid argument(s): ' . join( ', ', keys %args )
		if scalar( keys %args ) != 0;

	$self->analyze_changes();

	# Check the changed files.
	return $self->check_changed_files();
}

t/45-Hook/CommitMsg/10-run.t  view on Meta::CPAN


use strict;
use warnings;

use App::GitHooks::Constants qw( :HOOK_EXIT_CODES );
use App::GitHooks::Test;


# Since git ignores the return value of the hook, we can't test using the exit
# code here. Instead, we use the App::GitHooks::Plugin::Test::PrintSTDERR,
# which allows us to verify that the hook was properly triggered and that
# plugins for that hook are loaded correctly.

# List of tests to perform.
my $tests =
[
	{
		name        => 'Trigger plugins for commit-msg.',
		config      => "[Test::PrintSTDERR]\n"
			. "commit_msg = Triggered commit-msg.\n",
		expected    => qr/Triggered commit-msg./,

t/45-Hook/PrepareCommitMsg/10-run.t  view on Meta::CPAN


use strict;
use warnings;

use App::GitHooks::Constants qw( :HOOK_EXIT_CODES );
use App::GitHooks::Test;


# Since git ignores the return value of the prepare-commit-msg hook, we can't
# test using the exit code here. Instead, we use the
# App::GitHooks::Plugin::Test::PrintSTDERR, which allows us to verify that the
# hook was properly triggered and that plugins for that hook are loaded
# correctly.

# List of tests to perform.
my $tests =
[
	{
		name        => 'Trigger plugins for prepare-commit-msg.',
		config      => "[Test::PrintSTDERR]\n"
			. "prepare_commit_msg = Triggered prepare-commit-msg.\n",



( run in 0.397 second using v1.01-cache-2.11-cpan-73692580452 )