acts_as_git 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57d28b8bef810b5e765dbb866cfe9bcb62ae2a93
4
- data.tar.gz: 8b289a71897459306e711a6b89ee58574736c6f5
3
+ metadata.gz: 9cfac7da6682c3e215f5a77c5bf0ca2c8f60621c
4
+ data.tar.gz: d52a96b7575d9db099a7b3652467b43cfdd44a46
5
5
  SHA512:
6
- metadata.gz: 7cdcc4881e2072c571d229daf2b6c59a88ed8948effd5ebf64fc578a9076cababfd1ba00b646df77a6eabd9ddd02e88d8286f923eedaa13fdb0ca307cc28a1ac
7
- data.tar.gz: 982b943454ed3e833326b05018f1cf79f8ac3ee8e72977cf6a01b9e94062fa374f4de9e32881c8f9fa0494544431878f853b6535ff7c20f07968e1301faca5d3
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.get_commit
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.get_commit
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.1.1'
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
- def self.path(filename)
40
- File.join(repodir, filename)
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 commit
44
- if @commit
45
- @commit
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
- def get_commit
56
- (commit)? commit.oid: nil
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
- @commit = (commit)? @@repo.lookup(commit): nil
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
- repodir = self.repodir
69
- FileUtils.mkdir_p(repodir)
70
- begin
71
- @@repo = Rugged::Repository.new(repodir)
72
- rescue
73
- Rugged::Repository.init_at(repodir)
74
- @@repo = Rugged::Repository.new(repodir)
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 = self.class.path(filename)
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
- @commit = @@repo.head.target
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 = commit.tree.path(filename)
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
- @commit = @@repo.head.target
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
@@ -4,13 +4,28 @@ require_relative 'model'
4
4
  describe ActsAsGit do
5
5
  let(:subject) { TestPost.new }
6
6
  after do
7
- File.unlink(TestPost.path subject.filename) if File.exist?(TestPost.path subject.filename)
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.get_commit
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(TestPost.path(subject.filename))).to eql('aaaa') }
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?(TestPost.path(subject.filename))).to be_falsey }
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?(TestPost.path(subject.filename))).to be_falsey }
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?(TestPost.path(subject.filename))).to be_falsey }
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.1.1
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-06-30 00:00:00.000000000 Z
12
+ date: 2014-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rugged