Merge pull request #1335 from github/1318-local

1318 local
This commit is contained in:
Arfon Smith
2014-07-01 11:48:24 -05:00
10 changed files with 181 additions and 86 deletions

View File

@@ -1,4 +1,6 @@
before_install: before_install:
- git fetch origin master:master
- git fetch origin v2.0.0:v2.0.0
- sudo apt-get install libicu-dev -y - sudo apt-get install libicu-dev -y
- gem update --system 2.1.11 - gem update --system 2.1.11
rvm: rvm:

View File

@@ -5,6 +5,7 @@
require 'linguist/file_blob' require 'linguist/file_blob'
require 'linguist/repository' require 'linguist/repository'
require 'rugged'
path = ARGV[0] || Dir.pwd path = ARGV[0] || Dir.pwd
@@ -18,7 +19,8 @@ ARGV.shift
breakdown = true if ARGV[0] == "--breakdown" breakdown = true if ARGV[0] == "--breakdown"
if File.directory?(path) if File.directory?(path)
repo = Linguist::Repository.from_directory(path) rugged = Rugged::Repository.new(path)
repo = Linguist::Repository.new(rugged, rugged.head.target_id)
repo.languages.sort_by { |_, size| size }.reverse.each do |language, size| repo.languages.sort_by { |_, size| size }.reverse.each do |language, size|
percentage = ((size / repo.size.to_f) * 100) percentage = ((size / repo.size.to_f) * 100)
percentage = sprintf '%.2f' % percentage percentage = sprintf '%.2f' % percentage

View File

@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
s.add_dependency 'escape_utils', '~> 1.0.1' s.add_dependency 'escape_utils', '~> 1.0.1'
s.add_dependency 'mime-types', '~> 1.19' s.add_dependency 'mime-types', '~> 1.19'
s.add_dependency 'pygments.rb', '~> 0.6.0' s.add_dependency 'pygments.rb', '~> 0.6.0'
s.add_dependency 'rugged', '~> 0.21.0'
s.add_development_dependency 'json' s.add_development_dependency 'json'
s.add_development_dependency 'mocha' s.add_development_dependency 'mocha'

View File

@@ -313,15 +313,7 @@ module Linguist
# #
# Returns a Language or nil if none is detected # Returns a Language or nil if none is detected
def language def language
return @language if defined? @language @language ||= Language.detect(self)
if defined?(@data) && @data.is_a?(String)
data = @data
else
data = lambda { (binary_mime_type? || binary?) ? "" : self.data }
end
@language = Language.detect(name.to_s, data, mode)
end end
# Internal: Get the lexer of the blob. # Internal: Get the lexer of the blob.

View File

@@ -92,18 +92,17 @@ module Linguist
# Public: Detects the Language of the blob. # Public: Detects the Language of the blob.
# #
# name - String filename # blob - an object that implements the Linguist `Blob` interface;
# data - String blob data. A block also maybe passed in for lazy # see Linguist::LazyBlob and Linguist::FileBlob for examples
# loading. This behavior is deprecated and you should always
# pass in a String.
# mode - Optional String mode (defaults to nil)
# #
# Returns Language or nil. # Returns Language or nil.
def self.detect(name, data, mode = nil) def self.detect(blob)
name = blob.name.to_s
# A bit of an elegant hack. If the file is executable but extensionless, # A bit of an elegant hack. If the file is executable but extensionless,
# append a "magic" extension so it can be classified with other # append a "magic" extension so it can be classified with other
# languages that have shebang scripts. # languages that have shebang scripts.
if File.extname(name).empty? && mode && (mode.to_i(8) & 05) == 05 if File.extname(name).empty? && blob.mode && (blob.mode.to_i(8) & 05) == 05
name += ".script!" name += ".script!"
end end
@@ -114,7 +113,7 @@ module Linguist
# extension at all, in the case of extensionless scripts), we need to continue # extension at all, in the case of extensionless scripts), we need to continue
# our detection work # our detection work
if possible_languages.length > 1 if possible_languages.length > 1
data = data.call() if data.respond_to?(:call) data = blob.data
possible_language_names = possible_languages.map(&:name) possible_language_names = possible_languages.map(&:name)
# Don't bother with emptiness # Don't bother with emptiness

37
lib/linguist/lazy_blob.rb Normal file
View File

