Skip to content
Snippets Groups Projects
Verified Commit dd65dcef authored by Yorick Peterse's avatar Yorick Peterse
Browse files

Fix matching Git attributes using absolute paths

Git attributes can be defined using an absolute path. This commit fixes
the matching procedure so that attributes and paths with leading slashes
are supported properly.
parent 209cae3e
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -18,7 +18,7 @@ module Gitlab
class Attributes
# path - The path to the Git repository.
def initialize(path)
@path = path
@path = File.expand_path(path)
@patterns = nil
end
 
Loading
Loading
@@ -28,8 +28,10 @@ module Gitlab
#
# Returns a Hash.
def attributes(path)
full_path = File.join(@path, path)
patterns.each do |pattern, attrs|
return attrs if File.fnmatch(pattern, path)
return attrs if File.fnmatch?(pattern, full_path)
end
 
{}
Loading
Loading
@@ -107,7 +109,7 @@ module Gitlab
 
pattern, attrs = line.split(/\s+/, 2)
 
pairs << [pattern, parse_attributes(attrs)]
pairs << [File.join(@path, pattern), parse_attributes(attrs)]
end
 
# Newer entries take precedence over older entries.
Loading
Loading
require 'spec_helper'
 
describe Gitlab::Git::Attributes do
let(:path) { File.join(SUPPORT_PATH, 'with-git-attributes.git') }
let(:path) do
File.expand_path(File.join(SUPPORT_PATH, 'with-git-attributes.git'))
end
 
subject { described_class.new(path) }
 
Loading
Loading
@@ -29,6 +31,20 @@ describe Gitlab::Git::Attributes do
expect(subject.attributes('foo.cgi')).
to eq({ 'key' => 'value?p1=v1&p2=v2' })
end
it 'returns a Hash containing the attributes for an absolute path' do
expect(subject.attributes('/test.txt')).to eq({ 'text' => true })
end
it 'returns a Hash containing the attributes when a pattern is defined using an absolute path' do
# When a path is given without a leading slash it should still match
# patterns defined with a leading slash.
expect(subject.attributes('foo.png')).
to eq({ 'gitlab-language' => 'png' })
expect(subject.attributes('/foo.png')).
to eq({ 'gitlab-language' => 'png' })
end
end
 
context 'using a path without any attributes' do
Loading
Loading
@@ -44,20 +60,20 @@ describe Gitlab::Git::Attributes do
end
 
it 'parses an entry that uses a tab to separate the pattern and attributes' do
expect(subject.patterns['*.md']).
expect(subject.patterns[File.join(path, '*.md')]).
to eq({ 'gitlab-language' => 'markdown' })
end
 
it 'stores patterns in reverse order' do
first = subject.patterns.to_a[0]
 
expect(first[0]).to eq('*.md')
expect(first[0]).to eq(File.join(path, '*.md'))
end
 
# It's a bit hard to test for something _not_ being processed. As such we'll
# just test the number of entries.
it 'ignores any comments and empty lines' do
expect(subject.patterns.length).to eq(7)
expect(subject.patterns.length).to eq(8)
end
 
it 'does not parse anything when the attributes file does not exist' do
Loading
Loading
@@ -97,7 +113,7 @@ describe Gitlab::Git::Attributes do
 
describe '#each_line' do
it 'iterates over every line in the attributes file' do
args = [String] * 11 # the number of lines in the file
args = [String] * 12 # the number of lines in the file
 
expect { |b| subject.each_line(&b) }.to yield_successive_args(*args)
end
Loading
Loading
Loading
Loading
@@ -54,6 +54,7 @@ module SeedHelper
*.haml.* gitlab-language=haml
foo/bar.* foo
*.cgi key=value?p1=v1&p2=v2
/*.png gitlab-language=png
 
# This uses a tab instead of spaces to ensure the parser also supports this.
*.md\tgitlab-language=markdown
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment