acts_as_git 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/acts_as_git.gemspec +1 -1
- data/lib/acts_as_git.rb +52 -28
- data/spec/acts_as_git_spec.rb +21 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9cfac7da6682c3e215f5a77c5bf0ca2c8f60621c
|
4
|
+
data.tar.gz: d52a96b7575d9db099a7b3652467b43cfdd44a46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1798aeeb52216cb6bf052226f049f9a10e3e2f5d738c6b964a72d7a77dbebda505895d8f6b7591abd0139d6f1abcea6b5863ca68668ba9064a407ede40b2397b
|
7
|
+
data.tar.gz: 01ae6282deebde107aaeb128a14c5dada7beabbf9294fc03ba608c52e6ff52633cb70572aa2be46e19de0f134181e52653305d9743dff8e41969a240f2d54322
|
data/README.md
CHANGED
@@ -24,7 +24,7 @@ end
|
|
24
24
|
post = Post.new # create the directory `self.repodir` if not exist, and init repo.
|
25
25
|
post.body = 'content'
|
26
26
|
post.save # save the content into the file of `#filename`
|
27
|
-
post.
|
27
|
+
post.current
|
28
28
|
=> COMMIT_HASH_HOGE
|
29
29
|
|
30
30
|
# load
|
@@ -39,7 +39,7 @@ post.is_changed?
|
|
39
39
|
post.save
|
40
40
|
post.is_changed?
|
41
41
|
=> false
|
42
|
-
post.
|
42
|
+
post.current
|
43
43
|
=> COMMIT_HASH_FUGA
|
44
44
|
puts post.body
|
45
45
|
=> 'content2'
|
data/acts_as_git.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.homepage = "https://github.com/rail44/acts_as_git"
|
9
9
|
s.summary = "Make your field act as a git repo"
|
10
10
|
s.description = "Make your field act as a git repo. Save the content to a file, and load the content from a file."
|
11
|
-
s.version = '0.
|
11
|
+
s.version = '0.2.0'
|
12
12
|
s.date = Time.now.strftime("%Y-%m-%d")
|
13
13
|
|
14
14
|
s.extra_rdoc_files = Dir["*.rdoc"]
|
data/lib/acts_as_git.rb
CHANGED
@@ -22,6 +22,11 @@ module ActsAsGit
|
|
22
22
|
@@username = username
|
23
23
|
end
|
24
24
|
|
25
|
+
@@remote = nil
|
26
|
+
def self.remote=(remote)
|
27
|
+
@@remote = remote
|
28
|
+
end
|
29
|
+
|
25
30
|
def get_option(index)
|
26
31
|
option = {}
|
27
32
|
option[:author] = { :email => @@email, :name => @@username, :time => Time.now }
|
@@ -36,13 +41,19 @@ module ActsAsGit
|
|
36
41
|
def acts_as_git(params = {})
|
37
42
|
self.class_eval do
|
38
43
|
unless method_defined?(:save_with_file)
|
39
|
-
|
40
|
-
|
44
|
+
repodir = self.repodir
|
45
|
+
FileUtils.mkdir_p(repodir)
|
46
|
+
begin
|
47
|
+
@@repo = Rugged::Repository.new(repodir)
|
48
|
+
rescue
|
49
|
+
Rugged::Repository.init_at(repodir)
|
50
|
+
@@repo = Rugged::Repository.new(repodir)
|
41
51
|
end
|
52
|
+
@@origin = @@repo.remotes['origin'] || @@repo.remotes.create('origin', @@remote) if @@remote
|
42
53
|
|
43
|
-
def
|
44
|
-
if @
|
45
|
-
@
|
54
|
+
def current
|
55
|
+
if @current
|
56
|
+
@current
|
46
57
|
else
|
47
58
|
(@@repo.empty?)? nil: @@repo.head.target
|
48
59
|
end
|
@@ -52,12 +63,31 @@ module ActsAsGit
|
|
52
63
|
(@is_changed)? true: false
|
53
64
|
end
|
54
65
|
|
55
|
-
|
56
|
-
(
|
66
|
+
define_method :path do |field|
|
67
|
+
filename = params[field].bind(self).call
|
68
|
+
File.join(self.class.repodir, filename)
|
69
|
+
end
|
70
|
+
|
71
|
+
define_method :log do |field|
|
72
|
+
filename = params[field].bind(self).call
|
73
|
+
walker = Rugged::Walker.new(@@repo)
|
74
|
+
walker.sorting(Rugged::SORT_DATE)
|
75
|
+
walker.push(@@repo.head.target)
|
76
|
+
commits = []
|
77
|
+
walker.map do |commit|
|
78
|
+
if commit.diff(paths: [path(field)])
|
79
|
+
commit
|
80
|
+
end
|
81
|
+
end
|
57
82
|
end
|
58
83
|
|
59
84
|
define_method :checkout do |commit|
|
60
|
-
@
|
85
|
+
@current = case commit
|
86
|
+
when Rugged::Commit
|
87
|
+
commit
|
88
|
+
when String
|
89
|
+
@current = (commit)? @@repo.lookup(commit): nil
|
90
|
+
end
|
61
91
|
@is_changed = false
|
62
92
|
params.each do |field, filename_instance_method|
|
63
93
|
field = nil
|
@@ -65,13 +95,14 @@ module ActsAsGit
|
|
65
95
|
self
|
66
96
|
end
|
67
97
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
@@repo
|
98
|
+
def self.sync
|
99
|
+
cred = Rugged::Credentials::SshKeyFromAgent.new(username: 'git')
|
100
|
+
@@origin.fetch(credentials: cred)
|
101
|
+
branch = @@repo.branches["master"]
|
102
|
+
@@repo.checkout('origin/master', :strategy => :force)
|
103
|
+
@@repo.branches.delete(branch)
|
104
|
+
@@repo.create_branch('master')
|
105
|
+
@@repo.checkout('master', :strategy => :force)
|
75
106
|
end
|
76
107
|
|
77
108
|
define_method(:save_with_file) do |*args|
|
@@ -83,20 +114,16 @@ module ActsAsGit
|
|
83
114
|
if repodir and filename and content
|
84
115
|
oid = @@repo.write(content, :blob)
|
85
116
|
index = @@repo.index
|
86
|
-
path =
|
117
|
+
path = path(field)
|
87
118
|
action = File.exists?(path)? 'Update': 'Create'
|
88
119
|
FileUtils.mkdir_p(File.dirname(path))
|
89
|
-
File.open(path, 'w') do |f|
|
90
|
-
f.write(content)
|
91
|
-
end
|
92
120
|
index.add(path: filename, oid: oid, mode: 0100644)
|
93
121
|
option = self.class.get_option(index)
|
94
122
|
option[:message] = "#{action} #{filename} for field #{field_name} of #{self.class.name}"
|
95
123
|
Rugged::Commit.create(@@repo, option)
|
96
|
-
@
|
124
|
+
@current = @@repo.head.target
|
125
|
+
@@repo.checkout('HEAD', :strategy => :force)
|
97
126
|
@is_changed = false
|
98
|
-
index.read_tree(@commit.tree)
|
99
|
-
index.write
|
100
127
|
instance_variable_set(field_name, nil)
|
101
128
|
end
|
102
129
|
end
|
@@ -125,7 +152,7 @@ module ActsAsGit
|
|
125
152
|
return nil unless repodir
|
126
153
|
return nil unless filename
|
127
154
|
return nil if @@repo.empty?
|
128
|
-
return nil unless fileob =
|
155
|
+
return nil unless fileob = current.tree.path(filename)
|
129
156
|
oid = fileob[:oid]
|
130
157
|
file = StringIO.new(@@repo.lookup(oid).content)
|
131
158
|
file.seek(offset) if offset
|
@@ -138,17 +165,14 @@ module ActsAsGit
|
|
138
165
|
field_name = :"@#{field}"
|
139
166
|
filename = filename_instance_method.bind(self).call
|
140
167
|
index = @@repo.index
|
141
|
-
path = self.class.path(filename)
|
142
|
-
File.unlink(path) if File.exists?(path)
|
143
168
|
begin
|
144
169
|
index.remove(filename)
|
145
170
|
option = self.class.get_option(index)
|
146
171
|
option[:message] = "Remove #{filename} for field #{field_name} of #{self.class.name}"
|
147
172
|
Rugged::Commit.create(@@repo, option)
|
148
|
-
@
|
173
|
+
@current = @@repo.head.target
|
174
|
+
@@repo.checkout('HEAD', :strategy => :force)
|
149
175
|
@is_changed = false
|
150
|
-
index.read_tree(@commit.tree)
|
151
|
-
index.write
|
152
176
|
rescue Rugged::IndexError => e
|
153
177
|
end
|
154
178
|
end
|
data/spec/acts_as_git_spec.rb
CHANGED
@@ -4,13 +4,28 @@ require_relative 'model'
|
|
4
4
|
describe ActsAsGit do
|
5
5
|
let(:subject) { TestPost.new }
|
6
6
|
after do
|
7
|
-
|
7
|
+
path = subject.path(:body)
|
8
|
+
File.unlink(path) if File.exist?(path)
|
8
9
|
end
|
9
10
|
|
10
11
|
after(:context) do
|
11
12
|
FileUtils.rm_rf(File.join(TestPost.repodir, '.git')) if Dir.exist?(File.join(TestPost.repodir, '.git'))
|
12
13
|
end
|
13
14
|
|
15
|
+
context '#log' do
|
16
|
+
let(:commits) { [] }
|
17
|
+
before { subject.body = 'aaaa' }
|
18
|
+
before { subject.save }
|
19
|
+
before { commits << subject.current }
|
20
|
+
before { subject.body = 'bbbb' }
|
21
|
+
before { subject.save }
|
22
|
+
before { commits << subject.current }
|
23
|
+
before { subject.body = 'cccc' }
|
24
|
+
before { subject.save }
|
25
|
+
before { commits << subject.current }
|
26
|
+
it { expect(subject.log(:body)).to be == commits.reverse }
|
27
|
+
end
|
28
|
+
|
14
29
|
context '#body=' do
|
15
30
|
it { expect { subject.body = 'aaaa' }.not_to raise_error }
|
16
31
|
end
|
@@ -37,7 +52,7 @@ describe ActsAsGit do
|
|
37
52
|
before do
|
38
53
|
subject.body = 'aaaa'
|
39
54
|
subject.save
|
40
|
-
@commit = subject.
|
55
|
+
@commit = subject.current
|
41
56
|
subject.body = 'bbbb'
|
42
57
|
subject.save
|
43
58
|
end
|
@@ -53,13 +68,13 @@ describe ActsAsGit do
|
|
53
68
|
context 'save if body exists' do
|
54
69
|
before { subject.body = 'aaaa' }
|
55
70
|
before { subject.save }
|
56
|
-
it { expect(File.read(
|
71
|
+
it { expect(File.read(subject.path(:body))).to eql('aaaa') }
|
57
72
|
end
|
58
73
|
|
59
74
|
context 'does not save if body does not exist' do
|
60
75
|
before { subject.body = nil }
|
61
76
|
before { subject.save }
|
62
|
-
it { expect(File.exist?(
|
77
|
+
it { expect(File.exist?(subject.path(:body))).to be_falsey }
|
63
78
|
end
|
64
79
|
end
|
65
80
|
|
@@ -68,12 +83,12 @@ describe ActsAsGit do
|
|
68
83
|
before { subject.body = 'aaaa' }
|
69
84
|
before { subject.save }
|
70
85
|
before { subject.destroy }
|
71
|
-
it { expect(File.exist?(
|
86
|
+
it { expect(File.exist?(subject.path(:body))).to be_falsey }
|
72
87
|
end
|
73
88
|
|
74
89
|
context 'fine even if file does not exist' do
|
75
90
|
before { subject.destroy }
|
76
|
-
it { expect(File.exist?(
|
91
|
+
it { expect(File.exist?(subject.path(:body))).to be_falsey }
|
77
92
|
end
|
78
93
|
end
|
79
94
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_git
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Satoshi Amemiya
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-07-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rugged
|