Commit 96c627edd8ed3a74a28200d77d3fc42abe9fb19b

Authored by Dmitriy Zaporozhets
2 parents 8e8372d5 c0d312be

Merge pull request #3169 from hiroponz/refactor-network-graph

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