From 1fd59361b5c4ce323d633261377a9175d3f8f1f9 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 25 Jun 2014 20:26:44 +0200 Subject: [PATCH] Proper incremental diffing --- lib/linguist/repository.rb | 18 +++++++++--------- test/test_repository.rb | 33 +++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lib/linguist/repository.rb b/lib/linguist/repository.rb index 9fb6ebc7..e11d2b23 100644 --- a/lib/linguist/repository.rb +++ b/lib/linguist/repository.rb @@ -14,10 +14,10 @@ module Linguist # Public: Initialize a new Repository # # Returns a Repository - def initialize(repo, sha1, existing_stats = nil) + def initialize(repo, commit_oid, existing_stats = nil) @repository = repo - @current_sha1 = sha1 - @old_sha1, @old_stats = existing_stats if existing_stats + @commit_oid = commit_oid + @old_commit_oid, @old_stats = existing_stats if existing_stats end # Public: Returns a breakdown of language stats. @@ -66,12 +66,12 @@ module Linguist 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 : {} - old_commit = old_sha1 && Rugged::Commit.lookup(repository, old_sha1) - new_commit = Rugged::Commit.lookup(repository, new_sha1) + old_tree = old_commit_oid && Rugged::Commit.lookup(repository, old_commit_oid).tree + 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| old = delta.old_file[:path] @@ -99,10 +99,10 @@ module Linguist def cache @cache ||= begin - if @old_sha1 == @current_sha1 + if @old_commit_oid == @commit_oid @old_stats else - incremental_stats(@old_sha1, @current_sha1, @old_stats) + compute_stats(@old_commit_oid, @commit_oid, @old_stats) end end end diff --git a/test/test_repository.rb b/test/test_repository.rb index 13dbdaef..bd96e66d 100644 --- a/test/test_repository.rb +++ b/test/test_repository.rb @@ -3,19 +3,24 @@ require 'linguist/repository' require 'test/unit' class TestRepository < Test::Unit::TestCase - include Linguist + def rugged_repository + @rugged ||= Rugged::Repository.new(File.expand_path("../../.git", __FILE__)) + end - def linguist_repo - r = Rugged::Repository.new(File.expand_path("../../.git", __FILE__)) - Linguist::Repository.new(r, '31921838cdc252536ec07668f73d4b64d8022750') + def master_oid + @master_oid ||= Rugged::Object.rev_parse_oid(rugged_repository, 'master') + 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 @@ -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?("lib/linguist/language.rb") 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