eternity 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6a63e70964889425618e513c2b015c4dab49df29
4
- data.tar.gz: 9469a0aaef5ade727668538d82037bf6b37e2df7
3
+ metadata.gz: 9bb28d44f6a6270e1aa8d7da6b66067d5a097dbb
4
+ data.tar.gz: 1b316ac4f934458b9807717928f8bfd093453a1b
5
5
  SHA512:
6
- metadata.gz: c4ef8f7b20359f19675244b29e75d72fcd994af5c87bad10db3a1557177a460684d70295a0a173f1edabf12693ae7697f9beb557f88540a7c3c139627e86ed6e
7
- data.tar.gz: 0a7f43754360afb66f62dc9c7486baa455cb7c6513fee2c5ec62096f702bcd6a19bf19ca0cf31a9a4bc42c594447896a9583571168cff4b658d7dc455eae6acb
6
+ metadata.gz: 4dc81d334ca41182d027060ec32d1761856f80f8ca5de8e1ad79c5aca4de91026ce439a7ee9d68da1ed32d1a833021588a65653af195881d7da5f3f3af531fcc
7
+ data.tar.gz: 72308247b234e76c85ad6428860914ed8cce6bc54c073de199c41bb94354d984542baad5dd28a28a3e577ed699ea04a1fc0a6e22f275aa8a491a7e4dcdbafb3d
@@ -12,7 +12,7 @@ module Eternity
12
12
  end
13
13
 
14
14
  def time
15
- Time.parse(data['time']) if data['time']
15
+ Time.parse data['time'] if data['time']
16
16
  end
17
17
 
18
18
  def author
@@ -46,10 +46,6 @@ module Eternity
46
46
  Commit.new data['base']
47
47
  end
48
48
 
49
- def base_delta
50
- data['base_delta'] ? Blob.read(:delta, data['base_delta']) : {}
51
- end
52
-
53
49
  def history_times
54
50
  data['history_times'] ? Blob.read(:history_times, data['history_times']) : {}
55
51
  end
@@ -65,14 +61,6 @@ module Eternity
65
61
  history_times.key? commit.id
66
62
  end
67
63
 
68
- def base_history_at(commit)
69
- return [] unless commit
70
- history = [base]
71
- history += base.base_history_at commit unless base.id == commit.id
72
- raise "History not include commit #{commit.id}" unless history.map(&:id).include? commit.id
73
- history
74
- end
75
-
76
64
  def first?
77
65
  parent_ids.compact.empty?
78
66
  end
@@ -91,6 +79,14 @@ module Eternity
91
79
  end
92
80
  alias_method :eql?, :==
93
81
 
82
+ def hash
83
+ id.hash
84
+ end
85
+
86
+ def to_s
87
+ "#{time} - #{short_id} - #{author}: #{message}"
88
+ end
89
+
94
90
  def self.create(options)
95
91
  raise 'Author must be present' if options[:author].to_s.strip.empty?
96
92
  raise 'Message must be present' if options[:message].to_s.strip.empty?
@@ -109,7 +105,6 @@ module Eternity
109
105
  index: options.fetch(:index),
110
106
  delta: options.fetch(:delta),
111
107
  base: options[:parents].count == 2 ? options.fetch(:base) : options[:parents].first,
112
- base_delta: options[:parents].count == 2 ? options.fetch(:base_delta) : options.fetch(:delta),
113
108
  history_times: Blob.write(:history_times, history_times)
114
109
  }
115
110
 
@@ -24,6 +24,11 @@ module Eternity
24
24
  end
25
25
  end
26
26
 
27
+ def between(commit_1, commit_2)
28
+ commits = ([commit_2] + commit_2.history) - ([commit_1] + commit_1.history)
29
+ merge commits.reverse.map(&:delta)
30
+ end
31
+
27
32
  def revert(delta, commit)
28
33
  commit.with_index do |index|
29
34
  delta.each_with_object({}) do |(collection, changes), hash|
