From c87d28c9a807be3203c22f928d9e6bc3810d1aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20M=C3=B6hn?= Date: Wed, 11 Feb 2015 18:00:00 +0100 Subject: [PATCH 1/4] Update git-show recipe I couldn't get the diff as shown in the git-show recipe. Therefore update it to what I think it should be. Maybe there is a better way. Also add a section on how to assemble a git show-like message. It took me quite some searching in the Python docs to find out how to do it, especially the date and time part. So this might save people time. I wanted to add something that gives me a git show --stat equivalent, but couldn't figure it out. --- docs/recipes/git-show.rst | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/recipes/git-show.rst b/docs/recipes/git-show.rst index 0cba72d..249a6aa 100644 --- a/docs/recipes/git-show.rst +++ b/docs/recipes/git-show.rst @@ -31,7 +31,7 @@ Show SHA hash Show diff ====================================================================== - >>> diff = commit.tree.diff() + >>> diff = commit.parents[0].tree.diff_to_tree(commit.tree) ====================================================================== Show all files in commit @@ -40,6 +40,22 @@ Show all files in commit >>> for e in commit.tree: >>> print(e.name) +====================================================================== +Produce something like a `git show` 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) + >>> timestr = dt.strftime('%c %z') + >>> msg = '\n'.join(['commit {}'.format(commit.tree_id.hex), + ... 'Author: {} <{}>'.format(commit.author.name, commit.author.email), + ... 'Date: {}'.format(timestr), + ... '', + ... commit.message]) + ---------------------------------------------------------------------- References ---------------------------------------------------------------------- From 2b2beb80943fd2a7521c537bc7696c60cc28220d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20M=C3=B6hn?= Date: Thu, 12 Feb 2015 09:08:24 +0100 Subject: [PATCH 2/4] 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. --- docs/recipes/git-show.rst | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/docs/recipes/git-show.rst b/docs/recipes/git-show.rst index 249a6aa..0f4935d 100644 --- a/docs/recipes/git-show.rst +++ b/docs/recipes/git-show.rst @@ -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), From 69f539851bcc49948bc4996926c2523114678a21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20M=C3=B6hn?= Date: Fri, 13 Feb 2015 09:03:16 +0100 Subject: [PATCH 3/4] Clarify comments in git-show recipe --- docs/recipes/git-show.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/recipes/git-show.rst b/docs/recipes/git-show.rst index 0f4935d..4a17539 100644 --- a/docs/recipes/git-show.rst +++ b/docs/recipes/git-show.rst @@ -44,8 +44,8 @@ Show all files in commit 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`_:: +In order to display time zone information you have to create a subclass +of tzinfo as described in the `Python datetime documentation`_:: from datetime import tzinfo, timedelta class FixedOffset(tzinfo): @@ -58,10 +58,10 @@ tzinfo as described in the `Python datetime documentation`_:: return self.__offset def tzname(self, dt): - return None + return None # we don't know the time zone's name def dst(self, dt): - return timedelta(0) # dealing with DST would be too complicated + return timedelta(0) # we don't know about DST .. _Python datetime documentation: https://docs.python.org/2/library/datetime.html#tzinfo-objects From 1cb62ab578c63c48927a82dec2bb3fbb04d0c4b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20M=C3=B6hn?= Date: Fri, 13 Feb 2015 13:46:41 +0100 Subject: [PATCH 4/4] git-show recipe: Add the easy Python 3 way As @jdavid pointed out, Python 3 already provides a tzinfo subclass for fixed UTC offsets. Incorporate this in the recipe. Leave the old code with the self-made class, since many people are working with Python 2 and it is harder to find out there. --- docs/recipes/git-show.rst | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/recipes/git-show.rst b/docs/recipes/git-show.rst index 4a17539..aea7862 100644 --- a/docs/recipes/git-show.rst +++ b/docs/recipes/git-show.rst @@ -45,7 +45,9 @@ Produce something like a ``git show`` message ====================================================================== In order to display time zone information you have to create a subclass -of tzinfo as described in the `Python datetime documentation`_:: +of tzinfo. In Python 3.2+ you can do this fairly directly. In older +versions you have to make your own class as described in the `Python +datetime documentation`_:: from datetime import tzinfo, timedelta class FixedOffset(tzinfo): @@ -67,10 +69,15 @@ of tzinfo as described in the `Python datetime documentation`_:: Then you can make your message: + >>> # Until Python 2.7.9: >>> from __future__ import unicode_literals >>> from datetime import datetime - >>> >>> tzinfo = FixedOffset(commit.author.offset) + + >>> # From Python 3.2: + >>> from datetime import datetime, timezone, timedelta + >>> tzinfo = timezone( timedelta(minutes=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),