Commit e03a018d28488260eb6c69741680691426f823a6

Authored by Sato Hiroyuki
1 parent 784aa266

Refactor: rename module and class names.

* Module: Graph -> Network
* Class: JsonBuilder -> Graph
app/controllers/graph_controller.rb
... ... @@ -22,7 +22,7 @@ class GraphController < ProjectResourceController
22 22 format.html
23 23  
24 24 format.json do
25   - @graph = Graph::JsonBuilder.new(project, @ref, @commit)
  25 + @graph = Network::Graph.new(project, @ref, @commit)
26 26 end
27 27 end
28 28 end
... ...
app/models/graph/commit.rb
... ... @@ -1,39 +0,0 @@
1   -require "grit"
2   -
3   -module Graph
4   - class Commit
5   - include ActionView::Helpers::TagHelper
6   -
7   - attr_accessor :time, :spaces, :refs, :parent_spaces
8   -
9   - def initialize(commit)
10   - @_commit = commit
11   - @time = -1
12   - @spaces = []
13   - @parent_spaces = []
14   - end
15   -
16   - def method_missing(m, *args, &block)
17   - @_commit.send(m, *args, &block)
18   - end
19   -
20   - def add_refs(ref_cache, repo)
21   - if ref_cache.empty?
22   - repo.refs.each do |ref|
23   - ref_cache[ref.commit.id] ||= []
24   - ref_cache[ref.commit.id] << ref
25   - end
26   - end
27   - @refs = ref_cache[@_commit.id] if ref_cache.include?(@_commit.id)
28   - @refs ||= []
29   - end
30   -
31   - def space
32   - if @spaces.size > 0
33   - @spaces.first
34   - else
35   - 0
36   - end
37   - end
38   - end
39   -end
app/models/graph/json_builder.rb
... ... @@ -1,284 +0,0 @@
1   -require "grit"
2   -
3   -module Graph
4   - class JsonBuilder
5   - attr_accessor :days, :commits, :ref_cache, :repo
6   -
7   - def self.max_count
8   - @max_count ||= 650
9   - end
10   -
11   - def initialize project, ref, commit
12   - @project = project
13   - @ref = ref
14   - @commit = commit
15   - @repo = project.repo
16   - @ref_cache = {}
17   -
18   - @commits = collect_commits
19   - @days = index_commits
20   - end
21   -
22   - protected
23   -
24   - # Get commits from repository
25   - #
26   - def collect_commits
27   -
28   - @commits = Grit::Commit.find_all(repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup
29   -
30   - # Decorate with app/models/commit.rb
31   - @commits.map! { |commit| Commit.new(commit) }
32   -
33   - # Decorate with lib/gitlab/graph/commit.rb
34   - @commits.map! { |commit| Graph::Commit.new(commit) }
35   -
36   - # add refs to each commit
37   - @commits.each { |commit| commit.add_refs(ref_cache, repo) }
38   -
39   - @commits
40   - end
41   -
42   - # Method is adding time and space on the
43   - # list of commits. As well as returns date list
44   - # corelated with time set on commits.
45   - #
46   - # @param [Array<Graph::Commit>] commits to index
47   - #
48   - # @return [Array<TimeDate>] list of commit dates corelated with time on commits
49   - def index_commits
50   - days, times = [], []
51   - map = {}
52   -
53   - commits.reverse.each_with_index do |c,i|
54   - c.time = i
55   - days[i] = c.committed_date
56   - map[c.id] = c
57   - times[i] = c
58   - end
59   -
60   - @_reserved = {}
61   - days.each_index do |i|
62   - @_reserved[i] = []
63   - end
64   -
65   - commits_sort_by_ref.each do |commit|
66   - if map.include? commit.id then
67   - place_chain(map[commit.id], map)
68   - end
69   - end
70   -
71   - # find parent spaces for not overlap lines
72   - times.each do |c|
73   - c.parent_spaces.concat(find_free_parent_spaces(c, map, times))
74   - end
75   -
76   - days
77   - end
78   -
79   - # Skip count that the target commit is displayed in center.
80   - def to_commit
81   - commits = Grit::Commit.find_all(repo, nil, {date_order: true})
82   - commit_index = commits.index do |c|
83   - c.id == @commit.id
84   - end
85   -
86   - if commit_index && (self.class.max_count / 2 < commit_index) then
87   - # get max index that commit is displayed in the center.
88   - commit_index - self.class.max_count / 2
89   - else
90   - 0
91   - end
92   - end
93   -
94   - def commits_sort_by_ref
95   - commits.sort do |a,b|
96   - if include_ref?(a)
97   - -1
98   - elsif include_ref?(b)
99   - 1
100   - else
101   - b.committed_date <=> a.committed_date
102   - end
103   - end
104   - end
105   -
106   - def include_ref?(commit)
107   - heads = commit.refs.select do |ref|
108   - ref.is_a?(Grit::Head) or ref.is_a?(Grit::Remote) or ref.is_a?(Grit::Tag)
109   - end
110   -
111   - heads.map! do |head|
112   - head.name
113   - end
114   -
115   - heads.include?(@ref)
116   - end
117   -
118   - def find_free_parent_spaces(commit, map, times)
119   - spaces = []
120   -
121   - commit.parents.each do |p|
122   - if map.include?(p.id) then
123   - parent = map[p.id]
124   -
125   - range = if commit.time < parent.time then
126   - commit.time..parent.time
127   - else
128   - parent.time..commit.time
129   - end
130   -
131   - space = if commit.space >= parent.space then
132   - find_free_parent_space(range, parent.space, -1, commit.space, times)
133   - else
134   - find_free_parent_space(range, commit.space, -1, parent.space, times)
135   - end
136   -
137   - mark_reserved(range, space)
138   - spaces << space
139   - end
140   - end
141   -
142   - spaces
143   - end
144   -
145   - def find_free_parent_space(range, space_base, space_step, space_default, times)
146   - if is_overlap?(range, times, space_default) then
147   - find_free_space(range, space_step, space_base, space_default)
148   - else
149   - space_default
150   - end
151   - end
152   -
153   - def is_overlap?(range, times, overlap_space)
154   - range.each do |i|
155   - if i != range.first &&
156   - i != range.last &&
157   - times[i].spaces.include?(overlap_space) then
158   -
159   - return true;
160   - end
161   - end
162   -
163   - false
164   - end
165   -
166   - # Add space mark on commit and its parents
167   - #
168   - # @param [Graph::Commit] the commit object.
169   - # @param [Hash<String,Graph::Commit>] map of commits
170   - def place_chain(commit, map, parent_time = nil)
171   - leaves = take_left_leaves(commit, map)
172   - if leaves.empty?
173   - return
174   - end
175   -
176   - time_range = leaves.last.time..leaves.first.time
177   - space_base = get_space_base(leaves, map)
178   - space = find_free_space(time_range, 2, space_base)
179   - leaves.each do |l|
180   - l.spaces << space
181   - # Also add space to parent
182   - l.parents.each do |p|
183   - if map.include?(p.id)
184   - parent = map[p.id]
185   - if parent.space > 0
186   - parent.spaces << space
187   - end
188   - end
189   - end
190   - end
191   -
192   - # and mark it as reserved
193   - min_time = leaves.last.time
194   - parents = leaves.last.parents.collect
195   - parents.each do |p|
196   - if map.include? p.id
197   - parent = map[p.id]
198   - if parent.time < min_time
199   - min_time = parent.time
200   - end
201   - end
202   - end
203   -
204   - if parent_time.nil?
205   - max_time = leaves.first.time
206   - else
207   - max_time = parent_time - 1
208   - end
209   - mark_reserved(min_time..max_time, space)
210   -
211   - # Visit branching chains
212   - leaves.each do |l|
213   - parents = l.parents.collect.select{|p| map.include? p.id and map[p.id].space.zero?}
214   - for p in parents
215   - place_chain(map[p.id], map, l.time)
216   - end
217   - end
218   - end
219   -
220   - def get_space_base(leaves, map)
221   - space_base = 1
222   - if leaves.last.parents.size > 0
223   - first_parent = leaves.last.parents.first
224   - if map.include?(first_parent.id)
225   - first_p = map[first_parent.id]
226   - if first_p.space > 0
227   - space_base = first_p.space
228   - end
229   - end
230   - end
231   - space_base
232   - end
233   -
234   - def mark_reserved(time_range, space)
235   - for day in time_range
236   - @_reserved[day].push(space)
237   - end
238   - end
239   -
240   - def find_free_space(time_range, space_step, space_base = 1, space_default = nil)
241   - space_default ||= space_base
242   -
243   - reserved = []
244   - for day in time_range
245   - reserved += @_reserved[day]
246   - end
247   - reserved.uniq!
248   -
249   - space = space_default
250   - while reserved.include?(space) do
251   - space += space_step
252   - if space < space_base then
253   - space_step *= -1
254   - space = space_base + space_step
255   - end
256   - end
257   -
258   - space
259   - end
260   -
261   - # Takes most left subtree branch of commits
262   - # which don't have space mark yet.
263   - #
264   - # @param [Graph::Commit] the commit object.
265   - # @param [Hash<String,Graph::Commit>] map of commits
266   - #
267   - # @return [Array<Graph::Commit>] list of branch commits
268   - def take_left_leaves(commit, map)
269   - leaves = []
270   - leaves.push(commit) if commit.space.zero?
271   -
272   - while true
273   - return leaves if commit.parents.count.zero?
274   - return leaves unless map.include? commit.parents.first.id
275   -
276   - commit = map[commit.parents.first.id]
277   -
278   - return leaves unless commit.space.zero?
279   -
280   - leaves.push(commit)
281   - end
282   - end
283   - end
284   -end
app/models/network/commit.rb 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +require "grit"
  2 +
  3 +module Network
  4 + class Commit
  5 + include ActionView::Helpers::TagHelper
  6 +
  7 + attr_accessor :time, :spaces, :refs, :parent_spaces
  8 +
  9 + def initialize(commit)
  10 + @_commit = commit
  11 + @time = -1
  12 + @spaces = []
  13 + @parent_spaces = []
  14 + end
  15 +
  16 + def method_missing(m, *args, &block)
  17 + @_commit.send(m, *args, &block)
  18 + end
  19 +
  20 + def add_refs(ref_cache, repo)
  21 + if ref_cache.empty?
  22 + repo.refs.each do |ref|
  23 + ref_cache[ref.commit.id] ||= []
  24 + ref_cache[ref.commit.id] << ref
  25 + end
  26 + end
  27 + @refs = ref_cache[@_commit.id] if ref_cache.include?(@_commit.id)
  28 + @refs ||= []
  29 + end
  30 +
  31 + def space
  32 + if @spaces.size > 0
  33 + @spaces.first
  34 + else
  35 + 0
  36 + end
  37 + end
  38 + end
  39 +end
... ...
app/models/network/graph.rb 0 → 100644
... ... @@ -0,0 +1,284 @@
  1 +require "grit"
  2 +
  3 +module Network
  4 + class Graph
  5 + attr_accessor :days, :commits, :ref_cache, :repo
  6 +
  7 + def self.max_count
  8 + @max_count ||= 650
  9 + end
  10 +
  11 + def initialize project, ref, commit
  12 + @project = project
  13 + @ref = ref
  14 + @commit = commit
  15 + @repo = project.repo
  16 + @ref_cache = {}
  17 +
  18 + @commits = collect_commits
  19 + @days = index_commits
  20 + end
  21 +
  22 + protected
  23 +
  24 + # Get commits from repository
  25 + #
  26 + def collect_commits
  27 +
  28 + @commits = Grit::Commit.find_all(repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup
  29 +
  30 + # Decorate with app/models/commit.rb
  31 + @commits.map! { |commit| Commit.new(commit) }
  32 +
  33 + # Decorate with app/model/network/commit.rb
  34 + @commits.map! { |commit| Network::Commit.new(commit) }
  35 +
  36 + # add refs to each commit
  37 + @commits.each { |commit| commit.add_refs(ref_cache, repo) }
  38 +
  39 + @commits
  40 + end
  41 +
  42 + # Method is adding time and space on the
  43 + # list of commits. As well as returns date list
  44 + # corelated with time set on commits.
  45 + #
  46 + # @param [Array<Graph::Commit>] commits to index
  47 + #
  48 + # @return [Array<TimeDate>] list of commit dates corelated with time on commits
  49 + def index_commits
  50 + days, times = [], []
  51 + map = {}
  52 +
  53 + commits.reverse.each_with_index do |c,i|
  54 + c.time = i
  55 + days[i] = c.committed_date
  56 + map[c.id] = c
  57 + times[i] = c
  58 + end
  59 +
  60 + @_reserved = {}
  61 + days.each_index do |i|
  62 + @_reserved[i] = []
  63 + end
  64 +
  65 + commits_sort_by_ref.each do |commit|
  66 + if map.include? commit.id then
  67 + place_chain(map[commit.id], map)
  68 + end
  69 + end
  70 +
  71 + # find parent spaces for not overlap lines
  72 + times.each do |c|
  73 + c.parent_spaces.concat(find_free_parent_spaces(c, map, times))
  74 + end
  75 +
  76 + days
  77 + end
  78 +
  79 + # Skip count that the target commit is displayed in center.
  80 + def to_commit
  81 + commits = Grit::Commit.find_all(repo, nil, {date_order: true})
  82 + commit_index = commits.index do |c|
  83 + c.id == @commit.id
  84 + end
  85 +
  86 + if commit_index && (self.class.max_count / 2 < commit_index) then
  87 + # get max index that commit is displayed in the center.
  88 + commit_index - self.class.max_count / 2
  89 + else
  90 + 0
  91 + end
  92 + end
  93 +
  94 + def commits_sort_by_ref
  95 + commits.sort do |a,b|
  96 + if include_ref?(a)
  97 + -1
  98 + elsif include_ref?(b)
  99 + 1
  100 + else
  101 + b.committed_date <=> a.committed_date
  102 + end
  103 + end
  104 + end
  105 +
  106 + def include_ref?(commit)
  107 + heads = commit.refs.select do |ref|
  108 + ref.is_a?(Grit::Head) or ref.is_a?(Grit::Remote) or ref.is_a?(Grit::Tag)
  109 + end
  110 +
  111 + heads.map! do |head|
  112 + head.name
  113 + end
  114 +
  115 + heads.include?(@ref)
  116 + end
  117 +
  118 + def find_free_parent_spaces(commit, map, times)
  119 + spaces = []
  120 +
  121 + commit.parents.each do |p|
  122 + if map.include?(p.id) then
  123 + parent = map[p.id]
  124 +
  125 + range = if commit.time < parent.time then
  126 + commit.time..parent.time
  127 + else
  128 + parent.time..commit.time
  129 + end
  130 +
  131 + space = if commit.space >= parent.space then
  132 + find_free_parent_space(range, parent.space, -1, commit.space, times)
  133 + else
  134 + find_free_parent_space(range, commit.space, -1, parent.space, times)
  135 + end
  136 +
  137 + mark_reserved(range, space)
  138 + spaces << space
  139 + end
  140 + end
  141 +
  142 + spaces
  143 + end
  144 +
  145 + def find_free_parent_space(range, space_base, space_step, space_default, times)
  146 + if is_overlap?(range, times, space_default) then
  147 + find_free_space(range, space_step, space_base, space_default)
  148 + else
  149 + space_default
  150 + end
  151 + end
  152 +
  153 + def is_overlap?(range, times, overlap_space)
  154 + range.each do |i|
  155 + if i != range.first &&
  156 + i != range.last &&
  157 + times[i].spaces.include?(overlap_space) then
  158 +
  159 + return true;
  160 + end
  161 + end
  162 +
  163 + false
  164 + end
  165 +
  166 + # Add space mark on commit and its parents
  167 + #
  168 + # @param [Graph::Commit] the commit object.
  169 + # @param [Hash<String,Graph::Commit>] map of commits
  170 + def place_chain(commit, map, parent_time = nil)
  171 + leaves = take_left_leaves(commit, map)
  172 + if leaves.empty?
  173 + return
  174 + end
  175 +
  176 + time_range = leaves.last.time..leaves.first.time
  177 + space_base = get_space_base(leaves, map)
  178 + space = find_free_space(time_range, 2, space_base)
  179 + leaves.each do |l|
  180 + l.spaces << space
  181 + # Also add space to parent
  182 + l.parents.each do |p|
  183 + if map.include?(p.id)
  184 + parent = map[p.id]
  185 + if parent.space > 0
  186 + parent.spaces << space
  187 + end
  188 + end
  189 + end
  190 + end
  191 +
  192 + # and mark it as reserved
  193 + min_time = leaves.last.time
  194 + parents = leaves.last.parents.collect
  195 + parents.each do |p|
  196 + if map.include? p.id
  197 + parent = map[p.id]
  198 + if parent.time < min_time
  199 + min_time = parent.time
  200 + end
  201 + end
  202 + end
  203 +
  204 + if parent_time.nil?
  205 + max_time = leaves.first.time
  206 + else
  207 + max_time = parent_time - 1
  208 + end
  209 + mark_reserved(min_time..max_time, space)
  210 +
  211 + # Visit branching chains
  212 + leaves.each do |l|
  213 + parents = l.parents.collect.select{|p| map.include? p.id and map[p.id].space.zero?}
  214 + for p in parents
  215 + place_chain(map[p.id], map, l.time)
  216 + end
  217 + end
  218 + end
  219 +
  220 + def get_space_base(leaves, map)
  221 + space_base = 1
  222 + if leaves.last.parents.size > 0
  223 + first_parent = leaves.last.parents.first
  224 + if map.include?(first_parent.id)
  225 + first_p = map[first_parent.id]
  226 + if first_p.space > 0
  227 + space_base = first_p.space
  228 + end
  229 + end
  230 + end
  231 + space_base
  232 + end
  233 +
  234 + def mark_reserved(time_range, space)
  235 + for day in time_range
  236 + @_reserved[day].push(space)
  237 + end
  238 + end
  239 +
  240 + def find_free_space(time_range, space_step, space_base = 1, space_default = nil)
  241 + space_default ||= space_base
  242 +
  243 + reserved = []
  244 + for day in time_range
  245 + reserved += @_reserved[day]
  246 + end
  247 + reserved.uniq!
  248 +
  249 + space = space_default
  250 + while reserved.include?(space) do
  251 + space += space_step
  252 + if space < space_base then
  253 + space_step *= -1
  254 + space = space_base + space_step
  255 + end
  256 + end
  257 +
  258 + space
  259 + end
  260 +
  261 + # Takes most left subtree branch of commits
  262 + # which don't have space mark yet.
  263 + #
  264 + # @param [Graph::Commit] the commit object.
  265 + # @param [Hash<String,Graph::Commit>] map of commits
  266 + #
  267 + # @return [Array<Graph::Commit>] list of branch commits
  268 + def take_left_leaves(commit, map)
  269 + leaves = []
  270 + leaves.push(commit) if commit.space.zero?
  271 +
  272 + while true
  273 + return leaves if commit.parents.count.zero?
  274 + return leaves unless map.include? commit.parents.first.id
  275 +
  276 + commit = map[commit.parents.first.id]
  277 +
  278 + return leaves unless commit.space.zero?
  279 +
  280 + leaves.push(commit)
  281 + end
  282 + end
  283 + end
  284 +end
... ...
features/steps/project/project_network_graph.rb
... ... @@ -8,8 +8,8 @@ class ProjectNetworkGraph &lt; Spinach::FeatureSteps
8 8 end
9 9  
10 10 When 'I visit project "Shop" network page' do
11   - # Stub Graph::JsonBuilder max_size to speed up test (10 commits vs. 650)
12   - Graph::JsonBuilder.stub(max_count: 10)
  11 + # Stub Graph max_size to speed up test (10 commits vs. 650)
  12 + Network::Graph.stub(max_count: 10)
13 13  
14 14 project = Project.find_by_name("Shop")
15 15 visit project_graph_path(project, "master")
... ... @@ -25,7 +25,7 @@ class ProjectNetworkGraph &lt; Spinach::FeatureSteps
25 25 end
26 26 end
27 27  
28   - And 'I switch ref to "stable"' do
  28 + When 'I switch ref to "stable"' do
29 29 page.select 'stable', :from => 'ref'
30 30 sleep 2
31 31 end
... ... @@ -40,7 +40,7 @@ class ProjectNetworkGraph &lt; Spinach::FeatureSteps
40 40 end
41 41 end
42 42  
43   - And 'I looking for a commit by SHA of "v2.1.0"' do
  43 + When 'I looking for a commit by SHA of "v2.1.0"' do
44 44 within ".content .search" do
45 45 fill_in 'q', :with => '98d6492'
46 46 find('button').click
... ...
features/steps/shared/paths.rb
... ... @@ -142,8 +142,8 @@ module SharedPaths
142 142 end
143 143  
144 144 Given "I visit my project's network page" do
145   - # Stub Graph::JsonBuilder max_size to speed up test (10 commits vs. 650)
146   - Graph::JsonBuilder.stub(max_count: 10)
  145 + # Stub Graph max_size to speed up test (10 commits vs. 650)
  146 + Network::Graph.stub(max_count: 10)
147 147  
148 148 visit project_graph_path(@project, root_ref)
149 149 end
... ...