Welcome to mirror list, hosted at ThFree Co, Russian Federation.

json_builder.rb « graph « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 90d384449f5869c2ef1e9bc7940d2ab1f994320a (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require "grit"

module Gitlab
  module Graph
    class JsonBuilder
      attr_accessor :days, :commits, :ref_cache, :repo

      def self.max_count
        @max_count ||= 650
      end

      def initialize project, ref
        @project = project
        @ref = ref
        @repo = project.repo
        @ref_cache = {}

        @commits = collect_commits
        @days = index_commits
      end

      def to_json(*args)
        {
          days: @days.compact.map { |d| [d.day, d.strftime("%b")] },
          commits: @commits.map(&:to_graph_hash)
        }.to_json(*args)
      end

    protected

      # Get commits from repository
      #
      def collect_commits
        @commits = Grit::Commit.find_all(repo, nil, {max_count: self.class.max_count}).dup

        # Decorate with app/models/commit.rb
        @commits.map! { |commit| ::Commit.new(commit) }

        # Decorate with lib/gitlab/graph/commit.rb
        @commits.map! { |commit| Gitlab::Graph::Commit.new(commit) }

        # add refs to each commit
        @commits.each { |commit| commit.add_refs(ref_cache, repo) }

        @commits
      end

      # Method is adding time and space on the
      # list of commits. As well as returns date list
      # corelated with time set on commits.
      #
      # @param [Array<Graph::Commit>] comits to index
      #
      # @return [Array<TimeDate>] list of commit dates corelated with time on commits
      def index_commits
        days, heads, times = [], [], []
        map = {}

        commits.reverse.each_with_index do |c,i|
          c.time = i
          days[i] = c.committed_date
          map[c.id] = c
          heads += c.refs unless c.refs.nil?
          times[i] = c
        end

        heads.select!{|h| h.is_a? Grit::Head or h.is_a? Grit::Remote}
        # sort heads so the master is top and current branches are closer
        heads.sort! do |a,b|
          if a.name == @ref
            -1
          elsif b.name == @ref
            1
          else
            b.commit.committed_date <=> a.commit.committed_date
          end
        end

        @_reserved = {}
        days.each_index do |i|
          @_reserved[i] = []
        end

        heads.each do |h|
          if map.include? h.commit.id then
            place_chain(map[h.commit.id], map)
          end
        end

        # find parent spaces for not overlap lines
        times.each do |c|
          c.parent_spaces.concat(find_free_parent_spaces(c, map, times))
        end

        days
      end

      def find_free_parent_spaces(commit, map, times)
        spaces = []

        commit.parents.each do |p|
          if map.include?(p.id) then
            parent = map[p.id]

            range = if commit.time < parent.time then
                      commit.time..parent.time
                    else
                      parent.time..commit.time
                    end

            space = if commit.space >= parent.space then
                      find_free_parent_space(range, map, parent.space, 1, commit.space, times)
                    else
                      find_free_parent_space(range, map, parent.space, -1, parent.space, times)
                    end

            mark_reserved(range, space)
            spaces << space
          end
        end

        spaces
      end

      def find_free_parent_space(range, map, space_base, space_step, space_default, times)
        if is_overlap?(range, times, space_default) then
          find_free_space(range, map, space_base, space_step)
        else
          space_default
        end
      end

      def is_overlap?(range, times, overlap_space)
        range.each do |i|
          if i != range.first &&
            i != range.last &&
            times[i].space == overlap_space then

            return true;
          end
        end

        false
      end

      # Add space mark on commit and its parents
      #
      # @param [Graph::Commit] the commit object.
      # @param [Hash<String,Graph::Commit>] map of commits
      def place_chain(commit, map, parent_time = nil)
        leaves = take_left_leaves(commit, map)
        if leaves.empty?
          return
        end
        time_range = leaves.last.time..leaves.first.time
        space = find_free_space(time_range, map, 1, 2)
        leaves.each{|l| l.space = space}
        # and mark it as reserved
        min_time = leaves.last.time
        parents = leaves.last.parents.collect
        parents.each do |p|
          if map.include? p.id
            parent = map[p.id]
            if parent.time < min_time
              min_time = parent.time
            end
          end
        end
        if parent_time.nil?
          max_time = leaves.first.time
        else
          max_time = parent_time - 1
        end
        mark_reserved(min_time..max_time, space)

        # Visit branching chains
        leaves.each do |l|
          parents = l.parents.collect.select{|p| map.include? p.id and map[p.id].space.zero?}
          for p in parents
            place_chain(map[p.id], map, l.time)
          end
        end
      end

      def mark_reserved(time_range, space)
        for day in time_range
          @_reserved[day].push(space)
        end
      end

      def find_free_space(time_range, map, space_base, space_step)
        reserved = []
        for day in time_range
          reserved += @_reserved[day]
        end

        space = space_base
        while reserved.include?(space) do
          space += space_step
          if space <= 0 then
            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.
      #
      # @param [Graph::Commit] the commit object.
      # @param [Hash<String,Graph::Commit>] map of commits
      #
      # @return [Array<Graph::Commit>] list of branch commits
      def take_left_leaves(commit, map)
        leaves = []
        leaves.push(commit) if commit.space.zero?

        while true
          return leaves if commit.parents.count.zero?
          return leaves unless map.include? commit.parents.first.id

          commit = map[commit.parents.first.id]

          return leaves unless commit.space.zero?

          leaves.push(commit)
        end
      end
    end
  end
end