Class: MethodLog::Commit

Inherits:
Object
  • Object
show all
Defined in:
lib/method_log/commit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sha, repository = nil) ⇒ Commit

Returns a new instance of Commit.



9
10
11
12
13
# File 'lib/method_log/commit.rb', line 9

def initialize(sha, repository = nil)
  @repository = repository
  @sha = sha
  @index = Rugged::Index.new
end

Instance Attribute Details

#shaObject (readonly)

Returns the value of attribute sha.



7
8
9
# File 'lib/method_log/commit.rb', line 7

def sha
  @sha
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
# File 'lib/method_log/commit.rb', line 61

def ==(other)
  sha == other.sha
end

#add(source_file) ⇒ Object



15
16
17
18
# 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 = {}) ⇒ Object



20
21
22
23
24
25
# 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

#authorObject



53
54
55
# File 'lib/method_log/commit.rb', line 53

def author
  commit.author
end

#contains?(source_file) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/method_log/commit.rb', line 42

def find(method_identifier)
  method_definition = nil
  method_name = method_identifier.split(Regexp.union('#', '.')).last
  source_files.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

#hashObject



65
66
67
# File 'lib/method_log/commit.rb', line 65

def hash
  sha.hash
end

#messageObject



57
58
59
# File 'lib/method_log/commit.rb', line 57

def message
  commit.message
end

#source_filesObject



27
28
29
30
31
32
33
34
35
36
# 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_sObject



69
70
71
# File 'lib/method_log/commit.rb', line 69

def to_s
  sha
end