Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add 'hash' API method.

  • Loading branch information...
commit 6ecfc87d70a5dd76c7c6779886646599d15e19ad 1 parent 8106286
@chrisrossi chrisrossi authored
Showing with 46 additions and 1 deletion.
  1. +29 −1 acidfs/__init__.py
  2. +16 −0 acidfs/tests.py
  3. +1 −0  docs/index.rst
View
30 acidfs/__init__.py
@@ -257,6 +257,17 @@ def open(self, path, mode='r', buffering=-1, encoding=None, errors=None,
raise ValueError("Bad mode: %s" % mode)
+ def hash(self, path=''):
+ """
+ Returns the sha1 hash of the object referred to by `path`. If `path` is
+ omitted the current working directory is used.
+ """
+ session = self._session()
+ obj = session.find(self._mkpath(path))
+ if not obj:
+ raise _NoSuchFileOrDirectory(path)
+ return obj.hash()
+
def listdir(self, path=''):
"""
Return list of files in indicated directory. If `path` is omitted, the
@@ -831,10 +842,12 @@ class _TreeNode(object):
parent = None
name = None
dirty = False
+ oid = None
@classmethod
def read(cls, db, oid):
node = cls(db)
+ node.oid = oid
contents = node.contents
with _popen(['git', 'ls-tree', oid],
stdout=subprocess.PIPE, cwd=db) as lstree:
@@ -902,6 +915,7 @@ def set(self, name, entry):
def set_dirty(self):
node = self
while node and not node.dirty:
+ node.oid = None
node.dirty = True
node = node.parent
@@ -930,6 +944,7 @@ def save(self):
proc.stdin.write(b'\n')
proc.stdin.close()
oid = proc.stdout.read().strip()
+ self.oid = _s(oid)
return oid
def empty(self):
@@ -938,6 +953,11 @@ def empty(self):
def __contains__(self, name):
return name in self.contents
+ def hash(self):
+ if not self.oid:
+ self.save()
+ return self.oid
+
class _Blob(object):
@@ -952,6 +972,9 @@ def find(self, path):
if not path:
return self
+ def hash(self):
+ return self.oid
+
class _NewBlob(io.RawIOBase):
@@ -978,7 +1001,7 @@ def close(self):
if retcode != 0:
raise subprocess.CalledProcessError(
retcode, 'git hash-object -w --stdin')
- self.parent.contents[self.name] = (b'blob', oid, None)
+ self.parent.contents[self.name] = (b'blob', _s(oid), None)
def writable(self):
return True
@@ -988,6 +1011,11 @@ def open(self):
return self.prev.open()
raise _NoSuchFileOrDirectory(_object_path(self))
+ def hash(self):
+ if self.prev:
+ return self.prev.hash()
+ raise _NoSuchFileOrDirectory(_object_path(self))
+
def find(self, path):
if not path:
return self
View
16 acidfs/tests.py
@@ -100,12 +100,20 @@ def assertDirectoryNotEmpty(self, path):
def test_read_write_file(self):
fs = self.make_one()
+ self.assertEqual(fs.hash(),
+ '4b825dc642cb6eb9a060e54bf8d69288fbee4904')
+ with self.assertNoSuchFileOrDirectory('foo'):
+ fs.hash('foo')
with fs.open('foo', 'wb') as f:
self.assertTrue(f.writable())
fprint(f, b'Hello')
with self.assertNoSuchFileOrDirectory('foo'):
fs.open('foo', 'rb')
+ with self.assertNoSuchFileOrDirectory('foo'):
+ fs.hash('foo')
self.assertEqual(fs.open('foo', 'rb').read(), b'Hello\n')
+ self.assertEqual(fs.hash('foo'),
+ 'e965047ad7c57865823c7d992b1d046ea66edf78')
actual_file = os.path.join(self.tmp, 'foo')
self.assertFalse(os.path.exists(actual_file))
transaction.commit()
@@ -139,10 +147,14 @@ def test_read_write_file_in_subfolder(self):
self.assertFalse(fs.isdir('foo'))
fs.mkdir('foo')
self.assertTrue(fs.isdir('foo'))
+ self.assertEqual(fs.hash('foo'),
+ '4b825dc642cb6eb9a060e54bf8d69288fbee4904')
with fs.open('foo/bar', 'wb') as f:
fprint(f, b'Hello')
with fs.open('foo/bar', 'rb') as f:
self.assertEqual(f.read(), b'Hello\n')
+ self.assertEqual(fs.hash('foo'),
+ 'c57c02051dae8e2e4803530c217ac38121f393d3')
actual_file = os.path.join(self.tmp, 'foo', 'bar')
self.assertFalse(os.path.exists(actual_file))
transaction.commit()
@@ -268,7 +280,11 @@ def test_modify_file(self):
with fs.open('foo', 'wb') as f:
fprint(f, b"Hello!")
self.assertEqual(fs.open('foo', 'rb').read(), b'Howdy!\n')
+ self.assertEqual(fs.hash('foo'),
+ 'c564dac563c1974addaa0ac0ae028fc92b2370f1')
self.assertEqual(fs.open('foo', 'rb').read(), b'Hello!\n')
+ self.assertEqual(fs.hash('foo'),
+ '10ddd6d257e01349d514541981aeecea6b2e741d')
self.assertEqual(open(path, 'rb').read(), b'Howdy!\n')
transaction.commit()
View
1  docs/index.rst
@@ -168,6 +168,7 @@ API
.. automethod:: empty
.. automethod:: get_base
.. automethod:: set_base
+ .. automethod:: hash
.. toctree::
:maxdepth: 2
Please sign in to comment.
Something went wrong with that request. Please try again.