mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Compare commits
26 Commits
test/maste
...
v3.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c8434ed64 | ||
|
|
9281bd043a | ||
|
|
6771f7c272 | ||
|
|
df09a746a0 | ||
|
|
d9be472ccb | ||
|
|
32828a9af5 | ||
|
|
d206131df0 | ||
|
|
65eaf98d0b | ||
|
|
12429b90fe | ||
|
|
621042e639 | ||
|
|
526244be11 | ||
|
|
bc53d0b55e | ||
|
|
907d3c5a36 | ||
|
|
898f1e215e | ||
|
|
324ac83489 | ||
|
|
00a873dcc7 | ||
|
|
bc34345a56 | ||
|
|
659d27cae5 | ||
|
|
29072d6eae | ||
|
|
1fd59361b5 | ||
|
|
5896bb8fa3 | ||
|
|
ea1fc90cf5 | ||
|
|
463f48f04f | ||
|
|
cd58a30c7c | ||
|
|
c4260ae681 | ||
|
|
b83a364b0e |
@@ -1,4 +1,6 @@
|
||||
before_install:
|
||||
- git fetch origin master:master
|
||||
- git fetch origin v2.0.0:v2.0.0
|
||||
- sudo apt-get install libicu-dev -y
|
||||
- gem update --system 2.1.11
|
||||
rvm:
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
require 'linguist/file_blob'
|
||||
require 'linguist/repository'
|
||||
require 'rugged'
|
||||
|
||||
path = ARGV[0] || Dir.pwd
|
||||
|
||||
@@ -18,7 +19,8 @@ ARGV.shift
|
||||
breakdown = true if ARGV[0] == "--breakdown"
|
||||
|
||||
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|
|
||||
percentage = ((size / repo.size.to_f) * 100)
|
||||
percentage = sprintf '%.2f' % percentage
|
||||
|
||||
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
|
||||
s.add_dependency 'escape_utils', '~> 1.0.1'
|
||||
s.add_dependency 'mime-types', '~> 1.19'
|
||||
s.add_dependency 'pygments.rb', '~> 0.6.0'
|
||||
s.add_dependency 'rugged', '~> 0.21.0'
|
||||
|
||||
s.add_development_dependency 'json'
|
||||
s.add_development_dependency 'mocha'
|
||||
|
||||
@@ -313,15 +313,7 @@ module Linguist
|
||||
#
|
||||
# Returns a Language or nil if none is detected
|
||||
def language
|
||||
return @language if defined? @language
|
||||
|
||||
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)
|
||||
@language ||= Language.detect(self)
|
||||
end
|
||||
|
||||
# Internal: Get the lexer of the blob.
|
||||
|
||||
@@ -92,18 +92,17 @@ module Linguist
|
||||
|
||||
# Public: Detects the Language of the blob.
|
||||
#
|
||||
# name - String filename
|
||||
# data - String blob data. A block also maybe passed in for lazy
|
||||
# loading. This behavior is deprecated and you should always
|
||||
# pass in a String.
|
||||
# mode - Optional String mode (defaults to nil)
|
||||
# blob - an object that implements the Linguist `Blob` interface;
|
||||
# see Linguist::LazyBlob and Linguist::FileBlob for examples
|
||||
#
|
||||
# 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,
|
||||
# append a "magic" extension so it can be classified with other
|
||||
# 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!"
|
||||
end
|
||||
|
||||
@@ -114,7 +113,7 @@ module Linguist
|
||||
# extension at all, in the case of extensionless scripts), we need to continue
|
||||
# our detection work
|
||||
if possible_languages.length > 1
|
||||
data = data.call() if data.respond_to?(:call)
|
||||
data = blob.data
|
||||
possible_language_names = possible_languages.map(&:name)
|
||||
|
||||
# Don't bother with emptiness
|
||||
|
||||
@@ -735,12 +735,14 @@ GLSL:
|
||||
- .glsl
|
||||
- .fp
|
||||
- .frag
|
||||
- .frg
|
||||
- .fshader
|
||||
- .geom
|
||||
- .glslv
|
||||
- .gshader
|
||||
- .shader
|
||||
- .vert
|
||||
- .vrx
|
||||
- .vshader
|
||||
|
||||
Genshi:
|
||||
|
||||
37
lib/linguist/lazy_blob.rb
Normal file
37
lib/linguist/lazy_blob.rb
Normal 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
|
||||
@@ -1,4 +1,5 @@
|
||||
require 'linguist/file_blob'
|
||||
require 'linguist/lazy_blob'
|
||||
require 'rugged'
|
||||
|
||||
module Linguist
|
||||
# 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
|
||||
# the entire project.
|
||||
class Repository
|
||||
# Public: Initialize a new Repository from a File directory
|
||||
#
|
||||
# base_path - A path String
|
||||
#
|
||||
# Returns a Repository
|
||||
def self.from_directory(base_path)
|
||||
new Dir["#{base_path}/**/*"].
|
||||
select { |f| File.file?(f) }.
|
||||
map { |path| FileBlob.new(path, base_path) }
|
||||
attr_reader :repository
|
||||
|
||||
# Public: Create a new Repository based on the stats of
|
||||
# an existing one
|
||||
def self.incremental(repo, commit_oid, old_commit_oid, old_stats)
|
||||
repo = self.new(repo, commit_oid)
|
||||
repo.load_existing_stats(old_commit_oid, old_stats)
|
||||
repo
|
||||
end
|
||||
|
||||
# Public: Initialize a new Repository
|
||||
# Public: Initialize a new Repository to be analyzed for language
|
||||
# data
|
||||
#
|
||||
# enum - Enumerator that responds to `each` and
|
||||
# yields Blob objects
|
||||
# repo - a Rugged::Repository object
|
||||
# commit_oid - the sha1 of the commit that will be analyzed;
|
||||
# this is usually the master branch
|
||||
#
|
||||
# Returns a Repository
|
||||
def initialize(enum)
|
||||
@enum = enum
|
||||
@computed_stats = false
|
||||
@language = @size = nil
|
||||
@sizes = Hash.new { 0 }
|
||||
@file_breakdown = Hash.new { |h,k| h[k] = Array.new }
|
||||
def initialize(repo, commit_oid)
|
||||
@repository = repo
|
||||
@commit_oid = commit_oid
|
||||
|
||||
raise TypeError, 'commit_oid must be a commit SHA1' unless commit_oid.is_a?(String)
|
||||
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
|
||||
|
||||
# Public: Returns a breakdown of language stats.
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# # => { Language['Ruby'] => 46319,
|
||||
# Language['JavaScript'] => 258 }
|
||||
# # => { 'Ruby' => 46319,
|
||||
# 'JavaScript' => 258 }
|
||||
#
|
||||
# Returns a Hash of Language keys and Integer size values.
|
||||
# Returns a Hash of language names and Integer size values.
|
||||
def languages
|
||||
compute_stats
|
||||
@sizes
|
||||
@sizes ||= begin
|
||||
sizes = Hash.new { 0 }
|
||||
cache.each do |_, (language, size)|
|
||||
sizes[language] += size
|
||||
end
|
||||
sizes
|
||||
end
|
||||
end
|
||||
|
||||
# Public: Get primary Language of repository.
|
||||
#
|
||||
# Returns a Language
|
||||
# Returns a language name
|
||||
def language
|
||||
compute_stats
|
||||
@language
|
||||
@language ||= begin
|
||||
primary = languages.max_by { |(_, size)| size }
|
||||
primary && primary[0]
|
||||
end
|
||||
end
|
||||
|
||||
# Public: Get the total size of the repository.
|
||||
#
|
||||
# Returns a byte size Integer
|
||||
def size
|
||||
compute_stats
|
||||
@size
|
||||
@size ||= languages.inject(0) { |s,(_,v)| s + v }
|
||||
end
|
||||
|
||||
# Public: Return the language breakdown of this repository by file
|
||||
#
|
||||
# Returns a map of language names => [filenames...]
|
||||
def breakdown_by_file
|
||||
compute_stats
|
||||
@file_breakdown
|
||||
@file_breakdown ||= begin
|
||||
breakdown = Hash.new { |h,k| h[k] = Array.new }
|
||||
cache.each do |filename, (language, _)|
|
||||
breakdown[language] << filename
|
||||
end
|
||||
breakdown
|
||||
end
|
||||
end
|
||||
|
||||
# Internal: Compute language breakdown for each blob in the Repository.
|
||||
# Public: Return the cached results of the analysis
|
||||
#
|
||||
# Returns nothing
|
||||
def compute_stats
|
||||
return if @computed_stats
|
||||
# This is a per-file breakdown that can be passed to other instances
|
||||
# of Linguist::Repository to perform incremental scans
|
||||
#
|
||||
# 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|
|
||||
# Skip files that are likely binary
|
||||
next if blob.likely_binary?
|
||||
protected
|
||||
def compute_stats(old_commit_oid, commit_oid, cache = nil)
|
||||
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
|
||||
next if blob.vendored? || blob.generated? || blob.language.nil?
|
||||
diff = Rugged::Tree.diff(repository, old_tree, new_tree)
|
||||
|
||||
# Only include programming languages and acceptable markup languages
|
||||
if blob.language.type == :programming || Language.detectable_markup.include?(blob.language.name)
|
||||
diff.each_delta do |delta|
|
||||
old = delta.old_file[:path]
|
||||
new = delta.new_file[:path]
|
||||
|
||||
# Build up the per-file breakdown stats
|
||||
@file_breakdown[blob.language.group.name] << blob.name
|
||||
file_map.delete(old)
|
||||
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
|
||||
|
||||
# Compute total size
|
||||
@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
|
||||
file_map
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -170,7 +170,9 @@
|
||||
"GLSL": [
|
||||
".fp",
|
||||
".frag",
|
||||
".glsl"
|
||||
".frg",
|
||||
".glsl",
|
||||
".vrx"
|
||||
],
|
||||
"Game Maker Language": [
|
||||
".gml"
|
||||
@@ -781,8 +783,8 @@
|
||||
"exception.zep.php"
|
||||
]
|
||||
},
|
||||
"tokens_total": 643487,
|
||||
"languages_total": 843,
|
||||
"tokens_total": 643530,
|
||||
"languages_total": 845,
|
||||
"tokens": {
|
||||
"ABAP": {
|
||||
"*/**": 1,
|
||||
@@ -23553,14 +23555,14 @@
|
||||
"////": 4,
|
||||
"High": 1,
|
||||
"quality": 2,
|
||||
"(": 435,
|
||||
"(": 437,
|
||||
"Some": 1,
|
||||
"browsers": 1,
|
||||
"may": 1,
|
||||
"freeze": 1,
|
||||
"or": 1,
|
||||
"crash": 1,
|
||||
")": 435,
|
||||
")": 437,
|
||||
"//#define": 10,
|
||||
"HIGHQUALITY": 2,
|
||||
"Medium": 1,
|
||||
@@ -23602,7 +23604,7 @@
|
||||
"eps": 5,
|
||||
"e": 4,
|
||||
"-": 108,
|
||||
";": 383,
|
||||
";": 391,
|
||||
"PI": 3,
|
||||
"vec3": 165,
|
||||
"sunDir": 5,
|
||||
@@ -23621,13 +23623,13 @@
|
||||
"tonemapping": 1,
|
||||
"mod289": 4,
|
||||
"x": 11,
|
||||
"{": 82,
|
||||
"{": 84,
|
||||
"return": 47,
|
||||
"floor": 8,
|
||||
"*": 115,
|
||||
"*": 116,
|
||||
"/": 24,
|
||||
"}": 82,
|
||||
"vec4": 73,
|
||||
"}": 84,
|
||||
"vec4": 77,
|
||||
"permute": 4,
|
||||
"x*34.0": 1,
|
||||
"+": 108,
|
||||
@@ -23743,7 +23745,7 @@
|
||||
"p*0.5": 1,
|
||||
"Other": 1,
|
||||
"bump": 2,
|
||||
"pos": 42,
|
||||
"pos": 43,
|
||||
"rayDir": 43,
|
||||
"s": 23,
|
||||
"Fade": 1,
|
||||
@@ -23931,8 +23933,8 @@
|
||||
"iResolution.x/iResolution.y*0.5": 1,
|
||||
"rd.x": 1,
|
||||
"rd.y": 1,
|
||||
"void": 31,
|
||||
"main": 5,
|
||||
"void": 33,
|
||||
"main": 7,
|
||||
"gl_FragCoord.xy": 7,
|
||||
"*0.25": 4,
|
||||
"*0.5": 1,
|
||||
@@ -23944,7 +23946,16 @@
|
||||
"col*exposure": 1,
|
||||
"x*": 2,
|
||||
".5": 1,
|
||||
"gl_FragColor": 3,
|
||||
"gl_FragColor": 4,
|
||||
"varying": 6,
|
||||
"v_color": 4,
|
||||
"uniform": 8,
|
||||
"mat4": 1,
|
||||
"u_MVPMatrix": 2,
|
||||
"attribute": 2,
|
||||
"a_position": 1,
|
||||
"a_color": 2,
|
||||
"gl_Position": 1,
|
||||
"#version": 2,
|
||||
"core": 1,
|
||||
"cbar": 2,
|
||||
@@ -23980,11 +23991,9 @@
|
||||
"AMBIENT": 2,
|
||||
"MAX_DIST": 3,
|
||||
"MAX_DIST_SQUARED": 3,
|
||||
"uniform": 7,
|
||||
"lightColor": 3,
|
||||
"[": 29,
|
||||
"]": 29,
|
||||
"varying": 4,
|
||||
"fragmentNormal": 2,
|
||||
"cameraVector": 2,
|
||||
"lightVector": 4,
|
||||
@@ -69657,7 +69666,7 @@
|
||||
"GAMS": 363,
|
||||
"GAP": 9944,
|
||||
"GAS": 133,
|
||||
"GLSL": 4033,
|
||||
"GLSL": 4076,
|
||||
"Game Maker Language": 13310,
|
||||
"Gnuplot": 1023,
|
||||
"Gosu": 410,
|
||||
@@ -69854,7 +69863,7 @@
|
||||
"GAMS": 1,
|
||||
"GAP": 7,
|
||||
"GAS": 1,
|
||||
"GLSL": 5,
|
||||
"GLSL": 7,
|
||||
"Game Maker Language": 13,
|
||||
"Gnuplot": 6,
|
||||
"Gosu": 4,
|
||||
@@ -70004,5 +70013,5 @@
|
||||
"fish": 3,
|
||||
"wisp": 1
|
||||
},
|
||||
"md5": "59afe9ab875947c5df3590aef023152d"
|
||||
"md5": "bb637c5d1f457edff3f8c2676d10807a"
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
module Linguist
|
||||
VERSION = "2.12.0"
|
||||
VERSION = "3.0.0"
|
||||
end
|
||||
|
||||
6
samples/GLSL/myfragment.frg
Normal file
6
samples/GLSL/myfragment.frg
Normal file
@@ -0,0 +1,6 @@
|
||||
varying vec4 v_color;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = v_color;
|
||||
}
|
||||
12
samples/GLSL/myvertex.vrx
Normal file
12
samples/GLSL/myvertex.vrx
Normal file
@@ -0,0 +1,12 @@
|
||||
uniform mat4 u_MVPMatrix;
|
||||
|
||||
attribute vec4 a_position;
|
||||
attribute vec4 a_color;
|
||||
|
||||
varying vec4 v_color;
|
||||
|
||||
void main()
|
||||
{
|
||||
v_color = a_color;
|
||||
gl_Position = u_MVPMatrix * pos;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
require 'linguist/heuristics'
|
||||
require 'linguist/language'
|
||||
require 'linguist/samples'
|
||||
require 'linguist/file_blob'
|
||||
|
||||
require 'test/unit'
|
||||
|
||||
@@ -35,7 +36,8 @@ class TestHeuristcs < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
@@ -3,22 +3,24 @@ require 'linguist/repository'
|
||||
require 'test/unit'
|
||||
|
||||
class TestRepository < Test::Unit::TestCase
|
||||
include Linguist
|
||||
|
||||
def repo(base_path)
|
||||
Repository.from_directory(base_path)
|
||||
def rugged_repository
|
||||
@rugged ||= Rugged::Repository.new(File.expand_path("../../.git", __FILE__))
|
||||
end
|
||||
|
||||
def linguist_repo
|
||||
repo(File.expand_path("../..", __FILE__))
|
||||
def master_oid
|
||||
'd40b4a33deba710e2f494db357c654fbe5d4b419'
|
||||
end
|
||||
|
||||
def linguist_repo(oid = master_oid)
|
||||
Linguist::Repository.new(rugged_repository, oid)
|
||||
end
|
||||
|
||||
def test_linguist_language
|
||||
# assert_equal Language['Ruby'], linguist_repo.language
|
||||
assert_equal 'Ruby', linguist_repo.language
|
||||
end
|
||||
|
||||
def test_linguist_languages
|
||||
# assert linguist_repo.languages[Language['Ruby']] > 10_000
|
||||
assert linguist_repo.languages['Ruby'] > 10_000
|
||||
end
|
||||
|
||||
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")
|
||||
end
|
||||
|
||||
def test_binary_override
|
||||
assert_equal repo(File.expand_path("../../samples/Nimrod", __FILE__)).language, Language["Nimrod"]
|
||||
def test_incremental_stats
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user