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

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

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

Резултати

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

Код

require 'digest/sha1'
require 'set'
def answer
AnswerBuilder.new
end
class AnswerBuilder
def positively(message)
@message = message
@is_success = true
self
end
def negatively(message)
@message = message
@is_success = false
self
end
def when
yield ? Answer.new(@message, @is_success) : nil
end
def with_result
Answer.new(@message, @is_success, yield)
end
def to
yield
Answer.new(@message, @is_success, nil)
end
end
class Answer
attr_reader :message, :result
def initialize(message, is_success, result = nil)
@message = message
@is_success = is_success
@result = result
end
def success?
@is_success
end
def error?
not @is_success
end
end
class ActionAdd
attr_reader :name, :object
def initialize(name, object)
@name = name
@object = object
@old_object = nil
end
def apply(store)
@old_object = store[@name] if store.key? @name
store[@name] = @object
end
def undo(store)
@old_object ? (store[@name] = @old_object) : (store.delete @name)
end
end
class ActionRemove
attr_reader :name
def initialize(name)
@name = name
@old_object = nil
end
def apply(store)
@old_object = store.delete @name
end
def undo(store)
store[@name] = @old_object
end
end
class Commit
attr_reader :message, :date, :hash, :parent
def initialize(message, changes, parent, store)
@message = message
@changes = changes
@date = Time.now
@hash = Digest::SHA1.hexdigest "#{date}#{message}"
@parent = parent
@store = store
@branches = [].to_set
end
def include_in_branch(name)
@branches.add name
end
def exclude_from_branch(name)
@branches.delete name
end
def on_branch?(name)
@branches.include? name
end
def objects
@store.values
end
def ==(other)
hash == other.hash
end
def to_s
date = @date.strftime "%a %b %d %H:%M %Y %z"
"Commit #{@hash}\nDate: #{date}\n\n\t#{@message}"
end
def apply
@changes.each { |change| change.apply @store }
end
def undo
@changes.reverse_each { |change| change.undo @store }
end
end
class Branch
include Enumerable
attr_reader :name, :head
def initialize(name, head)
@name = name
@head = head
each { |commit| commit.include_in_branch @name }
end
def each
commit = @head
while commit
yield commit
commit = commit.parent
end
end
def empty?
@head.nil?
end
def commit(message, changes, store)
@head = Commit.new(message, changes, @head, store)
@head.include_in_branch @name
@head.apply
@head
end
def drop_all_commits
each { |commit| commit.exclude_from_branch @name}
@head = nil
end
def checkout(hash)
return nil if none? { |commit| hash == commit.hash }
while @head.hash != hash
@head.undo
@head.exclude_from_branch @name
@head = @head.parent
end
@head
end
def undo(other)
take_while { |commit| not commit.on_branch? other }.each(&:undo)
end
def apply(other)
take_while { |commit| not commit.on_branch? other }.reverse_each(&:apply)
end
end
class StoreState
BRANCH_MASTER = 'master'
def initialize
@branches = { BRANCH_MASTER => Branch.new(BRANCH_MASTER, nil) }
@active_branch = @branches[BRANCH_MASTER]
@changes = []
@objects = {}
end
def perform(&block)
self.instance_eval(&block)
end
end
class ObjectStore
def self.init(&block)
store = ObjectStore.new
store.instance_eval(&block) if block_given?
store
end
def initialize
@state = StoreState.new
end
def add(name, object)
@state.perform do
answer.positively("Added #{name} to stage.").with_result do
@changes.push(ActionAdd.new name, object)
object
end
end
end
def remove(name)
@state.perform do
answer.negatively("Object #{name} is not committed.").when do
not @objects.key? name
end or
answer.positively("Added #{name} for removal.").with_result do
@changes.push ActionRemove.new(name)
@objects[name]
end
end
end
def commit(message)
@state.perform do
count = @changes.map(&:name).uniq.count
answer.negatively('Nothing to commit, working directory clean.').when do
@changes.empty?
end or
answer.positively("#{message}\n\t#{count} objects changed").with_result do
commit = @active_branch.commit(message, @changes, @objects)
@changes = []
commit
end
end
end
def head
@state.perform do
name = @active_branch.name
commit = @active_branch.head
answer.negatively("Branch #{name} does not have any commits yet.").when do
@active_branch.empty?
end or
answer.positively(commit.message).with_result do
commit
end
end
end
def get(name)
@state.perform do
answer.negatively("Object #{name} is not committed.").when do
not @objects.key? name
end or
answer.positively("Found object #{name}.").with_result do
@objects[name]
end
end
end
def log
@state.perform do
name = @active_branch.name
answer.negatively("Branch #{name} does not have any commits yet.").when do
@active_branch.empty?
end or
answer.positively(@active_branch.to_a.join("\n\n")).to { }
end
end
def checkout(hash)
@state.perform do
new_head = @active_branch.checkout hash
answer.negatively("Commit #{hash} does not exist.").when do
new_head.nil?
end or
answer.positively("HEAD is now at #{hash}.").with_result do
new_head
end
end
end
def branch
BranchController.new @state
end
end
class BranchController
def initialize(state)
@state = state
end
def create(name)
@state.perform do
answer.negatively("Branch #{name} already exists.").when do
@branches.key? name
end or
answer.positively("Created branch #{name}.").to do
@branches[name] = Branch.new(name, @active_branch.head)
end
end
end
def checkout(target_name)
@state.perform do
answer.negatively("Branch #{target_name} does not exist.").when do
not @branches.key? target_name
end or
answer.positively("Switched to branch #{target_name}.").to do
current_name = @active_branch.name
@active_branch.undo target_name
@active_branch = @branches[target_name]
@active_branch.apply current_name
end
end
end
def remove(name)
@state.perform do
answer.negatively("Branch #{name} does not exist.").when do
not @branches.key? name
end or
answer.negatively('Cannot remove current branch.').when do
@active_branch.name == name
end or
answer.positively("Removed branch #{name}.").to do
@branches[name].drop_all_commits
@branches.delete name
end
end
end
def list
@state.perform do
branch_names = @branches.keys.map.sort.map do |name|
@active_branch.name == name and "* #{name}" or " #{name}"
end
answer.positively(branch_names.join "\n").to { }
end
end
end

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