@@ -0,0 +1,37 @@
require 'linguist/blob_helper'
require 'rugged'
module Linguist
class LazyBlob
include BlobHelper
MAX_SIZE = 128 * 1024
attr_reader :repository
attr_reader :oid
attr_reader :name
attr_reader :mode
def initialize(repo, oid, name, mode = nil)
@repository = repo
@oid = oid
@name = name
@mode = mode
end
def data
load_blob!
@data
end
def size
load_blob!
@size
end
protected
def load_blob!
@data, @size = Rugged::Blob.to_buffer(repository, oid, MAX_SIZE) if @data.nil?
end
end
end

View File

@@ -1,4 +1,5 @@
require 'linguist/file_blob' require 'linguist/lazy_blob'
require 'rugged'
module Linguist module Linguist
# A Repository is an abstraction of a Grit::Repo or a basic file # A Repository is an abstraction of a Grit::Repo or a basic file
@@ -7,100 +8,146 @@ module Linguist
# Its primary purpose is for gathering language statistics across # Its primary purpose is for gathering language statistics across
# the entire project. # the entire project.
class Repository class Repository
# Public: Initialize a new Repository from a File directory attr_reader :repository
#
# base_path - A path String # Public: Create a new Repository based on the stats of
# # an existing one
# Returns a Repository def self.incremental(repo, commit_oid, old_commit_oid, old_stats)
def self.from_directory(base_path) repo = self.new(repo, commit_oid)
new Dir["#{base_path}/**/*"]. repo.load_existing_stats(old_commit_oid, old_stats)
select { |f| File.file?(f) }. repo
map { |path| FileBlob.new(path, base_path) }
end end
# Public: Initialize a new Repository # Public: Initialize a new Repository to be analyzed for language
# data
# #
# enum - Enumerator that responds to `each` and # repo - a Rugged::Repository object
# yields Blob objects # commit_oid - the sha1 of the commit that will be analyzed;
# this is usually the master branch
# #
# Returns a Repository # Returns a Repository
def initialize(enum) def initialize(repo, commit_oid)
@enum = enum @repository = repo
@computed_stats = false @commit_oid = commit_oid
@language = @size = nil
@sizes = Hash.new { 0 } raise TypeError, 'commit_oid must be a commit SHA1' unless commit_oid.is_a?(String)
@file_breakdown = Hash.new { |h,k| h[k] = Array.new } end
# Public: Load the results of a previous analysis on this repository
# to speed up the new scan.
#
# The new analysis will be performed incrementally as to only take
# into account the file changes since the last time the repository
# was scanned
#
# old_commit_oid - the sha1 of the commit that was previously analyzed
# old_stats - the result of the previous analysis, obtained by calling
# Repository#cache on the old repository
#
# Returns nothing
def load_existing_stats(old_commit_oid, old_stats)
@old_commit_oid = old_commit_oid
@old_stats = old_stats
nil
end end
# Public: Returns a breakdown of language stats. # Public: Returns a breakdown of language stats.
# #
# Examples # Examples
# #
# # => { Language['Ruby'] => 46319, # # => { 'Ruby' => 46319,
# Language['JavaScript'] => 258 } # 'JavaScript' => 258 }
# #
# Returns a Hash of Language keys and Integer size values. # Returns a Hash of language names and Integer size values.
def languages def languages
compute_stats @sizes ||= begin
@sizes sizes = Hash.new { 0 }
cache.each do |_, (language, size)|
sizes[language] += size
end
sizes
end
end end
# Public: Get primary Language of repository. # Public: Get primary Language of repository.
# #
# Returns a Language # Returns a language name
def language def language
compute_stats @language ||= begin
@language primary = languages.max_by { |(_, size)| size }
primary && primary[0]
end
end end
# Public: Get the total size of the repository. # Public: Get the total size of the repository.
# #
# Returns a byte size Integer # Returns a byte size Integer
def size def size
compute_stats @size ||= languages.inject(0) { |s,(_,v)| s + v }
@size
end end
# Public: Return the language breakdown of this repository by file # Public: Return the language breakdown of this repository by file
#
# Returns a map of language names => [filenames...]
def breakdown_by_file def breakdown_by_file
compute_stats @file_breakdown ||= begin
@file_breakdown breakdown = Hash.new { |h,k| h[k] = Array.new }
cache.each do |filename, (language, _)|
breakdown[language] << filename
end
breakdown
end
end end
# Internal: Compute language breakdown for each blob in the Repository. # Public: Return the cached results of the analysis
# #
# Returns nothing # This is a per-file breakdown that can be passed to other instances
def compute_stats # of Linguist::Repository to perform incremental scans
return if @computed_stats #
# Returns a map of filename => [language, size]
def cache
@cache ||= begin
if @old_commit_oid == @commit_oid
@old_stats
else
compute_stats(@old_commit_oid, @commit_oid, @old_stats)
end
end
end
@enum.each do |blob| protected
# Skip files that are likely binary def compute_stats(old_commit_oid, commit_oid, cache = nil)
next if blob.likely_binary? 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
# Skip vendored or generated blobs diff = Rugged::Tree.diff(repository, old_tree, new_tree)
next if blob.vendored? || blob.generated? || blob.language.nil?
# Only include programming languages and acceptable markup languages diff.each_delta do |delta|
if blob.language.type == :programming || Language.detectable_markup.include?(blob.language.name) old = delta.old_file[:path]
new = delta.new_file[:path]
# Build up the per-file breakdown stats file_map.delete(old)
@file_breakdown[blob.language.group.name] << blob.name next if delta.binary
@sizes[blob.language.group] += blob.size if [:added, :modified].include? delta.status
# Skip submodules
mode = delta.new_file[:mode]
next if (mode & 040000) != 0
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?
# Only include programming languages and acceptable markup languages
if blob.language.type == :programming || Language.detectable_markup.include?(blob.language.name)
file_map[new] = [blob.language.group.name, blob.size]
end
end end
end end
# Compute total size file_map
@size = @sizes.inject(0) { |s,(_,v)| s + v }
# Get primary language
if primary = @sizes.max_by { |(_, size)| size }
@language = primary[0]
end
@computed_stats = true
nil
end end
end end
end end

