Решение на Пета задача от Никола Терзиев

Обратно към всички решения

Към профила на Никола Терзиев

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 22 успешни тест(а)
  • 8 неуспешни тест(а)

Код

require 'digest'
require 'pp'
class Transaction
attr_reader :message, :result
def initialize(message, status, result = nil)
@message = message
@status = status
@result = result
end
def success?
@status == true
end
def error?
@status == false
end
end
class BlobObject
attr_reader :name, :object
attr_accessor :to_delete
def initialize(name, object, to_delete = false)
@name = name
@object = object
@to_delete = to_delete
end
def to_delete?
@to_delete
end
def ==(other)
@name == other.name && @object == other.object
end
end
class CommitObject
attr_reader :message, :date, :hash, :objects
def initialize(message, objects)
@message = message
@date = Time.now
@hash = Digest::SHA1.hexdigest("#{@date}#{@message}")
@objects = objects
end
def ==(other)
@hash == other.hash
end
def to_s
formatted_date = @date.strftime('%a %b %-d %H:%M %Y %z')
"Commit #{@hash}\nDate: #{formatted_date}\n\n\t#{@message}"
end
end
class Branch
attr_accessor :name, :commits, :staged, :object_state
def initialize(name, object_store)
@name = name
@object_store = object_store
@commits = []
@staged = []
@object_state = []
end
def create(branch_name)
if get_branch_index(branch_name).nil?
new_branch = Marshal.load(Marshal.dump(self))
new_branch.name = branch_name
@object_store.branches.push(new_branch)
Transaction.new("Created branch #{branch_name}.", true, new_branch)
else
Transaction.new("Branch #{branch_name} already exists.", false)
end
end
def checkout(branch_name)
branch_index = get_branch_index branch_name
if branch_index.nil?
Transaction.new("Branch #{branch_name} does not exist.", false)
else
@object_store.work_branch = @object_store.branches[branch_index]
Transaction.new("Switched to branch #{branch_name}.",
true,
@object_store.work_branch)
end
end
def remove(branch_name)
branch_index = get_branch_index branch_name
if branch_index.nil?
Transaction.new("Branch #{branch_name} does not exist.", false)
elsif @object_store.work_branch.name == branch_name
Transaction.new('Cannot remove current branch.', false)
else
@object_store.branches.delete_at(branch_index)
Transaction.new("Removed branch #{branch_name}.", true)
end
end
def list
branch_names = @object_store.branches.map(&:name).sort
branch_names.map! do |branch_name|
if branch_name == @object_store.work_branch.name
"* #{branch_name}"
else
" #{branch_name}"
end
end
Transaction.new(branch_names.join('\n'), true)
end
def commit_staged
object_count = staged.length
staged.each do |object|
if object.to_delete?
@object_state.delete(object)
else
@object_state.push(object)
end
end
staged.clear
object_count
end
def revert_to_commit(commit_hash)
commit_index = @commits.index { |commit| commit.hash == commit_hash }
return false if commit_index.nil?
@commits.drop(commit_index + 1).each do |commit|
_apply_reverse_commit commit
end
@commits = @commits.take(commit_index + 1)
true
end
def _apply_reverse_commit(commit)
commit.objects.each do |object|
if object.to_delete?
@object_state.push(object)
else
@object_state.delete(object)
end
end
end
def staged_object_index(name)
@staged.index { |item| item.name == name }
end
def object_state_index(name)
@object_state.index { |item| item.name == name }
end
def get_branch_index(branch_name)
@object_store.branches.index { |branch| branch.name == branch_name }
end
end
class ObjectStore
attr_accessor :branches, :work_branch
def initialize
@work_branch = Branch.new('master', self)
@branches = [@work_branch]
end
def self.init(&block)
object_store = ObjectStore.new
object_store.instance_eval &block if block_given?
object_store
end
def branch
@work_branch
end
def add(name, object)
object_index = @work_branch.staged_object_index name
@work_branch.staged.delete_at object_index unless object_index.nil?
blob_object = BlobObject.new(name, object)
@work_branch.staged.push(blob_object)
Transaction.new("Added #{name} to stage.", true, object)
end
def remove(name)
object_index = @work_branch.object_state_index name
if object_index.nil?
Transaction.new("Object #{name} is not committed.", false)
else
marked_object = @work_branch.object_state[object_index]
marked_object.to_delete = true
@work_branch.staged.push(marked_object)
Transaction.new("Added #{name} for removal.", true, marked_object.object)
end
end
def commit(message)
if @work_branch.staged.empty?
message = 'Nothing to commit, working directory clean.'
return Transaction.new(message, false)
end
stage_clone = @work_branch.staged.clone
length = @work_branch.commit_staged
commit = CommitObject.new(message, stage_clone)
@work_branch.commits.push(commit)
Transaction.new("#{message}\n\t#{length} objects changed", true, commit)
end
def checkout(commit_hash)
checkout_result = @work_branch.revert_to_commit commit_hash
if checkout_result
commit = @work_branch.commits.last
Transaction.new("HEAD is now at #{commit.hash}.", true, commit)
else
Transaction.new("Commit #{commit_hash} does not exist.", false)
end
end
def log
if @work_branch.commits.empty?
message = "Branch #{@work_branch.name} does not have any commits yet."
Transaction.new(message, false)
else
Transaction.new(@work_branch.commits.reverse.join("\n\n"), true)
end
end
def get(object_name)
commit_object = @work_branch.object_state.find do |item|
item.name == object_name
end
if commit_object.nil?
Transaction.new("Object #{object_name} is not committed.", false)
else
message = "Found object #{object_name}."
Transaction.new(message, true, commit_object.object)
end
end
def head
if @work_branch.commits.empty?
message = "Branch #{@work_branch.name} does not have any commits yet."
Transaction.new(message, false)
else
message = "#{@work_branch.commits.last.message}"
Transaction.new(message, true, @work_branch.commits.last)
end
end
end

