diff options
-rw-r--r-- | lib/gitlab/git/attributes_matcher.rb | 28 | ||||
-rw-r--r-- | spec/lib/gitlab/git/attributes_matcher_spec.rb | 33 |
2 files changed, 61 insertions, 0 deletions
diff --git a/lib/gitlab/git/attributes_matcher.rb b/lib/gitlab/git/attributes_matcher.rb new file mode 100644 index 00000000000..b1744544922 --- /dev/null +++ b/lib/gitlab/git/attributes_matcher.rb @@ -0,0 +1,28 @@ +module Gitlab + module Git + #TODO: refactor to take in attributes_hash and make LFS primary purpose + class AttributesMatcher + def initialize(repository, ref, recursive: false) + @repository = repository + @ref = ref + @recursive = recursive + end + + def lfs?(file_path) + matches_filter(file_path, 'lfs') + end + + def matches_filter?(file_path, filter) + lookup(file_path)['filter'] == filter + end + + def includes?(file_path, key) + lookup(file_path)[key].present? + end + + def lookup(file_path) + @repository.attributes_at(@ref, file_path, recursive: @recursive) + end + end + end +end diff --git a/spec/lib/gitlab/git/attributes_matcher_spec.rb b/spec/lib/gitlab/git/attributes_matcher_spec.rb new file mode 100644 index 00000000000..3a4514dbcfc --- /dev/null +++ b/spec/lib/gitlab/git/attributes_matcher_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe Gitlab::Git::AttributesMatcher do + let(:project) { create(:project, :repository) } + let(:repo) { project.repository } + let(:ref) { 'lfs' } + + subject { described_class.new(repo, ref) } + + describe '#matches_filter?' do + it 'is truthy if filter matches path' do + expect(subject.matches_filter?('large_file.lfs', 'lfs')).to be_truthy + end + + it "is falsey if filter doesn't match path" do + expect(subject.matches_filter?('small_file.txt', 'lfs')).to be_falsey + end + end + + describe '#includes?' do + it 'is truthy if key evalutes true' do + expect(subject.includes?('large_file.lfs', 'merge')).to be_truthy + end + + it "is falsey if key evalues false" do + expect(subject.includes?('large_file.lfs', 'text')).to be_falsey + end + + it "is falsey if key is missing" do + expect(subject.includes?('small_file.txt', 'merge')).to be_falsey + end + end +end |