Method: Eternity::Commit.create

Defined in:
lib/eternity/commit.rb

.create(options) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/eternity/commit.rb', line 110

def self.create(options)
  raise 'Author must be present' if options[:author].to_s.strip.empty?
  raise 'Message must be present' if options[:message].to_s.strip.empty?

  # TODO: Move to Repository and Patch
  history =
    if options[:parents].count == 2
      current_history_ids = [options[:parents][0]] + Commit.new(options[:parents][0]).history_ids
      target_history_ids = [options[:parents][1]] + Commit.new(options[:parents][1]).history_ids
      current_history_ids - target_history_ids + target_history_ids
    else
      parent_id = options[:parents][0]
      parent_id ? [parent_id] + Commit.new(parent_id).history_ids : []
    end

  data = {
    time:          Time.now,
    author:        options.fetch(:author),
    message:       options.fetch(:message),
    parents:       options.fetch(:parents),
    index:         options.fetch(:index),
    delta:         options.fetch(:delta),
    base:          options[:parents].count == 2 ? options.fetch(:base) : options[:parents].first,
    history:       Blob.write(:history, history)
  }

  new Blob.write(:commit, data)
end