Лог от изпълнението

........FFFF.F..F...........FF

Failures:

  1) ObjectStore can show log of changes for a single commit
     Failure/Error: expect(repo.log).to be_success("Commit #{commit_hash}\nDate: #{Time.now.strftime("%a %b %d %H:%M %Y %z")}\n\n\tSo cool!")
       expected #<Transaction:0x007f3c47c49d18 @message="Commit 1547771f2b73f2616ed6d42d69b7ca481bf006df\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!", @status=true, @result=nil> to be success "Commit 020f94292ff683fc536310fe33c03adfbf64bbca\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-i7bw8c/spec.rb:83:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) ObjectStore can show log of changes for a single commit
     Failure/Error: expect(repo.log).to be_success("Commit #{commit_hash}\nDate: #{Time.now.strftime("%a %b %d %H:%M %Y %z")}\n\n\tSo cool!")
       expected #<Transaction:0x007f3c47c29fe0 @message="Commit 1547771f2b73f2616ed6d42d69b7ca481bf006df\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!", @status=true, @result=nil> to be success "Commit 020f94292ff683fc536310fe33c03adfbf64bbca\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-i7bw8c/spec.rb:92:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) ObjectStore can show log of changes for multiple commits
     Failure/Error: expect(repo.log).to be_success("Commit #{commit2_hash}\nDate: #{current_time}\n\n\tSecond commit\n\nCommit #{commit1_hash}\nDate: #{current_time}\n\n\tFirst commit")
       expected #<Transaction:0x007f3c47be2960 @message="Commit a716da56201c2c39be9378609ab016a3cfb469ee\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSecond commit\n\nCommit 81be004f554082f488574cd4e7842a9d61098d90\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit", @status=true, @result=nil> to be success "Commit a6bf7130a3150d375443c25d06c1bf8077e4b4fb\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSecond commit\n\nCommit 9a97b358afe869ba1e523d99dd2101dfc0570dc0\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-i7bw8c/spec.rb:108:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) ObjectStore shows the log for the current branch only
     Failure/Error: expect(repo.log).to be_success("Commit #{commit1_hash}\nDate: #{current_time}\n\n\tFirst commit")
       expected #<Transaction:0x007f3c47bcae50 @message="Commit 81be004f554082f488574cd4e7842a9d61098d90\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit", @status=true, @result=nil> to be success "Commit 9a97b358afe869ba1e523d99dd2101dfc0570dc0\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-i7bw8c/spec.rb:128:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) ObjectStore can list branches
     Failure/Error: expect(repo.branch.list).to be_success("  develop\n  feature\n* master")
       expected #<Transaction:0x007f3c47b312c8 @message="  develop\\n  feature\\n* master", @status=true, @result=nil> to be success "  develop\n  feature\n* master"
     # /tmp/d20160111-5693-i7bw8c/spec.rb:140:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) ObjectStore can switch branches
     Failure/Error: expect(repo.branch.checkout("master")).to be_success("Switched to branch master.")
       expected #<Transaction:0x007f3c47adb238 @message="Branch master does not exist.", @status=false, @result=nil> to be success "Switched to branch master."
     # /tmp/d20160111-5693-i7bw8c/spec.rb:162:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) ObjectStore can show the objects in a repo after overwriting an object
     Failure/Error: expect(first_commit.objects).to match_array(["content1"])
     NoMethodError:
       undefined method `name' for "content1":String
     # /tmp/d20160111-5693-i7bw8c/solution.rb:37:in `=='
     # /tmp/d20160111-5693-i7bw8c/spec.rb:243:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) ObjectStore can show the objects of a repo after removing an object
     Failure/Error: expect(first_commit.objects).to match_array(["content1", "content2", "content3"])
     NoMethodError:
       undefined method `name' for "content1":String
     # /tmp/d20160111-5693-i7bw8c/solution.rb:37:in `=='
     # /tmp/d20160111-5693-i7bw8c/spec.rb:259:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.03259 seconds
