Skip to content

Commit

Permalink
Added special support for the git working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsanderson committed Jun 9, 2008
1 parent 979c50c commit e482447
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -24,7 +24,7 @@ namespace :git do
task :changed do |t|
_divider
puts "Changes since last commit:"
puts `./bin/ruby_diff --git HEAD --file ./`
puts `./bin/ruby_diff --git HEAD --git-wd ./`
_divider
end

Expand Down
8 changes: 7 additions & 1 deletion bin/ruby_diff
Expand Up @@ -19,7 +19,8 @@ end

feeder_mapping = {
:file => FileFeeder,
:git => GitFeeder
:git => GitFeeder,
:git_wd => GitWorkingDirFeeder,
}

feeders = []
Expand Down Expand Up @@ -52,6 +53,11 @@ opts.on('--verbose', "Shows more information while processing files"){
opts.on('--git PATH', "Use a git repository as a code source"){|path|
feeders << feeder_mapping[:git].new(path)
}

opts.on('--git-wd PATH', "Use the git working directory as a code source"){|path|
feeders << feeder_mapping[:git_wd].new(path)
}

opts.on('--file PATH', "Use a file system path as a code source"){|path|
feeders << feeder_mapping[:file].new(path)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_diff.rb
Expand Up @@ -15,6 +15,6 @@ module RubyDiff
end

# RubyDiff
%w(code_comparison structure_processor file_feeder git_feeder).each do |name|
%w(code_comparison structure_processor file_feeder git_support git_feeder git_working_dir_feeder).each do |name|
require File.expand_path(File.dirname(__FILE__) + "/ruby_diff/#{name}")
end
28 changes: 1 addition & 27 deletions lib/ruby_diff/git_feeder.rb
Expand Up @@ -4,6 +4,7 @@ class GitFeeder
attr_accessor :path

include Enumerable
include GitSupport

# Expects something in the form of REV:PATH
# --git REV:[PATH]
Expand Down Expand Up @@ -47,32 +48,5 @@ def each
end
end
end

def init_git(path, search_path='')
path = File.expand_path(path)
if File.exist?(File.join(path, ".git"))
# If this is the git repository
@working_dir = path
@search_path = search_path

else
next_search = File.join( File.basename(path), search_path )
next_path = File.dirname(path)

if next_path == path # We have reached the root, and can go no further
raise "Could not find a git working directory"
else
init_git(next_path, next_search)
end
end
end

def git command
output = `git #{command} 2>&1`.chomp
unless $?.success?
raise RuntimeError, output
end
output
end

end
28 changes: 28 additions & 0 deletions lib/ruby_diff/git_support.rb
@@ -0,0 +1,28 @@
module GitSupport
def init_git(path, search_path='')
path = File.expand_path(path)
if File.exist?(File.join(path, ".git"))
# If this is the git repository
@working_dir = path
@search_path = search_path

else
next_search = File.join( File.basename(path), search_path )
next_path = File.dirname(path)

if next_path == path # We have reached the root, and can go no further
raise "Could not find a git working directory"
else
init_git(next_path, next_search)
end
end
end

def git command
output = `git #{command} 2>&1`.chomp
unless $?.success?
raise RuntimeError, output
end
output
end
end
47 changes: 47 additions & 0 deletions lib/ruby_diff/git_working_dir_feeder.rb
@@ -0,0 +1,47 @@
class GitWorkingDirFeeder
attr_accessor :files
attr_accessor :path

include Enumerable
include GitSupport

# Expects something in the form of PATH
# --file [PATH]
def initialize(path)
@path = path

path = File.expand_path(path) if path
init_git(path || '.')
@file_pattern = if @search_path == ''
"**.rb"
elsif @search_path =~ /\.rb#{File::SEPARATOR}$/
# So appending each piece into the search path during init_git
# causes the search path to always end with a /
@search_path[0...-1]
else
File.join(@search_path,"**.rb")
end

@files = []

FileUtils.cd(@working_dir) do
git_list = git "ls-files"
git_list.each_line do |line|
file = line.chomp
if File.fnmatch(@file_pattern, file)
@files << file
end
end
end

end

def each
FileUtils.cd(@working_dir) do
@files.each do |file|
yield(open(file, 'r'){|io| io.read}, file)
end
end
end

end
3 changes: 1 addition & 2 deletions test/file_feeder_test.rb
Expand Up @@ -2,9 +2,8 @@
require 'test/unit/testcase'
require "ruby_diff"

DIR = File.join(File.dirname(__FILE__), "git_sample")

class FileFeederTestCase < Test::Unit::TestCase
DIR = File.join(File.dirname(__FILE__), "git_sample")

def test_find_all_files
assert File.exist?(DIR)
Expand Down
3 changes: 1 addition & 2 deletions test/git_feeder_test.rb
Expand Up @@ -2,9 +2,8 @@
require 'test/unit/testcase'
require "ruby_diff"

GIT_REPO = File.join(File.dirname(__FILE__), "git_sample")

class GitFeederTestCase < Test::Unit::TestCase
GIT_REPO = File.join(File.dirname(__FILE__), "git_sample")

def test_find_all_files
assert File.exist?(GIT_REPO)
Expand Down
36 changes: 36 additions & 0 deletions test/git_working_dir_feeder_test.rb
@@ -0,0 +1,36 @@
require 'test/unit'
require 'test/unit/testcase'
require "ruby_diff"

class GitWorkingDirFeederTestCase < Test::Unit::TestCase
DIR = File.join(File.dirname(__FILE__), "git_sample")

def test_find_all_files
assert File.exist?(DIR)
feeder = GitWorkingDirFeeder.new DIR
assert_equal 2, feeder.files.length
end

def test_find_single_file
assert File.exist?(DIR)
feeder = GitWorkingDirFeeder.new(File.join(DIR,'lib','chapter.rb'))
assert_equal 1, feeder.files.length
end

def test_find_files_in_sub_dir
feeder = GitWorkingDirFeeder.new(File.join(DIR,'lib'))
assert_equal 1, feeder.files.length
end

def test_files_are_suitable_for_processing
feeder = GitWorkingDirFeeder.new DIR
assert_nothing_raised do
sexps = feeder.map{|code,name| ParseTree.new.parse_tree_for_string(code)}

sexps.each do |sexp|
assert sexp.length > 0, "Parsed code should not be empty"
end
end
end

end

0 comments on commit e482447

Please sign in to comment.