ls-tree parser

This commit is contained in:
Tomasz Gieniusz 2012-10-16 22:03:25 +02:00
parent 67af71960f
commit 4eefcafd96
3 changed files with 29 additions and 5 deletions

View file

@ -3,7 +3,7 @@ module GitStats
class CommandParser class CommandParser
def parse(command, result) def parse(command, result)
cmd, params = command.scan(/git (.*) (.*)/).first.map(&:split).flatten cmd, params = command.scan(/git (.*) (.*)/).first.map(&:split).flatten
send("parse_#{cmd}", result, params) send("parse_#{cmd.underscore}", result, params)
end end
def parse_shortlog(result, params) def parse_shortlog(result, params)
@ -12,6 +12,14 @@ module GitStats
{commits: commits.to_i, name: name, email: email} {commits: commits.to_i, name: name, email: email}
end end
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 end
end end

View file

@ -8,10 +8,8 @@ module GitStats
attr_reader :repo, :hash, :stamp, :date, :author attr_reader :repo, :hash, :stamp, :date, :author
def files def files
@files ||= Command.new(repo, "git ls-tree -r #{self.hash}").run.lines.map do |line| @files ||= Command.new(repo, "git ls-tree -r #{self.hash}").run_and_parse.map do |file|
hash = line.split("\t")[0].split.last.strip Blob.new(repo: repo, filename: file[:filename], hash: file[:hash])
filename = line.split("\t")[1].strip
Blob.new(repo: repo, filename: filename, hash: hash)
end end
end end

18
spec/commit_spec.rb Normal file
View file

@ -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