Commit 43b91111 authored by Nick Thomas's avatar Nick Thomas

Deduplicate commits before rendering them in RefsController#logs_tree

parent d9833890
...@@ -36,14 +36,9 @@ class Projects::RefsController < Projects::ApplicationController ...@@ -36,14 +36,9 @@ class Projects::RefsController < Projects::ApplicationController
end end
def logs_tree def logs_tree
@offset = if params[:offset].present? @resolved_commits ||= {}
params[:offset].to_i
else
0
end
@limit = 25 @limit = 25
@offset = params[:offset].to_i
@path = params[:path] @path = params[:path]
contents = [] contents = []
...@@ -57,19 +52,17 @@ class Projects::RefsController < Projects::ApplicationController ...@@ -57,19 +52,17 @@ class Projects::RefsController < Projects::ApplicationController
file = @path ? File.join(@path, content.name) : content.name file = @path ? File.join(@path, content.name) : content.name
last_commit = @repo.last_commit_for_path(@commit.id, file) last_commit = @repo.last_commit_for_path(@commit.id, file)
commit_path = project_commit_path(@project, last_commit) if last_commit commit_path = project_commit_path(@project, last_commit) if last_commit
{ {
file_name: content.name, file_name: content.name,
# TODO: deduplicate commits so we don't render the same one multiple times commit: resolve_commit(last_commit),
commit: (Commit.new(last_commit, project) if last_commit),
type: content.type, type: content.type,
commit_path: commit_path commit_path: commit_path
} }
end end
end end
# The commit titles must be passed through Banzai before being shown. prerender_commits!
# Doing this here in bulk allows significant database work to be skipped.
prerender_commits!(@logs.map { |log| log[:commit] })
offset = (@offset + @limit) offset = (@offset + @limit)
if contents.size > offset if contents.size > offset
...@@ -89,9 +82,21 @@ class Projects::RefsController < Projects::ApplicationController ...@@ -89,9 +82,21 @@ class Projects::RefsController < Projects::ApplicationController
private private
def prerender_commits!(commits) # 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)
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
renderer = Banzai::ObjectRenderer.new(user: current_user, default_project: @project) renderer = Banzai::ObjectRenderer.new(user: current_user, default_project: @project)
renderer.render(commits, :full_title) # modifies the commit objects inplace renderer.render(commits, :full_title)
end end
def validate_ref_id def validate_ref_id
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment