File: Precommit.pm

package info (click to toggle)
libcode-tidyall-perl 0.84~ds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: perl: 5,244; lisp: 47; makefile: 2; sh: 1
file content (318 lines) | stat: -rw-r--r-- 8,712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package Code::TidyAll::Git::Precommit;

use strict;
use warnings;

use Capture::Tiny            qw(capture_stdout capture_stderr);
use Code::TidyAll::Git::Util qw(git_files_to_commit);
use Code::TidyAll;
use IPC::System::Simple qw(capturex run);
use Log::Any            qw($log);
use Path::Tiny          qw(path);
use Scope::Guard        qw(guard);
use Specio::Library::Builtins;
use Specio::Library::String;
use Try::Tiny;

use Moo;

our $VERSION = '0.84';

has conf_name => (
    is  => 'ro',
    isa => t('NonEmptyStr'),
);

has git_path => (
    is      => 'ro',
    isa     => t('NonEmptyStr'),
    default => 'git'
);

has no_stash => (
    is      => 'ro',
    isa     => t('Bool'),
    default => 0,
);

has tidyall_class => (
    is      => 'ro',
    isa     => t('ClassName'),
    default => 'Code::TidyAll'
);

has tidyall_options => (
    is      => 'ro',
    isa     => t('HashRef'),
    default => sub { {} }
);

sub check {
    my ( $class, %params ) = @_;

    my $fail_msg;

    try {
        my $self          = $class->new(%params);
        my $tidyall_class = $self->tidyall_class;

        # Find conf file at git root
        my $root_dir = capturex( $self->git_path, qw( rev-parse --show-toplevel ) );
        chomp($root_dir);
        $root_dir = path($root_dir);

        my @conf_names
            = $self->conf_name ? ( $self->conf_name ) : Code::TidyAll->default_conf_names;
        my ($conf_file) = grep { $_->is_file } map { $root_dir->child($_) } @conf_names
            or die sprintf( 'could not find conf file %s', join( ' or ', @conf_names ) );

        my $guard;
        unless ( $self->no_stash || $root_dir->child( '.git', 'MERGE_HEAD' )->exists ) {

            # We stash things to make sure that we only attempt to run tidyall
            # on changes in the index while ensuring that after the hook runs
            # the working directory is in the same state it was before the
            # commit.
            #
            # If there's nothing to stash there's no stash entry, in which
            # case popping would be very bad.
            my $pre_stash_state
                = capturex( [ 0, 1 ], $self->git_path, qw( rev-parse -q --verify refs/stash ) );
            run(
                $self->git_path, qw( stash save --keep-index --include-untracked ),
                'TidyAll pre-commit guard'
            );
            my $post_stash_state
                = capturex( [ 0, 1 ], $self->git_path, qw( rev-parse -q --verify refs/stash ) );
            unless ( $pre_stash_state eq $post_stash_state ) {
                $guard = guard {
                    my ($version) = capturex(qw( git version )) =~ /([0-9]+\.[0-9]+\.[0-9]+)/
                        or die 'Cannot determine version number from git version output!';
                    my $minor = ( split /\./, $version )[1];

                    # When pop is run quietly in 2.24.x it deletes files! See
                    # https://public-inbox.org/git/CAMcnqp22tEFva4vYHYLzY83JqDHGzDbDGoUod21Dhtnvv=h_Pg@mail.gmail.com/
                    # for the initial bug report. This was fixed in 2.25.
                    my @args = $minor == 24 ? () : ('-q');
                    run( $self->git_path, 'stash', 'pop', @args );
                }
            }
        }

        # Gather file paths to be committed
        my @files = git_files_to_commit($root_dir);

        my $tidyall = $tidyall_class->new_from_conf_file(
            $conf_file,
            no_cache   => 1,
            check_only => 1,
            mode       => 'commit',
            %{ $self->tidyall_options },
        );
        my @results = $tidyall->process_paths(@files);

        if ( my @error_results = grep { $_->error } @results ) {
            my $error_count = scalar(@error_results);
            $fail_msg = sprintf(
                "%d file%s did not pass tidyall check\n",
                $error_count, $error_count > 1 ? 's' : q{}
            );
        }
    }
    catch {
        my $error = $_;
        die "Error during pre-commit hook (use --no-verify to skip hook):\n$error";
    };
    die "$fail_msg\n" if $fail_msg;
}

1;

