Class: MultiGit::Commit::Builder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Builder, Base
Defined in:
lib/multi_git/commit.rb

Overview

A commit builder helps creating new commits by providing a simple interface, sane defaults and some validations.

You can create a new commit using the commit builder like this:

Examples:

Commit an small tree

#setup:
dir = `mktemp -d`
# example:
builder = MultiGit::Commit::Builder.new
builder.message = "My first commit"
builder.by "[email protected]"
builder["a_file"] = "some content"
# builder is now ready to be inserted
repository = MultiGit.open(dir, init: true)
commit = repository << builder #=> be_a MultiGit::Commit
commit['a_file'].content  #=> eql "some content"
# teardown:
`rm -rf #{dir}`

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#[], #type

Methods included from Utils::AbstractMethods

#abstract

Methods included from Builder

#to_builder

Constructor Details

#initialize(from = nil, &block) ⇒ Builder

Returns a new instance of Builder.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/multi_git/commit.rb', line 152

def initialize(from = nil, &block)
  @parents = []
  if from.kind_of? Tree
    @tree = from.to_builder
  elsif from.kind_of? Tree::Builder
    @tree = from
  elsif from.kind_of? Commit
    @tree = from.tree.to_builder
    @parents << from
  elsif from.nil?
    @tree = Tree::Builder.new
  end
  @author = nil
  @committer = nil
  @time = @commit_time = Time.now
  instance_eval(&block) if block
end

Instance Attribute Details

#authorHandle

Returns:



105
106
107
# File 'lib/multi_git/commit.rb', line 105

def author
  @author
end

#commit_timeTime

Returns:

  • (Time)


102
103
104
# File 'lib/multi_git/commit.rb', line 102

def commit_time
  @commit_time
end

#committerHandle

Returns:



107
108
109
# File 'lib/multi_git/commit.rb', line 107

def committer
  @committer
end

#message(*args) ⇒ String

Returns:

  • (String)


80
81
82
83
84
85
86
87
# File 'lib/multi_git/commit.rb', line 80

def message(*args)
  if args.any?
    self.message = args.first
    return self
  else
    return @message
  end
end

#parentsArray<Commit::Base> (readonly)

Returns:



97
98
99
# File 'lib/multi_git/commit.rb', line 97

def parents
  @parents
end

#timeTime

Returns:

  • (Time)


100
101
102
# File 'lib/multi_git/commit.rb', line 100

def time
  @time
end

Instance Method Details

#>>(repo) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/multi_git/commit.rb', line 170

def >>(repo)
  new_tree = repo << tree
  new_parents = parents.map{|p| repo.write(p).oid }
  c = committer || author    || Handle::DEFAULT
  a = author    || committer || Handle::DEFAULT
  return repo.make_commit(
    :time => time,
    :author => a,
    :commit_time => commit_time,
    :committer => c,
    :parents => new_parents,
    :tree => new_tree.oid,
    :message => message || "Thank you for using multi_git",
    :update_ref => []
  )
end

#[]=(path, content) ⇒ Object

See Also:

  • MultiGit::Commit::Builder.(MultiGit(MultiGit::TreeBuilder(MultiGit::TreeBuilder#[]=)


150
# File 'lib/multi_git/commit.rb', line 150

delegate :[]= => :tree

#at(time) ⇒ Object



142
143
144
145
# File 'lib/multi_git/commit.rb', line 142

def at(time)
  self.time = self.commit_time = time
  return self
end

#by(handle) ⇒ Object

DSL method to set author and committer in one step

Parameters:

Returns:

  • self



137
138
139
140
# File 'lib/multi_git/commit.rb', line 137

def by(handle)
  self.author = self.committer = handle
  return self
end

#tree { ... } ⇒ Tree::Builder

Yields:

  • allows

Returns:



92
93
94
95
# File 'lib/multi_git/commit.rb', line 92

def tree(&block)
  @tree.instance_eval(&block) if block
  @tree
end