diff --git a/bin/git-evolution b/bin/git-evolution index e929d18..8419383 100755 --- a/bin/git-evolution +++ b/bin/git-evolution @@ -2,4 +2,4 @@ require 'git_evolution' -GitEvolution.new.run(ARGV) +GitEvolution.run(ARGV) diff --git a/bin/git_evolution b/bin/git_evolution index e929d18..8419383 100755 --- a/bin/git_evolution +++ b/bin/git_evolution @@ -2,4 +2,4 @@ require 'git_evolution' -GitEvolution.new.run(ARGV) +GitEvolution.run(ARGV) diff --git a/git_evolution.gemspec b/git_evolution.gemspec index 525003f..aa91654 100644 --- a/git_evolution.gemspec +++ b/git_evolution.gemspec @@ -20,4 +20,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler', '~> 1.7' spec.add_development_dependency 'rake', '~> 10.0' + spec.add_development_dependency 'rugged', '~> 0.21.4' end diff --git a/lib/git_evolution.rb b/lib/git_evolution.rb index ef3b9be..26d6064 100644 --- a/lib/git_evolution.rb +++ b/lib/git_evolution.rb @@ -1,10 +1,14 @@ -class GitEvolution - def run(args) +require_relative './git_evolution/initialize.rb' + +module GitEvolution + def self.run(args) start_line = args[0] end_line = args[1] file = args[2] - results = `git --no-pager log -L#{start_line},#{end_line}:#{file} --follow #{file}` + repo = Repository.new(File.dirname(File.expand_path(file))) + + results = repo.line_history(start_line, end_line, file) commit_shas = results.scan(/^commit ([0-9a-f]{40})/) commit_shas = commit_shas.flatten diff --git a/lib/git_evolution/initialize.rb b/lib/git_evolution/initialize.rb new file mode 100644 index 0000000..f34a4c9 --- /dev/null +++ b/lib/git_evolution/initialize.rb @@ -0,0 +1,3 @@ +require 'rugged' + +Dir.glob(File.dirname(__FILE__) + '/**/*.rb') { |file| require file } diff --git a/lib/git_evolution/repository.rb b/lib/git_evolution/repository.rb new file mode 100644 index 0000000..8ae3e56 --- /dev/null +++ b/lib/git_evolution/repository.rb @@ -0,0 +1,17 @@ +module GitEvolution + class Repository + def initialize(directory_name) + @git_repo = Rugged::Repository.discover(File.expand_path(directory_name)) + end + + def dir + @git_repo.workdir + end + + def line_history(start_line, end_line, file) + Dir.chdir(dir) do + return `git --no-pager log -L#{start_line},#{end_line}:#{file} --follow #{file}` + end + end + end +end diff --git a/lib/git_evolution/version.rb b/lib/git_evolution/version.rb index 4d11ed1..e35259f 100644 --- a/lib/git_evolution/version.rb +++ b/lib/git_evolution/version.rb @@ -1,3 +1,3 @@ -class GitEvolution +module GitEvolution VERSION = '0.0.1' end diff --git a/spec/git_evolution/repository_spec.rb b/spec/git_evolution/repository_spec.rb new file mode 100644 index 0000000..2a73741 --- /dev/null +++ b/spec/git_evolution/repository_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +RSpec.describe GitEvolution::Repository do + describe '.new' do + context 'valid repository directory' do + before(:each) { RepositoryHelper.create_repository } + after(:each) { RepositoryHelper.delete_repository } + + it 'detects repository' do + repo = described_class.new(RepositoryHelper.repository_dir) + expect(repo.dir).to eq(RepositoryHelper.repository_dir) + end + end + + context 'invalid repository directory' do + let!(:tmp_dir) { Dir.mktmpdir } + after { FileUtils.rm_r(tmp_dir) } + + it 'detects no repository' do + expect { described_class.new(tmp_dir) }.to raise_error(Rugged::RepositoryError) + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..71bf98c --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,8 @@ +$LOAD_PATH.unshift File.expand_path('../lib', __FILE__) + +Bundler.require(:test) +Bundler.require(:development) + +Dir.glob(Dir.pwd + '/spec/support/**/*.rb') { |file| require file } + +require 'git_evolution/initialize' diff --git a/spec/support/repository_helper.rb b/spec/support/repository_helper.rb new file mode 100644 index 0000000..cb4f0ff --- /dev/null +++ b/spec/support/repository_helper.rb @@ -0,0 +1,16 @@ +module RepositoryHelper + module_function + + def create_repository + @tmp_git_dir = Dir.mktmpdir + Rugged::Repository.init_at(@tmp_git_dir) + end + + def delete_repository + FileUtils.rm_r(@tmp_git_dir) + end + + def repository_dir + File.realpath(@tmp_git_dir) + '/' + end +end