Решение на Пета задача от Методи Димитров

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

Към профила на Методи Димитров

Резултати

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

Код

class Operation
attr_reader :name, :message, :success, :error, :result, :proc
def initialize(message, success: true, error: false, result: nil)
@message, @success, @error, @result = message, success, error, result
end
def success?()
@success
end
def error?()
@error
end
end
class ObjectsHash
attr_accessor :objects
def initialize()
@objects = {}
end
def add(name, value)
@objects[name] = value
end
def remove(name)
@objects.delete name
end
end
class Commit
attr_accessor :date, :message, :hash, :objects
def initialize(message, date, hash, objects)
@date, @message, @hash, @objects = date, message, hash, objects
end
end
class ObjectStore
require 'digest/sha1'
attr_reader :branch
def initialize()
@branch = Branch.new
@working_directory = []
instance_eval &Proc.new
end
def self.init(&block)
ObjectStore.new &block
end
def add(name, object)
message = "Added #{name} to stage."
@working_directory << Proc.new { add(name, object) }
Operation.new(message, result: object)
end
def commit(message)
if @working_directory.empty?
commit_message = "Nothing to commit, working directory clean."
else
commit_message = "#{message}\n\t#{@working_directory.count}" +
" objects changed"
end
@branch.push(message, @working_directory)
@working_directory = []
Operation.new(commit_message)
end
def remove(name)
if branch.objects.objects.include? name
message = "Added #{name} for removal."
else
message = "Object #{name} is not committed."
end
@working_directory << Proc.new { remove(name) }
Operation.new(message)
end
def checkout(commit_hash)
@branch.update(commit_hash)
end
#def log()
# if @branch.commit_history.empty?
# message = "Branch #{@branch.current_branch} " +
# "does not have any commits yet."
# else
# message = @branch
# .commit_history
# .reverse
# .map do |message, time, working_directory|
# "Commit " +
# Digest::SHA1.hexdigest("#{time.ctime}#{message}").to_s +
# "\nDate: #{time.ctime}\n\n\t#{message}\n\n"
# end.reduce(:+)
# end
# Operation.new(message)
#end
def head()
latest_commit = @branch.commit_history.last
time = latest_commit[1].ctime
message = latest_commit[0]
hash = Digest::SHA1.hexdigest("#{time}#{message}").to_s
Commit.new(message, time, hash, @branch.objects)
end
def get(name)
end
end
class Branch
require 'date'
attr_reader :current_branch, :commit_history
def initialize()
@commit_history = []
@current_branch = "master"
@branch = {@current_branch => ObjectsHash.new}
end
def objects()
@branch[@current_branch]
end
def create(branch_name)
@branch[branch_name] = @branch[@current_branch].dup
end
def checkout(branch_name)
@current_branch = branch_name
end
def remove(branch_name)
@branch.delete branch_name
end
def list()
message = @branch.keys.sort.map do |branch|
" " +
((branch == @current_branch) ? "*" : " ") +
branch.to_s +
"\n"
end.reduce(:+)
Operation.new(message)
end
def push(message, working_directory)
time = Time.now
@commit_history << [message, time, working_directory]
working_directory.each do |operation|
@branch[@current_branch].instance_eval &operation
end
end
def update(commit_name)
@branch[@current_branch] = ObjectsHash.new
commit_index = @commit_history
.index { |commit, _, _| commit == commit_name }
@commit_history = @commit_history[0..commit_index]
@commit_history.each do |_, _, operation|
operation.each do |change|
@branch[@current_branch].instance_eval &change
end
end
end
end

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

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) ObjectStore can add objects
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:22: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 commit objects
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:27: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 cannot commit without changed objects
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:34: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 can remove objects
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:39: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 commit changes which include only removed objects
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:47: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 cannot remove objects that are not committed
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:57: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 head
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:64: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 cannot show head for empty repository
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:73: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)>'

  9) ObjectStore can show log of changes for a single commit
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:78: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)>'

  10) ObjectStore can show log of changes for a single commit
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:87: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)>'

  11) ObjectStore can show log of changes for multiple commits
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:96: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)>'

  12) ObjectStore shows the log for the current branch only
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:112: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)>'

  13) ObjectStore cannot show log for empty repository
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:132: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)>'

  14) ObjectStore can list branches
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:137: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)>'

  15) ObjectStore can create branches
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:144: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)>'

  16) ObjectStore cannot create branch if already exists
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:149: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)>'

  17) ObjectStore can switch branches
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:154: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)>'

  18) ObjectStore cannot switch to nonexisting branch
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:167: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)>'

  19) ObjectStore can remove branch
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:172: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)>'

  20) ObjectStore cannot remove current branch
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:178: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)>'

  21) ObjectStore cannot remove nonexisting branch
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:183: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)>'

  22) ObjectStore can be initialized with block
     Failure/Error: actual.success? == true &&
     NoMethodError:
       undefined method `success?' for #<Commit:0x007fbd9b7d2840>
     # /tmp/d20160111-5693-5wmr1k/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:195: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)>'

  23) ObjectStore can return objects
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:199: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)>'

  24) ObjectStore cannot return not committed objects
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:206: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)>'

  25) ObjectStore cannot return objects when no commits
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:213: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)>'

  26) ObjectStore can checkout commits
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:218: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)>'

  27) ObjectStore cannot checkout commits with nonexisting hashes
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:228: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)>'

  28) ObjectStore cannot checkout commits in empty repository
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:235: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)>'

  29) ObjectStore can show the objects in a repo after overwriting an object
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:240: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)>'

  30) ObjectStore can show the objects of a repo after removing an object
     Failure/Error: repo = ObjectStore.init
     ArgumentError:
       tried to create Proc object without a block
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:48:in `initialize'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `new'
     # /tmp/d20160111-5693-5wmr1k/solution.rb:52:in `init'
     # /tmp/d20160111-5693-5wmr1k/spec.rb:254: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.0124 seconds
