Class: Eternity::Commit
- Inherits:
-
Object
- Object
- Eternity::Commit
- Defined in:
- lib/eternity/commit.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .base_of(commit_1, commit_2) ⇒ Object
- .clear_history_cache ⇒ Object
- .create(options) ⇒ Object
- .exists?(id) ⇒ Boolean
- .history_cache_key ⇒ Object
Instance Method Summary collapse
- #==(commit) ⇒ Object (also: #eql?)
- #author ⇒ Object
- #base ⇒ Object
- #delta ⇒ Object
- #fast_forward?(commit) ⇒ Boolean
- #first? ⇒ Boolean
- #hash ⇒ Object
- #history ⇒ Object
- #history_ids ⇒ Object
-
#initialize(id) ⇒ Commit
constructor
A new instance of Commit.
- #merge? ⇒ Boolean
- #message ⇒ Object
- #nil? ⇒ Boolean
- #parent_ids ⇒ Object
- #parents ⇒ Object
- #short_id ⇒ Object
- #time ⇒ Object
- #to_s ⇒ Object
- #with_index ⇒ Object
Constructor Details
#initialize(id) ⇒ Commit
Returns a new instance of Commit.
6 7 8 |
# File 'lib/eternity/commit.rb', line 6 def initialize(id) @id = id end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/eternity/commit.rb', line 4 def id @id end |
Class Method Details
.base_of(commit_1, commit_2) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/eternity/commit.rb', line 119 def self.base_of(commit_1, commit_2) history_1 = [commit_1.id] history_2 = [commit_2.id] base_1 = commit_1 base_2 = commit_2 while (history_1 & history_2).empty? base_1 = base_1.base if base_1 base_2 = base_2.base if base_2 history_1 << base_1.id if base_1 history_2 << base_2.id if base_2 end Commit.new (history_1 & history_2).first end |
.clear_history_cache ⇒ Object
148 149 150 151 152 |
# File 'lib/eternity/commit.rb', line 148 def self.clear_history_cache Eternity.connection.call('KEYS', history_cache_key['*']).each_slice(1000) do |keys| Eternity.connection.call 'DEL', *keys end end |
.create(options) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/eternity/commit.rb', line 90 def self.create() raise 'Author must be present' if [:author].to_s.strip.empty? raise 'Message must be present' if [:message].to_s.strip.empty? # TODO: Move to Repository and Patch history = if [:parents].count == 2 current_history_ids = [[:parents][0]] + Commit.new([:parents][0]).history_ids target_history_ids = [[:parents][1]] + Commit.new([:parents][1]).history_ids current_history_ids - target_history_ids + target_history_ids else parent_id = [:parents][0] parent_id ? [parent_id] + Commit.new(parent_id).history_ids : [] end data = { time: Time.now, author: .fetch(:author), message: .fetch(:message), parents: .fetch(:parents), index: .fetch(:index), delta: .fetch(:delta), base: [:parents].count == 2 ? .fetch(:base) : [:parents].first, history: Blob.write(:history, history) } new Blob.write(:commit, data) end |
.exists?(id) ⇒ Boolean
137 138 139 140 141 142 |
# File 'lib/eternity/commit.rb', line 137 def self.exists?(id) Blob.read :commit, id true rescue false end |
.history_cache_key ⇒ Object
144 145 146 |
# File 'lib/eternity/commit.rb', line 144 def self.history_cache_key Eternity.keyspace[:cache][:history] end |
Instance Method Details
#==(commit) ⇒ Object Also known as: eql?
76 77 78 79 |
# File 'lib/eternity/commit.rb', line 76 def ==(commit) commit.class == self.class && commit.id == id end |
#author ⇒ Object
18 19 20 |
# File 'lib/eternity/commit.rb', line 18 def data['author'] end |
#base ⇒ Object
45 46 47 |
# File 'lib/eternity/commit.rb', line 45 def base Commit.new data['base'] end |
#delta ⇒ Object
41 42 43 |
# File 'lib/eternity/commit.rb', line 41 def delta data['delta'] ? Blob.read(:delta, data['delta']) : {} end |
#fast_forward?(commit) ⇒ Boolean
58 59 60 61 62 |
# File 'lib/eternity/commit.rb', line 58 def fast_forward?(commit) return false if nil? return true if commit.nil? history_ids.include? commit.id end |
#first? ⇒ Boolean
64 65 66 |
# File 'lib/eternity/commit.rb', line 64 def first? parent_ids.compact.empty? end |
#hash ⇒ Object
82 83 84 |
# File 'lib/eternity/commit.rb', line 82 def hash id.hash end |
#history ⇒ Object
54 55 56 |
# File 'lib/eternity/commit.rb', line 54 def history history_ids.map { |id| Commit.new id } end |
#history_ids ⇒ Object
49 50 51 52 |
# File 'lib/eternity/commit.rb', line 49 def history_ids return [] if nil? Blob.read :history, data['history'] end |
#merge? ⇒ Boolean
68 69 70 |
# File 'lib/eternity/commit.rb', line 68 def merge? parent_ids.count == 2 end |
#message ⇒ Object
22 23 24 |
# File 'lib/eternity/commit.rb', line 22 def data['message'] end |
#nil? ⇒ Boolean
72 73 74 |
# File 'lib/eternity/commit.rb', line 72 def nil? id.nil? end |
#parent_ids ⇒ Object
26 27 28 |
# File 'lib/eternity/commit.rb', line 26 def parent_ids data['parents'] || [nil] end |
#parents ⇒ Object
30 31 32 |
# File 'lib/eternity/commit.rb', line 30 def parents parent_ids.map { |id| Commit.new id } end |
#short_id ⇒ Object
10 11 12 |
# File 'lib/eternity/commit.rb', line 10 def short_id id ? id[0,7] : nil end |
#time ⇒ Object
14 15 16 |
# File 'lib/eternity/commit.rb', line 14 def time Time.parse data['time'] if data['time'] end |
#to_s ⇒ Object
86 87 88 |
# File 'lib/eternity/commit.rb', line 86 def to_s "#{time} - #{short_id} - #{}: #{}" end |