Proper incremental diffing

This commit is contained in:
Vicent Marti
2014-06-25 20:26:44 +02:00
parent 5896bb8fa3
commit 1fd59361b5
2 changed files with 36 additions and 15 deletions

View File

@@ -14,10 +14,10 @@ module Linguist
# Public: Initialize a new Repository # Public: Initialize a new Repository
# #
# Returns a Repository # Returns a Repository
def initialize(repo, sha1, existing_stats = nil) def initialize(repo, commit_oid, existing_stats = nil)
@repository = repo @repository = repo
@current_sha1 = sha1 @commit_oid = commit_oid
@old_sha1, @old_stats = existing_stats if existing_stats @old_commit_oid, @old_stats = existing_stats if existing_stats
end end
# Public: Returns a breakdown of language stats. # Public: Returns a breakdown of language stats.
@@ -66,12 +66,12 @@ module Linguist
end end
end end
def incremental_stats(old_sha1, new_sha1, cache = nil) def compute_stats(old_commit_oid, commit_oid, cache = nil)
file_map = cache ? cache.dup : {} file_map = cache ? cache.dup : {}
old_commit = old_sha1 && Rugged::Commit.lookup(repository, old_sha1) old_tree = old_commit_oid && Rugged::Commit.lookup(repository, old_commit_oid).tree
new_commit = Rugged::Commit.lookup(repository, new_sha1) new_tree = Rugged::Commit.lookup(repository, commit_oid).tree
diff = Rugged::Tree.diff(repository, old_commit, new_commit) diff = Rugged::Tree.diff(repository, old_tree, new_tree)
diff.each_delta do |delta| diff.each_delta do |delta|
old = delta.old_file[:path] old = delta.old_file[:path]
@@ -99,10 +99,10 @@ module Linguist
def cache def cache
@cache ||= begin @cache ||= begin
if @old_sha1 == @current_sha1 if @old_commit_oid == @commit_oid
@old_stats @old_stats
else else
incremental_stats(@old_sha1, @current_sha1, @old_stats) compute_stats(@old_commit_oid, @commit_oid, @old_stats)
end end
end end
end end

View File

@@ -3,19 +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__))
end
def linguist_repo def master_oid
r = Rugged::Repository.new(File.expand_path("../../.git", __FILE__)) @master_oid ||= Rugged::Object.rev_parse_oid(rugged_repository, 'master')
Linguist::Repository.new(r, '31921838cdc252536ec07668f73d4b64d8022750') 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
@@ -27,4 +32,20 @@ class TestRepository < Test::Unit::TestCase
assert linguist_repo.breakdown_by_file["Ruby"].include?("bin/linguist") assert linguist_repo.breakdown_by_file["Ruby"].include?("bin/linguist")
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_incremental_stats
old_commit = Rugged::Object.rev_parse_oid(rugged_repository, 'v2.0.0')
old_repo = linguist_repo(old_commit)
assert old_repo.languages['Ruby'] > 10_000
assert old_repo.size > 30_000
old_cache = [old_commit, old_repo.cache]
new_repo = Linguist::Repository.new(rugged_repository, master_oid, old_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