Commit 44a35c2a authored by Mark Chao's avatar Mark Chao

Allow FoundBlob to access language from gitattributes

Extract language_from_git_attributes as a concern so it can
ben included in two blob classes.
parent d2e7b2e9
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
class Blob < SimpleDelegator class Blob < SimpleDelegator
prepend EE::Blob prepend EE::Blob
include Presentable include Presentable
include BlobLanguageFromGitAttributes
CACHE_TIME = 60 # Cache raw blobs referred to by a (mutable) ref for 1 minute CACHE_TIME = 60 # Cache raw blobs referred to by a (mutable) ref for 1 minute
CACHE_TIME_IMMUTABLE = 3600 # Cache blobs referred to by an immutable reference for 1 hour CACHE_TIME_IMMUTABLE = 3600 # Cache blobs referred to by an immutable reference for 1 hour
...@@ -181,13 +182,6 @@ class Blob < SimpleDelegator ...@@ -181,13 +182,6 @@ 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
......
# frozen_string_literal: true
# Applicable for blob classes with project attribute
module BlobLanguageFromGitAttributes
extend ActiveSupport::Concern
def language_from_gitattributes
return nil unless project
repository = project.repository
repository.gitattribute(path, 'gitlab-language')
end
end
...@@ -82,7 +82,7 @@ module Gitlab ...@@ -82,7 +82,7 @@ module Gitlab
ref: ref, ref: ref,
startline: startline, startline: startline,
data: data.join, data: data.join,
project_id: project ? project.id : nil project: project
) )
end end
......
...@@ -5,8 +5,9 @@ module Gitlab ...@@ -5,8 +5,9 @@ module Gitlab
class FoundBlob class FoundBlob
include EncodingHelper include EncodingHelper
include Presentable include Presentable
include BlobLanguageFromGitAttributes
attr_reader :id, :filename, :basename, :ref, :startline, :data, :project_id attr_reader :id, :filename, :basename, :ref, :startline, :data, :project
def initialize(opts = {}) def initialize(opts = {})
@id = opts.fetch(:id, nil) @id = opts.fetch(:id, nil)
...@@ -16,17 +17,15 @@ module Gitlab ...@@ -16,17 +17,15 @@ module Gitlab
@startline = opts.fetch(:startline, nil) @startline = opts.fetch(:startline, nil)
@data = encode_utf8(opts.fetch(:data, nil)) @data = encode_utf8(opts.fetch(:data, nil))
@per_page = opts.fetch(:per_page, 20) @per_page = opts.fetch(:per_page, 20)
@project_id = opts.fetch(:project_id, nil) @project = opts.fetch(:project, nil)
end end
def path def path
filename filename
end end
# Since search results often contain many items, def project_id
# not triggering lookup can avoid n+1 queries. @project&.id
def language_from_gitattributes
nil
end end
def present def present
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
describe 'User searches for wiki pages', :js do describe 'User searches for wiki pages', :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :wiki_repo, namespace: user.namespace) } let(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) }
let!(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'test_wiki', content: 'Some Wiki content' }) } let!(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'test_wiki', content: 'Some Wiki content' }) }
before do before do
......
...@@ -224,22 +224,6 @@ describe Blob do ...@@ -224,22 +224,6 @@ 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
......
# frozen_string_literal: true
require 'spec_helper'
describe BlobLanguageFromGitAttributes do
include FakeBlobHelpers
let(:project) { build(:project, :repository) }
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
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