adamsanderson / ruby_diff

Higher level ruby code comparison

This URL has Read+Write access

commit  291331712dd22b0fe300fd959bf7ecd9f7cc0fd8
tree    46a4b938d814ecee04ad315c0aedb80b9521cc5f
parent  cd324a4d8373e04454d11489486903fe99c4298f
ruby_diff / lib / ruby_diff / git_feeder.rb
100644 60 lines (49 sloc) 1.523 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
55
56
57
58
59
60
# A Feeder reads in files for RubyDiff's processor to
# run over. GitFeeder reads them from a git repository.
#
# Example Usage:
# ruby_diff --git v0.1:lib --git v0.1:lib
# ruby_diff --git HEAD^2 --git HEAD^1 --git HEAD
class GitFeeder
  GitFile = Struct.new :access, :type, :hash, :name
  
  attr_accessor :files
  attr_accessor :path
  
  include Enumerable
  include GitSupport
  
  # Expects something in the form of REV:PATH
  # --git REV:[PATH]
  def initialize(path)
    @path = path
    
    rev,path = path.split(":",2)
    raise ArgumentError.new("Must supply a git revision") unless rev
    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-tree -r #{rev}"
      git_list.each_line do |line|
        file = GitFile.new(*line.chomp.split(/\s+/,4))
        
        if file.type == 'blob' and File.fnmatch(@file_pattern, file.name)
          @files << file
        end
      end
    end
    
  end
    
  def each
    FileUtils.cd(@working_dir) do
      @files.each do |file|
        code = git "show #{file.hash}"
        yield(code, file.name)
      end
    end
  end
  
end