Commit 218e1f09 authored by Sean McGivern's avatar Sean McGivern

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.
parent 90f95f2b
...@@ -156,7 +156,9 @@ class Blob < SimpleDelegator ...@@ -156,7 +156,9 @@ class Blob < SimpleDelegator
end end
def file_type def file_type
Gitlab::FileDetector.type_of(path) name = File.basename(path)
Gitlab::FileDetector.type_of(path) || Gitlab::FileDetector.type_of(name)
end end
def video? def video?
......
...@@ -6,10 +6,10 @@ module Gitlab ...@@ -6,10 +6,10 @@ module Gitlab
module FileDetector module FileDetector
PATTERNS = { PATTERNS = {
# Project files # Project files
readme: /\Areadme/i, readme: /\Areadme[^\/]*\z/i,
changelog: /\A(changelog|history|changes|news)/i, changelog: /\A(changelog|history|changes|news)[^\/]*\z/i,
license: /\A(licen[sc]e|copying)(\..+|\z)/i, license: /\A(licen[sc]e|copying)(\.[^\/]+)?\z/i,
contributing: /\Acontributing/i, contributing: /\Acontributing[^\/]*\z/i,
version: 'version', version: 'version',
avatar: /\Alogo\.(png|jpg|gif)\z/, avatar: /\Alogo\.(png|jpg|gif)\z/,
...@@ -17,20 +17,20 @@ module Gitlab ...@@ -17,20 +17,20 @@ module Gitlab
gitignore: '.gitignore', gitignore: '.gitignore',
koding: '.koding.yml', koding: '.koding.yml',
gitlab_ci: '.gitlab-ci.yml', gitlab_ci: '.gitlab-ci.yml',
route_map: 'route-map.yml', route_map: '.gitlab/route-map.yml',
# Dependency files # Dependency files
cartfile: /\ACartfile/, cartfile: /\ACartfile[^\/]*\z/,
composer_json: 'composer.json', composer_json: 'composer.json',
gemfile: /\A(Gemfile|gems\.rb)\z/, gemfile: /\A(Gemfile|gems\.rb)\z/,
gemfile_lock: 'Gemfile.lock', gemfile_lock: 'Gemfile.lock',
gemspec: /\.gemspec\z/, gemspec: /\A[^\/]*\.gemspec\z/,
godeps_json: 'Godeps.json', godeps_json: 'Godeps.json',
package_json: 'package.json', package_json: 'package.json',
podfile: 'Podfile', podfile: 'Podfile',
podspec_json: /\.podspec\.json\z/, podspec_json: /\A[^\/]*\.podspec\.json\z/,
podspec: /\.podspec\z/, podspec: /\A[^\/]*\.podspec\z/,
requirements_txt: /requirements\.txt\z/, requirements_txt: /\A[^\/]*requirements\.txt\z/,
yarn_lock: 'yarn.lock' yarn_lock: 'yarn.lock'
}.freeze }.freeze
...@@ -63,13 +63,11 @@ module Gitlab ...@@ -63,13 +63,11 @@ module Gitlab
# type_of('README.md') # => :readme # type_of('README.md') # => :readme
# type_of('VERSION') # => :version # type_of('VERSION') # => :version
def self.type_of(path) def self.type_of(path)
name = File.basename(path)
PATTERNS.each do |type, search| PATTERNS.each do |type, search|
did_match = if search.is_a?(Regexp) did_match = if search.is_a?(Regexp)
name =~ search path =~ search
else else
name.casecmp(search) == 0 path.casecmp(search) == 0
end end
return type if did_match return type if did_match
......
...@@ -18,6 +18,10 @@ describe Gitlab::FileDetector do ...@@ -18,6 +18,10 @@ describe Gitlab::FileDetector do
expect(described_class.type_of('README.md')).to eq(:readme) expect(described_class.type_of('README.md')).to eq(:readme)
end 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 it 'returns the type of a changelog file' do
%w(CHANGELOG HISTORY CHANGES NEWS).each do |file| %w(CHANGELOG HISTORY CHANGES NEWS).each do |file|
expect(described_class.type_of(file)).to eq(:changelog) expect(described_class.type_of(file)).to eq(:changelog)
......
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