Class: VCSToolkit::Objects::Commit

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

Instance Attribute Summary collapse

Attributes inherited from Object

#id, #object_type

Instance Method Summary collapse

Methods inherited from Object

#==, #hash, #named?

Methods included from Serializable

#from_hash, #serialize_on

Methods included from Utils::HashableObject

included

Constructor Details

#initialize(message:, tree:, parents:, author:, date:, id: nil, **context) ⇒ Commit

Returns a new instance of Commit.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vcs_toolkit/objects/commit.rb', line 10

def initialize(message:, tree:, parents:, author:, date:, id: nil, **context)
  @message = message
  @tree    = tree
  @parents = parents
  @author  = author
  @date    = date

  super id:          id,
        object_type: :commit,
        **context
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



6
7
8
# File 'lib/vcs_toolkit/objects/commit.rb', line 6

def author
  @author
end

#dateObject (readonly)

Returns the value of attribute date.



6
7
8
# File 'lib/vcs_toolkit/objects/commit.rb', line 6

def date
  @date
end

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/vcs_toolkit/objects/commit.rb', line 6

def message
  @message
end

#parentsObject (readonly)

Returns the value of attribute parents.



6
7
8
# File 'lib/vcs_toolkit/objects/commit.rb', line 6

def parents
  @parents
end

#treeObject (readonly)

Returns the value of attribute tree.



6
7
8
# File 'lib/vcs_toolkit/objects/commit.rb', line 6

def tree
  @tree
end

Instance Method Details

#common_ancestor(other_commit, object_store) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/vcs_toolkit/objects/commit.rb', line 65

def common_ancestor(other_commit, object_store)
  my_ancestors = history(object_store).to_set

  other_commit.enum_for(:history, object_store).find do |ancestor|
    my_ancestors.include? ancestor
  end
end

#history(object_store) ⇒ Object

Enumerates all commits in the current commit’s history. If a block is given each commit is yielded to it.



26
27
28
29
30
31
32
# File 'lib/vcs_toolkit/objects/commit.rb', line 26

def history(object_store)
  history_diff(object_store) do |commit|
    yield commit if block_given?

    false
  end
end

#history_diff(object_store) ⇒ Object

Enumerates commits in the current commit’s history.

Each commit is yielded and if the block result is a trueish value the commit’s parents are not enumerated.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vcs_toolkit/objects/commit.rb', line 40

def history_diff(object_store)
  commits      = {id => self}
  commit_queue = [self]

  until commit_queue.empty?
    commit = commit_queue.shift

    if yield commit
      commits.delete commit.id
      next
    end

    commit.parents.each do |parent_id|
      unless commits.key? parent_id
        parent = object_store.fetch parent_id

        commits[parent_id] = parent
        commit_queue << parent
      end
    end
  end

  commits.values
end