mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98b99e38bb | ||
|
|
d8e3bec499 | ||
|
|
7c759d4d29 | ||
|
|
41d438b47e | ||
|
|
41911d6921 | ||
|
|
dca18d77cb | ||
|
|
040af5dad2 | ||
|
|
4867c49bd9 | ||
|
|
a354eddf4b | ||
|
|
9b78c533a5 | ||
|
|
090ea576b9 | ||
|
|
6a2d33a4b3 | ||
|
|
2c62da7834 | ||
|
|
0145a0adb2 |
@@ -8,20 +8,19 @@ require 'tmpdir'
|
|||||||
require 'zlib'
|
require 'zlib'
|
||||||
|
|
||||||
class GitLinguist
|
class GitLinguist
|
||||||
attr_reader :repo_path
|
|
||||||
attr_reader :commit_oid
|
|
||||||
attr_reader :incremental
|
|
||||||
|
|
||||||
def initialize(path, commit_oid, incremental = true)
|
def initialize(path, commit_oid, incremental = true)
|
||||||
@repo_path = path
|
@repo_path = path
|
||||||
@commit_oid = commit_oid || rugged.head.target_id
|
@commit_oid = commit_oid
|
||||||
@incremental = incremental
|
@incremental = incremental
|
||||||
end
|
end
|
||||||
|
|
||||||
def linguist
|
def linguist
|
||||||
repo = Linguist::Repository.new(rugged, commit_oid)
|
if @commit_oid.nil?
|
||||||
|
raise "git-linguist must be called with a specific commit OID to perform language computation"
|
||||||
|
end
|
||||||
|
repo = Linguist::Repository.new(rugged, @commit_oid)
|
||||||
|
|
||||||
if incremental && stats = load_language_stats
|
if @incremental && stats = load_language_stats
|
||||||
old_commit_oid, old_stats = stats
|
old_commit_oid, old_stats = stats
|
||||||
|
|
||||||
# A cache with NULL oid means that we want to froze
|
# A cache with NULL oid means that we want to froze
|
||||||
@@ -33,19 +32,19 @@ class GitLinguist
|
|||||||
|
|
||||||
result = yield repo
|
result = yield repo
|
||||||
|
|
||||||
save_language_stats(commit_oid, repo.cache)
|
save_language_stats(@commit_oid, repo.cache)
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_language_stats
|
def load_language_stats
|
||||||
version, commit_oid, stats = load_cache
|
version, oid, stats = load_cache
|
||||||
if version == LANGUAGE_STATS_CACHE_VERSION && commit_oid && stats
|
if version == LANGUAGE_STATS_CACHE_VERSION && oid && stats
|
||||||
[commit_oid, stats]
|
[oid, stats]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_language_stats(commit_oid, stats)
|
def save_language_stats(oid, stats)
|
||||||
cache = [LANGUAGE_STATS_CACHE_VERSION, commit_oid, stats]
|
cache = [LANGUAGE_STATS_CACHE_VERSION, oid, stats]
|
||||||
write_cache(cache)
|
write_cache(cache)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -64,25 +63,28 @@ class GitLinguist
|
|||||||
LANGUAGE_STATS_CACHE_VERSION = "v3:#{Linguist::VERSION}"
|
LANGUAGE_STATS_CACHE_VERSION = "v3:#{Linguist::VERSION}"
|
||||||
|
|
||||||
def rugged
|
def rugged
|
||||||
@rugged ||= Rugged::Repository.bare(repo_path)
|
@rugged ||= Rugged::Repository.bare(@repo_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache_file
|
def cache_file
|
||||||
File.join(repo_path, LANGUAGE_STATS_CACHE)
|
File.join(@repo_path, LANGUAGE_STATS_CACHE)
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_cache(object)
|
def write_cache(object)
|
||||||
tmp_path = Dir::Tmpname.make_tmpname(cache_file, nil)
|
return unless File.directory? @repo_path
|
||||||
|
|
||||||
File.open(tmp_path, "wb") do |f|
|
begin
|
||||||
marshal = Marshal.dump(object)
|
tmp_path = Dir::Tmpname.make_tmpname(cache_file, nil)
|
||||||
f.write(Zlib::Deflate.deflate(marshal))
|
File.open(tmp_path, "wb") do |f|
|
||||||
|
marshal = Marshal.dump(object)
|
||||||
|
f.write(Zlib::Deflate.deflate(marshal))
|
||||||
|
end
|
||||||
|
|
||||||
|
File.rename(tmp_path, cache_file)
|
||||||
|
rescue => e
|
||||||
|
(File.unlink(tmp_path) rescue nil)
|
||||||
|
raise e
|
||||||
end
|
end
|
||||||
|
|
||||||
File.rename(tmp_path, cache_file)
|
|
||||||
tmp_path = nil
|
|
||||||
ensure
|
|
||||||
(File.unlink(tmp_path) rescue nil) if tmp_path
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_cache
|
def load_cache
|
||||||
@@ -97,24 +99,18 @@ end
|
|||||||
def git_linguist(args)
|
def git_linguist(args)
|
||||||
incremental = true
|
incremental = true
|
||||||
commit = nil
|
commit = nil
|
||||||
git_dir = nil
|
|
||||||
|
|
||||||
parser = OptionParser.new do |opts|
|
parser = OptionParser.new do |opts|
|
||||||
opts.banner = "Usage: git-linguist [OPTIONS] stats|breakdown|dump-cache|clear|disable"
|
opts.banner = "Usage: git-linguist [OPTIONS] stats|breakdown|dump-cache|clear|disable"
|
||||||
|
|
||||||
opts.on("-f", "--force", "Force a full rescan") { incremental = false }
|
opts.on("-f", "--force", "Force a full rescan") { incremental = false }
|
||||||
opts.on("--git-dir=DIR", "Path to the git repository") { |v| git_dir = v }
|
|
||||||
opts.on("--commit=COMMIT", "Commit to index") { |v| commit = v}
|
opts.on("--commit=COMMIT", "Commit to index") { |v| commit = v}
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.parse!(args)
|
parser.parse!(args)
|
||||||
|
|
||||||
git_dir ||= begin
|
git_dir = `git rev-parse --git-dir`.strip
|
||||||
pwd = Dir.pwd
|
raise "git-linguist must be ran in a Git repository" unless $?.success?
|
||||||
dotgit = File.join(pwd, ".git")
|
|
||||||
File.directory?(dotgit) ? dotgit : pwd
|
|
||||||
end
|
|
||||||
|
|
||||||
wrapper = GitLinguist.new(git_dir, commit, incremental)
|
wrapper = GitLinguist.new(git_dir, commit, incremental)
|
||||||
|
|
||||||
case args.pop
|
case args.pop
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|||||||
s.homepage = "https://github.com/github/linguist"
|
s.homepage = "https://github.com/github/linguist"
|
||||||
s.license = "MIT"
|
s.license = "MIT"
|
||||||
|
|
||||||
s.files = Dir['lib/**/*'] - ['lib/linguist/grammars.rb']
|
s.files = Dir['lib/**/*'] - ['lib/linguist/grammars.rb'] + ['LICENSE']
|
||||||
s.executables = ['linguist', 'git-linguist']
|
s.executables = ['linguist', 'git-linguist']
|
||||||
|
|
||||||
s.add_dependency 'charlock_holmes', '~> 0.7.3'
|
s.add_dependency 'charlock_holmes', '~> 0.7.3'
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
# Use "text" if a mode does not exist.
|
# Use "text" if a mode does not exist.
|
||||||
# wrap - Boolean wrap to enable line wrapping (default: false)
|
# wrap - Boolean wrap to enable line wrapping (default: false)
|
||||||
# extensions - An Array of associated extensions (the first one is
|
# extensions - An Array of associated extensions (the first one is
|
||||||
# considered the primary extension)
|
# considered the primary extension, the others should be
|
||||||
|
# listed alphabetically)
|
||||||
# interpreters - An Array of associated interpreters
|
# interpreters - An Array of associated interpreters
|
||||||
# searchable - Boolean flag to enable searching (defaults to true)
|
# searchable - Boolean flag to enable searching (defaults to true)
|
||||||
# search_term - Deprecated: Some languages maybe indexed under a
|
# search_term - Deprecated: Some languages maybe indexed under a
|
||||||
|
|||||||
@@ -126,12 +126,13 @@ module Linguist
|
|||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
MAX_TREE_SIZE = 100_000
|
||||||
|
|
||||||
def compute_stats(old_commit_oid, cache = nil)
|
def compute_stats(old_commit_oid, cache = nil)
|
||||||
|
return {} if current_tree.count_recursive(MAX_TREE_SIZE) >= MAX_TREE_SIZE
|
||||||
|
|
||||||
old_tree = old_commit_oid && Rugged::Commit.lookup(repository, old_commit_oid).tree
|
old_tree = old_commit_oid && Rugged::Commit.lookup(repository, old_commit_oid).tree
|
||||||
|
|
||||||
read_index
|
read_index
|
||||||
|
|
||||||
diff = Rugged::Tree.diff(repository, old_tree, current_tree)
|
diff = Rugged::Tree.diff(repository, old_tree, current_tree)
|
||||||
|
|
||||||
# Clear file map and fetch full diff if any .gitattributes files are changed
|
# Clear file map and fetch full diff if any .gitattributes files are changed
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module Linguist
|
module Linguist
|
||||||
VERSION = "4.6.0"
|
VERSION = "4.6.3"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class TestPedantic < Minitest::Test
|
|||||||
assert_sorted LANGUAGES.keys
|
assert_sorted LANGUAGES.keys
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_extensions_are_sorted
|
def test_nonprimary_extensions_are_sorted
|
||||||
LANGUAGES.each do |name, language|
|
LANGUAGES.each do |name, language|
|
||||||
extensions = language['extensions']
|
extensions = language['extensions']
|
||||||
assert_sorted extensions[1..-1].map(&:downcase) if extensions && extensions.size > 1
|
assert_sorted extensions[1..-1].map(&:downcase) if extensions && extensions.size > 1
|
||||||
|
|||||||
2
vendor/grammars/language-javascript
vendored
2
vendor/grammars/language-javascript
vendored
Submodule vendor/grammars/language-javascript updated: c5c381e378...7b14bbb041
Reference in New Issue
Block a user