Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added special support for the git working directory
- Loading branch information
1 parent
979c50c
commit e482447
Showing
9 changed files
with
123 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |