Class: Gitlab::TreeSummary

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize, MarkupHelper
Defined in:
lib/gitlab/tree_summary.rb

Constant Summary collapse

CACHE_EXPIRE_IN =
1.hour
MAX_OFFSET =
2**31 - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MarkupHelper

#cross_project_reference, #first_line_in_markdown, #link_to_html, #link_to_markdown, #link_to_markdown_field, #markdown, #markdown_field, #markup, #render_wiki_content

Constructor Details

#initialize(commit, project, user, params = {}) ⇒ TreeSummary

Returns a new instance of TreeSummary.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gitlab/tree_summary.rb', line 13

def initialize(commit, project, user, params = {})
  @commit = commit
  @project = project
  @user = user

  @path = params.fetch(:path, nil).presence
  @offset = [params.fetch(:offset, 0).to_i, MAX_OFFSET].min
  @limit = (params.fetch(:limit, 25) || 25).to_i

  # Ensure that if multiple tree entries share the same last commit, they share
  # a ::Commit instance. This prevents us from rendering the same commit title
  # multiple times
  @resolved_commits = {}
end

Instance Attribute Details

#commitObject (readonly)

Returns the value of attribute commit.



11
12
13
# File 'lib/gitlab/tree_summary.rb', line 11

def commit
  @commit
end

#limitObject (readonly)

Returns the value of attribute limit.



11
12
13
# File 'lib/gitlab/tree_summary.rb', line 11

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



11
12
13
# File 'lib/gitlab/tree_summary.rb', line 11

def offset
  @offset
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/gitlab/tree_summary.rb', line 11

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



11
12
13
# File 'lib/gitlab/tree_summary.rb', line 11

def project
  @project
end

#resolved_commitsObject (readonly)

Returns the value of attribute resolved_commits.



11
12
13
# File 'lib/gitlab/tree_summary.rb', line 11

def resolved_commits
  @resolved_commits
end

#userObject (readonly)

Returns the value of attribute user.



11
12
13
# File 'lib/gitlab/tree_summary.rb', line 11

def user
  @user
end

Instance Method Details

#fetch_logsObject



55
56
57
58
59
# File 'lib/gitlab/tree_summary.rb', line 55

def fetch_logs
  logs = summarize

  [logs.first(limit).as_json, next_offset(logs.size)]
end

#summarizeObject

Creates a summary of the tree entries for a commit, within the window of entries defined by the offset and limit parameters. This consists of two return values:

- An Array of Hashes containing the following keys:
    - file_name:   The full path of the tree entry
    - commit:      The last ::Commit to touch this entry in the tree
    - commit_path: URI of the commit in the web interface
    - commit_title_html: Rendered commit title


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gitlab/tree_summary.rb', line 37

def summarize
  return [] if offset < 0

  commits_hsh = fetch_last_cached_commits_list
  prerender_commit_full_titles!(commits_hsh.values)

  commits_hsh.map do |path_key, commit|
    commit = cache_commit(commit)

    {
      file_name: File.basename(path_key).force_encoding(Encoding::UTF_8),
      commit: commit,
      commit_path: commit_path(commit),
      commit_title_html: markdown_field(commit, :full_title)
    }
  end
end