@@ -26,22 +26,6 @@ module Eternity
26
26
  @delta ||= TransparentProxy.new { calculate_delta }
27
27
  end
28
28
 
29
- private
30
-
31
- def current_delta
32
- @current_delta ||= base_delta_of current_commit, base_commit
33
- end
34
-
35
- def target_delta
36
- @target_delta ||= base_delta_of target_commit, base_commit
37
- end
38
-
39
- def base_delta_of(commit, base)
40
- return {} if commit == base
41
- history = [commit] + commit.base_history_at(base)[0..-2]
42
- Delta.merge history.reverse.map(&:base_delta)
43
- end
44
-
45
29
  end
46
30
 
47
31
 
@@ -62,6 +46,14 @@ module Eternity
62
46
 
63
47
  private
64
48
 
49
+ def current_delta
50
+ @current_delta ||= Delta.between target_commit, current_commit
51
+ end
52
+
53
+ def target_delta
54
+ @target_delta ||= Delta.between current_commit, target_commit
55
+ end
56
+
65
57
  def calculate_delta
66
58
  return {} if merged?
67
59
 
@@ -106,7 +98,6 @@ module Eternity
106
98
  end
107
99
 
108
100
  log :calculate_delta
109
-
110
101
  end
111
102
 
112
103
 
@@ -117,12 +108,38 @@ module Eternity
117
108
 
118
109
  private
119
110
 
111
+ def current_delta
112
+ @current_delta ||= Delta.between base_commit, current_commit
113
+ end
114
+
115
+ def target_delta
116
+ @target_delta ||= Delta.between base_commit, target_commit
117
+ end
118
+
119
+ def diff_delta
120
+ @diff_delta ||= Delta.merge [Delta.revert(current_delta, base_commit), target_delta]
121
+ end
122
+
120
123
  def calculate_delta
121
- Delta.merge [Delta.revert(current_delta, base_commit), target_delta]
124
+ target_commit.with_index do |target_index|
125
+ diff_delta.each_with_object({}) do |(collection, elements), hash|
126
+ hash[collection] = {}
127
+
128
+ elements.each do |id, change|
129
+ if change['data']
130
+ sha1 = Blob.digest(Blob.serialize(change['data']))
131
+ change['data'] = target_index[collection][id].data if target_index[collection].include?(id) && sha1 != target_index[collection][id].sha1
132
+ end
133
+
134
+ hash[collection][id] = change if change
135
+ end
136
+
137
+ hash.delete collection if hash[collection].empty?
138
+ end
139
+ end
122
140
  end
123
141
 
124
142
  log :calculate_delta
125
-
126
143
  end
127
144
 
128
145
  end
@@ -189,7 +189,7 @@ module Eternity
189
189
 
190
190
  raise 'Already merged' if patch.merged?
191
191
 
192
- commit! message: "Merge #{target_commit.short_id} into #{current_commit.short_id}",
192
+ commit! message: "Merge #{target_commit.short_id} into #{current_commit.short_id} (#{name})",
193
193
  author: 'System',
194
194
  parents: [current_commit.id, target_commit.id],
195
195
  index: write_index(patch.delta),
@@ -1,3 +1,3 @@
1
1
  module Eternity
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -27,6 +27,7 @@ describe Repository, 'Commit' do
27
27
  current_commit.wont_be :merge?
28
28
  current_commit.delta.must_equal 'countries' => {'AR' => {'action' => 'insert', 'data' => {'name' => 'Argentina'}}}
29
29
  current_commit.must_equal_index 'countries' => {'AR' => digest(name: 'Argentina')}
30
+ current_commit.to_s.must_equal "#{current_commit.time} - #{current_commit.short_id} - #{current_commit.author}: #{current_commit.message}"
30
31
  end
31
32
  end
32
33
 
@@ -42,7 +43,7 @@ describe Repository, 'Commit' do
42
43
  current_commit.time.must_be_instance_of Time
43
44
  current_commit.author.must_equal 'User'
44
45
  current_commit.message.must_equal 'Commit 2'
