mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	Adding linguist_vendored? and linguist_generated? to include overrides from .gitattributes
This commit is contained in:
		| @@ -4,7 +4,7 @@ require 'rugged' | ||||
|  | ||||
| module Linguist | ||||
|   class LazyBlob | ||||
|     GIT_ATTR = ['linguist-ignore', 'linguist-lang'] | ||||
|     GIT_ATTR = ['linguist-language', 'linguist-vendored', 'linguist-generated'] | ||||
|     GIT_ATTR_OPTS = { :priority => [:index], :skip_system => true } | ||||
|     GIT_ATTR_FLAGS = Rugged::Repository::Attributes.parse_opts(GIT_ATTR_OPTS) | ||||
|  | ||||
| @@ -29,12 +29,16 @@ module Linguist | ||||
|         name, GIT_ATTR, GIT_ATTR_FLAGS) | ||||
|     end | ||||
|  | ||||
|     def ignored? | ||||
|       !!git_attributes['linguist-ignore'] | ||||
|     def linguist_vendored? | ||||
|       vendored? || !!git_attributes['linguist-vendored'] | ||||
|     end | ||||
|  | ||||
|     def linguist_generated? | ||||
|       generated? || !!git_attributes['linguist-generated'] | ||||
|     end | ||||
|  | ||||
|     def overriden_language | ||||
|       if lang = git_attributes['linguist-lang'] | ||||
|       if lang = git_attributes['linguist-language'] | ||||
|         Language.find_by_name(lang) | ||||
|       end | ||||
|     end | ||||
|   | ||||
| @@ -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.ignored? || blob.vendored? || blob.generated? || blob.language.nil? | ||||
|           next if blob.linguist_vendored? || blob.linguist_generated? || blob.language.nil? | ||||
|  | ||||
|           # Only include programming languages and acceptable markup languages | ||||
|           if blob.language.type == :programming || Language.detectable_markup.include?(blob.language.name) | ||||
|   | ||||
							
								
								
									
										37
									
								
								test/test_repository.rb
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										37
									
								
								test/test_repository.rb
									
									
									
									
										vendored
									
									
								
							| @@ -1,6 +1,7 @@ | ||||
| require 'linguist/repository' | ||||
| require 'linguist/lazy_blob' | ||||
| require 'test/unit' | ||||
|  | ||||
| require 'pry' | ||||
| class TestRepository < Test::Unit::TestCase | ||||
|   def rugged_repository | ||||
|     @rugged ||= Rugged::Repository.new(File.expand_path("../../.git", __FILE__)) | ||||
| @@ -47,13 +48,15 @@ class TestRepository < Test::Unit::TestCase | ||||
|     assert_equal linguist_repo.cache, new_repo.cache | ||||
|   end | ||||
|  | ||||
|   def test_git_attributes | ||||
|     # See https://github.com/github/linguist/blob/525304738ebdb7ab3b7d2bf9a7514cc428faa273/.gitattributes | ||||
|   def test_repo_git_attributes | ||||
|     # See https://github.com/github/linguist/blob/3770a90251c8a940367300c9f6f97bb64b369bf8/.gitattributes | ||||
|     # | ||||
|     # It looks like this: | ||||
|     # test/*.rb linguist-ignore | ||||
|     # lib/linguist.rb linguist-lang=Java | ||||
|     attr_commit = '525304738ebdb7ab3b7d2bf9a7514cc428faa273' | ||||
|     # test/*.rb linguist-vendored | ||||
|     # lib/linguist.rb linguist-language=Java | ||||
|     # lib/linguist/classifier.rb linguist-generated | ||||
|  | ||||
|     attr_commit = '178d4756efb647d5f8607d74fe47a852329d7516' | ||||
|     repo = linguist_repo(attr_commit) | ||||
|  | ||||
|     assert repo.breakdown_by_file.has_key?("Java") | ||||
| @@ -62,7 +65,27 @@ class TestRepository < Test::Unit::TestCase | ||||
|     assert repo.breakdown_by_file.has_key?("Ruby") | ||||
|     assert !repo.breakdown_by_file["Ruby"].empty? | ||||
|     repo.breakdown_by_file["Ruby"].each do |file| | ||||
|       assert !file.start_with?("test/") | ||||
|       assert !file.start_with?("test/"), "Failing for #{file}" | ||||
|     end | ||||
|   end | ||||
|  | ||||
|   def test_linguist_generated? | ||||
|     attr_commit = '178d4756efb647d5f8607d74fe47a852329d7516' | ||||
|     file = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'lib/linguist/classifier.rb') | ||||
|  | ||||
|     # overridden in .gitattributes | ||||
|     assert file.linguist_generated? | ||||
|     # from lib/linguist/generated.rb | ||||
|     assert !file.generated? | ||||
|   end | ||||
|  | ||||
|   def test_linguist_vendored? | ||||
|     attr_commit = '178d4756efb647d5f8607d74fe47a852329d7516' | ||||
|     file = Linguist::LazyBlob.new(rugged_repository, attr_commit, 'test/test_md5.rb') | ||||
|  | ||||
|     # overridden .gitattributes | ||||
|     assert file.linguist_vendored? | ||||
|     # from lib/linguist/vendor.yml | ||||
|     assert !file.vendored? | ||||
|   end | ||||
| end | ||||
|   | ||||
		Reference in New Issue
	
	Block a user