Correct git-show recipe

Make the diff generation more idiomatic and fix the assembling of the
timestamp. git-show normally prints the author time, so use this instead
of the commit time. Also fix how tzinfo is obtained. Of course we have
to use the author's time zone and not some fixed one as I had written
before.
This commit is contained in:
Richard Möhn
2015-02-12 09:08:24 +01:00
parent c87d28c9a8
commit 2b2beb8094

View File

@@ -31,7 +31,7 @@ Show SHA hash
Show diff
======================================================================
>>> diff = commit.parents[0].tree.diff_to_tree(commit.tree)
>>> diff = repo.diff(commit.parents[0], commit)
======================================================================
Show all files in commit
@@ -41,14 +41,37 @@ Show all files in commit
>>> print(e.name)
======================================================================
Produce something like a `git show` message
Produce something like a ``git show`` message
======================================================================
In order to display timezone information you have to create a subclass of
tzinfo as described in the `Python datetime documentation`_::
from datetime import tzinfo, timedelta
class FixedOffset(tzinfo):
"""Fixed offset in minutes east from UTC."""
def __init__(self, offset):
self.__offset = timedelta(minutes = offset)
def utcoffset(self, dt):
return self.__offset
def tzname(self, dt):
return None
def dst(self, dt):
return timedelta(0) # dealing with DST would be too complicated
.. _Python datetime documentation: https://docs.python.org/2/library/datetime.html#tzinfo-objects
Then you can make your message:
>>> from __future__ import unicode_literals
>>> from datetime import datetime
>>> import pytz
>>> tzinfo = pytz.timezone('Europe/Berlin')
>>> dt = datetime.fromtimestamp(float(commit.commit_time), tzinfo)
>>>
>>> tzinfo = FixedOffset(commit.author.offset)
>>> dt = datetime.fromtimestamp(float(commit.author.time), tzinfo)
>>> timestr = dt.strftime('%c %z')
>>> msg = '\n'.join(['commit {}'.format(commit.tree_id.hex),
... 'Author: {} <{}>'.format(commit.author.name, commit.author.email),