forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merged_commits.rb
31 lines (25 loc) · 858 Bytes
/
merged_commits.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true
module Overcommit::Hook::PreRebase
# Prevents rebasing commits that have already been merged into one of
# a specified set of branches.
class MergedCommits < Base
def run
# Allow rebasing a detached HEAD since no refs are changed.
return :pass if detached_head? || illegal_commits.empty?
message = 'Cannot rebase commits that have already been merged into ' \
"one of #{branches.join(', ')}"
[:fail, message]
end
private
def branches
@branches ||= config['branches']
end
def illegal_commits
@illegal_commits ||= rebased_commits.select do |commit_sha1|
branches_containing_commit =
Overcommit::GitRepo.branches_containing_commit(commit_sha1)
(branches_containing_commit & branches).any?
end
end
end
end