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

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

Към профила на Николай Станев

Резултати

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

Код

class Message
def initialize(message, success)
@message = message
@success = success
end
def message
@message
end
def success?
@success
end
def error?
! @success
end
end
class AddMessage < Message
def initialize(message, *result)
@message = message
@success = true
@result = result[0]
end
def message
"Added #{@message} to stage."
end
def result
return @result if @success
end
end
class CommitMessage < Message
def initialize(message, success, changed_objects)
@message = message
@success = success
@changed_objects = changed_objects
end
def message
if @success
"#{@message}\n#{@changed_objects} objects changed"
else
"Nothing to commit, working directory clean."
end
end
end
class RemoveMessage < Message
def message
if @success
"Added #{@message} for removal."
else
"Object #{@message} is not commited."
end
end
end
class CheckoutMessage < Message
def initialize(message, success, *result)
@message = message
@success = success
@result = result[0]
end
def message
if @success
"HEAD is now at #{@message}."
else
"Commit #{@message} does not exist."
end
end
def result
return @result if @success
end
end
class LogMessage < Message
def initialize(branch_name, success, commits)
@branch_name = branch_name
@success = success
@commits = commits
end
def message
if @success
string = ""
@commits.each{ |commit| string += information(commit) }
string
else
"Branch #{@branch_name} does not have any commits yet."
end
end
def information(commit)
log = "Commit #{commit.commit_hash} \n"\
"Date: #{commit.date}"\
"\n\t #{commit.message}\n"
end
end
class CreateMessage < Message
def message
if @success
"Created branch #{@message}."
else
"Branch #{@message} already exists."
end
end
end
class BranchCheckoutMessage < Message
def message
if @success
"Switched to #{@message}."
else
"Branch #{@message} does not exists."
end
end
end
class BranchRemoveMessage < Message
def initialize(branch_name, success, *current)
@branch_name = branch_name
@success = success
@current = current[0]
end
def message
if @success
"Removed branch #{@branch_name}."
else
if @current
"Cannot remove current branch."
else
"Branch #{@branch_name} does not exist."
end
end
end
end
class BranchListMessage < Message
end
class ObjectElement
attr_accessor :name
attr_accessor :info
def initialize(name, info)
@name = name
@info = info
end
def == (other)
if @name == other
true
else
false
end
end
end
class Commit
require 'digest/sha1'
attr_accessor :commit_hash
attr_accessor :message
attr_accessor :date
attr_accessor :repo_state
def initialize(message, repo_state)
@message = message
@repo_state = repo_state
@date = Time.now.asctime
@commit_hash = Digest::SHA1.hexdigest (@date + @message)
end
def include?(object_name)
repo_state.each { |object| return true if object == object_name }
false
end
end
class ObjectStore
attr_accessor :branch
attr_accessor :commit_store
attr_accessor :head
attr_accessor :working_commit
attr_accessor :changes
def initialize
@working_commit = []
@commit_store = []
@changes = 0
@branch = BranchController.new(@commit_store)
end
def self.init(&block)
repo = ObjectStore.new
if block_given?
repo.instance_eval(&block)
end
repo
end
def add(name, object)
@working_commit.push(ObjectElement.new(name , object))
@changes += 1
AddMessage.new(name, object)
end
def commit(message)
return CommitMessage.new(message, false, 0) if @changes == 0
@commit_store.push(Commit.new(message, @working_commit.dup))
@head = @commit_store.last
m = CommitMessage.new(message, true, @changes)
@changes = 0
m
end
def remove(name)
for_remove = @working_commit.select{ |x| x if x == name}
if for_remove[0] != nil
@working_commit.delete(for_remove[0])
@changes += 1
RemoveMessage.new(name, true)
else
RemoveMessage.new(name, false)
end
end
def checkout(commit_hash)
@commit_store.each do |commit|
message = CheckoutMessage.new(commit_hash, true, commit)
@working_commit = commit.repo_state
changes += 1
@head = commit
return message if commit.commit_hash == commit_hash
end
CheckoutMessage.new(commit_hash, false)
end
def log
unless @commit_store.empty?
LogMessage.new(branch.name, true, @commit_store)
else
LogMessage.new(branch.name, false, @commit_store)
end
end
end
class BranchController
attr_accessor :branches
attr_accessor :active_branch
attr_accessor :active_repo
def initialize(repo_state)
@branches = []
@branches.push(Branch.new("master", repo_state.dup))
@active_branch = @branches[0]
@active_repo = repo_state
end
def name
@active_branch.name
end
def create(branch_name)
message = CreateMessage.new(branch_name, false)
return message if @branches.any?{ |branch| branch.name == branch_name }
@branches.push(Branch.new(branch_name, active_repo.dup))
CreateMessage.new(branch_name, true)
end
def remove(branch_name)
if @active_branch.name == branch_name
BranchRemoveMessage.new(branch_name, false, true)
elsif @branches.any?{ |branch| branch.name == branch_name }
selected = @branches.select{ |branch| branch.name == branch_name }
@branches.delete(selected[0])
BranchRemoveMessage.new(branch_name, true)
else
BranchRemoveMessage.new(branch_name, false, false)
end
end
def list
names = []
@branches.each{ |branch| names.push(branch.name) }
names.each{ |name| name << "\n" }
names.sort!
names[names.index(active_branch.name)] = "* " + active_branch.name
BranchListMessage.new(names.join, true)
end
def checkout(branch_name)
message = BranchCheckoutMessage.new(branch_name, false)
selected = @branches.select{|branch| branch.name == branch_name}
if selected[0] == nil
BranchCheckoutMessage.new(branch_name, false)
else
@active_branch.repo_state = @active_repo.dup
@active_branch = selected[0]
@active_repo.replace(selected[0].repo_state)
BranchCheckoutMessage.new(branch_name, true)
end
end
end
class Branch
attr_accessor :name
attr_accessor :repo_state
def initialize(name, repo_state)
@name = name
@repo_state = repo_state
end
end

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