# ABSTRACT: Git pre-commit hook that requires files to be tidyall'd

__END__

=pod

=encoding UTF-8

=head1 NAME

Code::TidyAll::Git::Precommit - Git pre-commit hook that requires files to be tidyall'd

=head1 VERSION

version 0.84

=head1 SYNOPSIS

  In .git/hooks/pre-commit:

    #!/usr/bin/env perl
    use strict;
    use warnings;

    use Code::TidyAll::Git::Precommit;
    Code::TidyAll::Git::Precommit->check();

=head1 DESCRIPTION

This module implements a L<Git pre-commit
hook|http://git-scm.com/book/en/Customizing-Git-Git-Hooks> that checks if all
files are tidied and valid according to L<tidyall>, and rejects the commit if
not. Files/commits are never modified by this hook.

See also L<Code::TidyAll::Git::Prereceive>, which validates pushes to a shared
repo.

The tidyall configuration file (F<tidyall.ini> or F<.tidyallrc>) must be
checked into git in the repo root directory i.e. next to the .git directory.

By default, the hook will stash any changes not in the index beforehand, and
restore them afterwards, via

    git stash save --keep-index --include-untracked
    ....
    git stash pop

This means that if the configuration file has uncommitted changes that are not
in the index, they will not affect the tidyall run.

=head1 METHODS

This class provides one method:

=head2 Code::TidyAll::Git::Precommit->check(%params)

Checks that all files being added or modified in this commit are tidied and
valid according to L<tidyall>. If not, then the entire commit is rejected and
the reason(s) are output to the client. e.g.

    % git commit -m "fixups" CHI.pm CHI/Driver.pm
    2 files did not pass tidyall check
    lib/CHI.pm: *** 'PerlTidy': needs tidying
    lib/CHI/Driver.pm: *** 'PerlCritic': Code before strictures are enabled
      at /tmp/Code-TidyAll-0e6K/Driver.pm line 2
      [TestingAndDebugging::RequireUseStrict]

In an emergency the hook can be bypassed by passing --no-verify to commit:

    % git commit --no-verify ...

or you can just move F<.git/hooks/pre-commit> out of the way temporarily.

This class passes mode = "commit" by default to tidyall; see
L<modes|tidyall/MODES>.

Key/value parameters:

=over 4

=item * conf_name

A conf file name to search for instead of the defaults.

=item * git_path

Path to git to use in commands, e.g. '/usr/bin/git' or '/usr/local/bin/git'. By
default, it just uses 'git', which will search the user's C<PATH>.

=item * no_stash

Don't attempt to stash changes not in the index. This means the hook will
process files that are not going to be committed.

=item * tidyall_class

Subclass to use instead of L<Code::TidyAll>.

=item * tidyall_options

A hashref of options to pass to the L<Code::TidyAll> constructor.

=back

=head1 USING AND (NOT) ENFORCING THIS HOOK

This hook must be placed manually in each copy of the repo - there is no way to
automatically distribute or enforce it. However, you can make things easier on
yourself or your developers as follows:

=over

=item *

Create a directory called F<git/hooks> at the top of your repo (note no dot
prefix).

    mkdir -p git/hooks

=item *

Commit your pre-commit script in F<git/hooks/pre-commit> containing:

    #!/usr/bin/env perl

    use strict;
    use warnings;

    use Code::TidyAll::Git::Precommit;
    Code::TidyAll::Git::Precommit->check();

=item *

Add a setup script in F<git/setup.sh> containing

    #!/bin/bash
    chmod +x git/hooks/pre-commit
    cd .git/hooks
    ln -s ../../git/hooks/pre-commit

=item *

Run C<git/setup.sh> (or tell your developers to run it) once for each new clone
of the repo

=back

See L<this Stack Overflow
question||http://stackoverflow.com/questions/3703159/git-remote-shared-pre-commit-hook>
for more information on pre-commit hooks and the impossibility of enforcing
their use.

See also L<Code::TidyAll::Git::Prereceive>, which enforces tidyall on pushes to
a remote shared repository.

=head1 SUPPORT

Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.

=head1 SOURCE

The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.

=head1 AUTHORS

=over 4

=item *

Jonathan Swartz <swartz@pobox.com>

=item *

Dave Rolsky <autarch@urth.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2011 - 2023 by Jonathan Swartz.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

The full text of the license can be found in the
F<LICENSE> file included with this distribution.

=cut