forked from sigmike/peer4commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_sha_set.rb
More file actions
48 lines (38 loc) · 986 Bytes
/
commit_sha_set.rb
File metadata and controls
48 lines (38 loc) · 986 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class CommitShaSet
def initialize(commits)
@commits = commits
@commit_by_sha = {}
@commits.each do |commit|
@commit_by_sha[commit.sha] = commit
end
end
def commit(sha)
@commit_by_sha[sha]
end
def parents(sha)
commit = commit(sha)
return [] unless commit
commit.parents.map(&:sha)
end
def merged_commits(merge_sha)
parents = parents(merge_sha)
return nil if parents.size != 2
a, b = parents
pairs = [[a, b], [b, a]]
pairs.each do |parent, other_parent|
merged_commits = find_commits_between(parent, other_parent)
return merged_commits if merged_commits
end
nil
end
def find_commits_between(base, target)
return [] if base == target
parents(base).each do |parent|
next unless parent
return [base] if parent == target
parent_result = find_commits_between(parent, target)
return [base] + parent_result if parent_result
end
return nil
end
end