45
- current_commit.parent_ids.must_equal [commit_1.id]
46
+ current_commit.parents.must_equal [commit_1]
46
47
  current_commit.wont_be :first?
47
48
  current_commit.wont_be :merge?
48
49
  current_commit.delta.must_equal 'countries' => {'UY' => {'action' => 'insert', 'data' => {'name' => 'Uruguay'}}}
@@ -50,6 +51,7 @@ describe Repository, 'Commit' do
50
51
  'AR' => digest(name: 'Argentina'),
51
52
  'UY' => digest(name: 'Uruguay')
52
53
  }
54
+ current_commit.to_s.must_equal "#{current_commit.time} - #{current_commit.short_id} - #{current_commit.author}: #{current_commit.message}"
53
55
  end
54
56
  end
55
57
 
@@ -1,12 +1,12 @@
1
1
  require 'minitest_helper'
2
2
 
3
- describe Repository, 'Pull' do
3
+ describe 'Delta' do
4
4
 
5
5
  let(:repo_1) { Repository.new :test_1 }
6
6
  let(:repo_2) { Repository.new :test_2 }
7
7
  let(:repo_3) { Repository.new :test_3 }
8
8
 
9
- it 'test' do
9
+ it 'Merge and Checkout' do
10
10
  repo_1[:countries].insert 'AR', name: 'Argentina'
11
11
  commit_1 = repo_1.commit author: 'User 1', message: 'Commit 1'
12
12
  repo_1.push
@@ -107,4 +107,113 @@ describe Repository, 'Pull' do
107
107
  }
108
108
  end
109
109
 
