Class: GitFeeder

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby_diff/git_feeder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GitFeeder

Expects something in the form of REV:PATH

--git REV:[PATH]

Raises:

  • (ArgumentError)


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
# File 'lib/ruby_diff/git_feeder.rb', line 10

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 "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

Instance Attribute Details

#filesObject

Returns the value of attribute files.



3
4
5
# File 'lib/ruby_diff/git_feeder.rb', line 3

def files
  @files
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/ruby_diff/git_feeder.rb', line 4

def path
  @path
end

Instance Method Details

#eachObject



42
43
44
45
46
47
48
49
# File 'lib/ruby_diff/git_feeder.rb', line 42

def each
  FileUtils.cd(@working_dir) do
    @files.each do |file|
      code = git "git-show #{file.hash}"
      yield(code)      
    end
  end
end

#git(command) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/ruby_diff/git_feeder.rb', line 69

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

#init_git(path, search_path = '') ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_diff/git_feeder.rb', line 51

def init_git(path, search_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.split(path).last, 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