Commit bd1b8ae9 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'relative-link-filter-ref' into 'master'

Do not look up commit again when it is passed to RelativeLinkFilter

## What does this MR do?

Use `context[:commit]` in RelativeLinkFilter instead of looking up commit using `context[:ref]`.

## Why was this MR needed?

Even though the commit object was already passed, unnecessary I/O is done to retrieve the commit object.

## What are the relevant issue numbers?

Fixes #20026

See merge request !5455
parents 936729e5 e63eccf9
...@@ -52,7 +52,7 @@ module Banzai ...@@ -52,7 +52,7 @@ module Banzai
relative_url_root, relative_url_root,
context[:project].path_with_namespace, context[:project].path_with_namespace,
uri_type(file_path), uri_type(file_path),
ref || context[:project].default_branch, # if no ref exists, point to the default branch ref,
file_path file_path
].compact.join('/').squeeze('/').chomp('/') ].compact.join('/').squeeze('/').chomp('/')
...@@ -116,7 +116,7 @@ module Banzai ...@@ -116,7 +116,7 @@ module Banzai
end end
def current_commit def current_commit
@current_commit ||= context[:commit] || ref ? repository.commit(ref) : repository.head_commit @current_commit ||= context[:commit] || repository.commit(ref)
end end
def relative_url_root def relative_url_root
...@@ -124,7 +124,7 @@ module Banzai ...@@ -124,7 +124,7 @@ module Banzai
end end
def ref def ref
context[:ref] context[:ref] || context[:project].default_branch
end end
def repository def repository
......
...@@ -3,7 +3,7 @@ require 'spec_helper' ...@@ -3,7 +3,7 @@ require 'spec_helper'
describe Banzai::Filter::RelativeLinkFilter, lib: true do describe Banzai::Filter::RelativeLinkFilter, lib: true do
def filter(doc, contexts = {}) def filter(doc, contexts = {})
contexts.reverse_merge!({ contexts.reverse_merge!({
commit: project.commit, commit: commit,
project: project, project: project,
project_wiki: project_wiki, project_wiki: project_wiki,
ref: ref, ref: ref,
...@@ -28,6 +28,7 @@ describe Banzai::Filter::RelativeLinkFilter, lib: true do ...@@ -28,6 +28,7 @@ describe Banzai::Filter::RelativeLinkFilter, lib: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:project_path) { project.path_with_namespace } let(:project_path) { project.path_with_namespace }
let(:ref) { 'markdown' } let(:ref) { 'markdown' }
let(:commit) { project.commit(ref) }
let(:project_wiki) { nil } let(:project_wiki) { nil }
let(:requested_path) { '/' } let(:requested_path) { '/' }
...@@ -77,7 +78,13 @@ describe Banzai::Filter::RelativeLinkFilter, lib: true do ...@@ -77,7 +78,13 @@ describe Banzai::Filter::RelativeLinkFilter, lib: true do
expect { filter(act) }.not_to raise_error expect { filter(act) }.not_to raise_error
end end
context 'with a valid repository' do it 'ignores ref if commit is passed' do
doc = filter(link('non/existent.file'), commit: project.commit('empty-branch') )
expect(doc.at_css('a')['href']).
to eq "/#{project_path}/#{ref}/non/existent.file" # non-existent files have no leading blob/raw/tree
end
shared_examples :valid_repository do
it 'rebuilds absolute URL for a file in the repo' do it 'rebuilds absolute URL for a file in the repo' do
doc = filter(link('/doc/api/README.md')) doc = filter(link('/doc/api/README.md'))
expect(doc.at_css('a')['href']). expect(doc.at_css('a')['href']).
...@@ -189,4 +196,13 @@ describe Banzai::Filter::RelativeLinkFilter, lib: true do ...@@ -189,4 +196,13 @@ describe Banzai::Filter::RelativeLinkFilter, lib: true do
include_examples :relative_to_requested include_examples :relative_to_requested
end end
end end
context 'with a valid commit' do
include_examples :valid_repository
end
context 'with a valid ref' do
let(:commit) { nil } # force filter to use ref instead of commit
include_examples :valid_repository
end
end end
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