Решение на Пета задача от Димитър Ангелов

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

Към профила на Димитър Ангелов

Резултати

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

Код

require 'digest/sha1'
require 'date'
module CommitOperations
def add name, object
success = true
message = "Added #{name} to stage."
result = object
if success == true then @count += 1
@data[name] = object
end
Operation.new success, message, result
end
def remove name
success = if @data.key? name then true
else false
end
message = if success then "Added #{name} for removal."
else "Object #{name} is not committed."
end
result = if success then @data.delete name
else nil
end
if success == true then @count += 1
end
Operation.new success, message, result
end
def commit message
success = if @count > 0 then true
else false
end
message = if success then "#{message}\n\t#{@count} objects changed"
else "Nothing to commit, working directory clean."
end
if success then time = Time.now
hash = Digest::SHA1.hexdigest "#{time}#{message}"
result = Commit.new hash, message, time, @data
@count = 0
@commits = {hash => result}.merge! @commits
end
Operation.new success, message, result
end
def checkout commit_hash
success = if @commits.key? commit_hash then true
else false
end
message = if success then "HEAD is now at #{commit_hash}."
else "Commit #{commit_hash} does not exist."
end
result = @commits[commit_hash]
Operation.new success, message, result
end
end
module BranchOperations
def create branch_name
success = if @object_store.branches.key? branch_name then false
else true
end
message = if success == false then "Branch #{branch_name} already exists."
else @object_store.branches[branch_name] =
Branch.new branch_name, @object_store
"Created branch #{branch_name}."
end
Operation.new success, message
end
def checkout branch_name
success = if @object_store.branches.key? branch_name then true
else false
end
message = if success == true then @object_store.current = branch_name
"Switched to branch #{branch_name}."
else "Branch #{branch_name} does not exist."
end
Operation.new success, message
end
def remove branch_name
in_current = false
success = if branch_name == @object_store.current then in_current = true
false
elsif @object_store.branches.key? branch_name then true
else false
end
message = if success then @object_store.branches.delete branch_name
"Removed branch #{branch_name}."
elsif in_current == true then "Cannot remove current branch."
else "Branch #{branch_name} does not exist."
end
Operation.new success, message
end
def list
message = ""
sorted_keys = @object_store.branches.keys.sort{|key1, key2| key1 <=> key2}
sorted_keys.each{|key| if key == @object_store.current &&
key == sorted_keys.last then message << "* #{key.to_s}"
elsif key == @object_store.current then
message << "* #{key.to_s}\n"
elsif key == sorted_keys.last then message << " #{key.to_s}"
else message << " #{key.to_s}\n"
end}
success = true
Operation.new success, message
end
end
class Branch
include BranchOperations
def initialize name, object_store
@name = name
@object_store = object_store
end
end
class ObjectStore
include CommitOperations
def initialize
@branches = {}
@commits = {}
@data = {}
@count = 0
@current = "master"
end
attr_accessor :branches, :current
def ObjectStore.init &block
repo = new
repo.instance_eval &block if block_given?
repo
end
def branch
if @branches.empty? then @branches["master"] =
Branch.new "master", self.clone
end
@branches[@current]
end
def log
success = if @commits.empty? then false
else true
end
message = if success then @commits.each_value {|commit|
puts "Commit #{commit.hash}
Date: #{commit.date.strftime('%a %b %d %H:%M %Y %z')}\n
\t#{commit.message}"}
else "Branch #{@current} does not have any commits yet."
end
Operation.new success, message
end
def head
success = if @commits.empty? then false
else true
end
message = if success then @commits[@commits.keys.first].message
else "Branch #{@current} does not have any @commits yet"
end
result = if success then @commits[@commits.keys.first]
else result = nil
end
Operation.new success, message, result
end
def get name
success = if @data.key? name then true
else false
end
message = if success then "Found object #{name}."
else "Object #{name} is not committed."
end
result = if success then @data[name]
else result = nil
end
Operation.new success, message, result
end
end
class Operation
def initialize success, message, result = nil
@success = success
@message = message
@result = result
end
def message
@message
end
def success?
@success
end
def error?
if @success == true then false
else true
end
end
def result
@result
end
end
class Commit
def initialize name, message, time, data
@name = name
@message = message
@time = time
@data = data
end
def date
@time
end
def message
@message
end
def hash
@name
end
def objects
objects_array = []
@data.each_value{|value| objects_array.insert 0, value}
objects_array
end
end

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

......FFCommit d978f0a37314c899b96fa61836a92129c967ad35
Date: Mon Jan 11 11:54 2016 +0200

	So cool!
	2 objects changed
FCommit d978f0a37314c899b96fa61836a92129c967ad35
Date: Mon Jan 11 11:54 2016 +0200

	So cool!
	2 objects changed
FCommit 05949332b0233805f9900058788ab6408223156b
Date: Mon Jan 11 11:54 2016 +0200

	Second commit
	1 objects changed
Commit b8f93540b9de120f58ebf42edbf44638c608193b
Date: Mon Jan 11 11:54 2016 +0200

	First commit
	1 objects changed
FCommit 05949332b0233805f9900058788ab6408223156b
Date: Mon Jan 11 11:54 2016 +0200

	Second commit
	1 objects changed
Commit b8f93540b9de120f58ebf42edbf44638c608193b
Date: Mon Jan 11 11:54 2016 +0200

	First commit
	1 objects changed
F....F....F...F....

Failures:

  1) ObjectStore can show head
     Failure/Error: expect(repo.head).to be_success("There we go", last_commit)
       expected #<Operation:0x007f6b851ca280 @success=true, @message="There we go\n\t1 objects changed", @result=#<Commit:0x007f6b851ca3c0 @name="bbb4c90f7b4d7d770d90ed059d91b1a3196b74da", @message="There we go\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>> to be success "There we go" and #<Commit:0x007f6b851ca3c0 @name="bbb4c90f7b4d7d770d90ed059d91b1a3196b74da", @message="There we go\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>
     # /tmp/d20160111-5693-stsz1x/spec.rb:69: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 cannot show head for empty repository
     Failure/Error: expect(repo.head).to be_error("Branch master does not have any commits yet.")
       expected #<Operation:0x007f6b851a6a88 @success=false, @message="Branch master does not have any @commits yet", @result=nil> to be error "Branch master does not have any commits yet."
     # /tmp/d20160111-5693-stsz1x/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)>'

  3) 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 #<Operation:0x007f6b8518b490 @success=true, @message={"d978f0a37314c899b96fa61836a92129c967ad35"=>#<Commit:0x007f6b8518baa8 @name="d978f0a37314c899b96fa61836a92129c967ad35", @message="So cool!\n\t2 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>}, @result=nil> to be success "Commit b63d7849721e9725a10be0c13bc3d9e904bbc6a5\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-stsz1x/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)>'

  4) 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 #<Operation:0x007f6b8516b5f0 @success=true, @message={"d978f0a37314c899b96fa61836a92129c967ad35"=>#<Commit:0x007f6b8516bc08 @name="d978f0a37314c899b96fa61836a92129c967ad35", @message="So cool!\n\t2 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>}, @result=nil> to be success "Commit b63d7849721e9725a10be0c13bc3d9e904bbc6a5\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tSo cool!"
     # /tmp/d20160111-5693-stsz1x/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)>'

  5) 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 #<Operation:0x007f6b8512a258 @success=true, @message={"05949332b0233805f9900058788ab6408223156b"=>#<Commit:0x007f6b8512b270 @name="05949332b0233805f9900058788ab6408223156b", @message="Second commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>, "b8f93540b9de120f58ebf42edbf44638c608193b"=>#<Commit:0x007f6b8512b838 @name="b8f93540b9de120f58ebf42edbf44638c608193b", @message="First commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>}, @result=nil> to be success "Commit 6881e42186535ba1e13d64e313ae59f1bb2f4473\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tSecond commit\n\nCommit 97127a3fcc9cade1747ef2d930ff0b4739b086b2\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-stsz1x/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)>'

  6) 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 #<Operation:0x007f6b85103978 @success=true, @message={"05949332b0233805f9900058788ab6408223156b"=>#<Commit:0x007f6b85109d00 @name="05949332b0233805f9900058788ab6408223156b", @message="Second commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>, "b8f93540b9de120f58ebf42edbf44638c608193b"=>#<Commit:0x007f6b8510b4c0 @name="b8f93540b9de120f58ebf42edbf44638c608193b", @message="First commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>}, @result=nil> to be success "Commit 97127a3fcc9cade1747ef2d930ff0b4739b086b2\nDate: Mon Jan 11 11:54 2016 +0200\n\n\tFirst commit"
     # /tmp/d20160111-5693-stsz1x/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)>'

  7) ObjectStore can switch branches
     Failure/Error: expect(repo.head).to be_success("Second commit", second_commit)
       expected #<Operation:0x007f6b85004860 @success=true, @message="Second commit\n\t1 objects changed", @result=#<Commit:0x007f6b85004f90 @name="05949332b0233805f9900058788ab6408223156b", @message="Second commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>> to be success "Second commit" and #<Commit:0x007f6b85004f90 @name="05949332b0233805f9900058788ab6408223156b", @message="Second commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content2"}>
     # /tmp/d20160111-5693-stsz1x/spec.rb:161: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 be initialized with block
     Failure/Error: expect(repo.head).to be_success("Second commit", $second_commit)
       expected #<Operation:0x007f6b85c27548 @success=true, @message="Second commit\n\t1 objects changed", @result=#<Commit:0x007f6b85c27750 @name="05949332b0233805f9900058788ab6408223156b", @message="Second commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content"}>> to be success "Second commit" and #<Commit:0x007f6b85c27750 @name="05949332b0233805f9900058788ab6408223156b", @message="Second commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"object1"=>"content1", "object2"=>"content"}>
     # /tmp/d20160111-5693-stsz1x/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)>'

  9) ObjectStore can checkout commits
     Failure/Error: expect(repo.head).to be_success("First commit", first_commit)
       expected #<Operation:0x007f6b85cb8200 @success=true, @message="Second commit\n\t1 objects changed", @result=#<Commit:0x007f6b85cb9e98 @name="05949332b0233805f9900058788ab6408223156b", @message="Second commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"number"=>21}>> to be success "First commit" and #<Commit:0x007f6b85cba1e0 @name="b8f93540b9de120f58ebf42edbf44638c608193b", @message="First commit\n\t1 objects changed", @time=2016-01-11 11:54:27 +0200, @data={"number"=>21}>
     # /tmp/d20160111-5693-stsz1x/spec.rb:224: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.03246 seconds
