gitrb 0.1.3 → 0.1.4

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.
data/gitrb.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'gitrb'
3
- s.version = '0.1.3'
3
+ s.version = '0.1.4'
4
4
  s.summary = 'Pure ruby git implementation'
5
5
  s.author = 'Daniel Mendler'
6
6
  s.email = 'mail@daniel-mendler.de'
@@ -33,6 +33,7 @@ module Gitrb
33
33
  @logger = options[:logger] || Logger.new(nil)
34
34
  @encoding = options[:encoding] || DEFAULT_ENCODING
35
35
  @lock = {}
36
+ @transaction = Mutex.new
36
37
 
37
38
  @path = options[:path]
38
39
  @path.chomp!('/')
@@ -52,13 +53,16 @@ module Gitrb
52
53
 
53
54
  # Switch branch
54
55
  def branch=(branch)
55
- @branch = branch
56
- load
56
+ raise 'Forbidden from within a transaction' if in_transaction?
57
+ @transaction.synchronize do
58
+ @branch = branch
59
+ load
60
+ end
57
61
  end
58
62
 
59
63
  # Has our repository been changed on disk?
60
64
  def changed?
61
- head.nil? || head.id != read_head_id
65
+ !head || head.id != read_head_id
62
66
  end
63
67
 
64
68
  # Load the repository, if it has been changed on disk.
@@ -66,9 +70,18 @@ module Gitrb
66
70
  load if changed?
67
71
  end
68
72
 
73
+ # Clear cached objects
74
+ def clear
75
+ raise 'Forbidden from within a transaction' if in_transaction?
76
+ @transaction.synchronize do
77
+ @objects.clear
78
+ load
79
+ end
80
+ end
81
+
69
82
  # Is there any transaction going on?
70
83
  def in_transaction?
71
- @lock[Thread.current.object_id]
84
+ !!@lock[Thread.current.object_id]
72
85
  end
73
86
 
74
87
  # Diff
@@ -286,14 +299,6 @@ module Gitrb
286
299
  User.new(name, email)
287
300
  end
288
301
 
289
- def dup
290
- super.instance_eval do
291
- @objects = Trie.new
292
- load
293
- self
294
- end
295
- end
296
-
297
302
  private
298
303
 
299
304
  def check_git_version
@@ -327,6 +332,7 @@ module Gitrb
327
332
  # Tries to get lock on lock file, load the this repository if
328
333
  # has changed in the repository.
329
334
  def start_transaction
335
+ @transaction.lock
330
336
  file = File.open("#{head_path}.lock", 'w')
331
337
  file.flock(File::LOCK_EX)
332
338
  @lock[Thread.current.object_id] = file
@@ -349,6 +355,7 @@ module Gitrb
349
355
  @lock[Thread.current.object_id].close rescue nil
350
356
  @lock.delete(Thread.current.object_id)
351
357
  File.unlink("#{head_path}.lock") rescue nil
358
+ @transaction.unlock
352
359
  end
353
360
 
354
361
  def get_type(id, expected)
@@ -359,7 +366,7 @@ module Gitrb
359
366
 
360
367
  def load_packs
361
368
  @packs = Trie.new
362
- @objects = Trie.new
369
+ @objects = Synchronized.new(Trie.new)
363
370
 
364
371
  packs_path = "#{@path}/objects/pack"
365
372
  if File.directory?(packs_path)
@@ -382,7 +389,7 @@ module Gitrb
382
389
  @head = nil
383
390
  @root = Tree.new(:repository => self)
384
391
  end
385
- @logger.debug "gitrb: Reloaded, head is #{@head ? head.id : 'nil'}"
392
+ @logger.debug "gitrb: Reloaded, head is #{head ? head.id : 'nil'}"
386
393
  end
387
394
 
388
395
  # Returns the hash value of an object string.
@@ -425,6 +432,5 @@ module Gitrb
425
432
  def legacy_loose_object?(buf)
426
433
  buf[0].ord == 0x78 && ((buf[0].ord << 8) | buf[1].ord) % 31 == 0
427
434
  end
428
-
429
435
  end
430
436
  end
data/lib/gitrb/util.rb CHANGED
@@ -18,4 +18,26 @@ module Gitrb
18
18
  end
19
19
  end
20
20
  end
21
+
22
+ class Synchronized
23
+ def initialize(obj)
24
+ @obj = obj
25
+ @mutex = Mutex.new
26
+ end
27
+
28
+ def method_missing(*args)
29
+ @mutex.synchronize { @obj.send(*args) }
30
+ end
31
+ end
32
+ end
33
+
34
+ # str[0] returns a 1-char string in Ruby 1.9 but a
35
+ # Fixnum in 1.8. Monkeypatch a fix if we're on 1.8.
36
+ if !1.respond_to?(:ord)
37
+ class Fixnum
38
+ def ord
39
+ self
40
+ end
41
+ end
21
42
  end
43
+
data/lib/gitrb.rb CHANGED
@@ -4,6 +4,7 @@ require 'fileutils'
4
4
  require 'logger'
5
5
  require 'enumerator'
6
6
  require 'stringio'
7
+ require 'thread'
7
8
 
8
9
  require 'gitrb/util'
9
10
  require 'gitrb/gitobject'
@@ -17,13 +18,3 @@ require 'gitrb/pack'
17
18
  require 'gitrb/commit'
18
19
  require 'gitrb/trie'
19
20
  require 'gitrb/repository'
20
-
21
- # str[0] returns a 1-char string in Ruby 1.9 but a
22
- # Fixnum in 1.8. Monkeypatch a fix if we're on 1.8.
23
- if !1.respond_to?(:ord)
24
- class Fixnum
25
- def ord
26
- self
27
- end
28
- end
29
- end
@@ -151,12 +151,6 @@ describe Gitrb do
151
151
  repo.head.should_not be_nil
152
152
  end
153
153
 
154
- it 'should detect changes' do
155
- file 'a', 'Hello'
156
-
157
- repo.should be_changed
158
- end
159
-
160
154
  it 'should rollback a transaction' do
161
155
  file 'a/b', 'Hello'
162
156
  file 'c/d', 'World'
@@ -271,4 +265,33 @@ describe Gitrb do
271
265
  tag.message.should =~ /message/
272
266
  end
273
267
 
268
+ it 'should detect changes and refresh' do
269
+ file 'a', 'data'
270
+ repo.root['a'].should be_nil
271
+ repo.should be_changed
272
+ repo.refresh
273
+ repo.should_not be_changed
274
+ repo.root['a'].data.should == 'data'
275
+ end
276
+
277
+ it 'should clear cache' do
278
+ file 'a', 'data'
279
+ repo.refresh
280
+ repo.root['a'].data.should == 'data'
281
+ repo.clear
282
+ repo.root['a'].data.should == 'data'
283
+ end
284
+
285
+ it 'should forbid branch switching from within transaction' do
286
+ repo.transaction do
287
+ lambda { repo.branch = 'test' }.should raise_error(RuntimeError)
288
+ end
289
+ end
290
+
291
+ it 'should forbid clearing from within transaction' do
292
+ repo.transaction do
293
+ lambda { repo.clear }.should raise_error(RuntimeError)
294
+ end
295
+ end
296
+
274
297
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Daniel Mendler