2013-03-07 01:42:30 -05:00
|
|
|
module Network
|
|
|
|
class Graph
|
2013-08-19 23:05:23 -04:00
|
|
|
attr_reader :days, :commits, :map, :notes, :repo
|
2013-02-25 23:28:11 -05:00
|
|
|
|
|
|
|
def self.max_count
|
|
|
|
@max_count ||= 650
|
|
|
|
end
|
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def initialize(project, ref, commit, filter_ref)
|
2013-02-25 23:28:11 -05:00
|
|
|
@project = project
|
|
|
|
@ref = ref
|
|
|
|
@commit = commit
|
2013-04-12 04:49:11 -04:00
|
|
|
@filter_ref = filter_ref
|
2013-08-19 23:05:23 -04:00
|
|
|
@repo = project.repository
|
2013-02-25 23:28:11 -05:00
|
|
|
|
|
|
|
@commits = collect_commits
|
|
|
|
@days = index_commits
|
2013-04-24 11:06:31 -04:00
|
|
|
@notes = collect_notes
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
|
2013-03-07 01:42:30 -05:00
|
|
|
protected
|
2013-02-25 23:28:11 -05:00
|
|
|
|
2013-04-24 11:06:31 -04:00
|
|
|
def collect_notes
|
|
|
|
h = Hash.new(0)
|
2016-05-30 08:51:42 -04:00
|
|
|
|
|
|
|
@project
|
|
|
|
.notes
|
|
|
|
.where('noteable_type = ?', 'Commit')
|
|
|
|
.group('notes.commit_id')
|
|
|
|
.select('notes.commit_id, count(notes.id) as note_count')
|
|
|
|
.each do |item|
|
|
|
|
h[item.commit_id] = item.note_count.to_i
|
|
|
|
end
|
|
|
|
|
2013-04-24 11:06:31 -04:00
|
|
|
h
|
|
|
|
end
|
|
|
|
|
2013-02-25 23:28:11 -05:00
|
|
|
# Get commits from repository
|
|
|
|
#
|
|
|
|
def collect_commits
|
2013-04-03 14:05:10 -04:00
|
|
|
find_commits(count_to_display_commit_in_center).map do |commit|
|
|
|
|
# Decorate with app/model/network/commit.rb
|
2013-08-19 23:05:23 -04:00
|
|
|
Network::Commit.new(commit)
|
2013-03-07 05:16:51 -05:00
|
|
|
end
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Method is adding time and space on the
|
|
|
|
# list of commits. As well as returns date list
|
2013-07-29 06:46:00 -04:00
|
|
|
# correlated with time set on commits.
|
2013-02-25 23:28:11 -05:00
|
|
|
#
|
2013-07-29 06:46:00 -04:00
|
|
|
# @return [Array<TimeDate>] list of commit dates correlated with time on commits
|
2013-02-25 23:28:11 -05:00
|
|
|
def index_commits
|
2013-03-07 05:16:51 -05:00
|
|
|
days = []
|
2013-03-07 05:52:46 -05:00
|
|
|
@map = {}
|
2013-03-20 05:17:12 -04:00
|
|
|
@reserved = {}
|
2013-02-25 23:28:11 -05:00
|
|
|
|
2016-06-29 09:23:44 -04:00
|
|
|
@commits.each_with_index do |c, i|
|
2013-02-25 23:28:11 -05:00
|
|
|
c.time = i
|
|
|
|
days[i] = c.committed_date
|
2013-03-07 05:52:46 -05:00
|
|
|
@map[c.id] = c
|
|
|
|
@reserved[i] = []
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
commits_sort_by_ref.each do |commit|
|
2013-03-07 06:36:40 -05:00
|
|
|
place_chain(commit)
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# find parent spaces for not overlap lines
|
2013-03-07 05:16:51 -05:00
|
|
|
@commits.each do |c|
|
2013-03-07 05:52:46 -05:00
|
|
|
c.parent_spaces.concat(find_free_parent_spaces(c))
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
days
|
|
|
|
end
|
|
|
|
|
|
|
|
# Skip count that the target commit is displayed in center.
|
2013-03-07 05:16:51 -05:00
|
|
|
def count_to_display_commit_in_center
|
2013-03-18 21:22:55 -04:00
|
|
|
offset = -1
|
|
|
|
skip = 0
|
|
|
|
while offset == -1
|
|
|
|
tmp_commits = find_commits(skip)
|
|
|
|
if tmp_commits.size > 0
|
|
|
|
index = tmp_commits.index do |c|
|
|
|
|
c.id == @commit.id
|
|
|
|
end
|
|
|
|
|
|
|
|
if index
|
|
|
|
# Find the target commit
|
|
|
|
offset = index + skip
|
|
|
|
else
|
|
|
|
skip += self.class.max_count
|
|
|
|
end
|
|
|
|
else
|
2015-01-18 10:29:37 -05:00
|
|
|
# Can't find the target commit in the repo.
|
2013-03-18 21:22:55 -04:00
|
|
|
offset = 0
|
|
|
|
end
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
|
2016-05-30 06:18:27 -04:00
|
|
|
if self.class.max_count / 2 < offset
|
2013-02-25 23:28:11 -05:00
|
|
|
# get max index that commit is displayed in the center.
|
2013-03-18 21:22:55 -04:00
|
|
|
offset - self.class.max_count / 2
|
2013-02-25 23:28:11 -05:00
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-18 21:00:29 -04:00
|
|
|
def find_commits(skip = 0)
|
2013-04-03 14:05:10 -04:00
|
|
|
opts = {
|
|
|
|
max_count: self.class.max_count,
|
|
|
|
skip: skip
|
|
|
|
}
|
|
|
|
|
2013-08-19 23:05:23 -04:00
|
|
|
opts[:ref] = @commit.id if @filter_ref
|
2013-04-12 04:49:11 -04:00
|
|
|
|
2013-08-19 23:05:23 -04:00
|
|
|
@repo.find_commits(opts)
|
2013-03-18 21:00:29 -04:00
|
|
|
end
|
|
|
|
|
2013-02-25 23:28:11 -05:00
|
|
|
def commits_sort_by_ref
|
2016-06-29 09:23:44 -04:00
|
|
|
@commits.sort do |a, b|
|
2013-02-25 23:28:11 -05:00
|
|
|
if include_ref?(a)
|
|
|
|
-1
|
|
|
|
elsif include_ref?(b)
|
|
|
|
1
|
|
|
|
else
|
|
|
|
b.committed_date <=> a.committed_date
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_ref?(commit)
|
2013-08-19 23:05:23 -04:00
|
|
|
commit.ref_names(@repo).include?(@ref)
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
|
2013-03-07 05:52:46 -05:00
|
|
|
def find_free_parent_spaces(commit)
|
2013-02-25 23:28:11 -05:00
|
|
|
spaces = []
|
|
|
|
|
2013-03-07 06:36:40 -05:00
|
|
|
commit.parents(@map).each do |parent|
|
2013-03-20 05:17:12 -04:00
|
|
|
range = commit.time..parent.time
|
2013-03-07 06:36:40 -05:00
|
|
|
|
2016-05-30 06:18:27 -04:00
|
|
|
space = if commit.space >= parent.space
|
2013-03-07 06:36:40 -05:00
|
|
|
find_free_parent_space(range, parent.space, -1, commit.space)
|
|
|
|
else
|
|
|
|
find_free_parent_space(range, commit.space, -1, parent.space)
|
|
|
|
end
|
|
|
|
|
|
|
|
mark_reserved(range, space)
|
|
|
|
spaces << space
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
spaces
|
|
|
|
end
|
|
|
|
|
2013-03-07 05:16:51 -05:00
|
|
|
def find_free_parent_space(range, space_base, space_step, space_default)
|
2016-05-30 06:18:27 -04:00
|
|
|
if is_overlap?(range, space_default)
|
2013-02-27 08:37:38 -05:00
|
|
|
find_free_space(range, space_step, space_base, space_default)
|
2013-02-25 23:28:11 -05:00
|
|
|
else
|
|
|
|
space_default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-07 05:16:51 -05:00
|
|
|
def is_overlap?(range, overlap_space)
|
2013-02-25 23:28:11 -05:00
|
|
|
range.each do |i|
|
|
|
|
if i != range.first &&
|
|
|
|
i != range.last &&
|
2016-05-30 06:18:27 -04:00
|
|
|
@commits[i].spaces.include?(overlap_space)
|
2013-02-25 23:28:11 -05:00
|
|
|
|
2016-05-30 05:55:00 -04:00
|
|
|
return true
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add space mark on commit and its parents
|
|
|
|
#
|
2013-03-07 05:52:46 -05:00
|
|
|
# @param [::Commit] the commit object.
|
|
|
|
def place_chain(commit, parent_time = nil)
|
|
|
|
leaves = take_left_leaves(commit)
|
2013-02-25 23:28:11 -05:00
|
|
|
if leaves.empty?
|
|
|
|
return
|
|
|
|
end
|
2013-02-27 08:37:38 -05:00
|
|
|
|
2013-03-20 05:17:12 -04:00
|
|
|
time_range = leaves.first.time..leaves.last.time
|
2013-03-07 05:52:46 -05:00
|
|
|
space_base = get_space_base(leaves)
|
2013-02-27 09:43:33 -05:00
|
|
|
space = find_free_space(time_range, 2, space_base)
|
2013-02-27 08:37:38 -05:00
|
|
|
leaves.each do |l|
|
|
|
|
l.spaces << space
|
|
|
|
end
|
|
|
|
|
2013-02-25 23:28:11 -05:00
|
|
|
# and mark it as reserved
|
|
|
|
if parent_time.nil?
|
2013-03-20 05:17:12 -04:00
|
|
|
min_time = leaves.first.time
|
2013-02-25 23:28:11 -05:00
|
|
|
else
|
2013-03-20 05:17:12 -04:00
|
|
|
min_time = parent_time + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
max_time = leaves.last.time
|
|
|
|
leaves.last.parents(@map).each do |parent|
|
|
|
|
if max_time < parent.time
|
|
|
|
max_time = parent.time
|
|
|
|
end
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
mark_reserved(min_time..max_time, space)
|
|
|
|
|
|
|
|
# Visit branching chains
|
|
|
|
leaves.each do |l|
|
2013-03-07 06:36:40 -05:00
|
|
|
parents = l.parents(@map).select{|p| p.space.zero?}
|
2016-06-01 05:18:11 -04:00
|
|
|
parents.each do |p|
|
2013-03-07 05:52:46 -05:00
|
|
|
place_chain(p, l.time)
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-07 05:52:46 -05:00
|
|
|
def get_space_base(leaves)
|
2013-02-27 09:43:33 -05:00
|
|
|
space_base = 1
|
2013-03-07 06:36:40 -05:00
|
|
|
parents = leaves.last.parents(@map)
|
|
|
|
if parents.size > 0
|
|
|
|
if parents.first.space > 0
|
|
|
|
space_base = parents.first.space
|
2013-02-27 09:43:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
space_base
|
|
|
|
end
|
|
|
|
|
2013-02-25 23:28:11 -05:00
|
|
|
def mark_reserved(time_range, space)
|
2016-06-01 05:18:11 -04:00
|
|
|
time_range.each do |day|
|
2013-03-07 05:52:46 -05:00
|
|
|
@reserved[day].push(space)
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-27 09:43:33 -05:00
|
|
|
def find_free_space(time_range, space_step, space_base = 1, space_default = nil)
|
|
|
|
space_default ||= space_base
|
|
|
|
|
2013-02-25 23:28:11 -05:00
|
|
|
reserved = []
|
2016-06-01 05:18:11 -04:00
|
|
|
time_range.each do |day|
|
2014-10-09 03:47:47 -04:00
|
|
|
reserved.push(*@reserved[day])
|
2013-02-25 23:28:11 -05:00
|
|
|
end
|
|
|
|
reserved.uniq!
|
|
|
|
|
2013-02-27 07:49:53 -05:00
|
|
|
space = space_default
|
2016-05-30 05:37:14 -04:00
|
|
|
while reserved.include?(space)
|
2013-02-25 23:28:11 -05:00
|
|
|
space += space_step
|
2016-05-30 06:18:27 -04:00
|
|
|
if space < space_base
|
2013-02-25 23:28:11 -05:00
|
|
|
space_step *= -1
|
|
|
|
space = space_base + space_step
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
space
|
|
|
|
end
|
|
|
|
|
|
|
|
# Takes most left subtree branch of commits
|
|
|
|
# which don't have space mark yet.
|
|
|
|
#
|
2013-03-07 05:52:46 -05:00
|
|
|
# @param [::Commit] the commit object.
|
2013-02-25 23:28:11 -05:00
|
|
|
#
|
2013-03-07 05:52:46 -05:00
|
|
|
# @return [Array<Network::Commit>] list of branch commits
|
|
|
|
def take_left_leaves(raw_commit)
|
|
|
|
commit = @map[raw_commit.id]
|
2013-02-25 23:28:11 -05:00
|
|
|
leaves = []
|
|
|
|
leaves.push(commit) if commit.space.zero?
|
|
|
|
|
2016-05-23 13:43:58 -04:00
|
|
|
loop do
|
2013-03-07 06:36:40 -05:00
|
|
|
return leaves if commit.parents(@map).count.zero?
|
2013-02-25 23:28:11 -05:00
|
|
|
|
2013-03-07 06:36:40 -05:00
|
|
|
commit = commit.parents(@map).first
|
2013-02-25 23:28:11 -05:00
|
|
|
|
|
|
|
return leaves unless commit.space.zero?
|
|
|
|
|
|
|
|
leaves.push(commit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|