Match full file path in FileDetector

The basename appears to have been a holdover from the past - it doesn't look
necessary now. Some of the regexes were unanchored on one side, so explicitly
ensure that they only match the root.

Apart from that, this means that pushing to foo/README.md will no longer
invalidate the main README cache for a project.
This commit is contained in:
Sean McGivern 2017-10-11 16:47:03 +01:00
parent 90f95f2b8c
commit 218e1f0963
3 changed files with 19 additions and 15 deletions

View file

@ -156,7 +156,9 @@ class Blob < SimpleDelegator
end
def file_type
Gitlab::FileDetector.type_of(path)
name = File.basename(path)
Gitlab::FileDetector.type_of(path) || Gitlab::FileDetector.type_of(name)
end
def video?

View file

@ -6,10 +6,10 @@ module Gitlab
module FileDetector
PATTERNS = {
# Project files
readme: /\Areadme/i,
changelog: /\A(changelog|history|changes|news)/i,
license: /\A(licen[sc]e|copying)(\..+|\z)/i,
contributing: /\Acontributing/i,
readme: /\Areadme[^\/]*\z/i,
changelog: /\A(changelog|history|changes|news)[^\/]*\z/i,
license: /\A(licen[sc]e|copying)(\.[^\/]+)?\z/i,
contributing: /\Acontributing[^\/]*\z/i,
version: 'version',
avatar: /\Alogo\.(png|jpg|gif)\z/,
@ -17,20 +17,20 @@ module Gitlab
gitignore: '.gitignore',
koding: '.koding.yml',
gitlab_ci: '.gitlab-ci.yml',
route_map: 'route-map.yml',
route_map: '.gitlab/route-map.yml',
# Dependency files
cartfile: /\ACartfile/,
cartfile: /\ACartfile[^\/]*\z/,
composer_json: 'composer.json',
gemfile: /\A(Gemfile|gems\.rb)\z/,
gemfile_lock: 'Gemfile.lock',
gemspec: /\.gemspec\z/,
gemspec: /\A[^\/]*\.gemspec\z/,
godeps_json: 'Godeps.json',
package_json: 'package.json',
podfile: 'Podfile',
podspec_json: /\.podspec\.json\z/,
podspec: /\.podspec\z/,
requirements_txt: /requirements\.txt\z/,
podspec_json: /\A[^\/]*\.podspec\.json\z/,
podspec: /\A[^\/]*\.podspec\z/,
requirements_txt: /\A[^\/]*requirements\.txt\z/,
yarn_lock: 'yarn.lock'
}.freeze
@ -63,13 +63,11 @@ module Gitlab
# type_of('README.md') # => :readme
# type_of('VERSION') # => :version
def self.type_of(path)
name = File.basename(path)
PATTERNS.each do |type, search|
did_match = if search.is_a?(Regexp)
name =~ search
path =~ search
else
name.casecmp(search) == 0
path.casecmp(search) == 0
end
return type if did_match

View file

@ -18,6 +18,10 @@ describe Gitlab::FileDetector do
expect(described_class.type_of('README.md')).to eq(:readme)
end
it 'returns nil for a README file in a directory' do
expect(described_class.type_of('foo/README.md')).to be_nil
end
it 'returns the type of a changelog file' do
%w(CHANGELOG HISTORY CHANGES NEWS).each do |file|
expect(described_class.type_of(file)).to eq(:changelog)