30 examples, 9 failures

Failed examples:

rspec /tmp/d20160111-5693-stsz1x/spec.rb:63 # ObjectStore can show head
rspec /tmp/d20160111-5693-stsz1x/spec.rb:72 # ObjectStore cannot show head for empty repository
rspec /tmp/d20160111-5693-stsz1x/spec.rb:77 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-stsz1x/spec.rb:86 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-stsz1x/spec.rb:95 # ObjectStore can show log of changes for multiple commits
rspec /tmp/d20160111-5693-stsz1x/spec.rb:111 # ObjectStore shows the log for the current branch only
rspec /tmp/d20160111-5693-stsz1x/spec.rb:153 # ObjectStore can switch branches
rspec /tmp/d20160111-5693-stsz1x/spec.rb:187 # ObjectStore can be initialized with block
rspec /tmp/d20160111-5693-stsz1x/spec.rb:217 # ObjectStore can checkout commits

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

Димитър обнови решението на 22.11.2015 23:27 (преди над 8 години)

+require 'digest/sha1'
+require 'date'
+module CommitOperations
+ def add name, object
+ success = true
+ message = "Added #{name} to stage"
+ result = object
+ if success == true then @count += 1
+ @data[name] = object
+ end
+ Operation.new success, message, result
+ end
+ def remove name
+ success = if @data.key? name then true
+ else false
+ end
+ message = if success then "Added #{name} for removal."
+ else "Object #{name} is not committed."
+ end
+ result = if success then @data.delete name
+ else nil
+ end
+ if success == true then @count += 1
+ end
+ Operation.new success, message, result
+ end
+ def commit message
+ success = if @count > 0 then true
+ else false
+ end
+ message = if success then "#{message}\n\t#{@count} objects changed"
+ else "Nothing to commit, working directory clean."
+ end
+ if success then time = Time.now
+ hash = Digest::SHA1.hexdigest "#{time}#{message}"
+ the_commit = Commit.new hash, message, time, @data
+ @count = 0
+ #result = @commits[hash] = the_commit
+ end
+ Operation.new success, message #, result
+ end
+ def checkout commit_hash
+ success = if @commits.key? commit_hash then true
+ else false
+ end
+ message = if success then "HEAD is now at #{commit_hash}"
+ else "Commit #{commit_hash} does not exist."
+ end
+ result = @commits[commit_hash]
+ Operation.new success, message, result
+ end
+end
+
+module BranchOperations
+ def create branch_name
+ success = if @object_store.branches.key? branch_name then false
+ else true
+ end
+ message = if success == false then "Branch #{branch_name} already exists."
+ else "Created branch #{branch_name}."
+ @object_store.branches[branch_name] =
+ Branch.new branch_name, @object_store
+ end
+ Operation.new success, message
+ end
+ def checkout branch_name
+ success = if @object_store.branches.key? branch_name then true
+ else false
+ end
+ message = if success == true then "Switched to branch #{branch_name}."
+ @object_store.current = branch_name
+ else "Branch #{branch_name} does not exist."
+ end
+ Operation.new success, message
+ end
+ def remove branch_name
+ in_current = false
+ success = if branch_name == @object_store.current then in_current = true
+ false
+ elsif @object_store.branches.key? branch_name then true
+ else false
+ end
+ message = if success then "Removed branch #{branch_name}."
+ @object_store.branches.delete branch_name
+ elsif in_current == true then "Cannot remove current branch."
+ else "Branch #{branch_name} does not exist."
+ end
+ Operation.new success, message
+ end
+ def list
+ message = ""
+ sorted_keys = @object_store.branches.keys.sort{|key1, key2| key1 <=> key2}
+ sorted_keys.each{|key| if key == @object_store.current then
+ message << "* #{key.to_s}\n"
+ else message << " #{key.to_s}\n"
+ end}
+ success = true
+ Operation.new success, message
+ end
+end
+
+class Branch
+ include BranchOperations
+ def initialize name, object_store
+ @name = name
+ @object_store = object_store
+ end
+end
+
+class ObjectStore
+ include CommitOperations
+ def initialize
+ @branches = {}
+ @commits = {}
+ @data = {}
+ @count = 0
+ @current = "master"
+ end
+ attr_accessor :branches, :current
+ def ObjectStore.init &block
+ repo = new
+ repo.instance_eval &block if block_given?
+ repo
+ end
+ def branch
+ if @branches.empty? then @branches["master"] =
+ Branch.new "master", self.clone
+ end
+ @branches[@current]
+ end
+ def log
+ success = if @commits.empty? then false
+ else true
+ end
+ message = if success then @commits.each_value {|commit|
+ puts "Commit #{commit.hash}\nDate: #{commit.date}\n\n\t#{commit.message}"}
+ else "Branch #{@current} does not have any @commits yet"
+ end
+ Operation.new success, message
+ end
+ def head
+ success = if @commits.empty? then false
+ else true
+ end
+ message = if success then @commits[@commits.keys.last].message
+ else "Branch #{@current} does not have any @commits yet"
+ end
+ result = if success then @commits[@commits.keys.last]
+ else result = nil
+ end
+ Operation.new success, message, result
+ end
+ def get name
+ success = if @data.key? name then true
+ else false
+ end
+ message = if success then "Found object #{name}."
+ else "Object #{name} is not committed."
+ end
+ result = if success then @data[name]
+ else result = nil
+ end
+ Operation.new success, message, result
+ end
+end
+
+class Operation
+ def initialize success, message, result = nil
+ @success = success
+ @message = message
+ @result = result
+ end
+ def message
+ @message
+ end
+ def success?
+ @success
+ end
+ def error?
+ if @success == true then false
+ else true
+ end
+ end
+ def result
+ @result
+ end
+end
+
+class Commit
+ def initialize name, message, time, data
+ @name = name
+ @message = message
+ @time = time
+ @data = data
+ end
+ def date
+ @time.strftime('%a %b %d %H:%M %Y %z')
+ end
+ def message
+ @message
+ end
+ def hash
+ @name
+ end
+ def objects
+ objects_array = []
+ @data.each_value{|value| objects_array.insert 0, value}
+ objects_array
+ end
+end

