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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MarkupHelper

#asciidoc?, #cross_project_reference, #first_line_in_markdown, #gitlab_markdown?, #link_to_html, #link_to_markdown, #link_to_markdown_field, #markdown, #markdown_field, #markup, #markup?, #markup_unsafe, #plain?, #render_wiki_content

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

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

Returns a new instance of TreeSummary.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/tree_summary.rb', line 16

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

#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


49
50
51
52
53
54
55
56
57
58
# File 'lib/gitlab/tree_summary.rb', line 49

def fetch_logs
  cache_key = ['projects', project.id, 'logs', commit.id, path, offset]
  Rails.cache.fetch(cache_key, expires_in: CACHE_EXPIRE_IN) do
    logs, _ = summarize

    new_offset = next_offset if more?

    [logs.as_json, new_offset]
  end
end

#more?Boolean

Does the tree contain more entries after the given offset + limit?

Returns:

  • (Boolean)

61
62
63
# File 'lib/gitlab/tree_summary.rb', line 61

def more?
  all_contents[next_offset].present?
end

#next_offsetObject

The offset of the next batch of tree entries. If more? returns false, this batch will be empty


67
68
69
# File 'lib/gitlab/tree_summary.rb', line 67

def next_offset
  [all_contents.size + 1, offset + limit].min
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
    - type:        One of :blob, :tree, or :submodule
    - commit:      The last ::Commit to touch this entry in the tree
    - commit_path: URI of the commit in the web interface
- An Array of the unique ::Commit objects in the first value

41
42
43
44
45
46
47
# File 'lib/gitlab/tree_summary.rb', line 41

def summarize
  summary = contents
    .map { |content| build_entry(content) }
    .tap { |summary| fill_last_commits!(summary) }

  [summary, commits]
end