110
+ it 'Commit -> Pull -> Push (multiple times)' do
111
+ repo_1[:countries].insert 'AR', name: 'Argentina'
112
+ repo_1.commit author: 'User 1', message: 'Added Argentina'
113
+ repo_1.push
114
+
115
+ delta = repo_2.pull
116
+ delta.must_equal 'countries' => {
117
+ 'AR' => {'action' => 'insert', 'data' => {'name' => 'Argentina'}}
118
+ }
119
+
120
+ repo_2.current_commit.must_equal_index 'countries' => {
121
+ 'AR' => digest(name: 'Argentina')
122
+ }
123
+
124
+ repo_2[:countries].insert 'BR', name: 'Brasil'
125
+ repo_2.commit author: 'User 2', message: 'Added Brasil'
126
+
127
+ delta = repo_2.pull
128
+ delta.must_be_empty
129
+
130
+ repo_2.current_commit.must_equal_index 'countries' => {
131
+ 'AR' => digest(name: 'Argentina'),
132
+ 'BR' => digest(name: 'Brasil')
133
+ }
134
+
135
+ repo_2.push
136
+
137
+ repo_1[:countries].insert 'UY', name: 'Uruguay'
138
+ repo_1.commit author: 'User 1', message: 'Added Uruguay'
139
+
140
+ delta = repo_1.pull
141
+ delta.must_equal 'countries' => {
142
+ 'BR' => {'action' => 'insert', 'data' => {'name' => 'Brasil'}}
143
+ }
144
+
145
+ repo_1.current_commit.must_equal_index 'countries' => {
146
+ 'AR' => digest(name: 'Argentina'),
147
+ 'BR' => digest(name: 'Brasil'),
148
+ 'UY' => digest(name: 'Uruguay')
149
+ }
150
+
151
+ repo_1.push
152
+
153
+ repo_2[:countries].insert 'CL', name: 'Chile'
154
+ repo_2.commit author: 'User 2', message: 'Added Chile'
155
+
156
+ delta = repo_2.pull
157
+ delta.must_equal 'countries' => {
158
+ 'UY' => {'action' => 'insert', 'data' => {'name' => 'Uruguay'}}
159
+ }
160
+
161
+ repo_2.current_commit.must_equal_index 'countries' => {
162
+ 'AR' => digest(name: 'Argentina'),
163
+ 'BR' => digest(name: 'Brasil'),
164
+ 'UY' => digest(name: 'Uruguay'),
165
+ 'CL' => digest(name: 'Chile')
166
+ }
167
+
168
+ repo_2.push
169
+
170
+ repo_1[:countries].update 'UY', name: 'Republica Oriental del Uruguay'
171
+ repo_1.commit author: 'User 1', message: 'Updated Uruguay'
172
+
173
+ delta = repo_1.pull
174
+ delta.must_equal 'countries' => {
175
+ 'CL' => {'action' => 'insert', 'data' => {'name' => 'Chile'}}
176
+ }
177
+
178
+ repo_1.current_commit.must_equal_index 'countries' => {
179
+ 'AR' => digest(name: 'Argentina'),
180
+ 'BR' => digest(name: 'Brasil'),
181
+ 'UY' => digest(name: 'Republica Oriental del Uruguay'),
182
+ 'CL' => digest(name: 'Chile')
183
+ }
184
+
185
+ repo_1.push
186
+
187
+ repo_2[:countries].delete 'CL'
188
+ repo_2.commit author: 'User 2', message: 'Deleted Chile'
189
+
190
+ delta = repo_2.pull
191
+ delta.must_equal 'countries' => {
192
+ 'UY' => {'action' => 'update', 'data' => {'name' => 'Republica Oriental del Uruguay'}}
193
+ }
194
+
195
+ repo_2.current_commit.must_equal_index 'countries' => {
196
+ 'AR' => digest(name: 'Argentina'),
197
+ 'BR' => digest(name: 'Brasil'),
198
+ 'UY' => digest(name: 'Republica Oriental del Uruguay')
199
+ }
200
+
201
+ repo_2.push
202
+
203
+ repo_1[:countries].insert 'CO', name: 'Colombia'
204
+ repo_1.commit author: 'User 1', message: 'Added Colombia'
205
+
206
+ delta = repo_1.pull
207
+ delta.must_equal 'countries' => {
208
+ 'CL' => {'action' => 'delete'}
209
+ }
210
+
211
+ repo_1.current_commit.must_equal_index 'countries' => {
212
+ 'AR' => digest(name: 'Argentina'),
213
+ 'BR' => digest(name: 'Brasil'),
214
+ 'UY' => digest(name: 'Republica Oriental del Uruguay'),
215
+ 'CO' => digest(name: 'Colombia')
216
+ }
217
+ end
218
+
110
219
  end
@@ -20,6 +20,14 @@ Eternity.configure do |config|
20
20
  config.logger.level = Logger::ERROR
21
21
  end
22
22
 
23
+ class Time
24
+ @last = Time.now
25
+
26
+ def self.now
27
+ @last += 1
28
+ end
29
+ end
30
+
23
31
  class Minitest::Spec
24
32
  def redis
25
33
  Eternity.redis
@@ -111,12 +111,8 @@ describe Repository, 'Pull' do
111
111
 
112
112
  repository.current_commit.tap do |commit|
113
113
  commit.must_be :merge?
114
- commit.parent_ids.must_equal [commit_4.id, commit_3.id]
114
+ commit.parents.must_equal [commit_4, commit_3]
115
115
  commit.base.id.must_equal commit_1.id
116
- commit.base_delta.must_equal 'countries' => {
117
- 'BR' => {'action' => 'insert', 'data' => {'name' => 'Brasil'}},
118
- 'UY' => {'action' => 'insert', 'data' => {'name' => 'Uruguay'}}
119
- }
120
116
  commit.delta.must_be_empty
