| 1 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | package Git::Hooks::CheckRewrite; | ||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Git::Hooks plugin for checking against unsafe rewrites | ||||||||||||||||||||||||||||||||
| 5 | 20 | $Git::Hooks::CheckRewrite::VERSION = '4.0.0'; | |||||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use v5.30.0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 20 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 20 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 20 | ▶ | use Log::Any '$log'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 20 | ▶ | use Git::Hooks; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 20 | my $CFG = __PACKAGE__ =~ s/.*::/githooks./r; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | ########## | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | # Returns a Path::Tiny object represeting the name of a file where we record | ||||||||||||||||||||||||||||||||
| 17 | # information about a commit that has to be shared between the pre- and | ||||||||||||||||||||||||||||||||
| 18 | # post-commit hooks. | ||||||||||||||||||||||||||||||||
| 19 | 17 | ▶ | sub _record_filename { | ||||||||||||||||||||||||||||||
|
Subroutine
_record_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 17 | my ($git) = @_; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 17 | return path($git->git_dir())->child('GITHOOKS_CHECKREWRITE'); | |||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | # Returns all branches containing a specific commit. The command "git | ||||||||||||||||||||||||||||||||
| 26 | # branch" returns the branch names prefixed with two characters, which | ||||||||||||||||||||||||||||||||
| 27 | # we have to get rid of using substr. | ||||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | sub _branches_containing { | ||||||||||||||||||||||||||||||
|
Subroutine
_branches_containing: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 6 | my ($git, $commit) = @_; | |||||||||||||||||||||||||||||||
| 30 | 6 | return map { substr($_, 2) } $git->run('branch', '-a', '--contains', $commit); | |||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 8 | ▶ | sub record_commit_parents { | ||||||||||||||||||||||||||||||
|
Subroutine
record_commit_parents: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 34 | 8 | my ($git) = @_; | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 8 | $log->debug(__PACKAGE__ . "::record_commit_parents"); | |||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | # Here we record the HEAD commit's own id and it's parent's ids in | ||||||||||||||||||||||||||||||||
| 39 | # a file under the git directory. The file has two lines in this | ||||||||||||||||||||||||||||||||
| 40 | # format: | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # commit SHA1 | ||||||||||||||||||||||||||||||||
| 43 | # SHA1 SHA1 ... | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | # The first line holds the HEAD commit's id and the second the ids of | ||||||||||||||||||||||||||||||||
| 46 | # its parents. In a brand new repository the rev-list command applied to | ||||||||||||||||||||||||||||||||
| 47 | # HEAD dies in error because HEAD isn't defined yet. This is why we | ||||||||||||||||||||||||||||||||
| 48 | # evaluate the command and replace its value by the empty string below. | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | my $commit_parents = eval { $git->run(qw/rev-list --pretty=format:%P -n 1 HEAD/) } || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
eval {
do {
$git->run('rev-list', '--pretty=format:%P', '-n', '1', 'HEAD')
}
} || ''
MC/DC100%1/1
eval {
do {
$git->run('rev-list', '--pretty=format:%P', '-n', '1', 'HEAD')
}
} || ''
eval {
do {
$git->run('rev-list', '--pretty=format:%P', '-n', '1', 'HEAD')
}
}
Truth table100%2/2
A
A= eval {
do {
$git->run('rev-list', '--pretty=format:%P', '-n', '1', 'HEAD')
}
}
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 8 | _record_filename($git)->spew($commit_parents); | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 8 | return 1; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 9 | ▶ | sub check_commit_amend { | ||||||||||||||||||||||||||||||
|
Subroutine
check_commit_amend: called CC=7 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 58 | 9 | my ($git) = @_; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 9 | $log->debug(__PACKAGE__ . "::check_commit_amend"); | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 9 | my $record_file = _record_filename($git); | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 9 | ▶ | -r $record_file | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
-r $record_file || $git->fault("I cannot read \$record_file.\nPlease, check if you forgot to create the pre-commit hook.\n") && (return 0)
$git->fault("I cannot read \$record_file.\nPlease, check if you forgot to create the pre-commit hook.\n") and return 0
MC/DC0%0/2
-r $record_file || $git->fault("I cannot read \$record_file.\nPlease, check if you forgot to create the pre-commit hook.\n") && (return 0)
-r $record_file
$git->fault("I cannot read \$record_file.\nPlease, check if you forgot to create the pre-commit hook.\n")
Truth table33%1/3
A || B
A= -r $record_fileB= $git->fault("I cannot read \$record_file.\nPlease, check if you forgot to create the pre-commit hook.\n")
| |||||||||||||||||||||||||||||||||
| 65 | or $git->fault(<<'EOS') | ||||||||||||||||||||||||||||||||
| 66 | I cannot read $record_file. | ||||||||||||||||||||||||||||||||
| 67 | Please, check if you forgot to create the pre-commit hook. | ||||||||||||||||||||||||||||||||
| 68 | EOS | ||||||||||||||||||||||||||||||||
| 69 | and return 0; | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 9 | my ($old_commit, $old_parents) = $record_file->lines({chomp => 1}); | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | # For a brand new repository the commit information is empty and we | ||||||||||||||||||||||||||||||||
| 74 | # don't have to check anything. | ||||||||||||||||||||||||||||||||
| 75 | 9 | ▶ | return 1 unless $old_commit; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 8 | $old_commit =~ s/^commit\s+//; | |||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | # the repo's first commit has no parents. | ||||||||||||||||||||||||||||||||
| 80 | 8 | ▶ | $old_parents //= ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$old_parents //= ""
MC/DC100%1/1
$old_parents //= ""
$old_parents
Truth table100%2/2
A
A= $old_parents
| |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 8 | my $new_parents = ($git->run(qw/rev-list --pretty=format:%P -n 1 HEAD/))[1]; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 8 | ▶ | return 1 if $new_parents ne $old_parents; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | # Find all branches containing $old_commit | ||||||||||||||||||||||||||||||||
| 87 | 3 | my @branches = _branches_containing($git, $old_commit); | |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | if (@branches > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | # $old_commit is reachable by at least one branch, which means | ||||||||||||||||||||||||||||||||
| 91 | # the amend was unsafe. | ||||||||||||||||||||||||||||||||
| 92 | 2 | my $branches = join "\n ", @branches; | |||||||||||||||||||||||||||||||
| 93 | 2 | $git->fault(<<"EOS"); | |||||||||||||||||||||||||||||||
| 94 | You just performed an unsafe "git commit --amend" because your | ||||||||||||||||||||||||||||||||
| 95 | original HEAD ($old_commit) is still reachable by the following | ||||||||||||||||||||||||||||||||
| 96 | branch(es): | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | $branches | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | Consider amending it again with the following command: | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | git commit --amend | ||||||||||||||||||||||||||||||||
| 103 | EOS | ||||||||||||||||||||||||||||||||
| 104 | 2 | return 0; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 1 | return 1; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 3 | ▶ | sub check_rebase { | ||||||||||||||||||||||||||||||
|
Subroutine
check_rebase: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 111 | 3 | my ($git, $upstream, $branch) = @_; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 3 | $log->debug(__PACKAGE__ . "::check_rebase", {upstream => $upstream, branch => $branch}); | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 3 | ▶ | unless (defined $branch) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | # This means we're rebasing the current branch. We try to grok | ||||||||||||||||||||||||||||||||
| 117 | # it's name using git symbolic-ref. | ||||||||||||||||||||||||||||||||
| 118 | 2 | my $success = eval { $branch = $git->run(qw/symbolic-ref -q HEAD/) }; | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | # The command git symbolic-ref fails if we're in a | ||||||||||||||||||||||||||||||||
| 121 | # detached HEAD. In this situation we don't care about the | ||||||||||||||||||||||||||||||||
| 122 | # rewriting. | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | return 1 unless defined $success; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 3 | my @rebased_sequence = $git->run(qw/rev-list --topo-order --reverse/, "$upstream..$branch"); | |||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | # If $upstream is a decendant of $branch, the @rebased_sequence is | ||||||||||||||||||||||||||||||||
| 130 | # empty. In this situation the rebase will turn out to be a simple | ||||||||||||||||||||||||||||||||
| 131 | # fast-forward merge from $branch on $upstream and there is | ||||||||||||||||||||||||||||||||
| 132 | # nothing to lose. | ||||||||||||||||||||||||||||||||
| 133 | 3 | ▶ | return 1 unless @rebased_sequence; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | # Find the base commit of the rebased sequence | ||||||||||||||||||||||||||||||||
| 136 | 3 | my $base_commit = $rebased_sequence[0]; | |||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | # Find all branches containing that commit | ||||||||||||||||||||||||||||||||
| 139 | 3 | my @branches = _branches_containing($git, $base_commit); | |||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 3 | ▶ | if (@branches > 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | # The base commit is reachable by more than one branch, which | ||||||||||||||||||||||||||||||||
| 143 | # means the rewrite is unsafe. | ||||||||||||||||||||||||||||||||
| 144 | 2 | my $branches = join("\n ", grep {$_ ne $branch} @branches); | |||||||||||||||||||||||||||||||
| 145 | 2 | $git->fault(<<"EOS"); | |||||||||||||||||||||||||||||||
| 146 | You just performed an unsafe rebase because it would rewrite commits shared | ||||||||||||||||||||||||||||||||
| 147 | by $branch and the following other branch(es): | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | $branches | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | If the rebase was just effected, you can reset your branch to its previous | ||||||||||||||||||||||||||||||||
| 152 | commit with the command: | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | git reset --hard \@{1} | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | But be sure to understand the consequences of this command, as it can | ||||||||||||||||||||||||||||||||
| 157 | potentially make you lose work. | ||||||||||||||||||||||||||||||||
| 158 | EOS | ||||||||||||||||||||||||||||||||
| 159 | 2 | return 0; | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 1 | return 1; | |||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | # Install hooks | ||||||||||||||||||||||||||||||||
| 166 | 20 | PRE_COMMIT(\&record_commit_parents); | |||||||||||||||||||||||||||||||
| 167 | 20 | POST_COMMIT(\&check_commit_amend); | |||||||||||||||||||||||||||||||
| 168 | 20 | PRE_REBASE(\&check_rebase); | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 20 | 1; | |||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||||||