Skip to content

Commit

Permalink
Added locker to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Naiman committed Feb 5, 2015
1 parent 90d4bc0 commit 54f9220
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
1 change: 1 addition & 0 deletions eternity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'redic', '~> 1.2.0'
spec.add_dependency 'restruct', '~> 0.0.3'
spec.add_dependency 'class_config', '~> 0.0.1'
spec.add_dependency 'locky', '~> 0.0.1'

if RUBY_PLATFORM == 'java'
spec.add_dependency 'msgpack-jruby'
Expand Down
5 changes: 5 additions & 0 deletions lib/eternity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'forwardable'
require 'restruct'
require 'base64'
require 'locky'

module Eternity

Expand All @@ -24,6 +25,10 @@ module Eternity
attr_config :blob_path, File.join(Dir.home, '.eternity')
attr_config :logger, Logger.new(STDOUT)

def self.locker_adapter
@locker_adapter ||= Restruct::Hash.new redis: redis, id: keyspace[:locker]
end

def self.redis_keys
redis.call 'KEYS', keyspace['*']
end
Expand Down
61 changes: 35 additions & 26 deletions lib/eternity/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(name)
@tracker = Tracker.new self
@current = Restruct::Hash.new redis: Eternity.redis, id: id[:current]
@branches = Restruct::Hash.new redis: Eternity.redis, id: id[:branches]
@locker = Locky.new @name, Eternity.locker_adapter
end

def [](collection)
Expand Down Expand Up @@ -56,9 +57,11 @@ def current_branch
def commit(options)
raise 'Nothing to commit' unless changes?

commit! message: options.fetch(:message),
author: options.fetch(:author),
time: options.fetch(:time) { Time.now }
locker.lock :commit do
commit! message: options.fetch(:message),
author: options.fetch(:author),
time: options.fetch(:time) { Time.now }
end
end

def branch(name)
Expand All @@ -71,22 +74,24 @@ def branch(name)
def checkout(options)
raise "Can't checkout with uncommitted changes" if changes?

original_commit = current_commit
locker.lock :checkout do
original_commit = current_commit

commit_id, branch = extract_commit_and_branch options
commit_id, branch = extract_commit_and_branch options

if commit_id
raise "Invalid commit #{commit_id}" unless Commit.exists? commit_id
current[:commit] = commit_id
branches[branch] = commit_id
else
current.delete :commit
branches.delete branch
end
if commit_id
raise "Invalid commit #{commit_id}" unless Commit.exists? commit_id
current[:commit] = commit_id
branches[branch] = commit_id
else
current.delete :commit
branches.delete branch
end

current[:branch] = branch
current[:branch] = branch

Patch.diff(original_commit, current_commit).delta
Patch.diff(original_commit, current_commit).delta
end
end

def merge(options)
Expand Down Expand Up @@ -125,7 +130,9 @@ def pull
end

def revert
Delta.revert(delta, current_commit).tap { tracker.revert }
locker.lock :revert do
Delta.revert(delta, current_commit).tap { tracker.revert }
end
end

def log
Expand All @@ -148,7 +155,7 @@ def to_h

private

attr_reader :tracker, :current
attr_reader :tracker, :current, :locker

def commit!(options)
changes = delta
Expand All @@ -164,18 +171,20 @@ def commit!(options)
end

def merge!(target_commit)
patch = Patch.merge current_commit, target_commit
locker.lock :merge do
patch = Patch.merge current_commit, target_commit

raise 'Already merged' if patch.merged?
raise 'Already merged' if patch.merged?

commit! message: "Merge #{target_commit.short_id} into #{current_commit.short_id}",
author: 'System',
parents: [current_commit.id, target_commit.id],
index: write_index(patch.delta),
base: patch.base_commit.id,
base_delta: Blob.write(:delta, patch.base_delta)
commit! message: "Merge #{target_commit.short_id} into #{current_commit.short_id}",
author: 'System',
parents: [current_commit.id, target_commit.id],
index: write_index(patch.delta),
base: patch.base_commit.id,
base_delta: Blob.write(:delta, patch.base_delta)

patch.delta
patch.delta
end
end

def write_index(delta)
Expand Down

0 comments on commit 54f9220

Please sign in to comment.