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