30 examples, 8 failures

Failed examples:

rspec /tmp/d20160111-5693-i7bw8c/spec.rb:77 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-i7bw8c/spec.rb:86 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-i7bw8c/spec.rb:95 # ObjectStore can show log of changes for multiple commits
rspec /tmp/d20160111-5693-i7bw8c/spec.rb:111 # ObjectStore shows the log for the current branch only
rspec /tmp/d20160111-5693-i7bw8c/spec.rb:136 # ObjectStore can list branches
rspec /tmp/d20160111-5693-i7bw8c/spec.rb:153 # ObjectStore can switch branches
rspec /tmp/d20160111-5693-i7bw8c/spec.rb:239 # ObjectStore can show the objects in a repo after overwriting an object
rspec /tmp/d20160111-5693-i7bw8c/spec.rb:253 # ObjectStore can show the objects of a repo after removing an object

История (2 версии и 0 коментара)

Никола обнови решението на 22.11.2015 02:07 (преди над 8 години)

+require 'digest'
+require 'pp'
+
+class Transaction
+ attr_reader :message, :result
+
+ def initialize(message, status, result = nil)
+ @message = message
+ @status = status
+ @result = result
+ end
+
+ def success?
+ @status == true
+ end
+
+ def error?
+ @status == false
+ end
+end
+
+class BlobObject
+ attr_reader :name, :object
+ attr_accessor :to_delete
+
+ def initialize(name, object, to_delete = false)
+ @name = name
+ @object = object
+ @to_delete = to_delete
+ end
+
+ def to_delete?
+ @to_delete
+ end
+
+ def ==(other)
+ @name == other.name && @object == other.object
+ end
+end
+
+class CommitObject
+ attr_reader :message, :date, :hash, :objects
+
+ def initialize(message, objects)
+ @message = message
+ @date = Time.now
+ @hash = Digest::SHA1.hexdigest("#{@date}#{@message}")
+ @objects = objects
+ end
+
+ def ==(other)
+ @hash == other.hash
+ end
+
+ def to_s
+ formatted_date = @date.strftime('%a %b %-d %H:%M %Y %z')
+ "Commit #{@hash}\nDate: #{formatted_date}\n\n\t#{@message}"
+ end
+end
+
+class Branch
+ attr_accessor :name, :commits, :staged, :object_state
+
+ def initialize(name, object_store)
+ @name = name
+ @object_store = object_store
+ @commits = []
+ @staged = []
+ @object_state = []
+ end
+
+ def create(branch_name)
+ if get_branch_index(branch_name).nil?
+ new_branch = Marshal.load(Marshal.dump(self))
+ new_branch.name = branch_name
+ @object_store.branches.push(new_branch)
+ Transaction.new("Created branch #{branch_name}.", true, new_branch)
+ else
+ Transaction.new("Branch #{branch_name} already exists.", false)
+ end
+ end
+
+ def checkout(branch_name)
+ branch_index = get_branch_index branch_name
+ if branch_index.nil?
+ Transaction.new("Branch #{branch_name} does not exist.", false)
+ else
+ @object_store.work_branch = @object_store.branches[branch_index]
+ Transaction.new("Switched to branch #{branch_name}.",
+ true,
+ @object_store.work_branch)
+ end
+ end
+
+ def remove(branch_name)
+ branch_index = get_branch_index branch_name
+ if branch_index.nil?
+ Transaction.new("Branch #{branch_name} does not exist.", false)
+ elsif @object_store.work_branch.name == branch_name
+ Transaction.new('Cannot remove current branch.', false)
+ else
+ @object_store.branches.delete_at(branch_index)
+ Transaction.new("Removed branch #{branch_name}.", true)
+ end
+ end
+
+ def list
+ pp @object_store.work_branch
+ branch_names = @object_store.branches.map(&:name).sort
+ branch_names.map! do |branch_name|
+ if branch_name == @object_store.work_branch.name
+ "* #{branch_name}"
+ else
+ " #{branch_name}"
+ end
+ end
+ Transaction.new(branch_names.join('\n'), true)
+ end
+
+ def commit_staged
+ object_count = staged.length
+ staged.each do |object|
+ if object.to_delete?
+ @object_state.delete(object)
+ else
+ @object_state.push(object)
+ end
+ end
+ staged.clear
+ object_count
+ end
+
+ def revert_to_commit(commit_hash)
+ commit_index = @commits.index { |commit| commit.hash == commit_hash }
+ return false if commit_index.nil?
+ @commits.drop(commit_index + 1).each do |commit|
+ _apply_reverse_commit commit
+ end
+ @commits = @commits.take(commit_index + 1)
+ true
+ end
+
+ def _apply_reverse_commit(commit)
+ commit.objects.each do |object|
+ if object.to_delete?
+ @object_state.push(object)
+ else
+ @object_state.delete(object)
+ end
+ end
+ end
+
+ def staged_object_index(name)
+ @staged.index { |item| item.name == name }
+ end
+
+ def object_state_index(name)
+ @object_state.index { |item| item.name == name }
+ end
+
+ def get_branch_index(branch_name)
+ @object_store.branches.index { |branch| branch.name == branch_name }
+ end
+end
+
+class ObjectStore
+ attr_accessor :branches, :work_branch
+ def initialize
+ @work_branch = Branch.new('master', self)
+ @branches = [@work_branch]
+ end
+
+ def self.init(&block)
+ object_store = ObjectStore.new
+ object_store.instance_eval &block if block_given?
+ object_store
+ end
+
+ def branch
+ @work_branch
+ end
+
+ def add(name, object)
+ object_index = @work_branch.staged_object_index name
+ @work_branch.staged.delete_at object_index unless object_index.nil?
+ blob_object = BlobObject.new(name, object)
+ @work_branch.staged.push(blob_object)
+ Transaction.new("Added #{name} to stage.", true, object)
+ end
+
+ def remove(name)
+ object_index = @work_branch.object_state_index name
+ if object_index.nil?
+ Transaction.new("Object #{name} is not committed.", false)
+ else
+ marked_object = @work_branch.object_state[object_index]
+ marked_object.to_delete = true
+ @work_branch.staged.push(marked_object)
+ Transaction.new("Added #{name} for removal.", true, marked_object.object)
+ end
+ end
+
+ def commit(message)
+ if @work_branch.staged.empty?
+ message = 'Nothing to commit, working directory clean.'
+ return Transaction.new(message, false)
+ end
+ stage_clone = @work_branch.staged.clone
+ length = @work_branch.commit_staged
+ commit = CommitObject.new(message, stage_clone)
+ @work_branch.commits.push(commit)
+ Transaction.new("#{message}\n\t#{length} objects changed", true, commit)
+ end
+
+ def checkout(commit_hash)
+ checkout_result = @work_branch.revert_to_commit commit_hash
+ if checkout_result
+ commit = @work_branch.commits.last
+ Transaction.new("HEAD is now at #{commit.hash}.", true, commit)
+ else
+ Transaction.new("Commit #{commit_hash} does not exist.", false)
+ end
+ end
+
+ def log
+ pp @work_branch
+ if @work_branch.commits.empty?
+ message = "Branch #{@work_branch.name} does not have any commits yet."
+ Transaction.new(message, false)
+ else
+ Transaction.new(@work_branch.commits.reverse.join("\n"), true)
+ end
+ end
+
+ def get(object_name)
+ commit_object = @work_branch.object_state.find do |item|
+ item.name == object_name
+ end
+ if commit_object.nil?
+ Transaction.new("Object #{object_name} is not committed.", false)
+ else
+ message = "Found object #{object_name}."
+ Transaction.new(message, true, commit_object.object)
+ end
+ end
+
+ def head
+ if @work_branch.commits.empty?
+ message = "Branch #{@work_branch.name} does not have any commits yet."
+ Transaction.new(message, false)
+ else
+ message = "#{@work_branch.commits.last.message}"
+ Transaction.new(message, true, @work_branch.commits.last)
+ end
+ end
+end

