BigW Consortium Gitlab

json_builder.rb 6.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
require "grit"

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

      def self.max_count
        @max_count ||= 650
      end

12
      def initialize project, ref, commit
13
        @project = project
14
        @ref = ref
15
        @commit = commit
16 17 18 19 20 21
        @repo = project.repo
        @ref_cache = {}

        @commits = collect_commits
        @days = index_commits
      end
22

23
      def to_json(*args)
Koen Punt committed
24 25 26
        {
          days: @days.compact.map { |d| [d.day, d.strftime("%b")] },
          commits: @commits.map(&:to_graph_hash)
27
        }.to_json(*args)
28
      end
29

Koen Punt committed
30
    protected
31 32 33 34

      # Get commits from repository
      #
      def collect_commits
35

36
        @commits = Grit::Commit.find_all(repo, nil, {topo_order: true, max_count: self.class.max_count, skip: to_commit}).dup
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

        # 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.
      #
Sato Hiroyuki committed
54
      # @param [Array<Graph::Commit>] commits to index
55 56 57
      #
      # @return [Array<TimeDate>] list of commit dates corelated with time on commits
      def index_commits
58
        days, times = [], []
59 60 61 62 63 64
        map = {}

        commits.reverse.each_with_index do |c,i|
          c.time = i
          days[i] = c.committed_date
          map[c.id] = c
65
          times[i] = c
66 67 68 69 70 71 72
        end

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

73 74 75
        commits_sort_by_ref.each do |commit|
          if map.include? commit.id then
            place_chain(map[commit.id], map)
76 77 78
          end
        end

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

84 85 86
        days
      end

87 88
      # Skip count that the target commit is displayed in center.
      def to_commit
89
        commits = Grit::Commit.find_all(repo, nil, {topo_order: true})
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
        commit_index = commits.index do |c|
          c.id == @commit.id
        end

        if commit_index && (self.class.max_count / 2 < commit_index) then
          # get max index that commit is displayed in the center.
          commit_index - self.class.max_count / 2
        else
          0
        end
      end

      def commits_sort_by_ref
        commits.sort do |a,b|
          if include_ref?(a)
            -1
          elsif include_ref?(b)
            1
          else
            b.committed_date <=> a.committed_date
          end
        end
      end

      def include_ref?(commit)
        heads = commit.refs.select do |ref|
116
          ref.is_a?(Grit::Head) or ref.is_a?(Grit::Remote) or ref.is_a?(Grit::Tag)
117 118 119 120 121 122 123 124 125
        end

        heads.map! do |head|
          head.name
        end

        heads.include?(@ref)
      end

126 127 128 129 130 131 132 133 134 135 136 137 138 139
      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
140
                      find_free_parent_space(range, parent.space, 1, commit.space, times)
141
                    else
142
                      find_free_parent_space(range, parent.space, -1, parent.space, times)
143 144 145 146 147 148 149 150 151 152
                    end

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

        spaces
      end

153
      def find_free_parent_space(range, space_base, space_step, space_default, times)
154
        if is_overlap?(range, times, space_default) then
155
          find_free_space(range, space_base, space_step)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        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

174 175 176 177 178 179 180 181 182 183 184
      # 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
        # and mark it as reserved
        min_time = leaves.last.time
185
        max_space = 1
186 187 188 189 190 191 192
        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
193 194 195
            if max_space < parent.space then
              max_space = parent.space
            end
196 197 198 199 200 201 202
          end
        end
        if parent_time.nil?
          max_time = leaves.first.time
        else
          max_time = parent_time - 1
        end
203 204 205 206 207

        time_range = leaves.last.time..leaves.first.time
        space = find_free_space(time_range, max_space, 2)
        leaves.each{|l| l.space = space}

208
        mark_reserved(min_time..max_time, space)
209 210 211

        # Visit branching chains
        leaves.each do |l|
Sato Hiroyuki committed
212
          parents = l.parents.collect.select{|p| map.include? p.id and map[p.id].space.zero?}
213 214 215 216 217 218 219 220 221 222 223 224
          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

225
      def find_free_space(time_range, space_base, space_step)
226 227 228 229
        reserved = []
        for day in time_range
          reserved += @_reserved[day]
        end
230
        reserved.uniq!
231

232 233 234 235 236 237 238
        space = space_base
        while reserved.include?(space) do
          space += space_step
          if space <= 0 then
            space_step *= -1
            space = space_base + space_step
          end
Sato Hiroyuki committed
239 240
        end

241
        space
Sato Hiroyuki committed
242 243
      end

244 245 246 247 248 249 250 251 252 253 254 255
      # 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
Sato Hiroyuki committed
256 257 258 259
          return leaves if commit.parents.count.zero?
          return leaves unless map.include? commit.parents.first.id

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

Sato Hiroyuki committed
261
          return leaves unless commit.space.zero?
262 263 264 265 266 267 268

          leaves.push(commit)
        end
      end
    end
  end
end