121
117
  commit.must_equal_index 'countries' => {
122
118
  'AR' => digest(name: 'Argentina'),
@@ -150,9 +146,8 @@ describe Repository, 'Pull' do
150
146
 
151
147
  repository.current_commit.tap do |commit|
152
148
  commit.must_be :merge?
153
- commit.parent_ids.must_equal [commit_3.id, commit_2.id]
149
+ commit.parents.must_equal [commit_3, commit_2]
154
150
  commit.base.id.must_equal commit_1.id
155
- commit.base_delta.must_equal 'countries' => {'AR' => {'action' => 'update', 'data' => {'name' => 'Argentina 2'}}}
156
151
  commit.delta.must_be_empty
157
152
  commit.must_equal_index 'countries' => {'AR' => digest(name: 'Argentina 2')}
158
153
  end
@@ -182,9 +177,8 @@ describe Repository, 'Pull' do
182
177
 
183
178
  repository.current_commit.tap do |commit|
184
179
  commit.must_be :merge?
185
- commit.parent_ids.must_equal [commit_3.id, commit_2.id]
180
+ commit.parents.must_equal [commit_3, commit_2]
186
181
  commit.base.id.must_equal commit_1.id
187
- commit.base_delta.must_equal 'countries' => {'AR' => {'action' => 'update', 'data' => {'name' => 'Argentina', 'code' => 'ARG', 'number' => 54}}}
188
182
  commit.delta.must_be_empty
189
183
  commit.must_equal_index 'countries' => {'AR' => digest(name: 'Argentina', code: 'ARG', number: 54)}
190
184
  end
@@ -214,9 +208,8 @@ describe Repository, 'Pull' do
214
208
 
215
209
  repository.current_commit.tap do |commit|
216
210
  commit.must_be :merge?
217
- commit.parent_ids.must_equal [commit_3.id, commit_2.id]
211
+ commit.parents.must_equal [commit_3, commit_2]
218
212
  commit.base.id.must_equal commit_1.id
219
- commit.base_delta.must_equal 'countries' => {'X' => {'action' => 'insert', 'data' => {'name' => 'X1', 'number' => 2, 'code' => 1}}}
220
213
  commit.delta.must_be_empty
221
214
  commit.must_equal_index 'countries' => {
222
215
  'AR' => digest(name: 'Argentina'),
@@ -249,9 +242,8 @@ describe Repository, 'Pull' do
249
242
 
250
243
  repository.current_commit.tap do |commit|
251
244
  commit.must_be :merge?
252
- commit.parent_ids.must_equal [commit_3.id, commit_2.id]
245
+ commit.parents.must_equal [commit_3, commit_2]
253
246
  commit.base.id.must_equal commit_1.id
254
- commit.base_delta.must_equal 'countries' => {'AR' => {'action' => 'delete'}}
255
247
  commit.delta.must_be_empty
256
248
  commit.must_have_empty_index
257
249
  end
@@ -281,9 +273,8 @@ describe Repository, 'Pull' do
281
273
 
282
274
  repository.current_commit.tap do |commit|
283
275
  commit.must_be :merge?
284
- commit.parent_ids.must_equal [commit_3.id, commit_2.id]
276
+ commit.parents.must_equal [commit_3, commit_2]
285
277
  commit.base.id.must_equal commit_1.id
286
- commit.base_delta.must_equal 'countries' => {'AR' => {'action' => 'update', 'data' => {'name' => 'Argentina', 'code' => 'ARG'}}}
287
278
  commit.delta.must_be_empty
288
279
  commit.must_equal_index 'countries' => {'AR' => digest(name: 'Argentina', code: 'ARG')}
289
280
  end
@@ -66,7 +66,7 @@ describe Repository, 'Push' do
66
66
  other_repository.push
67
67
 
68
68
  Branch[:master].id.must_equal other_repository.current_commit.id
69
- Branch[:master].parent_ids.must_equal [commit_2.id, commit_1.id]
69
+ Branch[:master].parents.must_equal [commit_2, commit_1]
70
70
  end
71
71
 
72
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eternity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-16 00:00:00.000000000 Z
11
+ date: 2015-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redic
@@ -247,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
247
  version: '0'
248
248
  requirements: []
249
249
  rubyforge_project:
250
- rubygems_version: 2.2.2
250
+ rubygems_version: 2.4.3
251
251
  signing_key:
252
252
  specification_version: 4
253
253
  summary: Distributed database version control system