public
Description: Higher level ruby code comparison
Homepage: http://endofline.wordpress.com
Clone URL: git://github.com/adamsanderson/ruby_diff.git
ruby_diff / lib / ruby_diff / git_working_dir_feeder.rb
100644 54 lines (45 sloc) 1.282 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# A Feeder reads in files for RubyDiff's processor to
# run over. GitWorkingDirFeeder reads them from a git repository. Usually
# used in conjunction with the GitFeeder.
#
# Example Usage:
# ruby_diff --git v0.1:lib --git-wd lib
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