Никола обнови решението на 22.11.2015 03:00 (преди над 8 години)

require 'digest'
require 'pp'
class Transaction
attr_reader :message, :result
def initialize(message, status, result = nil)
@message = message
@status = status
@result = result
end
def success?
@status == true
end
def error?
@status == false
end
end
class BlobObject
attr_reader :name, :object
attr_accessor :to_delete
def initialize(name, object, to_delete = false)
@name = name
@object = object
@to_delete = to_delete
end
def to_delete?
@to_delete
end
def ==(other)
@name == other.name && @object == other.object
end
end
class CommitObject
attr_reader :message, :date, :hash, :objects
def initialize(message, objects)
@message = message
@date = Time.now
@hash = Digest::SHA1.hexdigest("#{@date}#{@message}")
@objects = objects
end
def ==(other)
@hash == other.hash
end
def to_s
formatted_date = @date.strftime('%a %b %-d %H:%M %Y %z')
"Commit #{@hash}\nDate: #{formatted_date}\n\n\t#{@message}"
end
end
class Branch
attr_accessor :name, :commits, :staged, :object_state
def initialize(name, object_store)
@name = name
@object_store = object_store
@commits = []
@staged = []
@object_state = []
end
def create(branch_name)
if get_branch_index(branch_name).nil?
new_branch = Marshal.load(Marshal.dump(self))
new_branch.name = branch_name
@object_store.branches.push(new_branch)
Transaction.new("Created branch #{branch_name}.", true, new_branch)
else
Transaction.new("Branch #{branch_name} already exists.", false)
end
end
def checkout(branch_name)
branch_index = get_branch_index branch_name
if branch_index.nil?
Transaction.new("Branch #{branch_name} does not exist.", false)
else
@object_store.work_branch = @object_store.branches[branch_index]
Transaction.new("Switched to branch #{branch_name}.",
true,
@object_store.work_branch)
end
end
def remove(branch_name)
branch_index = get_branch_index branch_name
if branch_index.nil?
Transaction.new("Branch #{branch_name} does not exist.", false)
elsif @object_store.work_branch.name == branch_name
Transaction.new('Cannot remove current branch.', false)
else
@object_store.branches.delete_at(branch_index)
Transaction.new("Removed branch #{branch_name}.", true)
end
end
def list
- pp @object_store.work_branch
branch_names = @object_store.branches.map(&:name).sort
branch_names.map! do |branch_name|
if branch_name == @object_store.work_branch.name
"* #{branch_name}"
else
" #{branch_name}"
end
end
Transaction.new(branch_names.join('\n'), true)
end
def commit_staged
object_count = staged.length
staged.each do |object|
if object.to_delete?
@object_state.delete(object)
else
@object_state.push(object)
end
end
staged.clear
object_count
end
def revert_to_commit(commit_hash)
commit_index = @commits.index { |commit| commit.hash == commit_hash }
return false if commit_index.nil?
@commits.drop(commit_index + 1).each do |commit|
_apply_reverse_commit commit
end
@commits = @commits.take(commit_index + 1)
true
end
def _apply_reverse_commit(commit)
commit.objects.each do |object|
if object.to_delete?
@object_state.push(object)
else
@object_state.delete(object)
end
end
end
def staged_object_index(name)
@staged.index { |item| item.name == name }
end
def object_state_index(name)
@object_state.index { |item| item.name == name }
end
def get_branch_index(branch_name)
@object_store.branches.index { |branch| branch.name == branch_name }
end
end
class ObjectStore
attr_accessor :branches, :work_branch
def initialize
@work_branch = Branch.new('master', self)
@branches = [@work_branch]
end
def self.init(&block)
object_store = ObjectStore.new
object_store.instance_eval &block if block_given?
object_store
end
def branch
@work_branch
end
def add(name, object)
object_index = @work_branch.staged_object_index name
@work_branch.staged.delete_at object_index unless object_index.nil?
blob_object = BlobObject.new(name, object)
@work_branch.staged.push(blob_object)
Transaction.new("Added #{name} to stage.", true, object)
end
def remove(name)
object_index = @work_branch.object_state_index name
if object_index.nil?
Transaction.new("Object #{name} is not committed.", false)
else
marked_object = @work_branch.object_state[object_index]
marked_object.to_delete = true
@work_branch.staged.push(marked_object)
Transaction.new("Added #{name} for removal.", true, marked_object.object)
end
end
def commit(message)
if @work_branch.staged.empty?
message = 'Nothing to commit, working directory clean.'
return Transaction.new(message, false)
end
stage_clone = @work_branch.staged.clone
length = @work_branch.commit_staged
commit = CommitObject.new(message, stage_clone)
@work_branch.commits.push(commit)
Transaction.new("#{message}\n\t#{length} objects changed", true, commit)
end
def checkout(commit_hash)
checkout_result = @work_branch.revert_to_commit commit_hash
if checkout_result
commit = @work_branch.commits.last
Transaction.new("HEAD is now at #{commit.hash}.", true, commit)
else
Transaction.new("Commit #{commit_hash} does not exist.", false)
end
end
def log
- pp @work_branch
if @work_branch.commits.empty?
message = "Branch #{@work_branch.name} does not have any commits yet."
Transaction.new(message, false)
else
- Transaction.new(@work_branch.commits.reverse.join("\n"), true)
+ Transaction.new(@work_branch.commits.reverse.join("\n\n"), true)
end
end
def get(object_name)
commit_object = @work_branch.object_state.find do |item|
item.name == object_name
end
if commit_object.nil?
Transaction.new("Object #{object_name} is not committed.", false)
else
message = "Found object #{object_name}."
Transaction.new(message, true, commit_object.object)
end
end
def head
if @work_branch.commits.empty?
message = "Branch #{@work_branch.name} does not have any commits yet."
Transaction.new(message, false)
else
message = "#{@work_branch.commits.last.message}"
Transaction.new(message, true, @work_branch.commits.last)
end
end
-end
+end