From 4eefcafd96d39b7cd772f2422d644ef3710df0bc Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Tue, 16 Oct 2012 22:03:25 +0200 Subject: [PATCH] ls-tree parser --- lib/git_stats/git_data/command_parser.rb | 10 +++++++++- lib/git_stats/git_data/commit.rb | 6 ++---- spec/commit_spec.rb | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 spec/commit_spec.rb diff --git a/lib/git_stats/git_data/command_parser.rb b/lib/git_stats/git_data/command_parser.rb index 72488ca21..8b47187a6 100644 --- a/lib/git_stats/git_data/command_parser.rb +++ b/lib/git_stats/git_data/command_parser.rb @@ -3,7 +3,7 @@ module GitStats class CommandParser def parse(command, result) cmd, params = command.scan(/git (.*) (.*)/).first.map(&:split).flatten - send("parse_#{cmd}", result, params) + send("parse_#{cmd.underscore}", result, params) end def parse_shortlog(result, params) @@ -12,6 +12,14 @@ module GitStats {commits: commits.to_i, name: name, email: email} end end + + def parse_ls_tree(result, params) + result.lines.map do |line| + mode, type, hash, filename = line.scan(/(.*) (.*) (.*)\t(.*)/).first.map(&:strip) + {mode: mode, type: type, hash: hash, filename: filename} + end + end + end end end \ No newline at end of file diff --git a/lib/git_stats/git_data/commit.rb b/lib/git_stats/git_data/commit.rb index 5ade7ad51..7c1fb41d0 100644 --- a/lib/git_stats/git_data/commit.rb +++ b/lib/git_stats/git_data/commit.rb @@ -8,10 +8,8 @@ module GitStats attr_reader :repo, :hash, :stamp, :date, :author def files - @files ||= Command.new(repo, "git ls-tree -r #{self.hash}").run.lines.map do |line| - hash = line.split("\t")[0].split.last.strip - filename = line.split("\t")[1].strip - Blob.new(repo: repo, filename: filename, hash: hash) + @files ||= Command.new(repo, "git ls-tree -r #{self.hash}").run_and_parse.map do |file| + Blob.new(repo: repo, filename: file[:filename], hash: file[:hash]) end end diff --git a/spec/commit_spec.rb b/spec/commit_spec.rb new file mode 100644 index 000000000..4102500b7 --- /dev/null +++ b/spec/commit_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe GitStats::GitData::Commit do + let(:commit) { build(:commit) } + + describe 'git output parsing' do + it 'should parse git ls-tree output' do + GitStats::GitData::Command.any_instance.should_receive(:run_in_repo).and_return("100644 blob 5ade7ad51a75ee7db4eb06cecd3918d38134087d lib/git_stats/git_data/commit.rb +100644 blob db01e94677a8f72289848e507a52a43de2ea109a lib/git_stats/git_data/repo.rb +") + + commit.files.should == [ + GitStats::GitData::Blob.new(repo: commit.repo, hash: "5ade7ad51a75ee7db4eb06cecd3918d38134087d", filename: "lib/git_stats/git_data/commit.rb"), + GitStats::GitData::Blob.new(repo: commit.repo, hash: "db01e94677a8f72289848e507a52a43de2ea109a", filename: "lib/git_stats/git_data/repo.rb"), + ] + end + end +end \ No newline at end of file