Files
deb-python-pygit2/docs/recipes/git-show.rst
2015-02-13 09:03:16 +01:00

3.0 KiB

git-show

Showing a commit

$> git show d370f56
>>> repo = pygit2.Repository('/path/to/repository')
>>> commit = repo.revparse_single('d370f56')

Show log message

>>> message = commit.message

Show SHA hash

>>> hash = commit.hex

Show diff

>>> diff = repo.diff(commit.parents[0], commit)

Show all files in commit

>>> for e in commit.tree: >>> print(e.name)

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:

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 # we don't know the time zone's name

    def dst(self, dt):
        return timedelta(0) # we don't know about DST

Then you can make your message:

>>> from __future__ import unicode_literals >>> from datetime import datetime >>> >>> 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), ... 'Date: {}'.format(timestr), ... '', ... commit.message])

References