class MethodLog::Commit

Attributes

sha[R]

Public Class Methods

new(sha, repository = nil) click to toggle source
# File lib/method_log/commit.rb, line 9
def initialize(sha, repository = nil)
  @repository = repository
  @sha = sha
  @index = Rugged::Index.new
end

Public Instance Methods

==(other) click to toggle source
# File lib/method_log/commit.rb, line 61
def ==(other)
  sha == other.sha
end
add(source_file) click to toggle source
# File lib/method_log/commit.rb, line 15
def add(source_file)
  oid = @repository.write(source_file.source, :blob)
  @index.add(path: source_file.path, oid: oid, mode: 0100644)
end
apply(options = {}) click to toggle source
# File lib/method_log/commit.rb, line 20
def apply(options = {})
  user, message = options[:user], options[:message]
  tree = @index.write_tree(@repository)
  parents = @repository.empty? ? [] : [@repository.head.target].compact
  @sha = Rugged::Commit.create(@repository, tree: tree, parents: parents, update_ref: 'HEAD', author: user, committer: user, message: message)
end
author() click to toggle source
# File lib/method_log/commit.rb, line 53
def author
  commit.author
end
contains?(source_file) click to toggle source
# File lib/method_log/commit.rb, line 38
def contains?(source_file)
  source_files_by_path[source_file.path] == source_file
end
find(method_identifier, last_source_file = nil) click to toggle source
# File lib/method_log/commit.rb, line 42
def find(method_identifier, last_source_file = nil)
  method_definition = nil
  method_name = method_identifier.split(Regexp.union('#', '.')).last
  source_files.sort_by { |sf| last_source_file == sf ? 1 : 0 }.each do |source_file|
    next unless source_file.source[Regexp.new(method_name)]
    method_finder = MethodFinder.new(source_file)
    break if method_definition = method_finder.find(method_identifier)
  end
  method_definition
end
hash() click to toggle source
# File lib/method_log/commit.rb, line 65
def hash
  sha.hash
end
message() click to toggle source
# File lib/method_log/commit.rb, line 57
def message
  commit.message
end
source_files() click to toggle source
# File lib/method_log/commit.rb, line 27
def source_files
  @source_files ||= Enumerator.new do |yielder|
    commit.tree.walk_blobs do |root, blob_hash|
      name = blob_hash[:name]
      next unless File.extname(name) == '.rb'
      path = root.empty? ? name : File.join(root, name)
      yielder << SourceFile.new(path: path, repository: @repository, sha: blob_hash[:oid])
    end
  end
end
to_s() click to toggle source
# File lib/method_log/commit.rb, line 69
def to_s
  sha
end

Private Instance Methods

commit() click to toggle source
# File lib/method_log/commit.rb, line 75
def commit
  @commit ||= @repository.lookup(sha)
end
source_files_by_path() click to toggle source
# File lib/method_log/commit.rb, line 79
def source_files_by_path
  source_files.inject({}) do |hash, source_file|
    hash[source_file.path] = source_file
    hash
  end
end