From b160a396789549c65f6366e3cbf5647348793377 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 29 Sep 2014 13:48:40 -0400 Subject: [PATCH 1/4] Remove linguist_* prefix from vendored? and generated? --- lib/linguist/lazy_blob.rb | 8 ++++---- lib/linguist/repository.rb | 2 +- test/test_repository.rb | 14 ++++---------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/linguist/lazy_blob.rb b/lib/linguist/lazy_blob.rb index 131cafcf..3ece29f2 100644 --- a/lib/linguist/lazy_blob.rb +++ b/lib/linguist/lazy_blob.rb @@ -29,19 +29,19 @@ module Linguist name, GIT_ATTR, GIT_ATTR_FLAGS) end - def linguist_vendored? + def vendored? if git_attributes['linguist-vendored'] return result_for_key('linguist-vendored') else - return vendored? + return super end end - def linguist_generated? + def generated? if git_attributes['linguist-generated'] return result_for_key('linguist-generated') else - return generated? + return super end end diff --git a/lib/linguist/repository.rb b/lib/linguist/repository.rb index ae3dd1f3..f2bcb785 100644 --- a/lib/linguist/repository.rb +++ b/lib/linguist/repository.rb @@ -142,7 +142,7 @@ module Linguist blob = Linguist::LazyBlob.new(repository, delta.new_file[:oid], new, mode.to_s(8)) # Skip vendored or generated blobs - next if blob.linguist_vendored? || blob.linguist_generated? || blob.language.nil? + next if blob.vendored? || blob.generated? || blob.language.nil? # Only include programming languages and acceptable markup languages if blob.language.type == :programming || Language.detectable_markup.include?(blob.language.name) diff --git a/test/test_repository.rb b/test/test_repository.rb index d2ce510a..dbd7e3a9 100644 --- a/test/test_repository.rb +++ b/test/test_repository.rb @@ -69,7 +69,7 @@ class TestRepository < Test::Unit::TestCase end - def test_linguist_generated? + def test_linguist_override_generated? attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' file = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Rakefile') @@ -84,9 +84,7 @@ class TestRepository < Test::Unit::TestCase # check we're getting the correct assignment back from .gitattributes assert file.result_for_key('linguist-generated') # overridden in .gitattributes - assert file.linguist_generated? - # from lib/linguist/generated.rb - assert !file.generated? + assert file.generated? end def test_linguist_override_vendored? @@ -103,9 +101,7 @@ class TestRepository < Test::Unit::TestCase # check we're getting the correct assignment back from .gitattributes assert override_vendored.result_for_key('linguist-vendored') # overridden .gitattributes - assert override_vendored.linguist_vendored? - # from lib/linguist/vendor.yml - assert !override_vendored.vendored? + assert override_vendored.vendored? end def test_linguist_override_unvendored? @@ -124,8 +120,6 @@ class TestRepository < Test::Unit::TestCase # check we're getting the correct assignment back from .gitattributes assert !override_unvendored.result_for_key('linguist-vendored') # overridden .gitattributes - assert !override_unvendored.linguist_vendored? - # from lib/linguist/vendor.yml - assert override_unvendored.vendored? + assert !override_unvendored.vendored? end end From 6edf4498ce53bcb057191d02c5df92731b311da2 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 29 Sep 2014 14:12:36 -0400 Subject: [PATCH 2/4] Move overridden_language to just #language --- lib/linguist/lazy_blob.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/linguist/lazy_blob.rb b/lib/linguist/lazy_blob.rb index 3ece29f2..6b2e5e47 100644 --- a/lib/linguist/lazy_blob.rb +++ b/lib/linguist/lazy_blob.rb @@ -53,15 +53,14 @@ module Linguist return true end end - - def overriden_language - if lang = git_attributes['linguist-language'] - Language.find_by_name(lang) - end - end - def language - @language ||= (overriden_language || Language.detect(self)) + return @language if defined?(@language) + + @language = if lang = git_attributes['linguist-language'] + Language.find_by_name(lang) + else + super + end end def data From 1c6483a499aa6374c2b11ade558e54f0873fd92e Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 29 Sep 2014 14:13:44 -0400 Subject: [PATCH 3/4] Simplify boolean attribute handling --- lib/linguist/lazy_blob.rb | 22 ++++++++++------------ test/test_repository.rb | 6 ------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/linguist/lazy_blob.rb b/lib/linguist/lazy_blob.rb index 6b2e5e47..807312f9 100644 --- a/lib/linguist/lazy_blob.rb +++ b/lib/linguist/lazy_blob.rb @@ -30,29 +30,21 @@ module Linguist end def vendored? - if git_attributes['linguist-vendored'] - return result_for_key('linguist-vendored') + if attr = git_attributes['linguist-vendored'] + return boolean_attribute(attr) else return super end end def generated? - if git_attributes['linguist-generated'] - return result_for_key('linguist-generated') + if attr = git_attributes['linguist-generated'] + return boolean_attribute(attr) else return super end end - def result_for_key(keyname) - key = git_attributes[keyname] - if key == "false" || key.nil? - return false - else - return true - end - end def language return @language if defined?(@language) @@ -74,6 +66,12 @@ module Linguist end protected + + # Returns true if the attribute is present and not the string "false". + def boolean_attribute(attr) + attr != "false" + end + def load_blob! @data, @size = Rugged::Blob.to_buffer(repository, oid, MAX_SIZE) if @data.nil? end diff --git a/test/test_repository.rb b/test/test_repository.rb index dbd7e3a9..3401d655 100644 --- a/test/test_repository.rb +++ b/test/test_repository.rb @@ -81,8 +81,6 @@ class TestRepository < Test::Unit::TestCase # TODO: get rid of this (would like this to come from git data) file.stubs(:git_attributes).returns(git_attrs) - # check we're getting the correct assignment back from .gitattributes - assert file.result_for_key('linguist-generated') # overridden in .gitattributes assert file.generated? end @@ -98,8 +96,6 @@ class TestRepository < Test::Unit::TestCase # TODO: get rid of this (would like this to come from git data) override_vendored.stubs(:git_attributes).returns(git_attrs) - # check we're getting the correct assignment back from .gitattributes - assert override_vendored.result_for_key('linguist-vendored') # overridden .gitattributes assert override_vendored.vendored? end @@ -117,8 +113,6 @@ class TestRepository < Test::Unit::TestCase # TODO: get rid of this (would like this to come from git data) override_unvendored.stubs(:git_attributes).returns(git_attrs) - # check we're getting the correct assignment back from .gitattributes - assert !override_unvendored.result_for_key('linguist-vendored') # overridden .gitattributes assert !override_unvendored.vendored? end From 2b411aad90b1b296b55eb39a4c1ad75c9a49b7ba Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 29 Sep 2014 15:04:48 -0400 Subject: [PATCH 4/4] Extract #read_index for tests --- lib/linguist/repository.rb | 22 +++++++++++++++------- test/test_repository.rb | 26 ++++---------------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/lib/linguist/repository.rb b/lib/linguist/repository.rb index f2bcb785..1f9e09c4 100644 --- a/lib/linguist/repository.rb +++ b/lib/linguist/repository.rb @@ -110,22 +110,30 @@ module Linguist if @old_commit_oid == @commit_oid @old_stats else - compute_stats(@old_commit_oid, @commit_oid, @old_stats) + compute_stats(@old_commit_oid, @old_stats) end end end + def read_index + attr_index = Rugged::Index.new + attr_index.read_tree(current_tree) + repository.index = attr_index + end + + def current_tree + @tree ||= Rugged::Commit.lookup(repository, @commit_oid).tree + end + protected - def compute_stats(old_commit_oid, commit_oid, cache = nil) + + def compute_stats(old_commit_oid, cache = nil) file_map = cache ? cache.dup : {} old_tree = old_commit_oid && Rugged::Commit.lookup(repository, old_commit_oid).tree - new_tree = Rugged::Commit.lookup(repository, commit_oid).tree - diff = Rugged::Tree.diff(repository, old_tree, new_tree) + read_index - attr_index = Rugged::Index.new - attr_index.read_tree(new_tree) - repository.index = attr_index + diff = Rugged::Tree.diff(repository, old_tree, current_tree) diff.each_delta do |delta| old = delta.old_file[:path] diff --git a/test/test_repository.rb b/test/test_repository.rb index 3401d655..13155796 100644 --- a/test/test_repository.rb +++ b/test/test_repository.rb @@ -68,51 +68,33 @@ class TestRepository < Test::Unit::TestCase assert !repo.breakdown_by_file["Ruby"].empty? end - def test_linguist_override_generated? attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' + linguist_repo(attr_commit).read_index file = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Rakefile') - git_attrs = { "linguist-language" => nil, - "linguist-vendored" => nil, - "linguist-generated"=> true } - - # TODO: get rid of this (would like this to come from git data) - file.stubs(:git_attributes).returns(git_attrs) - # overridden in .gitattributes assert file.generated? end def test_linguist_override_vendored? attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' + repo = linguist_repo(attr_commit).read_index + override_vendored = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Gemfile') - git_attrs = { "linguist-language" => nil, - "linguist-vendored" => true, - "linguist-generated"=> nil } - - # TODO: get rid of this (would like this to come from git data) - override_vendored.stubs(:git_attributes).returns(git_attrs) - # overridden .gitattributes assert override_vendored.vendored? end def test_linguist_override_unvendored? attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' + repo = linguist_repo(attr_commit).read_index # lib/linguist/vendor.yml defines this as vendored. override_unvendored = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'test/fixtures/foo.rb') - git_attrs = { "linguist-language" => nil, - "linguist-vendored" => "false", - "linguist-generated"=> nil } - - # TODO: get rid of this (would like this to come from git data) - override_unvendored.stubs(:git_attributes).returns(git_attrs) - # overridden .gitattributes assert !override_unvendored.vendored? end