........FFFF..................

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 #<Answer:0x007f41784a2cf0 @message="Commit a0a86cab4d0b8249f2854b333a92c7cdb9cb61b7\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!", @is_success=true, @result=nil> to be success "Commit 020f94292ff683fc536310fe33c03adfbf64bbca\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-1mjy2ns/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 #<Answer:0x007f417847fed0 @message="Commit a0a86cab4d0b8249f2854b333a92c7cdb9cb61b7\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!", @is_success=true, @result=nil> to be success "Commit 020f94292ff683fc536310fe33c03adfbf64bbca\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-1mjy2ns/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 #<Answer:0x007f41783eb528 @message="Commit 511703120059c7582296e3d2a0812767ac4f9533\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSecond commit\n\nCommit 5649e1fb079fb19bd95f47f081f026927b1cf03a\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit", @is_success=true, @result=nil> to be success "Commit a6bf7130a3150d375443c25d06c1bf8077e4b4fb\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tSecond commit\n\nCommit 9a97b358afe869ba1e523d99dd2101dfc0570dc0\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-1mjy2ns/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 #<Answer:0x007f41783c9158 @message="Commit 5649e1fb079fb19bd95f47f081f026927b1cf03a\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit", @is_success=true, @result=nil> to be success "Commit 9a97b358afe869ba1e523d99dd2101dfc0570dc0\nDate: Mon Jan 11 11:55 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-1mjy2ns/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)>'

Finished in 0.03744 seconds
30 examples, 4 failures

Failed examples:

rspec /tmp/d20160111-5693-1mjy2ns/spec.rb:77 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-1mjy2ns/spec.rb:86 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-1mjy2ns/spec.rb:95 # ObjectStore can show log of changes for multiple commits
rspec /tmp/d20160111-5693-1mjy2ns/spec.rb:111 # ObjectStore shows the log for the current branch only

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

Теодор обнови решението на 22.11.2015 14:20 (преди около 9 години)

