mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	Merge pull request #1563 from github/bkeepers/vendored-ignored-gitattributes
A few tweaks to ignored methods
This commit is contained in:
		| @@ -29,39 +29,30 @@ module Linguist | |||||||
|         name, GIT_ATTR, GIT_ATTR_FLAGS) |         name, GIT_ATTR, GIT_ATTR_FLAGS) | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     def linguist_vendored? |     def vendored? | ||||||
|       if git_attributes['linguist-vendored'] |       if attr = git_attributes['linguist-vendored'] | ||||||
|         return result_for_key('linguist-vendored') |         return boolean_attribute(attr) | ||||||
|       else |       else | ||||||
|         return vendored? |         return super | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     def linguist_generated? |     def generated? | ||||||
|       if git_attributes['linguist-generated'] |       if attr = git_attributes['linguist-generated'] | ||||||
|         return result_for_key('linguist-generated') |         return boolean_attribute(attr) | ||||||
|       else |       else | ||||||
|         return generated? |         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 overriden_language |  | ||||||
|       if lang = git_attributes['linguist-language'] |  | ||||||
|         Language.find_by_name(lang) |  | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     def language |     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 |     end | ||||||
|  |  | ||||||
|     def data |     def data | ||||||
| @@ -75,6 +66,12 @@ module Linguist | |||||||
|     end |     end | ||||||
|  |  | ||||||
|     protected |     protected | ||||||
|  |  | ||||||
|  |     # Returns true if the attribute is present and not the string "false". | ||||||
|  |     def boolean_attribute(attr) | ||||||
|  |       attr != "false" | ||||||
|  |     end | ||||||
|  |  | ||||||
|     def load_blob! |     def load_blob! | ||||||
|       @data, @size = Rugged::Blob.to_buffer(repository, oid, MAX_SIZE) if @data.nil? |       @data, @size = Rugged::Blob.to_buffer(repository, oid, MAX_SIZE) if @data.nil? | ||||||
|     end |     end | ||||||
|   | |||||||
| @@ -110,22 +110,30 @@ module Linguist | |||||||
|         if @old_commit_oid == @commit_oid |         if @old_commit_oid == @commit_oid | ||||||
|           @old_stats |           @old_stats | ||||||
|         else |         else | ||||||
|           compute_stats(@old_commit_oid, @commit_oid, @old_stats) |           compute_stats(@old_commit_oid, @old_stats) | ||||||
|         end |         end | ||||||
|       end |       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 |     protected | ||||||
|     def compute_stats(old_commit_oid, commit_oid, cache = nil) |  | ||||||
|  |     def compute_stats(old_commit_oid, cache = nil) | ||||||
|       file_map = cache ? cache.dup : {} |       file_map = cache ? cache.dup : {} | ||||||
|       old_tree = old_commit_oid && Rugged::Commit.lookup(repository, old_commit_oid).tree |       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 |       diff = Rugged::Tree.diff(repository, old_tree, current_tree) | ||||||
|       attr_index.read_tree(new_tree) |  | ||||||
|       repository.index = attr_index |  | ||||||
|  |  | ||||||
|       diff.each_delta do |delta| |       diff.each_delta do |delta| | ||||||
|         old = delta.old_file[:path] |         old = delta.old_file[:path] | ||||||
| @@ -142,7 +150,7 @@ module Linguist | |||||||
|           blob = Linguist::LazyBlob.new(repository, delta.new_file[:oid], new, mode.to_s(8)) |           blob = Linguist::LazyBlob.new(repository, delta.new_file[:oid], new, mode.to_s(8)) | ||||||
|  |  | ||||||
|           # Skip vendored or generated blobs |           # 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 |           # Only include programming languages and acceptable markup languages | ||||||
|           if blob.language.type == :programming || Language.detectable_markup.include?(blob.language.name) |           if blob.language.type == :programming || Language.detectable_markup.include?(blob.language.name) | ||||||
|   | |||||||
| @@ -68,64 +68,34 @@ class TestRepository < Test::Unit::TestCase | |||||||
|     assert !repo.breakdown_by_file["Ruby"].empty? |     assert !repo.breakdown_by_file["Ruby"].empty? | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  |   def test_linguist_override_generated? | ||||||
|   def test_linguist_generated? |  | ||||||
|     attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' |     attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' | ||||||
|  |     linguist_repo(attr_commit).read_index | ||||||
|  |  | ||||||
|     file = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Rakefile') |     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) |  | ||||||
|  |  | ||||||
|     # check we're getting the correct assignment back from .gitattributes |  | ||||||
|     assert file.result_for_key('linguist-generated') |  | ||||||
|     # overridden in .gitattributes |     # overridden in .gitattributes | ||||||
|     assert file.linguist_generated? |     assert file.generated? | ||||||
|     # from lib/linguist/generated.rb |  | ||||||
|     assert !file.generated? |  | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def test_linguist_override_vendored? |   def test_linguist_override_vendored? | ||||||
|     attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' |     attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' | ||||||
|  |     repo = linguist_repo(attr_commit).read_index | ||||||
|  |  | ||||||
|     override_vendored = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'Gemfile') |     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) |  | ||||||
|  |  | ||||||
|     # check we're getting the correct assignment back from .gitattributes |  | ||||||
|     assert override_vendored.result_for_key('linguist-vendored') |  | ||||||
|     # overridden .gitattributes |     # overridden .gitattributes | ||||||
|     assert override_vendored.linguist_vendored? |     assert override_vendored.vendored? | ||||||
|     # from lib/linguist/vendor.yml |  | ||||||
|     assert !override_vendored.vendored? |  | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def test_linguist_override_unvendored? |   def test_linguist_override_unvendored? | ||||||
|     attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' |     attr_commit = 'b533b682d5d4012ca42f4fc998b45169ec41fe33' | ||||||
|  |     repo = linguist_repo(attr_commit).read_index | ||||||
|  |  | ||||||
|     # lib/linguist/vendor.yml defines this as vendored. |     # lib/linguist/vendor.yml defines this as vendored. | ||||||
|     override_unvendored = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'test/fixtures/foo.rb') |     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) |  | ||||||
|  |  | ||||||
|     # check we're getting the correct assignment back from .gitattributes |  | ||||||
|     assert !override_unvendored.result_for_key('linguist-vendored') |  | ||||||
|     # overridden .gitattributes |     # overridden .gitattributes | ||||||
|     assert !override_unvendored.linguist_vendored? |     assert !override_unvendored.vendored? | ||||||
|     # from lib/linguist/vendor.yml |  | ||||||
|     assert override_unvendored.vendored? |  | ||||||
|   end |   end | ||||||
| end | end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user