Permalink
Browse files

Tree was not detecting updates properly.

  • Loading branch information...
1 parent dc2148a commit 0a09851e3f62d17232b7e7db17547785dadd1195 @georgi georgi committed May 5, 2009
Showing with 52 additions and 76 deletions.
  1. +19 −24 README.md
  2. +1 −2 lib/git_store.rb
  3. +1 −17 lib/git_store/tree.rb
  4. +10 −1 test/commit_spec.rb
  5. +21 −14 test/git_store_spec.rb
  6. +0 −18 test/tree_spec.rb
View
@@ -10,11 +10,6 @@ in-memory representation, which can be modified and finally committed.
GitStore supports transactions, so that updates to the store either
fail or succeed completely.
-GitStore manages concurrent access by a file locking scheme. So only
-one process can start a transaction at one time. This is implemented
-by locking the `refs/head/<branch>.lock` file, which is also
-respected by the git binary.
-
### Installation
GitStore can be installed as gem easily:
@@ -41,22 +36,17 @@ storage you can use the 'yml' extension:
store.commit 'Added user and page'
- # Note, that directories will be created automatically.
- # Another way to access a path is:
-
- store['config', 'wiki.yml'] = { 'name' => 'My Personal Wiki' }
-
- # Finally you can access the git store as a Hash of Hashes, but in
- # this case you have to create the Tree objects manually:
-
- puts store['users']['wiki.yml']['name']
-
-
### Transactions
-If you access the repository from different processes, you should
-write to your store using transactions. If something goes wrong inside
-a transaction, all changes will be rolled back to the original state.
+GitStore manages concurrent access by a file locking scheme. So only
+one process can start a transaction at one time. This is implemented
+by locking the `refs/head/<branch>.lock` file, which is also
+respected by the git binary.
+
+If you access the repository from different processes or threads, you
+should write to the store using transactions. If something goes wrong
+inside a transaction, all changes will be rolled back to the original
+state.
store = GitStore.new('/path/to/repo')
@@ -65,15 +55,17 @@ a transaction, all changes will be rolled back to the original state.
store['pages/home.yml'] = Page.new('matthias', 'Home')
end
- # transaction without a block
+
+A transaction without a block looks like this:
store.start_transaction
store['pages/home.yml'] = Page.new('matthias', 'Home')
store.rollback # This will restore the original state
-### Where is my data?
+
+### Data Storage
When you call the `commit` method, your data is written back straight
into the git repository. No intermediate file representation. So if
@@ -82,6 +74,7 @@ you want to have a look at your data, you can use a git browser like
$ git checkout
+
### Iteration
Iterating over the data objects is quite easy. Furthermore you can
@@ -91,11 +84,13 @@ pages of a wiki:
store['pages/home.yml'] = Page.new('matthias', 'Home')
store['pages/about.yml'] = Page.new('matthias', 'About')
- store['pages/links.yml'] = WikiPage.new('matthias', 'Links')
store['config/wiki.yml'] = { 'name' => 'My Personal Wiki' }
- store.each { |obj| ... } # yields all pages and the config file
- store['pages'].each { |page| ... } # yields only the pages
+ # Enumerate all objects
+ store.each { |obj| ... }
+
+ # Enumerate only pages
+ store['pages'].each { |page| ... }
### Serialization
View
@@ -150,7 +150,7 @@ def load(from_disk = false)
@root = get(@head.tree)
end
- load_from_disk
+ load_from_disk if from_disk
end
def load_from_disk
@@ -224,7 +224,6 @@ def rollback
#
# Release the lock file.
def finish_transaction
-
Thread.current['git_store_lock'].close rescue nil
Thread.current['git_store_lock'] = nil
View
@@ -17,7 +17,7 @@ def initialize(store, id = nil, data = nil)
# Has this tree been modified?
def modified?
- @modified or trees.any? { |tree| tree.modified? }
+ @modified or @table.values.any? { |entry| Tree === entry and entry.modified? }
end
# Find or create a subtree with specified name.
@@ -159,22 +159,6 @@ def values
map { |path, data| data }
end
- def blobs
- values.select { |entry| Blob === entry }
- end
-
- def trees
- values.select { |entry| Tree === entry }
- end
-
- def diff(other)
- add = table.select { |k, v| not other.table[k] }
- del = other.table.select { |k, v| not table[k] }
- chg = table.select { |k, v| o = other.table[k] and o.id != v.id }
-
- return add, del, chg
- end
-
# Convert this tree into a hash object.
def to_hash
@table.inject({}) do |hash, (name, entry)|
View
@@ -67,7 +67,16 @@
b = store.commit
- pp b.diff(a)
+ diff = b.diff(a)
+
+ diff[0].a_path.should == 'x'
+ diff[0].deleted_file.should be_true
+
+ diff[1].a_path.should == 'y'
+ diff[1].diff.should == "--- a/y\n+++ b/y\n@@ -1,4 +1,4 @@\n \n First Line.\n-Second Line.\n Last Line.\n+Another Line."
+
+ diff[2].a_path.should == 'z'
+ diff[2].new_file.should be_true
end
end
@@ -82,6 +82,26 @@ def file(file, data)
store['x/a.yml'].should == [1,2,3,4,5]
end
+ it 'should detect modification' do
+ store.transaction do
+ store['x/a'] = 'a'
+ end
+
+ store.load
+
+ store['x/a'].should == 'a'
+
+ store.transaction do
+ store['x/a'] = 'b'
+ store['x'].should be_modified
+ store.root.should be_modified
+ end
+
+ store.load
+
+ store['x/a'].should == 'b'
+ end
+
it 'should resolve paths' do
file 'x/a', 'Hello'
file 'y/b', 'World'
@@ -188,7 +208,7 @@ def file(file, data)
end
store['a/b'] = 'Changed'
end
-
+
sleep 0.01 until ready
store.load
@@ -216,17 +236,4 @@ def file(file, data)
store.commits[1].message.should == 'added a'
end
- it "should load file histories" do
- store['a'] = 'a'
- store.commit 'added a'
-
- store['b'] = 'b'
- store.commit 'added b'
-
- store['b'] = 'c'
- store.commit 'changed b'
-
- store.history('b').should == ['c', 'b', nil]
- end
-
end
View
@@ -89,22 +89,4 @@
["100644", "blob", "63d8dbd40c23542e740659a7168a0ce3138ea748", "b"],
["100644", "blob", "3410062ba67c5ed59b854387a8bc0ec012479368", "c"]]
end
-
- it "should diff two trees" do
- a = GitStore::Tree.new(store)
- b = GitStore::Tree.new(store)
-
- a['x'] = 'a'
- a['y'] = 'b'
- a['z'] = 'c'
-
- b['x'] = 'a'
- b['k'] = 'x'
- b['z'] = 'x'
-
- b.diff(a).should ==
- [[['k', b.table['k']]],
- [['y', a.table['y']]],
- [['z', b.table['z']]]]
- end
end

0 comments on commit 0a09851

Please sign in to comment.