30 examples, 30 failures

Failed examples:

rspec /tmp/d20160111-5693-5wmr1k/spec.rb:21 # ObjectStore can add objects
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:26 # ObjectStore can commit objects
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:33 # ObjectStore cannot commit without changed objects
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:38 # ObjectStore can remove objects
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:46 # ObjectStore can commit changes which include only removed objects
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:56 # ObjectStore cannot remove objects that are not committed
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:63 # ObjectStore can show head
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:72 # ObjectStore cannot show head for empty repository
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:77 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:86 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:95 # ObjectStore can show log of changes for multiple commits
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:111 # ObjectStore shows the log for the current branch only
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:131 # ObjectStore cannot show log for empty repository
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:136 # ObjectStore can list branches
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:143 # ObjectStore can create branches
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:148 # ObjectStore cannot create branch if already exists
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:153 # ObjectStore can switch branches
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:166 # ObjectStore cannot switch to nonexisting branch
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:171 # ObjectStore can remove branch
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:177 # ObjectStore cannot remove current branch
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:182 # ObjectStore cannot remove nonexisting branch
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:187 # ObjectStore can be initialized with block
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:198 # ObjectStore can return objects
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:205 # ObjectStore cannot return not committed objects
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:212 # ObjectStore cannot return objects when no commits
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:217 # ObjectStore can checkout commits
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:227 # ObjectStore cannot checkout commits with nonexisting hashes
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:234 # ObjectStore cannot checkout commits in empty repository
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:239 # ObjectStore can show the objects in a repo after overwriting an object
rspec /tmp/d20160111-5693-5wmr1k/spec.rb:253 # ObjectStore can show the objects of a repo after removing an object

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

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

