From 58037e57c591b1c55594a0adb637d1880bfacaee Mon Sep 17 00:00:00 2001 From: Eric Sunshine Date: Tue, 13 Jan 2015 04:57:12 -0500 Subject: Repo.blob: respect reported blob size Batch output of git-cat-file has the form: SP SP LF LF It unconditionally includes a trailing line-feed which Repo.blob() incorrectly returns as part of blob content. For textual blobs, this extra character is often benign, however, for binary blobs, it can easily change the meaning of the data in unexpected or disastrous ways. Fix this by respecting the blob size reported by git-cat-file. (The alternate approach of unconditionally dropping the final LF also works, however, respecting the reported size is perhaps a bit more robust and "correct".) Signed-off-by: Eric Sunshine Signed-off-by: Alberto Bertogli --- git.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'git.py') diff --git a/git.py b/git.py index 9f73fd1..ad3952d 100644 --- a/git.py +++ b/git.py @@ -345,7 +345,7 @@ class Repo: ref = self.branch cmd = self.cmd('cat-file') cmd.raw(True) - cmd.batch = None + cmd.batch = '%(objectsize)' if isinstance(ref, unicode): ref = ref.encode('utf8') @@ -356,7 +356,7 @@ class Repo: if not head or head.strip().endswith('missing'): return None - return Blob(out.read()) + return Blob(out.read()[:int(head)]) def last_commit_timestamp(self): """Return the timestamp of the last commit.""" -- cgit v1.2.3