.F.FFFFFFFFF.F..FF...FFFFFF.FF

Failures:

  1) ObjectStore can commit objects
     Failure/Error: expect(repo.commit("So cool!")).to be_success("So cool!\n\t2 objects changed", repo.head.result)
     NoMethodError:
       undefined method `result' for #<Commit:0x007fbd913b7bc0>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:30: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 remove objects
     Failure/Error: (result == nil || actual.result == result) &&
     NoMethodError:
       undefined method `result' for #<RemoveMessage:0x007fbd91036e38>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:7:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-nwu3ym/spec.rb:43: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 commit changes which include only removed objects
     Failure/Error: expect(repo.commit("Removed object2")).to be_success("Removed object2\n\t1 objects changed", repo.head.result)
     NoMethodError:
       undefined method `result' for #<Commit:0x007fbd90e2c480>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:53: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 cannot remove objects that are not committed
     Failure/Error: expect(repo.remove("object2")).to be_error("Object object2 is not committed.")
       expected #<RemoveMessage:0x007fbd9100fe28 @message="object2", @success=false> to be error "Object object2 is not committed."
     # /tmp/d20160111-5693-nwu3ym/spec.rb:60: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 show head
     Failure/Error: last_commit = repo.commit("There we go").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd90ff2a30>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:68: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 show head for empty repository
     Failure/Error: result.message == message &&
     NoMethodError:
       undefined method `message' for nil:NilClass
     # /tmp/d20160111-5693-nwu3ym/spec.rb:15:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-nwu3ym/spec.rb:74: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 log of changes for a single commit
     Failure/Error: commit = repo.commit("So cool!").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd90fc6138>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:81: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 log of changes for a single commit
     Failure/Error: commit = repo.commit("So cool!").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd90fab608>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:90: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 multiple commits
     Failure/Error: commit1 = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd90fa87a0>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:98: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 shows the log for the current branch only
     Failure/Error: commit1 = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd90f9d080>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:114: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 list branches
     Failure/Error: expect(repo.branch.list).to be_success("  develop\n  feature\n* master")
       expected #<BranchListMessage:0x007fbd90f3abb0 @message="develop\nfeature\n* master\n", @success=true> to be success "  develop\n  feature\n* master"
     # /tmp/d20160111-5693-nwu3ym/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)>'

  12) ObjectStore can switch branches
     Failure/Error: first_commit = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd90e84608>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:156: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 switch to nonexisting branch
     Failure/Error: expect(repo.branch.checkout("develop")).to be_error("Branch develop does not exist.")
       expected #<BranchCheckoutMessage:0x007fbd90e78a38 @message="develop", @success=false> to be error "Branch develop does not exist."
     # /tmp/d20160111-5693-nwu3ym/spec.rb:168: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 be initialized with block
     Failure/Error: $second_commit = commit("Second commit").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd90c40978>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:193:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-nwu3ym/solution.rb:206:in `instance_eval'
     # /tmp/d20160111-5693-nwu3ym/solution.rb:206:in `init'
     # /tmp/d20160111-5693-nwu3ym/spec.rb:188: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 return objects
     Failure/Error: expect(repo.get("number")).to be_success("Found object number.", 21)
     NoMethodError:
       undefined method `get' for #<ObjectStore:0x007fbd90c38b38>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:202: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 return not committed objects
     Failure/Error: expect(repo.get("string")).to be_error("Object string is not committed.")
     NoMethodError:
       undefined method `get' for #<ObjectStore:0x007fbd90c330c0>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:209: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 cannot return objects when no commits
     Failure/Error: expect(repo.get("string")).to be_error("Object string is not committed.")
     NoMethodError:
       undefined method `get' for #<ObjectStore:0x007fbd90c2f420>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:214: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 can checkout commits
     Failure/Error: first_commit = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd90c23530>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:220: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 cannot checkout commits with nonexisting hashes
     Failure/Error: expect(repo.checkout("[not-present]")).to be_error("Commit [not-present] does not exist.")
     NoMethodError:
       undefined method `+' for nil:NilClass
     # /tmp/d20160111-5693-nwu3ym/solution.rb:241:in `block in checkout'
     # /tmp/d20160111-5693-nwu3ym/solution.rb:238:in `each'
     # /tmp/d20160111-5693-nwu3ym/solution.rb:238:in `checkout'
     # /tmp/d20160111-5693-nwu3ym/spec.rb:231: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 can show the objects in a repo after overwriting an object
     Failure/Error: first_commit = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd91ae6590>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:242: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 can show the objects of a repo after removing an object
     Failure/Error: first_commit = repo.commit("First commit").result
     NoMethodError:
       undefined method `result' for #<CommitMessage:0x007fbd91ae43f8>
     # /tmp/d20160111-5693-nwu3ym/spec.rb:258: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.02221 seconds
30 examples, 21 failures

Failed examples:

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

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

Николай обнови решението на 18.11.2015 10:20 (преди над 8 години)

+class Message
+ def initialize(message, success)
+ @message = message
+ @success = success
+ end
+
+ def message
+ @message
+ end
+
+ def success?
+ @success
+ end
+
+ def error?
+ ! @success
+ end
+end
+
+class AddMessage < Message
+ def initialize(message, *result)
+ @message = message
+ @success = true
+ @result = result[0]
+ end
+
+ def message
+ "Added #{@message} to stage."
+ end
+
+ def result
+ return @result if @success
+ end
+end
+
+class CommitMessage < Message
+ def initialize(message, success, changed_objects)
+ @message = message
+ @success = success
+ @changed_objects = changed_objects
+ end
+
+ def message
+ if @success
+ "#{@message}\n#{@changed_objects} objects changed"
+ else
+ "Nothing to commit, working directory clean."
+ end
+ end
+end
+
+class RemoveMessage < Message
+ def message
+ if @success
+ "Added #{@message} for removal."
+ else
+ "Object #{@message} is not commited."
+ end
+ end
+end
+
+class CheckoutMessage < Message
+ def initialize(message, success, *result)
+ @message = message
+ @success = success
+ @result = result[0]
+ end
+
+ def message
+ if @success
+ "HEAD is now at #{@message}."
+ else
+ "Commit #{@message} does not exist."
+ end
+ end
+
+ def result
+ return @result if @success
+ end
+end
+
+class LogMessage < Message
+ def initialize(branch_name, success, commits)
+ @branch_name = branch_name
+ @success = success
+ @commits = commits
+ end
+
+ def message
+ if @success
+ string = ""
+ @commits.each{ |commit| string += information(commit) }
+ string
+ else
+ "Branch #{@branch_name} does not have any commits yet."
+ end
+ end
+
+ def information(commit)
+ log = "Commit #{commit.commit_hash} \n"\
+ "Date: #{commit.date}"\
+ "\n\t #{commit.message}\n"
+ end
+end
+
+class CreateMessage < Message
+ def message
+ if @success
+ "Created branch #{@message}."
+ else
+ "Branch #{@message} already exists."
+ end
+ end
+end
+
+class BranchCheckoutMessage < Message
+ def message
+ if @success
+ "Switched to #{@message}."
+ else
+ "Branch #{@message} does not exists."
+ end
+ end
+end
+
+class BranchRemoveMessage < Message
+ def initialize(branch_name, success, *current)
+ @branch_name = branch_name
+ @success = success
+ @current = current[0]
+ end
+
+ def message
+ if @success
+ "Removed branch #{@branch_name}."
+ else
+ if @current
+ "Cannot remove current branch."
+ else
+ "Branch #{@branch_name} does not exist."
+ end
+ end
+ end
+end
+
+class BranchListMessage < Message
+
+end
+
+class ObjectElement
+ attr_accessor :name
+ attr_accessor :info
+
+ def initialize(name, info)
+ @name = name
+ @info = info
+ end
+
+ def == (other)
+ if @name == other
+ true
+ else
+ false
+ end
+ end
+end
+
+class Commit
+ require 'digest/sha1'
+
+ attr_accessor :commit_hash
+ attr_accessor :message
+ attr_accessor :date
+ attr_accessor :repo_state
+
+ def initialize(message, repo_state)
+ @message = message
+ @repo_state = repo_state
+ @date = Time.now.asctime
+ @commit_hash = Digest::SHA1.hexdigest (@date + @message)
+ end
+
+ def include?(object_name)
+ repo_state.each { |object| return true if object == object_name }
+ false
+ end
+end
+
+class ObjectStore
+ attr_accessor :branch
+ attr_accessor :commit_store
+ attr_accessor :head
+ attr_accessor :working_commit
+ attr_accessor :changes
+
+ def initialize
+ @working_commit = []
+ @commit_store = []
+ @changes = 0
+ @branch = BranchController.new(@commit_store)
+ end
+
+ def self.init
+ repo = ObjectStore.new
+ if block_given?
+ repo.instance_eval do
+ yield
+ end
+ end
+ repo
+ end
+
+ def add(name, object)
+ @working_commit.push(ObjectElement.new(name , object))
+ @changes += 1
+ AddMessage.new(name, object)
+ end
+
+ def commit(message)
+ return CommitMessage.new(message, false, 0) if @changes == 0
+ @commit_store.push(Commit.new(message, @working_commit.dup))
+ @head = @commit_store.last
+ m = CommitMessage.new(message, true, @changes)
+ @changes = 0
+ m
+ end
+
+ def remove(name)
+ for_remove = @working_commit.select{ |x| x if x == name}
+ if for_remove[0] != nil
+ @working_commit.delete(for_remove[0])
+ @changes += 1
+ RemoveMessage.new(name, true)
+ else
+ RemoveMessage.new(name, false)
+ end
+ end
+
+ def checkout(commit_hash)
+ @commit_store.each do |commit|
+ message = CheckoutMessage.new(commit_hash, true, commit)
+ @working_commit = commit.repo_state
+ changes += 1
+ @head = commit
+ return message if commit.commit_hash == commit_hash
+ end
+ CheckoutMessage.new(commit_hash, false)
+ end
+
+ def log
+ unless @commit_store.empty?
+ LogMessage.new(branch.name, true, @commit_store)
+ else
+ LogMessage.new(branch.name, false, @commit_store)
+ end
+ end
+
+end
+
+class BranchController
+ attr_accessor :branches
+ attr_accessor :active_branch
+ attr_accessor :active_repo
+ def initialize(repo_state)
+ @branches = []
+ @branches.push(Branch.new("master", repo_state.dup))
+ @active_branch = @branches[0]
+ @active_repo = repo_state
+ end
+
+ def name
+ @active_branch.name
+ end
+
+ def create(branch_name)
+ message = CreateMessage.new(branch_name, false)
+ return message if @branches.any?{ |branch| branch.name == branch_name }
+ @branches.push(Branch.new(branch_name, active_repo.dup))
+ CreateMessage.new(branch_name, true)
+ end
+
+ def remove(branch_name)
+ if @active_branch.name == branch_name
+ BranchRemoveMessage.new(branch_name, false, true)
+ elsif @branches.any?{ |branch| branch.name == branch_name }
+ selected = @branches.select{ |branch| branch.name == branch_name }
+ @branches.delete(selected[0])
+ BranchRemoveMessage.new(branch_name, true)
+ else
+ BranchRemoveMessage.new(branch_name, false, false)
+ end
+ end
+
+ def list
+ names = []
+ @branches.each{ |branch| names.push(branch.name) }
+ names.each{ |name| name << "\n" }
+ names.sort!
+ names[names.index(active_branch.name)] = "* " + active_branch.name
+ BranchListMessage.new(names.join, true)
+ end
+
+ def checkout(branch_name)
+ message = BranchCheckoutMessage.new(branch_name, false)
+ selected = @branches.select{|branch| branch.name == branch_name}
+ if selected[0] == nil
+ BranchCheckoutMessage.new(branch_name, false)
+ else
+ @active_branch.repo_state = @active_repo.dup
+ @active_branch = selected[0]
+ @active_repo.replace(selected[0].repo_state)
+ BranchCheckoutMessage.new(branch_name, true)
+ end
+ end
+end
+
+class Branch
+ attr_accessor :name
+ attr_accessor :repo_state
+ def initialize(name, repo_state)
+ @name = name
+ @repo_state = repo_state
+ end
+end

Николай обнови решението на 18.11.2015 12:10 (преди над 8 години)

class Message
def initialize(message, success)
@message = message
@success = success
end
def message
@message
end
def success?
@success
end
def error?
! @success
end
end
class AddMessage < Message
def initialize(message, *result)
@message = message
@success = true
@result = result[0]
end
def message
"Added #{@message} to stage."
end
def result
return @result if @success
end
end
class CommitMessage < Message
def initialize(message, success, changed_objects)
@message = message
@success = success
@changed_objects = changed_objects
end
def message
if @success
"#{@message}\n#{@changed_objects} objects changed"
else
"Nothing to commit, working directory clean."
end
end
end
class RemoveMessage < Message
def message
if @success
"Added #{@message} for removal."
else
"Object #{@message} is not commited."
end
end
end
class CheckoutMessage < Message
def initialize(message, success, *result)
@message = message
@success = success
@result = result[0]
end
def message
if @success
"HEAD is now at #{@message}."
else
"Commit #{@message} does not exist."
end
end
def result
return @result if @success
end
end
class LogMessage < Message
def initialize(branch_name, success, commits)
@branch_name = branch_name
@success = success
@commits = commits
end
def message
if @success
string = ""
@commits.each{ |commit| string += information(commit) }
string
else
"Branch #{@branch_name} does not have any commits yet."
end
end
def information(commit)
log = "Commit #{commit.commit_hash} \n"\
"Date: #{commit.date}"\
"\n\t #{commit.message}\n"
end
end
class CreateMessage < Message
def message
if @success
"Created branch #{@message}."
else
"Branch #{@message} already exists."
end
end
end
class BranchCheckoutMessage < Message
def message
if @success
"Switched to #{@message}."
else
"Branch #{@message} does not exists."
end
end
end
class BranchRemoveMessage < Message
def initialize(branch_name, success, *current)
@branch_name = branch_name
@success = success
@current = current[0]
end
def message
if @success
"Removed branch #{@branch_name}."
else
if @current
"Cannot remove current branch."
else
"Branch #{@branch_name} does not exist."
end
end
end
end
class BranchListMessage < Message
end
class ObjectElement
attr_accessor :name
attr_accessor :info
def initialize(name, info)
@name = name
@info = info
end
def == (other)
if @name == other
true
else
false
end
end
end
class Commit
require 'digest/sha1'
attr_accessor :commit_hash
attr_accessor :message
attr_accessor :date
attr_accessor :repo_state
def initialize(message, repo_state)
@message = message
@repo_state = repo_state
@date = Time.now.asctime
@commit_hash = Digest::SHA1.hexdigest (@date + @message)
end
def include?(object_name)
repo_state.each { |object| return true if object == object_name }
false
end
end
class ObjectStore
attr_accessor :branch
attr_accessor :commit_store
attr_accessor :head
attr_accessor :working_commit
attr_accessor :changes
def initialize
@working_commit = []
@commit_store = []
@changes = 0
@branch = BranchController.new(@commit_store)
end
- def self.init
+ def self.init(&block)
repo = ObjectStore.new
- if block_given?
- repo.instance_eval do
- yield
- end
+ if block_given?
+ repo.instance_eval(&block)
end
repo
end
def add(name, object)
@working_commit.push(ObjectElement.new(name , object))
@changes += 1
AddMessage.new(name, object)
end
def commit(message)
return CommitMessage.new(message, false, 0) if @changes == 0
@commit_store.push(Commit.new(message, @working_commit.dup))
@head = @commit_store.last
m = CommitMessage.new(message, true, @changes)
@changes = 0
m
end
def remove(name)
for_remove = @working_commit.select{ |x| x if x == name}
if for_remove[0] != nil
@working_commit.delete(for_remove[0])
@changes += 1
RemoveMessage.new(name, true)
else
RemoveMessage.new(name, false)
end
end
def checkout(commit_hash)
@commit_store.each do |commit|
message = CheckoutMessage.new(commit_hash, true, commit)
@working_commit = commit.repo_state
changes += 1
@head = commit
return message if commit.commit_hash == commit_hash
end
CheckoutMessage.new(commit_hash, false)
end
def log
unless @commit_store.empty?
LogMessage.new(branch.name, true, @commit_store)
else
LogMessage.new(branch.name, false, @commit_store)
end
end
end
class BranchController
attr_accessor :branches
attr_accessor :active_branch
attr_accessor :active_repo
def initialize(repo_state)
@branches = []
@branches.push(Branch.new("master", repo_state.dup))
@active_branch = @branches[0]
@active_repo = repo_state
end
def name
@active_branch.name
end
def create(branch_name)
message = CreateMessage.new(branch_name, false)
return message if @branches.any?{ |branch| branch.name == branch_name }
@branches.push(Branch.new(branch_name, active_repo.dup))
CreateMessage.new(branch_name, true)
end
def remove(branch_name)
if @active_branch.name == branch_name
BranchRemoveMessage.new(branch_name, false, true)
elsif @branches.any?{ |branch| branch.name == branch_name }
selected = @branches.select{ |branch| branch.name == branch_name }
@branches.delete(selected[0])
BranchRemoveMessage.new(branch_name, true)
else
BranchRemoveMessage.new(branch_name, false, false)
end
end
def list
names = []
@branches.each{ |branch| names.push(branch.name) }
names.each{ |name| name << "\n" }
names.sort!
names[names.index(active_branch.name)] = "* " + active_branch.name
BranchListMessage.new(names.join, true)
end
def checkout(branch_name)
message = BranchCheckoutMessage.new(branch_name, false)
selected = @branches.select{|branch| branch.name == branch_name}
if selected[0] == nil
BranchCheckoutMessage.new(branch_name, false)
else
@active_branch.repo_state = @active_repo.dup
@active_branch = selected[0]
@active_repo.replace(selected[0].repo_state)
BranchCheckoutMessage.new(branch_name, true)
end
end
end
class Branch
attr_accessor :name
attr_accessor :repo_state
def initialize(name, repo_state)
@name = name
@repo_state = repo_state
end
-end
+end

Николай обнови решението на 18.11.2015 12:10 (преди над 8 години)

class Message
def initialize(message, success)
@message = message
@success = success
end
def message
@message
end
def success?
@success
end
def error?
! @success
end
end
class AddMessage < Message
def initialize(message, *result)
@message = message
@success = true
@result = result[0]
end
def message
"Added #{@message} to stage."
end
def result
return @result if @success
end
end
class CommitMessage < Message
def initialize(message, success, changed_objects)
@message = message
@success = success
@changed_objects = changed_objects
end
def message
if @success
"#{@message}\n#{@changed_objects} objects changed"
else
"Nothing to commit, working directory clean."
end
end
end
class RemoveMessage < Message
def message
if @success
"Added #{@message} for removal."
else
"Object #{@message} is not commited."
end
end
end
class CheckoutMessage < Message
def initialize(message, success, *result)
@message = message
@success = success
@result = result[0]
end
def message
if @success
"HEAD is now at #{@message}."
else
"Commit #{@message} does not exist."
end
end
def result
return @result if @success
end
end
class LogMessage < Message
def initialize(branch_name, success, commits)
@branch_name = branch_name
@success = success
@commits = commits
end
def message
if @success
string = ""
@commits.each{ |commit| string += information(commit) }
string
else
"Branch #{@branch_name} does not have any commits yet."
end
end
def information(commit)
log = "Commit #{commit.commit_hash} \n"\
"Date: #{commit.date}"\
"\n\t #{commit.message}\n"
end
end
class CreateMessage < Message
def message
if @success
"Created branch #{@message}."
else
"Branch #{@message} already exists."
end
end
end
class BranchCheckoutMessage < Message
def message
if @success
"Switched to #{@message}."
else
"Branch #{@message} does not exists."
end
end
end
class BranchRemoveMessage < Message
def initialize(branch_name, success, *current)
@branch_name = branch_name
@success = success
@current = current[0]
end
def message
if @success
"Removed branch #{@branch_name}."
else
if @current
"Cannot remove current branch."
else
"Branch #{@branch_name} does not exist."
end
end
end
end
class BranchListMessage < Message
end
class ObjectElement
attr_accessor :name
attr_accessor :info
def initialize(name, info)
@name = name
@info = info
end
def == (other)
if @name == other
true
else
false
end
end
end
class Commit
require 'digest/sha1'
attr_accessor :commit_hash
attr_accessor :message
attr_accessor :date
attr_accessor :repo_state
def initialize(message, repo_state)
@message = message
@repo_state = repo_state
@date = Time.now.asctime
@commit_hash = Digest::SHA1.hexdigest (@date + @message)
end
def include?(object_name)
repo_state.each { |object| return true if object == object_name }
false
end
end
class ObjectStore
attr_accessor :branch
attr_accessor :commit_store
attr_accessor :head
attr_accessor :working_commit
attr_accessor :changes
def initialize
@working_commit = []
@commit_store = []
@changes = 0
@branch = BranchController.new(@commit_store)
end
def self.init(&block)
repo = ObjectStore.new
if block_given?
repo.instance_eval(&block)
end
repo
end
def add(name, object)
@working_commit.push(ObjectElement.new(name , object))
@changes += 1
AddMessage.new(name, object)
end
def commit(message)
return CommitMessage.new(message, false, 0) if @changes == 0
@commit_store.push(Commit.new(message, @working_commit.dup))
@head = @commit_store.last
m = CommitMessage.new(message, true, @changes)
@changes = 0
m
end
def remove(name)
for_remove = @working_commit.select{ |x| x if x == name}
if for_remove[0] != nil
@working_commit.delete(for_remove[0])
@changes += 1
RemoveMessage.new(name, true)
else
RemoveMessage.new(name, false)
end
end
def checkout(commit_hash)
@commit_store.each do |commit|
message = CheckoutMessage.new(commit_hash, true, commit)
@working_commit = commit.repo_state
changes += 1
@head = commit
return message if commit.commit_hash == commit_hash
end
CheckoutMessage.new(commit_hash, false)
end
def log
unless @commit_store.empty?
LogMessage.new(branch.name, true, @commit_store)
else
LogMessage.new(branch.name, false, @commit_store)
end
end
end
class BranchController
attr_accessor :branches
attr_accessor :active_branch
attr_accessor :active_repo
def initialize(repo_state)
@branches = []
@branches.push(Branch.new("master", repo_state.dup))
@active_branch = @branches[0]
@active_repo = repo_state
end
def name
@active_branch.name
end
def create(branch_name)
message = CreateMessage.new(branch_name, false)
return message if @branches.any?{ |branch| branch.name == branch_name }
@branches.push(Branch.new(branch_name, active_repo.dup))
CreateMessage.new(branch_name, true)
end
def remove(branch_name)
if @active_branch.name == branch_name
BranchRemoveMessage.new(branch_name, false, true)
elsif @branches.any?{ |branch| branch.name == branch_name }
selected = @branches.select{ |branch| branch.name == branch_name }
@branches.delete(selected[0])
BranchRemoveMessage.new(branch_name, true)
else
BranchRemoveMessage.new(branch_name, false, false)
end
end
def list
names = []
@branches.each{ |branch| names.push(branch.name) }
names.each{ |name| name << "\n" }
names.sort!
names[names.index(active_branch.name)] = "* " + active_branch.name
BranchListMessage.new(names.join, true)
end
def checkout(branch_name)
message = BranchCheckoutMessage.new(branch_name, false)
selected = @branches.select{|branch| branch.name == branch_name}
if selected[0] == nil
BranchCheckoutMessage.new(branch_name, false)
else
@active_branch.repo_state = @active_repo.dup
@active_branch = selected[0]
@active_repo.replace(selected[0].repo_state)
BranchCheckoutMessage.new(branch_name, true)
end
end
end
class Branch
attr_accessor :name
attr_accessor :repo_state
def initialize(name, repo_state)
@name = name
@repo_state = repo_state
end
-end
+end
+
+repo = ObjectStore.init do
+ add("value", 21)
+ commit("message")
+end
+puts repo.log.message

Николай обнови решението на 18.11.2015 12:11 (преди над 8 години)

class Message
def initialize(message, success)
@message = message
@success = success
end
def message
@message
end
def success?
@success
end
def error?
! @success
end
end
class AddMessage < Message
def initialize(message, *result)
@message = message
@success = true
@result = result[0]
end
def message
"Added #{@message} to stage."
end
def result
return @result if @success
end
end
class CommitMessage < Message
def initialize(message, success, changed_objects)
@message = message
@success = success
@changed_objects = changed_objects
end
def message
if @success
"#{@message}\n#{@changed_objects} objects changed"
else
"Nothing to commit, working directory clean."
end
end
end
class RemoveMessage < Message
def message
if @success
"Added #{@message} for removal."
else
"Object #{@message} is not commited."
end
end
end
class CheckoutMessage < Message
def initialize(message, success, *result)
@message = message
@success = success
@result = result[0]
end
def message
if @success
"HEAD is now at #{@message}."
else
"Commit #{@message} does not exist."
end
end
def result
return @result if @success
end
end
class LogMessage < Message
def initialize(branch_name, success, commits)
@branch_name = branch_name
@success = success
@commits = commits
end
def message
if @success
string = ""
@commits.each{ |commit| string += information(commit) }
string
else
"Branch #{@branch_name} does not have any commits yet."
end
end
def information(commit)
log = "Commit #{commit.commit_hash} \n"\
"Date: #{commit.date}"\
"\n\t #{commit.message}\n"
end
end
class CreateMessage < Message
def message
if @success
"Created branch #{@message}."
else
"Branch #{@message} already exists."
end
end
end
class BranchCheckoutMessage < Message
def message
if @success
"Switched to #{@message}."
else
"Branch #{@message} does not exists."
end
end
end
class BranchRemoveMessage < Message
def initialize(branch_name, success, *current)
@branch_name = branch_name
@success = success
@current = current[0]
end
def message
if @success
"Removed branch #{@branch_name}."
else
if @current
"Cannot remove current branch."
else
"Branch #{@branch_name} does not exist."
end
end
end
end
class BranchListMessage < Message
end
class ObjectElement
attr_accessor :name
attr_accessor :info
def initialize(name, info)
@name = name
@info = info
end
def == (other)
if @name == other
true
else
false
end
end
end
class Commit
require 'digest/sha1'
attr_accessor :commit_hash
attr_accessor :message
attr_accessor :date
attr_accessor :repo_state
def initialize(message, repo_state)
@message = message
@repo_state = repo_state
@date = Time.now.asctime
@commit_hash = Digest::SHA1.hexdigest (@date + @message)
end
def include?(object_name)
repo_state.each { |object| return true if object == object_name }
false
end
end
class ObjectStore
attr_accessor :branch
attr_accessor :commit_store
attr_accessor :head
attr_accessor :working_commit
attr_accessor :changes
def initialize
@working_commit = []
@commit_store = []
@changes = 0
@branch = BranchController.new(@commit_store)
end
def self.init(&block)
repo = ObjectStore.new
if block_given?
repo.instance_eval(&block)
end
repo
end
def add(name, object)
@working_commit.push(ObjectElement.new(name , object))
@changes += 1
AddMessage.new(name, object)
end
def commit(message)
return CommitMessage.new(message, false, 0) if @changes == 0
@commit_store.push(Commit.new(message, @working_commit.dup))
@head = @commit_store.last
m = CommitMessage.new(message, true, @changes)
@changes = 0
m
end
def remove(name)
for_remove = @working_commit.select{ |x| x if x == name}
if for_remove[0] != nil
@working_commit.delete(for_remove[0])
@changes += 1
RemoveMessage.new(name, true)
else
RemoveMessage.new(name, false)
end
end
def checkout(commit_hash)
@commit_store.each do |commit|
message = CheckoutMessage.new(commit_hash, true, commit)
@working_commit = commit.repo_state
changes += 1
@head = commit
return message if commit.commit_hash == commit_hash
end
CheckoutMessage.new(commit_hash, false)
end
def log
unless @commit_store.empty?
LogMessage.new(branch.name, true, @commit_store)
else
LogMessage.new(branch.name, false, @commit_store)
end
end
end
class BranchController
attr_accessor :branches
attr_accessor :active_branch
attr_accessor :active_repo
def initialize(repo_state)
@branches = []
@branches.push(Branch.new("master", repo_state.dup))
@active_branch = @branches[0]
@active_repo = repo_state
end
def name
@active_branch.name
end
def create(branch_name)
message = CreateMessage.new(branch_name, false)
return message if @branches.any?{ |branch| branch.name == branch_name }
@branches.push(Branch.new(branch_name, active_repo.dup))
CreateMessage.new(branch_name, true)
end
def remove(branch_name)
if @active_branch.name == branch_name
BranchRemoveMessage.new(branch_name, false, true)
elsif @branches.any?{ |branch| branch.name == branch_name }
selected = @branches.select{ |branch| branch.name == branch_name }
@branches.delete(selected[0])
BranchRemoveMessage.new(branch_name, true)
else
BranchRemoveMessage.new(branch_name, false, false)
end
end
def list
names = []
@branches.each{ |branch| names.push(branch.name) }
names.each{ |name| name << "\n" }
names.sort!
names[names.index(active_branch.name)] = "* " + active_branch.name
BranchListMessage.new(names.join, true)
end
def checkout(branch_name)
message = BranchCheckoutMessage.new(branch_name, false)
selected = @branches.select{|branch| branch.name == branch_name}
if selected[0] == nil
BranchCheckoutMessage.new(branch_name, false)
else
@active_branch.repo_state = @active_repo.dup
@active_branch = selected[0]
@active_repo.replace(selected[0].repo_state)
BranchCheckoutMessage.new(branch_name, true)
end
end
end
class Branch
attr_accessor :name
attr_accessor :repo_state
def initialize(name, repo_state)
@name = name
@repo_state = repo_state
end
-end
-
+end
-repo = ObjectStore.init do
- add("value", 21)
- commit("message")
-end
-puts repo.log.message