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

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

Към профила на Мартина Тонковска

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 23 успешни тест(а)
  • 7 неуспешни тест(а)

Код

require 'digest'
require 'pp'
class Transaction
attr_reader :message, :result
def initialize(message, return_code, result = nil)
@message = message
@status = return_code
@result = result
end
def success?
@status == true
end
def error?
@status == false
end
end
class CommitObject
attr_reader :name, :object
attr_accessor :for_deletion
def initialize(name, object = nil)
@name = name
@object = object
@for_deletion = false
end
def for_deletion?
@for_deletion
end
def ==(other)
@name == other.name
end
end
class CommitEntry
attr_reader :hash, :date, :message, :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
"Commit #{@hash}\nDate: #{@date.strftime('%a %b %-d %H:%M %Y %z')}\n\n\t" \
"#{@message}"
end
end
class Branch
attr_reader :name
attr_accessor :staged, :commits, :items
def initialize(branch_name, object_store, branch_items = [], commits = [])
@name = branch_name
@object_store = object_store
@items = branch_items
@staged = []
@commits = commits
end
def ==(other)
@name == other.name
end
def create(branch_name)
if @object_store.repository.any? { |branch| branch.name == branch_name }
Transaction.new("Branch #{branch_name} already exists.", false)
else
@object_store.repository.push(Branch.new(branch_name, @object_store,
items.clone, commits.clone))
Transaction.new("Created branch #{branch_name}.", true,
@object_store.repository.last)
end
end
def get_branch_index(branch_name)
@object_store.repository.index { |branch| branch.name == branch_name }
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.current_branch = branch_name
Transaction.new("Switched to branch #{branch_name}.", true,
@object_store.repository[branch_index])
end
end
def remove(branch_name)
if @object_store.current_branch == branch_name
return Transaction.new('Cannot remove current branch.', false)
end
branch_index = get_branch_index(branch_name)
if branch_index.nil?
Transaction.new("Branch #{branch_name} does not exist.", false)
else
@object_store.repository.delete_at(branch_index)
Transaction.new("Removed branch #{branch_name}.", true)
end
end
def list
branch_names = @object_store.repository.collect(&:name)
branch_names.sort!.map! do |name|
name == @object_store.current_branch ? "* #{name}" : " #{name}"
end
Transaction.new(branch_names.join('\n'), true)
end
end
class ObjectStore
attr_reader :repository
attr_accessor :current_branch
def initialize
@repository = [Branch.new('master', self)]
@current_branch = 'master'
end
def self.init(&block)
object_store = ObjectStore.new
object_store.instance_eval &block if block_given?
object_store
end
def head
if branch.commits.empty?
Transaction.new("Branch #{branch.name} does not have any commits yet.",
false)
else
Transaction.new("#{branch.commits.first.message}", true,
branch.commits.first)
end
end
def add(name, object)
branch.remove(name) if branch.staged.include?(CommitObject.new(name))
branch.staged.push(CommitObject.new(name, object))
Transaction.new("Added #{name} to stage.", true, object)
end
def log
if branch.commits.empty?
message = "Branch #{@current_branch} does not have any commits yet."
Transaction.new(message, false)
else
Transaction.new(branch.commits.join("\n\n"), true, branch.commits)
end
end
# case 1: cleans up staged array on commit by either
# removing or adding items to the repository
# case 2: rolls back to a previous commit,
# reversing each commit command along the way
def sweep(object, command = :commit)
if (object.for_deletion? and command != :rollback) ||
(!object.for_deletion? and command == :rollback)
branch.items.delete(object)
else
branch.items.push(object)
end
end
def commit(message)
if branch.staged.empty?
return Transaction.new('Nothing to commit, working directory clean.',
false)
end
# add the staged items to the repository
branch.staged.each do |object|
sweep(object)
end
branch.commits.insert(0, CommitEntry.new(message, branch.staged.clone))
length = branch.staged.length
branch.staged.clear
Transaction.new("#{message}\n\t#{length} objects changed", true,
branch.commits.first)
end
def remove(name)
for_deletion = object_exists?(name)
if for_deletion.equal? nil
Transaction.new("Object #{name} is not committed.", false)
else
for_deletion.for_deletion = true
branch.staged.push(for_deletion)
Transaction.new("Added #{name} for removal.", true, for_deletion.object)
end
end
def checkout(commit_hash)
commit_index = branch.commits.index { |entry| entry.hash == commit_hash }
if commit_index.nil?
return Transaction.new("Commit #{commit_hash} does not exist.", false)
end
0.upto(commit_index - 1) do |index|
branch.commits[index].objects.each do |commit_object|
sweep(commit_object, :rollback)
end
end
branch.commits = branch.commits.take(commit_index + 1).reverse
Transaction.new("HEAD is now at #{branch.commits.first.hash}.", true,
branch.commits.first)
end
def object_exists?(name)
branch.items.find { |commit_object| commit_object.name == name }
end
def branch
@repository.find { |branch| branch.name == @current_branch }
end
def get(object_name)
commit_object = branch.items.find { |item| item.name == object_name }
if commit_object.nil?
Transaction.new("Object #{object_name} is not committed.", false)
else
Transaction.new("Found object #{object_name}.", true,
commit_object.object)
end
end
end

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

