Blob: vend raw or cooked content
Some blob representations require raw blob content, however, the 'blob' view is unconditionally handed cooked (utf8-encoded) content, thus representations which need raw content are forced to reload the blob in raw form, which is ugly and expensive. The ultimate goal is to eliminate the wasteful blob reloading when raw content is needed. Toward that end, teach Blob how to vend raw or cooked content. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
This commit is contained in:
parent
0ba89d75e6
commit
1d79988228
20
git.py
20
git.py
@ -339,12 +339,12 @@ class Repo:
|
|||||||
ref = self.branch
|
ref = self.branch
|
||||||
return Tree(self, ref)
|
return Tree(self, ref)
|
||||||
|
|
||||||
def blob(self, path, ref = None, raw = False):
|
def blob(self, path, ref = None):
|
||||||
"""Returns a Blob instance for the given path."""
|
"""Returns a Blob instance for the given path."""
|
||||||
if not ref:
|
if not ref:
|
||||||
ref = self.branch
|
ref = self.branch
|
||||||
cmd = self.cmd('cat-file')
|
cmd = self.cmd('cat-file')
|
||||||
cmd.raw(raw)
|
cmd.raw(True)
|
||||||
cmd.batch = None
|
cmd.batch = None
|
||||||
|
|
||||||
if isinstance(ref, unicode):
|
if isinstance(ref, unicode):
|
||||||
@ -356,7 +356,7 @@ class Repo:
|
|||||||
if not head or head.strip().endswith('missing'):
|
if not head or head.strip().endswith('missing'):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return Blob(out.read(), raw)
|
return Blob(out.read())
|
||||||
|
|
||||||
def last_commit_timestamp(self):
|
def last_commit_timestamp(self):
|
||||||
"""Return the timestamp of the last commit."""
|
"""Return the timestamp of the last commit."""
|
||||||
@ -559,8 +559,12 @@ class Tree:
|
|||||||
class Blob:
|
class Blob:
|
||||||
"""A git blob."""
|
"""A git blob."""
|
||||||
|
|
||||||
def __init__(self, content, raw):
|
def __init__(self, raw_content):
|
||||||
if raw:
|
self.raw_content = raw_content
|
||||||
self.raw_content = content
|
self._utf8_content = None
|
||||||
else:
|
|
||||||
self.utf8_content = content
|
@property
|
||||||
|
def utf8_content(self):
|
||||||
|
if not self._utf8_content:
|
||||||
|
self._utf8_content = self.raw_content.decode('utf8', 'replace')
|
||||||
|
return self._utf8_content
|
||||||
|
4
utils.py
4
utils.py
@ -102,9 +102,9 @@ def embed_image_blob(repo, dirname, fname):
|
|||||||
mimetype = mimetypes.guess_type(fname)[0]
|
mimetype = mimetypes.guess_type(fname)[0]
|
||||||
|
|
||||||
# Unfortunately, bottle seems to require utf-8 encoded data.
|
# Unfortunately, bottle seems to require utf-8 encoded data.
|
||||||
# We have to refetch the blob with raw=True, because the utf-8 encoded
|
# We have to refetch the blob as raw data, because the utf-8 encoded
|
||||||
# version of the blob available in the bottle template discards binary data.
|
# version of the blob available in the bottle template discards binary data.
|
||||||
raw_blob = repo.blob(dirname + fname, raw = True)
|
raw_blob = repo.blob(dirname + fname)
|
||||||
|
|
||||||
return '<img style="max-width:100%;" src="data:{0};base64,{1}" />'.format( \
|
return '<img style="max-width:100%;" src="data:{0};base64,{1}" />'.format( \
|
||||||
mimetype, base64.b64encode(raw_blob.raw_content))
|
mimetype, base64.b64encode(raw_blob.raw_content))
|
||||||
|
Loading…
Reference in New Issue
Block a user