Load Git Attributes for Linguist-specific overrides

This commit is contained in:
Vicent Marti
2014-09-10 16:59:43 +02:00
committed by The rugged tests are fragile
parent 16c1aa2845
commit 3d39e842ec
3 changed files with 43 additions and 1 deletions

View File

@@ -1,8 +1,13 @@
require 'linguist/blob_helper'
require 'linguist/language'
require 'rugged'
module Linguist
class LazyBlob
GIT_ATTR = ['linguist-ignore', 'linguist-lang']
GIT_ATTR_OPTS = { :priority => [:index], :skip_system => true }
GIT_ATTR_FLAGS = Rugged::Repository::Attributes.parse_opts(GIT_ATTR_OPTS)
include BlobHelper
MAX_SIZE = 128 * 1024
@@ -19,6 +24,25 @@ module Linguist
@mode = mode
end
def git_attributes
@git_attributes ||= repository.fetch_attributes(
name, GIT_ATTR, GIT_ATTR_FLAGS)
end
def ignored?
!!git_attributes['linguist-ignore']
end
def overriden_language
if lang = git_attributes['linguist-lang']
Language.find_by_name(lang)
end
end
def language
@language ||= (overriden_language || Language.detect(self))
end
def data
load_blob!
@data

View File

@@ -123,6 +123,10 @@ module Linguist
diff = Rugged::Tree.diff(repository, old_tree, new_tree)
attr_index = Rugged::Index.new
attr_index.read_tree(new_tree)
repository.index = attr_index
diff.each_delta do |delta|
old = delta.old_file[:path]
new = delta.new_file[:path]
@@ -138,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.vendored? || blob.generated? || blob.language.nil?
next if blob.ignored? || 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)

View File

@@ -47,4 +47,18 @@ class TestRepository < Test::Unit::TestCase
assert_equal linguist_repo.cache, new_repo.cache
end
def test_git_attributes
attr_commit = '525304738ebdb7ab3b7d2bf9a7514cc428faa273'
repo = linguist_repo(attr_commit)
assert repo.breakdown_by_file.has_key?("Java")
assert repo.breakdown_by_file["Java"].include?("lib/linguist.rb")
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/")
end
end
end