........FFFF.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:0x007f1aa002d908 @message="Commit 65003733701977bd21736eedf7a1a27f072338dd\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!", @status=true, @result=[#<CommitEntry:0x007f1aa0044dd8 @message="So cool!", @date=2016-01-11 11:55:10 +0200, @hash="65003733701977bd21736eedf7a1a27f072338dd", @objects=[#<CommitObject:0x007f1aa0058180 @name="object1", @object="content1", @for_deletion=false>, #<CommitObject:0x007f1aa0047588 @name="object2", @object="content2", @for_deletion=false>]>]> to be success "Commit 020f94292ff683fc536310fe33c03adfbf64bbca\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-v0yn8l/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:0x007f1a9fff5e68 @message="Commit 65003733701977bd21736eedf7a1a27f072338dd\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!", @status=true, @result=[#<CommitEntry:0x007f1a9fff7d80 @message="So cool!", @date=2016-01-11 11:55:10 +0200, @hash="65003733701977bd21736eedf7a1a27f072338dd", @objects=[#<CommitObject:0x007f1a9fffc6c8 @name="object1", @object="content1", @for_deletion=false>, #<CommitObject:0x007f1a9fffc330 @name="object2", @object="content2", @for_deletion=false>]>]> to be success "Commit 020f94292ff683fc536310fe33c03adfbf64bbca\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-v0yn8l/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:0x007f1a9ffdef10 @message="Commit b9ebd78ab8bd210aed26da7d9576c77c30f89bb6\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSecond commit\n\nCommit 8640c7aedf0f0ac39cd0ad0553604259a63406cb\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit", @status=true, @result=[#<CommitEntry:0x007f1a9ffe03d8 @message="Second commit", @date=2016-01-11 11:55:10 +0200, @hash="b9ebd78ab8bd210aed26da7d9576c77c30f89bb6", @objects=[#<CommitObject:0x007f1a9ffe0900 @name="object2", @object="content2", @for_deletion=false>]>, #<CommitEntry:0x007f1a9ffe1238 @message="First commit", @date=2016-01-11 11:55:10 +0200, @hash="8640c7aedf0f0ac39cd0ad0553604259a63406cb", @objects=[#<CommitObject:0x007f1a9ffe1648 @name="object1", @object="content1", @for_deletion=false>]>]> 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-v0yn8l/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:0x007f1a9ff4e938 @message="Commit 8640c7aedf0f0ac39cd0ad0553604259a63406cb\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit", @status=true, @result=[#<CommitEntry:0x007f1a9ff52628 @message="First commit", @date=2016-01-11 11:55:10 +0200, @hash="8640c7aedf0f0ac39cd0ad0553604259a63406cb", @objects=[#<CommitObject:0x007f1a9ff52f10 @name="object1", @object="content1", @for_deletion=false>]>]> to be success "Commit 9a97b358afe869ba1e523d99dd2101dfc0570dc0\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-v0yn8l/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:0x007f1a9ff20f88 @message="  develop\\n  feature\\n* master", @status=true, @result=nil> to be success "  develop\n  feature\n* master"
     # /tmp/d20160111-5693-v0yn8l/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 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-v0yn8l/solution.rb:37:in `=='
     # /tmp/d20160111-5693-v0yn8l/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)>'

  7) 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-v0yn8l/solution.rb:37:in `=='
     # /tmp/d20160111-5693-v0yn8l/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.03407 seconds
30 examples, 7 failures

Failed examples:

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

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

Мартина обнови решението на 22.11.2015 03:02 (преди над 8 години)

+require 'digest'
+require 'pp'
+
+class Transaction
+ attr_reader :message, :result
+
+ def initialize(message, return_code, result = nil)
+ @message = message
+ @status = return_code
+ @result = result
+ end
+
+ def success?
+ @status == true
+ end
+
+ def error?
+ @status == false
+ end
+end
+
+class CommitObject
+ attr_reader :name, :object
+ attr_accessor :for_deletion
+
+ def initialize(name, object = nil)
+ @name = name
+ @object = object
+ @for_deletion = false
+ end
+
+ def for_deletion?
+ @for_deletion
+ end
+
+ def ==(other)
+ @name == other.name
+ end
+end
+
+class CommitEntry
+ attr_reader :hash, :date, :message, :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
+ "Commit #{@hash}\nDate: #{@date.strftime('%a %b %-d %H:%M %Y %z')}\n\n\t" \
+ "#{@message}"
+ end
+end
+
+class Branch
+ attr_reader :name
+ attr_accessor :staged, :commits, :items
+
+ def initialize(branch_name, object_store, branch_items = [], commits = [])
+ @name = branch_name
+ @object_store = object_store
+ @items = branch_items
+ @staged = []
+ @commits = commits
+ end
+
+ def ==(other)
+ @name == other.name
+ end
+
+ def create(branch_name)
+ if @object_store.repository.any? { |branch| branch.name == branch_name }
+ Transaction.new("Branch #{branch_name} already exists.", false)
+ else
+ @object_store.repository.push(Branch.new(branch_name, @object_store,
+ items.clone, commits.clone))
+ Transaction.new("Created branch #{branch_name}.", true,
+ @object_store.repository.last)
+ end
+ end
+
+ def get_branch_index(branch_name)
+ @object_store.repository.index { |branch| branch.name == branch_name }
+ 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.current_branch = branch_name
+ Transaction.new("Switched to branch #{branch_name}.", true,
+ @object_store.repository[branch_index])
+ end
+ end
+
+ def remove(branch_name)
+ if @object_store.current_branch == branch_name
+ return Transaction.new('Cannot remove current branch.', false)
+ end
+ branch_index = get_branch_index(branch_name)
+ if branch_index.nil?
+ Transaction.new("Branch #{branch_name} does not exist.", false)
+ else
+ @object_store.repository.delete_at(branch_index)
+ Transaction.new("Removed branch #{branch_name}.", true)
+ end
+ end
+
+ def list
+ branch_names = @object_store.repository.collect(&:name)
+ branch_names.sort!.map! do |name|
+ name == @object_store.current_branch ? "* #{name}" : " #{name}"
+ end
+ Transaction.new(branch_names.join('\n'), true)
+ end
+end
+
+class ObjectStore
+ attr_reader :repository
+ attr_accessor :current_branch
+
+ def initialize
+ @repository = [Branch.new('master', self)]
+ @current_branch = 'master'
+ end
+
+ def self.init(&block)
+ object_store = ObjectStore.new
+ object_store.instance_eval &block if block_given?
+ object_store
+ end
+
+ def head
+ if branch.commits.empty?
+ Transaction.new("Branch #{branch.name} does not have any commits yet.",
+ false)
+ else
+ Transaction.new("#{branch.commits.first.message}", true,
+ branch.commits.first)
+ end
+ end
+
+ def add(name, object)
+ branch.remove(name) if branch.staged.include?(CommitObject.new(name))
+ branch.staged.push(CommitObject.new(name, object))
+ Transaction.new("Added #{name} to stage.", true, object)
+ end
+
+ def log
+ if branch.commits.empty?
+ message = "Branch #{@current_branch} does not have any commits yet."
+ Transaction.new(message, false)
+ else
+ Transaction.new(branch.commits.join("\n\n"), true, branch.commits)
+ end
+ end
+
+ # cleans up staged array by either removing or adding items to the repository
+ def sweep(object)
+ if object.for_deletion?
+ branch.items.delete(object)
+ else
+ branch.items.push(object)
+ end
+ end
+
+ def commit(message)
+ if branch.staged.empty?
+ return Transaction.new('Nothing to commit, working directory clean.',
+ false)
+ end
+ # add the staged items to the repository
+ branch.staged.each do |object|
+ sweep(object)
+ end
+ branch.commits.insert(0, CommitEntry.new(message, branch.staged.clone))
+ length = branch.staged.length
+ branch.staged.clear
+ Transaction.new("#{message}\n\t#{length} objects changed", true,
+ branch.commits.first)
+ end
+
+ def remove(name)
+ for_deletion = object_exists?(name)
+ if for_deletion.equal? nil
+ Transaction.new("Object #{name} is not committed.", false)
+ else
+ for_deletion.for_deletion = true
+ branch.staged.push(for_deletion)
+ Transaction.new("Added #{name} for removal.", true, for_deletion.object)
+ end
+ end
+
+ def checkout(commit_hash)
+ commit_index = branch.commits.index { |entry| entry.hash == commit_hash }
+ if commit_index.nil?
+ return Transaction.new("Commit #{commit_hash} does not exist.", false)
+ end
+ 0.upto(commit_index - 1) do |index|
+ branch.commits[index].objects.each do |commit_object|
+ sweep(commit_object)
+ end
+ end
+ branch.commits = branch.commits.take(commit_index + 1).reverse
+ Transaction.new("HEAD is now at #{branch.commits.first.hash}.", true,
+ branch.commits.first)
+ end
+
+ def object_exists?(name)
+ branch.items.find { |commit_object| commit_object.name == name }
+ end
+
+ def branch
+ @repository.find { |branch| branch.name == @current_branch }
+ end
+
+ def get(object_name)
+ commit_object = branch.items.find { |item| item.name == object_name }
+ if commit_object.nil?
+ Transaction.new("Object #{object_name} is not committed.", false)
+ else
+ Transaction.new("Found object #{object_name}.", true,
+ commit_object.object)
+ end
+ end
+end

Мартина обнови решението на 23.11.2015 16:26 (преди над 8 години)

require 'digest'
require 'pp'
class Transaction
attr_reader :message, :result
def initialize(message, return_code, result = nil)
@message = message
@status = return_code
@result = result
end
def success?
@status == true
end
def error?
@status == false
end
end
class CommitObject
attr_reader :name, :object
attr_accessor :for_deletion
def initialize(name, object = nil)
@name = name
@object = object
@for_deletion = false
end
def for_deletion?
@for_deletion
end
def ==(other)
@name == other.name
end
end
class CommitEntry
attr_reader :hash, :date, :message, :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
"Commit #{@hash}\nDate: #{@date.strftime('%a %b %-d %H:%M %Y %z')}\n\n\t" \
"#{@message}"
end
end
class Branch
attr_reader :name
attr_accessor :staged, :commits, :items
def initialize(branch_name, object_store, branch_items = [], commits = [])
@name = branch_name
@object_store = object_store
@items = branch_items
@staged = []
@commits = commits
end
def ==(other)
@name == other.name
end
def create(branch_name)
if @object_store.repository.any? { |branch| branch.name == branch_name }
Transaction.new("Branch #{branch_name} already exists.", false)
else
@object_store.repository.push(Branch.new(branch_name, @object_store,
items.clone, commits.clone))
Transaction.new("Created branch #{branch_name}.", true,
@object_store.repository.last)
end
end
def get_branch_index(branch_name)
@object_store.repository.index { |branch| branch.name == branch_name }
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.current_branch = branch_name
Transaction.new("Switched to branch #{branch_name}.", true,
@object_store.repository[branch_index])
end
end
def remove(branch_name)
if @object_store.current_branch == branch_name
return Transaction.new('Cannot remove current branch.', false)
end
branch_index = get_branch_index(branch_name)
if branch_index.nil?
Transaction.new("Branch #{branch_name} does not exist.", false)
else
@object_store.repository.delete_at(branch_index)
Transaction.new("Removed branch #{branch_name}.", true)
end
end
def list
branch_names = @object_store.repository.collect(&:name)
branch_names.sort!.map! do |name|
name == @object_store.current_branch ? "* #{name}" : " #{name}"
end
Transaction.new(branch_names.join('\n'), true)
end
end
class ObjectStore
attr_reader :repository
attr_accessor :current_branch
def initialize
@repository = [Branch.new('master', self)]
@current_branch = 'master'
end
def self.init(&block)
object_store = ObjectStore.new
object_store.instance_eval &block if block_given?
object_store
end
def head
if branch.commits.empty?
Transaction.new("Branch #{branch.name} does not have any commits yet.",
false)
else
Transaction.new("#{branch.commits.first.message}", true,
branch.commits.first)
end
end
def add(name, object)
branch.remove(name) if branch.staged.include?(CommitObject.new(name))
branch.staged.push(CommitObject.new(name, object))
Transaction.new("Added #{name} to stage.", true, object)
end
def log
if branch.commits.empty?
message = "Branch #{@current_branch} does not have any commits yet."
Transaction.new(message, false)
else
Transaction.new(branch.commits.join("\n\n"), true, branch.commits)
end
end
- # cleans up staged array by either removing or adding items to the repository
- def sweep(object)
- if object.for_deletion?
+ # case 1: cleans up staged array on commit by either
+ # removing or adding items to the repository
+ # case 2: rolls back to a previous commit,
+ # reversing each commit command along the way
+ def sweep(object, command = :commit)
+ if (object.for_deletion? and command != :rollback) ||
+ (!object.for_deletion? and command == :rollback)
branch.items.delete(object)
else
branch.items.push(object)
end
end
def commit(message)
if branch.staged.empty?
return Transaction.new('Nothing to commit, working directory clean.',
false)
end
# add the staged items to the repository
branch.staged.each do |object|
sweep(object)
end
branch.commits.insert(0, CommitEntry.new(message, branch.staged.clone))
length = branch.staged.length
branch.staged.clear
Transaction.new("#{message}\n\t#{length} objects changed", true,
branch.commits.first)
end
def remove(name)
for_deletion = object_exists?(name)
if for_deletion.equal? nil
Transaction.new("Object #{name} is not committed.", false)
else
for_deletion.for_deletion = true
branch.staged.push(for_deletion)
Transaction.new("Added #{name} for removal.", true, for_deletion.object)
end
end
def checkout(commit_hash)
commit_index = branch.commits.index { |entry| entry.hash == commit_hash }
if commit_index.nil?
return Transaction.new("Commit #{commit_hash} does not exist.", false)
end
0.upto(commit_index - 1) do |index|
branch.commits[index].objects.each do |commit_object|
- sweep(commit_object)
+ sweep(commit_object, :rollback)
end
end
branch.commits = branch.commits.take(commit_index + 1).reverse
Transaction.new("HEAD is now at #{branch.commits.first.hash}.", true,
branch.commits.first)
end
def object_exists?(name)
branch.items.find { |commit_object| commit_object.name == name }
end
def branch
@repository.find { |branch| branch.name == @current_branch }
end
def get(object_name)
commit_object = branch.items.find { |item| item.name == object_name }
if commit_object.nil?
Transaction.new("Object #{object_name} is not committed.", false)
else
Transaction.new("Found object #{object_name}.", true,
commit_object.object)
end
end
end