summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Edwards-Jones <jedwardsjones@gitlab.com>2018-01-12 18:19:40 +0000
committerJames Edwards-Jones <jedwardsjones@gitlab.com>2018-01-12 18:19:40 +0000
commit7d626f5a8377b9634b1844624726b03615dd537c (patch)
tree77f27dad9a1e4fe3c9e046cb6e453fa390251ecf
parentd3eff5cbbfd2ac74d7898ef32556d9f5b073cbb3 (diff)
downloadgitlab-ce-7d626f5a8377b9634b1844624726b03615dd537c.tar.gz
AttributeMatcher helper for detecting LFS pathsjej/gitattributes-recursive-parse
-rw-r--r--lib/gitlab/git/attributes_matcher.rb28
-rw-r--r--spec/lib/gitlab/git/attributes_matcher_spec.rb33
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