Не бях сигурен дали commit трябва да връща резултат, защото за тестовете трябва, а в условието не. И тъй като дилемата беше голяма закоментирах това което трябва да се добави за да връща резултат :D

Димитър обнови решението на 23.11.2015 12:45 (преди над 8 години)

require 'digest/sha1'
require 'date'
module CommitOperations
def add name, object
success = true
- message = "Added #{name} to stage"
+ message = "Added #{name} to stage."
result = object
if success == true then @count += 1
@data[name] = object
end
Operation.new success, message, result
end
def remove name
success = if @data.key? name then true
else false
end
message = if success then "Added #{name} for removal."
else "Object #{name} is not committed."
end
result = if success then @data.delete name
else nil
end
if success == true then @count += 1
end
Operation.new success, message, result
end
def commit message
success = if @count > 0 then true
else false
end
message = if success then "#{message}\n\t#{@count} objects changed"
else "Nothing to commit, working directory clean."
end
if success then time = Time.now
hash = Digest::SHA1.hexdigest "#{time}#{message}"
- the_commit = Commit.new hash, message, time, @data
+ result = Commit.new hash, message, time, @data
@count = 0
- #result = @commits[hash] = the_commit
+ @commits = {hash => result}.merge! @commits
end
- Operation.new success, message #, result
+ Operation.new success, message, result
end
def checkout commit_hash
success = if @commits.key? commit_hash then true
else false
end
- message = if success then "HEAD is now at #{commit_hash}"
+ message = if success then "HEAD is now at #{commit_hash}."
else "Commit #{commit_hash} does not exist."
end
result = @commits[commit_hash]
Operation.new success, message, result
end
end
module BranchOperations
def create branch_name
success = if @object_store.branches.key? branch_name then false
else true
end
message = if success == false then "Branch #{branch_name} already exists."
- else "Created branch #{branch_name}."
- @object_store.branches[branch_name] =
+ else @object_store.branches[branch_name] =
Branch.new branch_name, @object_store
+ "Created branch #{branch_name}."
end
Operation.new success, message
end
def checkout branch_name
success = if @object_store.branches.key? branch_name then true
else false
end
- message = if success == true then "Switched to branch #{branch_name}."
- @object_store.current = branch_name
+ message = if success == true then @object_store.current = branch_name
+ "Switched to branch #{branch_name}."
else "Branch #{branch_name} does not exist."
end
Operation.new success, message
end
def remove branch_name
in_current = false
success = if branch_name == @object_store.current then in_current = true
false
elsif @object_store.branches.key? branch_name then true
else false
end
- message = if success then "Removed branch #{branch_name}."
- @object_store.branches.delete branch_name
+ message = if success then @object_store.branches.delete branch_name
+ "Removed branch #{branch_name}."
elsif in_current == true then "Cannot remove current branch."
else "Branch #{branch_name} does not exist."
end
Operation.new success, message
end
def list
message = ""
sorted_keys = @object_store.branches.keys.sort{|key1, key2| key1 <=> key2}
- sorted_keys.each{|key| if key == @object_store.current then
- message << "* #{key.to_s}\n"
- else message << " #{key.to_s}\n"
- end}
+ sorted_keys.each{|key| if key == @object_store.current &&
+ key == sorted_keys.last then message << "* #{key.to_s}"
+ elsif key == @object_store.current then
+ message << "* #{key.to_s}\n"
+ elsif key == sorted_keys.last then message << " #{key.to_s}"
+ else message << " #{key.to_s}\n"
+ end}
success = true
Operation.new success, message
end
end
class Branch
include BranchOperations
def initialize name, object_store
@name = name
@object_store = object_store
end
end
class ObjectStore
include CommitOperations
def initialize
@branches = {}
@commits = {}
@data = {}
@count = 0
@current = "master"
end
attr_accessor :branches, :current
def ObjectStore.init &block
repo = new
repo.instance_eval &block if block_given?
repo
end
def branch
if @branches.empty? then @branches["master"] =
Branch.new "master", self.clone
end
@branches[@current]
end
def log
success = if @commits.empty? then false
else true
end
message = if success then @commits.each_value {|commit|
- puts "Commit #{commit.hash}\nDate: #{commit.date}\n\n\t#{commit.message}"}
- else "Branch #{@current} does not have any @commits yet"
+ puts "Commit #{commit.hash}
+Date: #{commit.date.strftime('%a %b %d %H:%M %Y %z')}\n
+\t#{commit.message}"}
+ else "Branch #{@current} does not have any commits yet."
end
Operation.new success, message
end
def head
success = if @commits.empty? then false
else true
end
- message = if success then @commits[@commits.keys.last].message
+ message = if success then @commits[@commits.keys.first].message
else "Branch #{@current} does not have any @commits yet"
end
- result = if success then @commits[@commits.keys.last]
+ result = if success then @commits[@commits.keys.first]
else result = nil
end
Operation.new success, message, result
end
def get name
success = if @data.key? name then true
else false
end
message = if success then "Found object #{name}."
else "Object #{name} is not committed."
end
result = if success then @data[name]
else result = nil
end
Operation.new success, message, result
end
end
class Operation
def initialize success, message, result = nil
@success = success
@message = message
@result = result
end
def message
@message
end
def success?
@success
end
def error?
if @success == true then false
else true
end
end
def result
@result
end
end
class Commit
def initialize name, message, time, data
@name = name
@message = message
@time = time
@data = data
end
def date
- @time.strftime('%a %b %d %H:%M %Y %z')
+ @time
end
def message
@message
end
def hash
@name
end
def objects
objects_array = []
@data.each_value{|value| objects_array.insert 0, value}
objects_array
end
end