diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-10-22 10:15:47 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-10-22 10:15:47 +0200 |
commit | 30d822a468dc909aac5c83d078a59bfc85fc27aa (patch) | |
tree | 41e79c0bd9fe8d47ed2791600e80f6d9f4494ec9 /lib/git/utils.py | |
parent | aa921fee6014ef43bb2740240e9663e614e25662 (diff) | |
download | gitpython-30d822a468dc909aac5c83d078a59bfc85fc27aa.tar.gz |
index writing now creates a sha on the content making it possible to write valid indices after manually removing or altering entriesgst
Diffstat (limited to 'lib/git/utils.py')
-rw-r--r-- | lib/git/utils.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/git/utils.py b/lib/git/utils.py index 2aa8d33e..cdc7c55b 100644 --- a/lib/git/utils.py +++ b/lib/git/utils.py @@ -6,6 +6,58 @@ import os +try: + import hashlib +except ImportError: + import sha + +def make_sha(source=''): + """ + A python2.4 workaround for the sha/hashlib module fiasco + + Note + From the dulwich project + """ + try: + return hashlib.sha1(source) + except NameError: + sha1 = sha.sha(source) + return sha1 + + +class SHA1Writer(object): + """ + Wrapper around a file-like object that remembers the SHA1 of + the data written to it. It will write a sha when the stream is closed + or if the asked for explicitly usign write_sha. + + Note: + Based on the dulwich project + """ + __slots__ = ("f", "sha1") + + def __init__(self, f): + self.f = f + self.sha1 = make_sha("") + + def write(self, data): + self.sha1.update(data) + self.f.write(data) + + def write_sha(self): + sha = self.sha1.digest() + self.f.write(sha) + return sha + + def close(self): + sha = self.write_sha() + self.f.close() + return sha + + def tell(self): + return self.f.tell() + + class LazyMixin(object): """ Base class providing an interface to lazily retrieve attribute values upon |