| 1 | use warnings; | ||
| 2 | |||
| 3 | package Git::Hooks::CheckRewrite; | ||
| 4 | # ABSTRACT: Git::Hooks plugin for checking against unsafe rewrites | ||
| 5 | $Git::Hooks::CheckRewrite::VERSION = '3.3.1'; | ||
| 6 | use v5.16.0; | ||
| 7 | use utf8; | ||
| 8 | use Path::Tiny; | ||
| 9 | use Log::Any '$log'; | ||
| 10 | use Git::Hooks; | ||
| 11 | |||
| 12 | 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 | sub _record_filename { | ||
| 20 | my ($git) = @_; | ||
| 21 | |||
| 22 | 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 | sub _branches_containing { | ||
| 29 | my ($git, $commit) = @_; | ||
| 30 | return map { substr($_, 2) } $git->run('branch', '-a', '--contains', $commit); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub record_commit_parents { | ||
| 34 | my ($git) = @_; | ||
| 35 | |||
| 36 | $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 | my $commit_parents = eval { $git->run(qw/rev-list --pretty=format:%P -n 1 HEAD/) } || ''; | ||
| 51 | |||
| 52 | _record_filename($git)->spew($commit_parents); | ||
| 53 | |||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub check_commit_amend { | ||
| 58 | my ($git) = @_; | ||
| 59 | |||
| 60 | $log->debug(__PACKAGE__ . "::check_commit_amend"); | ||
| 61 | |||
| 62 | my $record_file = _record_filename($git); | ||
| 63 | |||
| 64 | -r $record_file | ||
| 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 | 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 | return 1 unless $old_commit; | ||
| 76 | |||
| 77 | $old_commit =~ s/^commit\s+//; | ||
| 78 | |||
| 79 | # the repo's first commit has no parents. | ||
| 80 | $old_parents //= ''; | ||
| 81 | |||
| 82 | my $new_parents = ($git->run(qw/rev-list --pretty=format:%P -n 1 HEAD/))[1]; | ||
| 83 | |||
| 84 | return 1 if $new_parents ne $old_parents; | ||
| 85 | |||
| 86 | # Find all branches containing $old_commit | ||
| 87 | my @branches = _branches_containing($git, $old_commit); | ||
| 88 | |||
| 89 | if (@branches > 0) { | ||
| 90 | # $old_commit is reachable by at least one branch, which means | ||
| 91 | # the amend was unsafe. | ||
| 92 | my $branches = join "\n ", @branches; | ||
| 93 | $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 | return 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub check_rebase { | ||
| 111 | my ($git, $upstream, $branch) = @_; | ||
| 112 | |||
| 113 | $log->debug(__PACKAGE__ . "::check_rebase", {upstream => $upstream, branch => $branch}); | ||
| 114 | |||
| 115 | unless (defined $branch) { | ||
| 116 | # This means we're rebasing the current branch. We try to grok | ||
| 117 | # it's name using git symbolic-ref. | ||
| 118 | 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 | return 1 unless defined $success; | ||
| 125 | } | ||
| 126 | |||
| 127 | 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 | return 1 unless @rebased_sequence; | ||
| 134 | |||
| 135 | # Find the base commit of the rebased sequence | ||
| 136 | my $base_commit = $rebased_sequence[0]; | ||
| 137 | |||
| 138 | # Find all branches containing that commit | ||
| 139 | my @branches = _branches_containing($git, $base_commit); | ||
| 140 | |||
| 141 | if (@branches > 1) { | ||
| 142 | # The base commit is reachable by more than one branch, which | ||
| 143 | # means the rewrite is unsafe. | ||
| 144 | my $branches = join("\n ", grep {$_ ne $branch} @branches); | ||
| 145 | $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 | return 0; | ||
| 160 | } | ||
| 161 | |||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | # Install hooks | ||
| 166 | PRE_COMMIT(\&record_commit_parents); | ||
| 167 | POST_COMMIT(\&check_commit_amend); | ||
| 168 | PRE_REBASE(\&check_rebase); | ||
| 169 | |||
| 170 | 1; | ||
| 171 | |||
| 172 | __END__ |