summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Chao <mchao@gitlab.com>2018-09-06 10:59:52 +0800
committerMark Chao <mchao@gitlab.com>2018-10-30 15:44:55 +0800
commit6580de78bb52323fcafaaf4d2e770590e4f1c586 (patch)
treee310ab5c7342b82c1ce89d7653871b26758e88fd
parent6f0378485761ae476bf373b7cddb611da05caa9d (diff)
downloadgitlab-ce-6580de78bb52323fcafaaf4d2e770590e4f1c586.tar.gz
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.
-rw-r--r--app/models/blob.rb7
-rw-r--r--lib/gitlab/search_results.rb6
-rw-r--r--spec/models/blob_spec.rb16
3 files changed, 29 insertions, 0 deletions
diff --git a/app/models/blob.rb b/app/models/blob.rb
index 31a839274b5..e5854415dd2 100644
--- a/app/models/blob.rb
+++ b/app/models/blob.rb
@@ -184,6 +184,13 @@ class Blob < SimpleDelegator
Gitlab::FileDetector.type_of(path) || Gitlab::FileDetector.type_of(name)
end
+ def language_from_gitattributes
+ return nil unless project
+
+ repository = project.repository
+ repository.gitattribute(path, 'gitlab-language')
+ end
+
def video?
UploaderHelper::VIDEO_EXT.include?(extension)
end
diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb
index 5ce3eda2ccb..f2795c739f5 100644
--- a/lib/gitlab/search_results.rb
+++ b/lib/gitlab/search_results.rb
@@ -25,6 +25,12 @@ module Gitlab
def no_highlighting?
false
end
+
+ # Since search results often contain many items,
+ # not triggering lookup can avoid n+1 queries.
+ def language_from_gitattributes
+ nil
+ end
end
attr_reader :current_user, :query, :per_page
diff --git a/spec/models/blob_spec.rb b/spec/models/blob_spec.rb
index 81e35e6c931..da3b29dffb6 100644
--- a/spec/models/blob_spec.rb
+++ b/spec/models/blob_spec.rb
@@ -224,6 +224,22 @@ describe Blob do
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
context 'when the blob is empty' do
it 'returns an empty viewer' do