| 1 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Git::Hooks::PrepareLog; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Git::Hooks plugin to prepare commit messages before being edited | ||||||||||||||||||||||||||||
| 5 | 6 | $Git::Hooks::PrepareLog::VERSION = '4.0.0'; | |||||||||||||||||||||||||||
| 6 | 6 | ▶ | use v5.30.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Log::Any '$log'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Git::Hooks; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | my $CFG = __PACKAGE__ =~ s/.*::/githooks./r; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | ############# | ||||||||||||||||||||||||||||
| 15 | # Grok hook configuration, check it and set defaults. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | sub _setup_config { | ||||||||||||||||||||||||||
|
Subroutine
_setup_config: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 18 | 6 | my ($git) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 6 | my $config = $git->get_config(); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | ▶ | $config->{lc $CFG} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$$config{lc $CFG} //= {}
MC/DC100%1/1
$$config{lc $CFG} //= {}
$$config{lc $CFG}
Truth table100%2/2
A
A= $$config{lc $CFG}
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 6 | my $default = $config->{lc $CFG}; | |||||||||||||||||||||||||||
| 25 | 6 | ▶ | $default->{'issue-place'} //= ['title [%I] %T']; | ||||||||||||||||||||||||||
|
Condition100%2/2
$default->{"issue-place"} //= ["title [%I] %T"]
MC/DC100%1/1
$default->{"issue-place"} //= ["title [%I] %T"]
$default->{"issue-place"}
Truth table100%2/2
A
A= $default->{"issue-place"}
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | return; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | ########## | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | sub insert_issue_in_title { | ||||||||||||||||||||||||||
|
Subroutine
insert_issue_in_title: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 3 | my ($git, $msg_file, $issue, $format) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | my $encoding = $git->get_config(i18n => 'commitEncoding') || 'utf-8'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
MC/DC0%0/1
$git->get_config('i18n', 'commitEncoding') || 'utf-8'
$git->get_config('i18n', 'commitEncoding')
Truth table50%1/2
A
A= $git->get_config('i18n', 'commitEncoding')
| |||||||||||||||||||||||||||||
| 36 | 3 | my @lines = path($msg_file)->lines({binmode => ":encoding($encoding)"}); | |||||||||||||||||||||||||||
| 37 | # The message title is the first line after comments | ||||||||||||||||||||||||||||
| 38 | 3 | foreach (@lines) { | |||||||||||||||||||||||||||
| 39 | 3 | ▶ | next if /^\s*#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 3 | $format =~ s/\%T/$_/; | |||||||||||||||||||||||||||
| 41 | 3 | $format =~ s/\%I/$issue/; | |||||||||||||||||||||||||||
| 42 | 3 | $_ = $format . "\n"; | |||||||||||||||||||||||||||
| 43 | 3 | last; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 3 | path($msg_file)->spew({binmode => ":encoding($encoding)"}, @lines); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | return; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub insert_issue_as_trailer { | ||||||||||||||||||||||||||
|
Subroutine
insert_issue_as_trailer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 1 | my ($git, $msg_file, $issue, $key) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | $key = ucfirst lc $key; | |||||||||||||||||||||||||||
| 54 | 1 | $git->run(qw/interpret-trailers --in-place --trailer/, "$key:$issue", $msg_file); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | return; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 6 | ▶ | sub insert_issue { | ||||||||||||||||||||||||||
|
Subroutine
insert_issue: called CC=9 SCAR=23.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 6 | my ($git, $msg_file) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Continue only if we have a pattern to match against branches | ||||||||||||||||||||||||||||
| 63 | 6 | ▶ | my $issue_branch_regex = $git->get_config($CFG => 'issue-branch-regex') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | or return 0; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 5 | my $branch_rx = eval { qr:(?p)\brefs/heads/\K$issue_branch_regex\b: }; | |||||||||||||||||||||||||||
| 67 | 5 | ▶ | unless (defined $branch_rx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $git->fault(<<"EOS", {option => 'issue-branch-regex', details => $@}); | |||||||||||||||||||||||||||
| 69 | Configuration error: the option must be a valid regular expression, but | ||||||||||||||||||||||||||||
| 70 | '$issue_branch_regex' isn't. Please, fix your configuration and try again. | ||||||||||||||||||||||||||||
| 71 | EOS | ||||||||||||||||||||||||||||
| 72 | 0 | return 1; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Continue only if we are in a named branch | ||||||||||||||||||||||||||||
| 76 | 5 | ▶ | my $branch = $git->get_current_branch | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | or return 0; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Try to grok the issue id from the current branch name. Do not continue if | ||||||||||||||||||||||||||||
| 80 | # we cannot grok it. | ||||||||||||||||||||||||||||
| 81 | 5 | my $issue; | |||||||||||||||||||||||||||
| 82 | 5 | ▶ | if ($branch =~ $branch_rx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 4 | ▶ | $issue = $1 || $ {^MATCH}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$1 || ${^MATCH}
MC/DC0%0/2
$1 || ${^MATCH}
$1
${^MATCH}
Truth table33%1/3
A || B
A= $1B= ${^MATCH}
| |||||||||||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||||||||||
| 85 | 1 | return 0; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | return 0 unless length $issue; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 4 | my $place = $git->get_config($CFG => 'issue-place'); | |||||||||||||||||||||||||||
| 91 | 4 | ▶ | if ($place =~ /^trailer\s+(?<key>[A-Za-z]+)\b/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 92 | 1 | insert_issue_as_trailer($git, $msg_file, $issue, $+{key}); | |||||||||||||||||||||||||||
| 93 | } elsif ($place =~ /^title\s+(?<format>.+?)\s*$/) { | ||||||||||||||||||||||||||||
| 94 | 3 | insert_issue_in_title($git, $msg_file, $issue, $+{format}); | |||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 0 | $git->fault(<<"EOS", {option => 'issue-place'}); | |||||||||||||||||||||||||||
| 97 | Configuration error: invalid option value ($place) | ||||||||||||||||||||||||||||
| 98 | Please, fix it and try again. | ||||||||||||||||||||||||||||
| 99 | EOS | ||||||||||||||||||||||||||||
| 100 | 0 | return 1; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | return 0; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 6 | ▶ | sub prepare_message { | ||||||||||||||||||||||||||
|
Subroutine
prepare_message: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 6 | my ($git, $msg_file, $source) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 6 | $log->debug(__PACKAGE__ . "::prepare_message($msg_file, $source)"); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Do not mess up with messages if there is already a previous source for it. | ||||||||||||||||||||||||||||
| 112 | 6 | ▶ | return 0 if defined $source && $source =~ /^(?:commit|merge|squash)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $source and $source =~ /^(?:commit|merge|squash)$/u
MC/DC0%0/2
defined $source and $source =~ /^(?:commit|merge|squash)$/u
defined $source
$source =~ /^(?:commit|merge|squash)$/u
Truth table33%1/3
A and B
A= defined $sourceB= $source =~ /^(?:commit|merge|squash)$/u
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 6 | _setup_config($git); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 6 | my $errors = 0; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 6 | $errors += insert_issue($git, $msg_file); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | return $errors; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Install hooks | ||||||||||||||||||||||||||||
| 124 | 6 | PREPARE_COMMIT_MSG(\&prepare_message); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 6 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||