+require 'digest/sha1'
+require 'set'
+
+
+def answer
+ AnswerBuilder.new
+end
+
+class AnswerBuilder
+ def positively(message)
+ @message = message
+ @is_success = true
+ self
+ end
+
+ def negatively(message)
+ @message = message
+ @is_success = false
+ self
+ end
+
+ def when
+ yield ? Answer.new(@message, @is_success) : nil
+ end
+
+ def with_result
+ Answer.new(@message, @is_success, yield)
+ end
+
+ def to
+ yield
+ Answer.new(@message, @is_success, nil)
+ end
+end
+
+class Answer
+ attr_reader :message, :result
+
+ def initialize(message, is_success, result = nil)
+ @message = message
+ @is_success = is_success
+ @result = result
+ end
+
+ def success?
+ @is_success
+ end
+
+ def error?
+ not @is_success
+ end
+end
+
+class ActionAdd
+ attr_reader :name, :object
+
+ def initialize(name, object)
+ @name = name
+ @object = object
+ @old_object = nil
+ end
+
+ def apply(store)
+ @old_object = store[@name] if store.key? @name
+ store[@name] = @object
+ end
+
+ def undo(store)
+ @old_object ? (store[@name] = @old_object) : (store.delete @name)
+ end
+end
+
+class ActionRemove
+ attr_reader :name
+
+ def initialize(name)
+ @name = name
+ @old_object = nil
+ end
+
+ def apply(store)
+ @old_object = store.delete @name
+ end
+
+ def undo(store)
+ store[@name] = @old_object
+ end
+end
+
+class Commit
+ attr_reader :message, :date, :hash, :parent
+
+ def initialize(message, changes, parent, store)
+ @message = message
+ @changes = changes
+ @date = Time.now.strftime "%a %b %d %T %Y %z"
+ @hash = Digest::SHA1.hexdigest "#{date}#{message}"
+ @parent = parent
+ @store = store
+ @branches = [].to_set
+ end
+
+ def include_in_branch(name)
+ @branches.add name
+ end
+
+ def exclude_from_branch(name)
+ @branches.delete name
+ end
+
+ def on_branch?(name)
+ @branches.include? name
+ end
+
+ def objects
+ @store.values
+ end
+
+ def ==(other)
+ hash == other.hash
+ end
+
+ def to_s
+ "Commit #{hash}\nDate: #{date}\n\n\t#{message}"
+ end
+
+ def apply
+ @changes.each { |change| change.apply @store }
+ end
+
+ def undo
+ @changes.reverse_each { |change| change.undo @store }
+ end
+end
+
+class Branch
+ include Enumerable
+
+ attr_reader :name, :head
+
+ def initialize(name, head)
+ @name = name
+ @head = head
+ each { |commit| commit.include_in_branch @name }
+ end
+
+ def each
+ commit = @head
+ while commit
+ yield commit
+ commit = commit.parent
+ end
+ end
+
+ def empty?
+ @head.nil?
+ end
+
+ def commit(message, changes, store)
+ @head = Commit.new(message, changes, @head, store)
+ @head.include_in_branch @name
+ @head.apply
+ end
+
+ def drop_all_commits
+ each { |commit| commit.exclude_from_branch @name}
+ @head = nil
+ end
+
+ def checkout(hash)
+ return nil if none? { |commit| hash == commit.hash }
+ while @head.hash != hash
+ @head.undo
+ @head.exclude_from_branch @name
+ @head = @head.parent
+ end
+ @head
+ end
+
+ def undo(other)
+ take_while { |commit| not commit.on_branch? other }.each(&:undo)
+ end
+
+ def apply(other)
+ take_while { |commit| not commit.on_branch? other }.reverse_each(&:apply)
+ end
+end
+
+class StoreState
+ BRANCH_MASTER = 'master'
+
+ def initialize
+ @branches = { BRANCH_MASTER => Branch.new(BRANCH_MASTER, nil) }
+ @active_branch = @branches[BRANCH_MASTER]
+ @changes = []
+ @objects = {}
+ end
+
+ def perform(&block)
+ self.instance_eval(&block)
+ end
+end
+
+class ObjectStore
+ def self.init(&block)
+ store = ObjectStore.new
+ store.instance_eval(&block) if block_given?
+ store
+ end
+
+ def initialize
+ @state = StoreState.new
+ end
+
+ def add(name, object)
+ @state.perform do
+ answer.positively("Added #{name} to stage.").with_result do
+ @changes.push(ActionAdd.new name, object)
+ object
+ end
+ end
+ end
+
+ def remove(name)
+ @state.perform do
+ answer.negatively("Object #{name} is not committed.").when do
+ not @objects.key? name
+ end or
+
+ answer.positively("Added #{name} for removal.").with_result do
+ @changes.push ActionRemove.new(name)
+ @objects[name]
+ end
+ end
+ end
+
+ def commit(message)
+ @state.perform do
+ count = @changes.map(&:name).uniq.count
+
+ answer.negatively('Nothing to commit, working directory clean.').when do
+ @changes.empty?
+ end or
+
+ answer.positively("#{message}\n\t#{count} objects changed").to do
+ @active_branch.commit(message, @changes, @objects)
+ @changes = []
+ end
+ end
+ end
+
+ def head
+ @state.perform do
+ name = @active_branch.name
+ commit = @active_branch.head
+
+ answer.negatively("Branch #{name} does not have any commits yet.").when do
+ @active_branch.empty?
+ end or
+
+ answer.positively(commit.message).with_result do
+ commit
+ end
+ end
+ end
+
+ def get(name)
+ @state.perform do
+ answer.negatively("Object #{name} is not committed.").when do
+ not @objects.key? name
+ end or
+
+ answer.positively("Found object #{name}.").with_result do
+ @objects[name]
+ end
+ end
+ end
+
+ def log
+ @state.perform do
+ name = @active_branch.name
+
+ answer.negatively("Branch #{name} does not have any commits yet.").when do
+ @active_branch.empty?
+ end or
+
+ answer.positively(@active_branch.to_a.join("\n\n")).to { }
+ end
+ end
+
+ def checkout(hash)
+ @state.perform do
+ new_head = @active_branch.checkout hash
+
+ answer.negatively("Commit #{hash} does not exist.").when do
+ new_head.nil?
+ end or
+
+ answer.positively("HEAD is now at #{hash}.").with_result do
+ new_head
+ end
+ end
+ end
+
+ def branch
+ BranchController.new @state
+ end
+end
+
+class BranchController
+ def initialize(state)
+ @state = state
+ end
+
+ def create(name)
+ @state.perform do
+ answer.negatively("Branch #{name} already exists.").when do
+ @branches.key? name
+ end or
+
+ answer.positively("Created branch #{name}.").to do
+ @branches[name] = Branch.new(name, @active_branch.head)
+ end
+ end
+ end
+
+ def checkout(target_name)
+ @state.perform do
+ answer.negatively("Branch #{target_name} does not exist.").when do
+ not @branches.key? target_name
+ end or
+
+ answer.positively("Switched to branch #{target_name}.").to do
+ current_name = @active_branch.name
+ @active_branch.undo target_name
+
+ @active_branch = @branches[target_name]
+ @active_branch.apply current_name
+ end
+ end
+ end
+
+ def remove(name)
+ @state.perform do
+ answer.negatively("Branch #{name} does not exist.").when do
+ not @branches.key? name
+ end or
+
+ answer.negatively('Cannot remove current branch.').when do
+ @active_branch.name == name
+ end or
+
+ answer.positively("Removed branch #{name}.").to do
+ @branches[name].drop_all_commits
+ @branches.delete name
+ end
+ end
+ end
+
+ def list
+ @state.perform do
+ branch_names = @branches.keys.map.sort.map do |name|
+ @active_branch.name == name and "* #{name}" or " #{name}"
+ end
+
+ answer.positively(branch_names.join "\n").to { }
+ end
+ end
+end

