Permalink
Browse files

Merge pull request #3 from teleological/adapter_v070

Compatibility with adapter v0.7.0
  • Loading branch information...
2 parents 24ff408 + 6a7665f commit 36e94204b866a1e97b65646cacd6de401797192d @tekknolagi tekknolagi committed Jul 8, 2015
Showing with 84 additions and 31 deletions.
  1. +1 −1 adapter-git.gemspec
  2. +43 −18 lib/adapter/git.rb
  3. +40 −10 spec/git_spec.rb
  4. +0 −2 spec/spec_helper.rb
View
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_dependency 'adapter', '~> 0.5.1'
+ s.add_dependency 'adapter', '~> 0.7.0'
s.add_dependency 'grit', '~> 2.0'
s.add_development_dependency 'rspec', '~> 2.0'
s.add_development_dependency 'rake'
View
@@ -8,36 +8,38 @@ def branch
options[:branch] || 'master'
end
- def head
- client.get_head(branch)
+ def head(head_branch = nil)
+ client.get_head(head_branch || branch)
end
- def key?(key)
- !(head && head.commit.tree / key_for(key)).nil?
+ def key?(key, options = nil)
+ key_head = head(options ? options[:branch] : nil)
+ !(key_head && key_head.commit.tree / key_for(key)).nil?
end
- def read(key)
- if head && blob = head.commit.tree / key_for(key)
+ def read(key, options = nil)
+ read_head = head(options ? options[:branch] : nil)
+ if read_head && blob = read_head.commit.tree / key_for(key)
decode(blob.data)
end
end
- def write(key, value)
- commit("Updated #{key}") do |index|
+ def write(key, value, options = nil)
+ commit("Updated #{key}", options) do |index|
index.add(key_for(key), encode(value))
end
end
- def delete(key)
+ def delete(key, options = nil)
read(key).tap do
- commit("Delete #{key}") {|index| index.delete(key_for(key)) }
+ commit("Delete #{key}", options) {|index| index.delete(key_for(key)) }
end
end
- def clear
- commit("Cleared") do |index|
+ def clear(options = nil)
+ commit("Cleared", options) do |index|
tree = index.current_tree
- tree = tree / options[:path] if options[:path] && tree
+ tree = tree / self.options[:path] if self.options[:path] && tree
if tree
tree.contents.each do |entry|
index.delete(key_for(entry.name))
@@ -55,22 +57,45 @@ def decode(value)
end
def key_for(key)
- File.join(*[options[:path], super].compact)
+ File.join(*[options[:path], serialize_key(key)].compact)
end
private
- def commit(message)
+ def commit(message, options)
+ options ||= {}
+
index = client.index
- if head
- commit = head.commit
+ commit_branch = options[:branch] || branch
+ if commit_head = head(commit_branch)
+ commit = commit_head.commit
index.current_tree = commit.tree
end
yield index
- index.commit(message, :parents => Array(commit), :head => branch) unless index.tree.empty?
+ message = options[:message] || message
+ commit_options = make_commit_options(commit, commit_branch, options)
+ index.commit(message, commit_options) unless index.tree.empty?
+ end
+
+ def make_commit_options(commit, head, options)
+ commit_options = { :parents => Array(commit), :head => head }
+ [:actor, :committer, :author, :committed_date, :authored_date].each do |k|
+ commit_options[k] = options[k] if options.has_key?(k)
+ end
+ commit_options
+ end
+
+ def serialize_key(key)
+ if key.is_a?(String)
+ key
+ elsif key.is_a?(Symbol)
+ key.to_s
+ else
+ Marshal.dump(key)
+ end
end
end
View
@@ -12,26 +12,32 @@
# Some adapter specs don't pass if there is not at least one commit in the
# repo since the git adapter short-circuits the key marshalling if there
# are no commits.
- adapter.set('specs', 'running')
+ adapter.write('specs', 'running')
end
it_should_behave_like 'an adapter'
it 'should create a branch when it does not exist' do
adapter.options[:branch] = 'foobar'
- adapter.set('foo', 'bar')
+ adapter.write('foo', 'bar')
client.get_head('foobar').should_not be_nil
end
+ it 'overrides configured branch with :branch option' do
+ adapter.options[:branch] = 'foobar'
+ adapter.write('foo', 'bar', :branch => 'bazqux')
+ client.get_head('bazqux').should_not be_nil
+ end
+
it 'should not raise error on clear when branch does not exist' do
client.git.fs_delete("refs/heads/#{adapter.branch}")
lambda { adapter.clear }.should_not raise_error
end
it 'should successfully delete a key' do
- adapter.set('foo', 'bar')
+ adapter.write('foo', 'bar')
adapter.delete('foo')
- adapter.get('foo').should be_nil
+ adapter.read('foo').should be_nil
end
it 'should not generate a commit message if there are no changes' do
@@ -49,25 +55,49 @@
it_should_behave_like 'an adapter'
it 'should store keys in the directory' do
- adapter.set('foo', 'bar')
+ adapter.write('foo', 'bar')
(adapter.head.commit.tree / 'foo').should be_nil
(adapter.head.commit.tree / 'db/things/foo').should_not be_nil
end
it 'should not clear other keys' do
other_adapter = Adapter[:git].new(client, :branch => 'adapter-git')
- other_adapter.set('foo', 'bar')
+ other_adapter.write('foo', 'bar')
- adapter.set('foo', 'baz')
+ adapter.write('foo', 'baz')
adapter.clear
- other_adapter.get('foo').should == 'bar'
- adapter.get('foo').should be_nil
+ other_adapter.read('foo').should == 'bar'
+ adapter.read('foo').should be_nil
end
it 'should not raise error on clear when branch does not exist' do
client.git.fs_delete("refs/heads/#{adapter.branch}")
lambda { adapter.clear }.should_not raise_error
end
+
+ it 'uses a default commit message if none specified' do
+ adapter.write('foo', 'bar')
+ adapter.head.commit.message.should == 'Updated foo'
+ end
+
+ it 'accepts a :message option for commit message' do
+ adapter.write('foo', 'bar', :message => 'Reticulating splines')
+ adapter.head.commit.message.should == 'Reticulating splines'
+ end
+
+ it 'accepts a commit :author option' do
+ author = Grit::Actor.new('Foo Bar', 'baz@qux.qx')
+ adapter.write('foo', 'bar', :author => author)
+ adapter.head.commit.author.name.should == author.name
+ adapter.head.commit.author.email.should == author.email
+ end
+
+ it 'accepts a option for :committed_date' do
+ commit_time = Time.now - 600
+ adapter.write('foo', 'bar', :committed_date => commit_time)
+ adapter.head.commit.committed_date.to_i.should == commit_time.to_i
+ end
+
end
-end
+end
View
@@ -14,7 +14,5 @@
log_path.mkpath
require 'adapter/spec/an_adapter'
-require 'adapter/spec/json_adapter'
-require 'adapter/spec/types'
require 'adapter-git'

0 comments on commit 36e9420

Please sign in to comment.