View File

@@ -1,3 +1,3 @@
module Linguist module Linguist
VERSION = "2.12.0" VERSION = "3.0.0"
end end

View File

@@ -1,6 +1,7 @@
require 'linguist/heuristics' require 'linguist/heuristics'
require 'linguist/language' require 'linguist/language'
require 'linguist/samples' require 'linguist/samples'
require 'linguist/file_blob'
require 'test/unit' require 'test/unit'
@@ -35,7 +36,8 @@ class TestHeuristcs < Test::Unit::TestCase
end end
def test_detect_still_works_if_nothing_matches def test_detect_still_works_if_nothing_matches
match = Language.detect("Hello.m", fixture("Objective-C/hello.m")) blob = Linguist::FileBlob.new(File.join(samples_path, "Objective-C/hello.m"))
match = Language.detect(blob)
assert_equal Language["Objective-C"], match assert_equal Language["Objective-C"], match
end end

View File

@@ -3,22 +3,24 @@ require 'linguist/repository'
require 'test/unit' require 'test/unit'
class TestRepository < Test::Unit::TestCase class TestRepository < Test::Unit::TestCase
include Linguist def rugged_repository
@rugged ||= Rugged::Repository.new(File.expand_path("../../.git", __FILE__))
def repo(base_path)
Repository.from_directory(base_path)
end end
def linguist_repo def master_oid
repo(File.expand_path("../..", __FILE__)) 'd40b4a33deba710e2f494db357c654fbe5d4b419'
end
def linguist_repo(oid = master_oid)
Linguist::Repository.new(rugged_repository, oid)
end end
def test_linguist_language def test_linguist_language
# assert_equal Language['Ruby'], linguist_repo.language assert_equal 'Ruby', linguist_repo.language
end end
def test_linguist_languages def test_linguist_languages
# assert linguist_repo.languages[Language['Ruby']] > 10_000 assert linguist_repo.languages['Ruby'] > 10_000
end end
def test_linguist_size def test_linguist_size
@@ -31,7 +33,18 @@ class TestRepository < Test::Unit::TestCase
assert linguist_repo.breakdown_by_file["Ruby"].include?("lib/linguist/language.rb") assert linguist_repo.breakdown_by_file["Ruby"].include?("lib/linguist/language.rb")
end end
def test_binary_override def test_incremental_stats
assert_equal repo(File.expand_path("../../samples/Nimrod", __FILE__)).language, Language["Nimrod"] old_commit = '3d7364877d6794f6cc2a86b493e893968a597332'
old_repo = linguist_repo(old_commit)
assert old_repo.languages['Ruby'] > 10_000
assert old_repo.size > 30_000
new_repo = Linguist::Repository.incremental(rugged_repository, master_oid, old_commit, old_repo.cache)
assert new_repo.languages['Ruby'] > old_repo.languages['Ruby']
assert new_repo.size > old_repo.size
assert_equal linguist_repo.cache, new_repo.cache
end end
end end