json_builder.rb 7.15 KB
Newer Older
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
require "grit"

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

    def self.max_count
      @max_count ||= 650
    end

    def initialize project, ref, commit
      @project = project
      @ref = ref
      @commit = commit
      @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

35
      @commits = Grit::Commit.find_all(repo, nil, {date_order: true, max_count: self.class.max_count, skip: to_commit}).dup
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

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

      # Decorate with lib/gitlab/graph/commit.rb
      @commits.map! { |commit| 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>] commits to index
    #
    # @return [Array<TimeDate>] list of commit dates corelated with time on commits
    def index_commits
      days, times = [], []
      map = {}

      commits.reverse.each_with_index do |c,i|
        c.time = i
        days[i] = c.committed_date
        map[c.id] = c
        times[i] = c
      end

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

      commits_sort_by_ref.each do |commit|
        if map.include? commit.id then
          place_chain(map[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

    # Skip count that the target commit is displayed in center.
    def to_commit
88
      commits = Grit::Commit.find_all(repo, nil, {date_order: true})
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
      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|
        ref.is_a?(Grit::Head) or ref.is_a?(Grit::Remote) or ref.is_a?(Grit::Tag)
      end

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

      heads.include?(@ref)
    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
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 143 144 145 146 147 148 149 150 151 152 153
                  end

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

      spaces
    end

    def find_free_parent_space(range, space_base, space_step, space_default, times)
      if is_overlap?(range, times, space_default) then
154
        find_free_space(range, space_step, space_base, space_default)
155 156 157 158 159 160 161 162 163
      else
        space_default
      end
    end

    def is_overlap?(range, times, overlap_space)
      range.each do |i|
        if i != range.first &&
          i != range.last &&
164
          times[i].spaces.include?(overlap_space) then
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

          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
182 183

      time_range = leaves.last.time..leaves.first.time
184 185
      space_base = get_space_base(leaves, map)
      space = find_free_space(time_range, 2, space_base)
186 187 188 189 190 191 192 193 194 195 196 197 198
      leaves.each do |l|
        l.spaces << space
        # Also add space to parent
        l.parents.each do |p|
          if map.include?(p.id)
            parent = map[p.id]
            if parent.space > 0
              parent.spaces << space
            end
          end
        end
      end

199 200 201 202 203 204 205 206 207 208 209
      # 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
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
      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

227 228 229 230 231 232 233 234 235 236 237 238 239 240
    def get_space_base(leaves, map)
      space_base = 1
      if leaves.last.parents.size > 0
        first_parent = leaves.last.parents.first
        if map.include?(first_parent.id)
          first_p = map[first_parent.id]
          if first_p.space > 0
            space_base = first_p.space
          end
        end
      end
      space_base
    end

241 242 243 244 245 246
    def mark_reserved(time_range, space)
      for day in time_range
        @_reserved[day].push(space)
      end
    end

247 248 249
    def find_free_space(time_range, space_step, space_base = 1, space_default = nil)
      space_default ||= space_base

250 251 252 253 254 255
      reserved = []
      for day in time_range
        reserved += @_reserved[day]
      end
      reserved.uniq!

256
      space = space_default
257 258
      while reserved.include?(space) do
        space += space_step
259
        if space < space_base then
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
          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