Теодор обнови решението на 22.11.2015 23:05 (преди около 9 години)

require 'digest/sha1'
require 'set'
def answer
AnswerBuilder.new
end
class AnswerBuilder
def positively(message)
@message = message
@is_success = true
self
end
def negatively(message)
@message = message
@is_success = false
self
end
def when
yield ? Answer.new(@message, @is_success) : nil
end
def with_result
Answer.new(@message, @is_success, yield)
end
def to
yield
Answer.new(@message, @is_success, nil)
end
end
class Answer
attr_reader :message, :result
def initialize(message, is_success, result = nil)
@message = message
@is_success = is_success
@result = result
end
def success?
@is_success
end
def error?
not @is_success
end
end
class ActionAdd
attr_reader :name, :object
def initialize(name, object)
@name = name
@object = object
@old_object = nil
end
def apply(store)
@old_object = store[@name] if store.key? @name
store[@name] = @object
end
def undo(store)
@old_object ? (store[@name] = @old_object) : (store.delete @name)
end
end
class ActionRemove
attr_reader :name
def initialize(name)
@name = name
@old_object = nil
end
def apply(store)
@old_object = store.delete @name
end
def undo(store)
store[@name] = @old_object
end
end
class Commit
attr_reader :message, :date, :hash, :parent
def initialize(message, changes, parent, store)
@message = message
@changes = changes
- @date = Time.now.strftime "%a %b %d %T %Y %z"
+ @date = Time.now
@hash = Digest::SHA1.hexdigest "#{date}#{message}"
@parent = parent
@store = store
@branches = [].to_set
end
def include_in_branch(name)
@branches.add name
end
def exclude_from_branch(name)
@branches.delete name
end
def on_branch?(name)
@branches.include? name
end
def objects
@store.values
end
def ==(other)
hash == other.hash
end
def to_s
- "Commit #{hash}\nDate: #{date}\n\n\t#{message}"
+ date = @date.strftime "%a %b %d %H:%M %Y %z"
+ "Commit #{@hash}\nDate: #{date}\n\n\t#{@message}"
end
def apply
@changes.each { |change| change.apply @store }
end
def undo
@changes.reverse_each { |change| change.undo @store }
end
end
class Branch
include Enumerable
attr_reader :name, :head
def initialize(name, head)
@name = name
@head = head
each { |commit| commit.include_in_branch @name }
end
def each
commit = @head
while commit
yield commit
commit = commit.parent
end
end
def empty?
@head.nil?
end
def commit(message, changes, store)
@head = Commit.new(message, changes, @head, store)
@head.include_in_branch @name
@head.apply
+ @head
end
def drop_all_commits
each { |commit| commit.exclude_from_branch @name}
@head = nil
end
def checkout(hash)
return nil if none? { |commit| hash == commit.hash }
while @head.hash != hash
@head.undo
@head.exclude_from_branch @name
@head = @head.parent
end
@head
end
def undo(other)
take_while { |commit| not commit.on_branch? other }.each(&:undo)
end
def apply(other)
take_while { |commit| not commit.on_branch? other }.reverse_each(&:apply)
end
end
class StoreState
BRANCH_MASTER = 'master'
def initialize
@branches = { BRANCH_MASTER => Branch.new(BRANCH_MASTER, nil) }
@active_branch = @branches[BRANCH_MASTER]
@changes = []
@objects = {}
end
def perform(&block)
self.instance_eval(&block)
end
end
class ObjectStore
def self.init(&block)
store = ObjectStore.new
store.instance_eval(&block) if block_given?
store
end
def initialize
@state = StoreState.new
end
def add(name, object)
@state.perform do
answer.positively("Added #{name} to stage.").with_result do
@changes.push(ActionAdd.new name, object)
object
end
end
end
def remove(name)
@state.perform do
answer.negatively("Object #{name} is not committed.").when do
not @objects.key? name
end or
answer.positively("Added #{name} for removal.").with_result do
@changes.push ActionRemove.new(name)
@objects[name]
end
end
end
def commit(message)
@state.perform do
count = @changes.map(&:name).uniq.count
answer.negatively('Nothing to commit, working directory clean.').when do
@changes.empty?
end or
- answer.positively("#{message}\n\t#{count} objects changed").to do
- @active_branch.commit(message, @changes, @objects)
+ answer.positively("#{message}\n\t#{count} objects changed").with_result do
+ commit = @active_branch.commit(message, @changes, @objects)
@changes = []
+ commit
end
end
end
def head
@state.perform do
name = @active_branch.name
commit = @active_branch.head
answer.negatively("Branch #{name} does not have any commits yet.").when do
@active_branch.empty?
end or
answer.positively(commit.message).with_result do
commit
end
end
end
def get(name)
@state.perform do
answer.negatively("Object #{name} is not committed.").when do
not @objects.key? name
end or
answer.positively("Found object #{name}.").with_result do
@objects[name]
end
end
end
def log
@state.perform do
name = @active_branch.name
answer.negatively("Branch #{name} does not have any commits yet.").when do
@active_branch.empty?
end or
answer.positively(@active_branch.to_a.join("\n\n")).to { }
end
end
def checkout(hash)
@state.perform do
new_head = @active_branch.checkout hash
answer.negatively("Commit #{hash} does not exist.").when do
new_head.nil?
end or
answer.positively("HEAD is now at #{hash}.").with_result do
new_head
end
end
end
def branch
BranchController.new @state
end
end
class BranchController
def initialize(state)
@state = state
end
def create(name)
@state.perform do
answer.negatively("Branch #{name} already exists.").when do
@branches.key? name
end or
answer.positively("Created branch #{name}.").to do
@branches[name] = Branch.new(name, @active_branch.head)
end
end
end
def checkout(target_name)
@state.perform do
answer.negatively("Branch #{target_name} does not exist.").when do
not @branches.key? target_name
end or
answer.positively("Switched to branch #{target_name}.").to do
current_name = @active_branch.name
@active_branch.undo target_name
@active_branch = @branches[target_name]
@active_branch.apply current_name
end
end
end
def remove(name)
@state.perform do
answer.negatively("Branch #{name} does not exist.").when do
not @branches.key? name
end or
answer.negatively('Cannot remove current branch.').when do
@active_branch.name == name
end or
answer.positively("Removed branch #{name}.").to do
@branches[name].drop_all_commits
@branches.delete name
end
end
end
def list
@state.perform do
branch_names = @branches.keys.map.sort.map do |name|
@active_branch.name == name and "* #{name}" or " #{name}"
end
answer.positively(branch_names.join "\n").to { }
end
end
end