Skip to content

Commit

Permalink
Merge pull request #48 from repotag/refactor_stats
Browse files Browse the repository at this point in the history
Refactor stats
  • Loading branch information
dometto committed Sep 16, 2019
2 parents 26626a8 + 1b97132 commit 09332b2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
*.gem
*.rbc
*.DS_Store
.bundle
.config
coverage
Expand Down
18 changes: 15 additions & 3 deletions lib/commit.rb
Expand Up @@ -40,9 +40,11 @@ def stats
df = DiffFormatter.new(DisabledOutputStream::INSTANCE)
df.set_repository(@jrepo)
df.set_context(0)
df.set_detect_renames(true)
parent_commit = @jcommit.parent_count > 0 ? @jcommit.get_parents[0] : nil
entries = df.scan(parent_commit, @jcommit)
results = {}

results = []
total_del = 0
total_ins = 0
entries.each do |entry|
Expand All @@ -57,9 +59,19 @@ def stats
end
total_del += del
total_ins += ins
results[file.getNewPath] = [ins, del, ins + del]
results << {
:new_file => file.getNewPath,
:old_file => file.getOldPath,
:new_additions => ins,
:new_deletions => del,
:changes => ins + del
}
end
return total_ins, total_del, results
return {
:total_additions => total_ins,
:total_deletions => total_del,
:files => results
}
end

def diff(options = {})
Expand Down
39 changes: 33 additions & 6 deletions spec/commit_spec.rb
Expand Up @@ -80,16 +80,43 @@

it "has stats" do
stats = Repo.new(TEST_REPO_PATH).commits[-2].stats
expect(stats[0]).to eq 8
expect(stats[1]).to eq 2
expect(stats[2]["postpatriarchialist.txt"]).to eq([2, 0, 2])
expect(stats[:total_additions]).to eq 8
expect(stats[:total_deletions]).to eq 2
expected_file_stat = {
:new_additions => 0,
:new_deletions => 2,
:changes => 2,
:new_file => 'deconstructions.txt',
:old_file => 'deconstructions.txt'
}
expect(stats[:files].first).to eq(expected_file_stat)
end

it "has stats that follow renames" do
stats = Repo.new(TEST_REPO_PATH).commits[0].stats
expected_file_stat = {
:new_additions => 0,
:new_deletions => 0,
:changes => 0,
:new_file => 'follow-rename.txt',
:old_file => 'rename-example.txt'
}
expect(stats[:files].first).to eq(expected_file_stat)
end

it "has stats on first commit" do
stats = Repo.new(TEST_REPO_PATH).commits[-1].stats
expect(stats[0]).to eq 228
expect(stats[1]).to eq 0
expect(stats[2]["postpatriarchialist.txt"]).to eq([75, 0, 75])
expect(stats[:total_additions]).to eq 228
expect(stats[:total_deletions]).to eq 0
expected_file_stat = {
:new_additions => 75,
:new_deletions => 0,
:changes => 75,
:new_file => 'postpatriarchialist.txt',
:old_file => '/dev/null'
}
result = stats[:files].find {|f| f[:new_file] == 'postpatriarchialist.txt'}
expect(result).to eq(expected_file_stat)
end

it "has diffs compared with parent commit" do
Expand Down
Binary file added spec/fixtures/.DS_Store
Binary file not shown.

0 comments on commit 09332b2

Please sign in to comment.