adapter-git 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ examples/test/
6
+ spec/test-repo/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright © 2011 Brandon Keepers
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ = adapter-git
2
+
3
+ Git adapter for adapter gem.
4
+
5
+ require 'adapter/git'
6
+
7
+ repo = Grit::Repo.init('path/to/repo')
8
+ adapter = Adapter[:git].new(repo, :branch => 'data', :path => 'db/records')
9
+ adapter.clear
10
+
11
+ adapter.write('foo', 'bar')
12
+ puts 'Should be bar: ' + adapter.read('foo').inspect
13
+
14
+ adapter.delete('foo')
15
+ puts 'Should be nil: ' + adapter.read('foo').inspect
16
+
17
+ adapter.write('foo', 'bar')
18
+ adapter.clear
19
+ puts 'Should be nil: ' + adapter.read('foo').inspect
20
+
21
+ puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
22
+
23
+ See examples/ or specs/ for more usage.
24
+
25
+ == Note on Patches/Pull Requests
26
+
27
+ * Fork the project.
28
+ * Make your feature addition or bug fix.
29
+ * Add tests for it. This is important so we don't break it in a future version unintentionally.
30
+ * Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine, but bump version in a commit by itself so we can ignore when we pull)
31
+ * Send us a pull request. Bonus points for topic branches.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "adapter/git/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "adapter-git"
7
+ s.version = Adapter::Git::VERSION
8
+ s.authors = ["Brandon Keepers"]
9
+ s.email = ["brandon@opensoul.org"]
10
+ s.homepage = ""
11
+ s.summary = %q{Adapter for git}
12
+ s.description = %q{Adapter for git}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency 'adapter', '~> 0.5.1'
20
+ s.add_dependency 'grit', '~> 2.0'
21
+ s.add_development_dependency 'rspec', '~> 2.0'
22
+ s.add_development_dependency 'rake'
23
+ end
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+
4
+ $:.unshift File.expand_path('../../lib', __FILE__)
5
+
6
+ require 'adapter/git'
7
+
8
+ require 'grit'
9
+
10
+ repo = Grit::Repo.init(File.expand_path('../test', __FILE__))
11
+ adapter = Adapter[:git].new(repo)
12
+ adapter.clear
13
+
14
+ adapter.write('foo', 'bar')
15
+ puts 'Should be bar: ' + adapter.read('foo').inspect
16
+
17
+ adapter.delete('foo')
18
+ puts 'Should be nil: ' + adapter.read('foo').inspect
19
+
20
+ adapter.write('foo', 'bar')
21
+ adapter.clear
22
+ puts 'Should be nil: ' + adapter.read('foo').inspect
23
+
24
+ puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
@@ -0,0 +1 @@
1
+ require "adapter/git"
@@ -0,0 +1,79 @@
1
+ require 'adapter'
2
+ require 'yaml'
3
+ require 'grit'
4
+
5
+ module Adapter
6
+ module Git
7
+ def branch
8
+ options[:branch] || 'master'
9
+ end
10
+
11
+ def head
12
+ client.get_head(branch)
13
+ end
14
+
15
+ def key?(key)
16
+ !(head && head.commit.tree / key_for(key)).nil?
17
+ end
18
+
19
+ def read(key)
20
+ if head && blob = head.commit.tree / key_for(key)
21
+ decode(blob.data)
22
+ end
23
+ end
24
+
25
+ def write(key, value)
26
+ commit("Updated #{key}") do |index|
27
+ index.add(key_for(key), encode(value))
28
+ end
29
+ end
30
+
31
+ def delete(key)
32
+ read(key).tap do
33
+ commit("Delete #{key}") {|index| index.delete(key_for(key)) }
34
+ end
35
+ end
36
+
37
+ def clear
38
+ commit("Cleared") do |index|
39
+ tree = index.current_tree
40
+ tree = tree / options[:path] if options[:path] && tree
41
+ if tree
42
+ tree.contents.each do |entry|
43
+ index.delete(key_for(entry.name))
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ def encode(value)
50
+ value.to_yaml
51
+ end
52
+
53
+ def decode(value)
54
+ YAML.load(value)
55
+ end
56
+
57
+ def key_for(key)
58
+ File.join(*[options[:path], super].compact)
59
+ end
60
+
61
+ private
62
+
63
+ def commit(message)
64
+ index = client.index
65
+
66
+ if head
67
+ commit = head.commit
68
+ index.current_tree = commit.tree
69
+ end
70
+
71
+ yield index
72
+
73
+ index.commit(message, :parents => Array(commit), :head => branch) unless index.tree.empty?
74
+ end
75
+
76
+ end
77
+ end
78
+
79
+ Adapter.define(:git, Adapter::Git)
@@ -0,0 +1,5 @@
1
+ module Adapter
2
+ module Git
3
+ VERSION = "0.5.0"
4
+ end
5
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+ require 'grit'
3
+
4
+ describe "Git adapter" do
5
+ let(:repo_dir) { File.expand_path('../test-repo', __FILE__) }
6
+ let(:adapter) { Adapter[:git].new(client, :branch => 'adapter-git') }
7
+ let(:client) { Grit::Repo.init(repo_dir) }
8
+
9
+ before do
10
+ FileUtils.rm_rf(repo_dir)
11
+
12
+ # Some adapter specs don't pass if there is not at least one commit in the
13
+ # repo since the git adapter short-circuits the key marshalling if there
14
+ # are no commits.
15
+ adapter.set('specs', 'running')
16
+ end
17
+
18
+ it_should_behave_like 'an adapter'
19
+
20
+ it 'should create a branch when it does not exist' do
21
+ adapter.options[:branch] = 'foobar'
22
+ adapter.set('foo', 'bar')
23
+ client.get_head('foobar').should_not be_nil
24
+ end
25
+
26
+ it 'should not raise error on clear when branch does not exist' do
27
+ client.git.fs_delete("refs/heads/#{adapter.branch}")
28
+ lambda { adapter.clear }.should_not raise_error
29
+ end
30
+
31
+ it 'should successfully delete a key' do
32
+ adapter.set('foo', 'bar')
33
+ adapter.delete('foo')
34
+ adapter.get('foo').should be_nil
35
+ end
36
+
37
+ it 'should not generate a commit message if there are no changes' do
38
+ adapter.clear
39
+ head = adapter.head.commit
40
+ adapter.clear
41
+ adapter.head.commit.id.should == head.id
42
+ end
43
+
44
+ context 'with the path option' do
45
+ before do
46
+ adapter.options[:path] = 'db/things'
47
+ end
48
+
49
+ it_should_behave_like 'an adapter'
50
+
51
+ it 'should store keys in the directory' do
52
+ adapter.set('foo', 'bar')
53
+ (adapter.head.commit.tree / 'foo').should be_nil
54
+ (adapter.head.commit.tree / 'db/things/foo').should_not be_nil
55
+ end
56
+
57
+ it 'should not clear other keys' do
58
+ other_adapter = Adapter[:git].new(client, :branch => 'adapter-git')
59
+ other_adapter.set('foo', 'bar')
60
+
61
+ adapter.set('foo', 'baz')
62
+ adapter.clear
63
+
64
+ other_adapter.get('foo').should == 'bar'
65
+ adapter.get('foo').should be_nil
66
+ end
67
+
68
+ it 'should not raise error on clear when branch does not exist' do
69
+ client.git.fs_delete("refs/heads/#{adapter.branch}")
70
+ lambda { adapter.clear }.should_not raise_error
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.expand_path('../../lib', __FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ Bundler.require(:default, :development)
7
+
8
+ require 'pathname'
9
+ require 'logger'
10
+
11
+ root_path = Pathname(__FILE__).dirname.join('..').expand_path
12
+ lib_path = root_path.join('lib')
13
+ log_path = root_path.join('log')
14
+ log_path.mkpath
15
+
16
+ require 'adapter/spec/an_adapter'
17
+ require 'adapter/spec/json_adapter'
18
+ require 'adapter/spec/types'
19
+
20
+ require 'adapter-git'
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adapter-git
3
+ version: !ruby/object:Gem::Version
4
+ hash: 11
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
+ platform: ruby
12
+ authors:
13
+ - Brandon Keepers
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-12 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: adapter
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 9
30
+ segments:
31
+ - 0
32
+ - 5
33
+ - 1
34
+ version: 0.5.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: grit
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 2
48
+ - 0
49
+ version: "2.0"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 2
63
+ - 0
64
+ version: "2.0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ description: Adapter for git
82
+ email:
83
+ - brandon@opensoul.org
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files: []
89
+
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE
94
+ - README.rdoc
95
+ - Rakefile
96
+ - adapter-git.gemspec
97
+ - examples/git.rb
98
+ - lib/adapter-git.rb
99
+ - lib/adapter/git.rb
100
+ - lib/adapter/git/version.rb
101
+ - spec/git_spec.rb
102
+ - spec/spec_helper.rb
103
+ has_rdoc: true
104
+ homepage: ""
105
+ licenses: []
106
+
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project:
133
+ rubygems_version: 1.6.1
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Adapter for git
137
+ test_files:
138
+ - spec/git_spec.rb
139
+ - spec/spec_helper.rb