| 1 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | package Git::Hooks::GerritChangeId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Git::Hooks plugin to insert a Change-Id in a commit message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 17 | $Git::Hooks::GerritChangeId::VERSION = '4.0.0'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use v5.30.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use Log::Any '$log'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use Git::Hooks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 17 | ▶ | use Git::Message; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 17 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 17 | my $CFG = __PACKAGE__ =~ s/.*::/githooks./r; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | ########## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub gen_change_id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gen_change_id: called CC=5 SCAR=17.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | my ($git, $msg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | my $filename = Path::Tiny->tempfile(UNLINK => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | open my $fh, '>', $filename ## no critic (RequireBriefOpen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
open(my $fh, '>', $filename) || $git->fault("Internal error: can't open '${filename}' for writing:", {'details', $!}) && croak()
$git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!}) and croak()
MC/DC0%0/2
open(my $fh, '>', $filename) || $git->fault("Internal error: can't open '${filename}' for writing:", {'details', $!}) && croak()
open(my $fh, '>', $filename)
$git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!})
Truth table33%1/3
A || B
A= open(my $fh, '>', $filename)B= $git->fault("Internal error: can't open '${filename}' for writing:", {"details", $!})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | or $git->fault("Internal error: can't open '$filename' for writing:", {details => $!}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | and croak; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | foreach my $info ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | [ tree => [qw/write-tree/] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | [ parent => [qw/rev-parse HEAD^0/] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | [ author => [qw/var GIT_AUTHOR_IDENT/] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | [ committer => [qw/var GIT_COMMITTER_IDENT/] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 8 | my $value = eval { $git->run($info->[1]->@*) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 8 | ▶ | if (defined $value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # It's OK if we can't find value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 8 | $fh->print("$info->[0] $value"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | $fh->print("\n", $msg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | $fh->close(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | return 'I' . $git->run(qw/hash-object -t blob/, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 33 | ▶ | sub insert_change_id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert_change_id: called CC=9 SCAR=25.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 33 | my ($git, $msg) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Does Change-Id: already exist? if so, exit (no change). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 33 | ▶ | return if $msg =~ /^Change-Id:/im; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 13 | my $cmsg = Git::Message->new($msg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # Don't mess with the message if it's empty. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 13 | ▶ | if ( (! defined $cmsg->title || $cmsg->title !~ /\S/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition55%5/9
defined $cmsg->title and $cmsg->title =~ /\S/u or defined $cmsg->body and $cmsg->body =~ /\S/u
defined $cmsg->title and $cmsg->title =~ /\S/u
defined $cmsg->body and $cmsg->body =~ /\S/u
MC/DC25%1/4
defined $cmsg->title and $cmsg->title =~ /\S/u or defined $cmsg->body and $cmsg->body =~ /\S/u
defined $cmsg->title
$cmsg->title =~ /\S/u
defined $cmsg->body
$cmsg->body =~ /\S/u
Truth table28%2/7
A and B or C and D
A= defined $cmsg->titleB= $cmsg->title =~ /\S/uC= defined $cmsg->bodyD= $cmsg->body =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | && (! defined $cmsg->body || $cmsg->body !~ /\S/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # (Signed-off-by footers don't count.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 11 | my @footer = $cmsg->get_footer_keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 11 | ▶ | return if @footer == 0 || @footer == 1 && $footer[0] eq 'signed-off-by'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
@footer == 0 or @footer == 1 and $footer[0] eq "signed-off-by"
@footer == 1 and $footer[0] eq "signed-off-by"
MC/DC0%0/3
@footer == 0 or @footer == 1 and $footer[0] eq "signed-off-by"
@footer == 0
@footer == 1
$footer[0] eq "signed-off-by"
Truth table25%1/4
A or B and C
A= @footer == 0B= @footer == 1C= $footer[0] eq "signed-off-by"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # Insert the Change-Id footer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | $cmsg->add_footer_values('Change-Id' => gen_change_id($git, $cmsg->as_string)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | return $cmsg->as_string; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 33 | ▶ | sub rewrite_message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rewrite_message: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 33 | my ($git, $commit_msg_file) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 33 | $log->debug(__PACKAGE__ . "::rewrite_message($commit_msg_file)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 33 | my $msg = eval { $git->read_commit_msg_file($commit_msg_file) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 33 | ▶ | unless (defined $msg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $git->fault("Internal error: cannot read commit message file '$commit_msg_file'", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | {details => $@}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # Rewrite the message file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 33 | ▶ | if (my $new_msg = insert_change_id($git, $msg)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | $git->write_commit_msg_file($commit_msg_file, $new_msg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 33 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # Install hooks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 17 | APPLYPATCH_MSG(\&rewrite_message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 17 | COMMIT_MSG(\&rewrite_message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 17 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||