diff options
author | Nick Thomas <nick@gitlab.com> | 2018-09-04 19:30:52 +0300 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2018-09-06 18:04:21 +0300 |
commit | bc3d1afe1398a32e3fab42b05092bca5f2c6c745 (patch) | |
tree | 323cbce1e75c2f8e1f33af8086c44c9174c78d87 /app/controllers | |
parent | 43b91111d698c97c9d07617bb36db8d92bb9a0d9 (diff) |
Extract tree summary logic out of RefsController#logs_tree
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/projects/refs_controller.rb | 65 |
1 files changed, 20 insertions, 45 deletions
diff --git a/app/controllers/projects/refs_controller.rb b/app/controllers/projects/refs_controller.rb index 97231b09c05..200054b4746 100644 --- a/app/controllers/projects/refs_controller.rb +++ b/app/controllers/projects/refs_controller.rb @@ -36,65 +36,40 @@ class Projects::RefsController < Projects::ApplicationController end def logs_tree - @resolved_commits ||= {} - @limit = 25 - @offset = params[:offset].to_i - @path = params[:path] + summary = ::Gitlab::TreeSummary.new( + @commit, + @project, + path: @path, + offset: params[:offset], + limit: 25 + ) - contents = [] - contents.push(*tree.trees) - contents.push(*tree.blobs) - contents.push(*tree.submodules) - - # n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37433 - @logs = Gitlab::GitalyClient.allow_n_plus_1_calls do - contents[@offset, @limit].to_a.map do |content| - file = @path ? File.join(@path, content.name) : content.name - last_commit = @repo.last_commit_for_path(@commit.id, file) - commit_path = project_commit_path(@project, last_commit) if last_commit - - { - file_name: content.name, - commit: resolve_commit(last_commit), - type: content.type, - commit_path: commit_path - } - end - end - - prerender_commits! - - offset = (@offset + @limit) - if contents.size > offset - @more_log_url = logs_file_project_ref_path(@project, @ref, @path || '', offset: offset) - end + @logs, commits = summary.summarize + @more_log_url = more_url(summary.next_offset) if summary.more? respond_to do |format| format.html { render_404 } format.json do - response.headers["More-Logs-Url"] = @more_log_url - + response.headers["More-Logs-Url"] = @more_log_url if summary.more? render json: @logs end - format.js + + # The commit titles must be rendered and redacted before being shown. + # Doing it here allows us to apply performance optimizations that avoid + # N+1 problems + format.js do + prerender_commit_full_titles!(commits) + end end end private - # 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 - def resolve_commit(raw_commit) - return nil unless raw_commit.present? - - @resolved_commits[raw_commit.id] ||= Commit.new(raw_commit, project) + def more_url(offset) + logs_file_project_ref_path(@project, @ref, @path, offset: offset) end - # The commit titles must be passed through Banzai before being shown. - # Doing this here in bulk allows significant database work to be skipped. - def prerender_commits! - commits = @resolved_commits.values + def prerender_commit_full_titles!(commits) renderer = Banzai::ObjectRenderer.new(user: current_user, default_project: @project) renderer.render(commits, :full_title) end |