+class Operation
+ attr_reader :name, :message, :success, :error, :result, :proc
+
+ def initialize(message, success: true, error: false, result: nil)
+ @message, @success, @error, @result = message, success, error, result
+ end
+
+ def success?()
+ @success
+ end
+
+ def error?()
+ @error
+ end
+end
+
+class ObjectsHash
+ attr_accessor :objects
+
+ def initialize()
+ @objects = {}
+ end
+
+ def add(name, value)
+ @objects[name] = value
+ end
+
+ def remove(name)
+ @objects.delete name
+ end
+end
+
+class Commit
+ attr_accessor :date, :message, :hash, :objects
+ def initialize(message, date, hash, objects)
+ @date, @message, @hash, @objects = date, message, hash, objects
+ end
+end
+
+class ObjectStore
+ require 'digest/sha1'
+
+ attr_reader :branch
+
+ def initialize()
+ @branch = Branch.new
+ @working_directory = []
+ instance_eval &Proc.new
+ end
+
+ def self.init(&block)
+ ObjectStore.new &block
+ end
+
+ def add(name, object)
+ message = "Added #{name} to stage."
+ @working_directory << Proc.new { add(name, object) }
+ Operation.new(message, result: object)
+ end
+
+ def commit(message)
+ if @working_directory.empty?
+ commit_message = "Nothing to commit, working directory clean."
+ else
+ commit_message = "#{message}\n\t#{@working_directory.count}" +
+ " objects changed"
+ end
+ @branch.push(message, @working_directory)
+ @working_directory = []
+ Operation.new(commit_message)
+ end
+
+ def remove(name)
+ if branch.objects.objects.include? name
+ message = "Added #{name} for removal."
+ else
+ message = "Object #{name} is not committed."
+ end
+ @working_directory << Proc.new { remove(name) }
+ Operation.new(message)
+
+ end
+
+ def checkout(commit_hash)
+ @branch.update(commit_hash)
+ end
+
+ #def log()
+ # if @branch.commit_history.empty?
+ # message = "Branch #{@branch.current_branch} " +
+ # "does not have any commits yet."
+ # else
+ # message = @branch
+ # .commit_history
+ # .reverse
+ # .map do |message, time, working_directory|
+ # "Commit " +
+ # Digest::SHA1.hexdigest("#{time.ctime}#{message}").to_s +
+ # "\nDate: #{time.ctime}\n\n\t#{message}\n\n"
+ # end.reduce(:+)
+ # end
+ # Operation.new(message)
+ #end
+
+ def head()
+ latest_commit = @branch.commit_history.last
+ time = latest_commit[1].ctime
+ message = latest_commit[0]
+ hash = Digest::SHA1.hexdigest("#{time}#{message}").to_s
+ Commit.new(message, time, hash, @branch.objects)
+ end
+
+ def get(name)
+ end
+
+end
+
+class Branch
+ require 'date'
+
+ attr_reader :current_branch, :commit_history
+
+ def initialize()
+ @commit_history = []
+ @current_branch = "master"
+ @branch = {@current_branch => ObjectsHash.new}
+ end
+
+ def objects()
+ @branch[@current_branch]
+ end
+
+ def create(branch_name)
+ @branch[branch_name] = @branch[@current_branch].dup
+ end
+
+ def checkout(branch_name)
+ @current_branch = branch_name
+ end
+
+ def remove(branch_name)
+ @branch.delete branch_name
+ end
+
+ def list()
+ message = @branch.keys.sort.map do |branch|
+ " " +
+ ((branch == @current_branch) ? "*" : " ") +
+ branch.to_s +
+ "\n"
+ end.reduce(:+)
+ Operation.new(message)
+ end
+
+ def push(message, working_directory)
+ time = Time.now
+ @commit_history << [message, time, working_directory]
+ working_directory.each do |operation|
+ @branch[@current_branch].instance_eval &operation
+ end
+ end
+
+ def update(commit_name)
+ @branch[@current_branch] = ObjectsHash.new
+ commit_index = @commit_history
+ .index { |commit, _, _| commit == commit_name }
+ @commit_history = @commit_history[0..commit_index]
+ @commit_history.each do |_, _, operation|
+ operation.each do |change|
+ @branch[@current_branch].instance_eval &change
+ end
+ end
+ end
+end