Skip to content

Commit

Permalink
Introduce Repository concept along with specs and module refactoring
Browse files Browse the repository at this point in the history
An initialize file is introduced to aid in loading up the required
classes and libraries.

A spec_helper and basic specs (for Repository) exists, along with a
RepositoryHelper to handle temporary git repos.

Refactor classes to use nested modules.

Change lib/git_evolution to be a module, thus executables had to change.
  • Loading branch information
kevinjalbert committed Feb 23, 2015
1 parent 24f6a50 commit 73b534c
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/git-evolution
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

require 'git_evolution'

GitEvolution.new.run(ARGV)
GitEvolution.run(ARGV)
2 changes: 1 addition & 1 deletion bin/git_evolution
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

require 'git_evolution'

GitEvolution.new.run(ARGV)
GitEvolution.run(ARGV)
1 change: 1 addition & 0 deletions git_evolution.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions lib/git_evolution.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/git_evolution/initialize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rugged'

Dir.glob(File.dirname(__FILE__) + '/**/*.rb') { |file| require file }
17 changes: 17 additions & 0 deletions lib/git_evolution/repository.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/git_evolution/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class GitEvolution
module GitEvolution
VERSION = '0.0.1'
end
24 changes: 24 additions & 0 deletions spec/git_evolution/repository_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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'
16 changes: 16 additions & 0 deletions spec/support/repository_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 73b534c

Please sign in to comment.