From 3d39e842eca0913ba5ec385429496ceacc3699f8 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 10 Sep 2014 16:59:43 +0200 Subject: [PATCH] Load Git Attributes for Linguist-specific overrides --- lib/linguist/lazy_blob.rb | 24 ++++++++++++++++++++++++ lib/linguist/repository.rb | 6 +++++- test/test_repository.rb | 14 ++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/linguist/lazy_blob.rb b/lib/linguist/lazy_blob.rb index bb262241..c7031283 100644 --- a/lib/linguist/lazy_blob.rb +++ b/lib/linguist/lazy_blob.rb @@ -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 diff --git a/lib/linguist/repository.rb b/lib/linguist/repository.rb index a89c81e6..9e0b7f1e 100644 --- a/lib/linguist/repository.rb +++ b/lib/linguist/repository.rb @@ -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) diff --git a/test/test_repository.rb b/test/test_repository.rb index f4c5ad70..4f7b938e 100644 --- a/test/test_repository.rb +++ b/test/test_repository.rb @@ -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