Commit 6580de78 authored by Mark Chao's avatar Mark Chao

Add access to Blob's language from gitattributes

Ported from Highlight class since it as a concept is more related to
blob, and this allows more flexibility.
parent 6f037848
...@@ -184,6 +184,13 @@ class Blob < SimpleDelegator ...@@ -184,6 +184,13 @@ class Blob < SimpleDelegator
Gitlab::FileDetector.type_of(path) || Gitlab::FileDetector.type_of(name) Gitlab::FileDetector.type_of(path) || Gitlab::FileDetector.type_of(name)
end end
def language_from_gitattributes
return nil unless project
repository = project.repository
repository.gitattribute(path, 'gitlab-language')
end
def video? def video?
UploaderHelper::VIDEO_EXT.include?(extension) UploaderHelper::VIDEO_EXT.include?(extension)
end end
......
...@@ -25,6 +25,12 @@ module Gitlab ...@@ -25,6 +25,12 @@ module Gitlab
def no_highlighting? def no_highlighting?
false false
end end
# Since search results often contain many items,
# not triggering lookup can avoid n+1 queries.
def language_from_gitattributes
nil
end
end end
attr_reader :current_user, :query, :per_page attr_reader :current_user, :query, :per_page
......
...@@ -224,6 +224,22 @@ describe Blob do ...@@ -224,6 +224,22 @@ describe Blob do
end end
end end
describe '#language_from_gitattributes' do
subject(:blob) { fake_blob(path: 'file.md') }
it 'returns return value from gitattribute' do
expect(blob.project.repository).to receive(:gitattribute).with(blob.path, 'gitlab-language').and_return('erb?parent=json')
expect(blob.language_from_gitattributes).to eq('erb?parent=json')
end
it 'returns nil if project is absent' do
allow(blob).to receive(:project).and_return(nil)
expect(blob.language_from_gitattributes).to eq(nil)
end
end
describe '#simple_viewer' do describe '#simple_viewer' do
context 'when the blob is empty' do context 'when the blob is empty' do
it 'returns an empty viewer' do it 'returns an empty viewer' do
......
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