Skip to content

Fails *silently* if color.diff is set to "always" #172

@frozencemetery

Description

@frozencemetery

If one sets color.diff in ~/.gitconfig to "always", then GitPython will silently fail to produce diffs. It looks like this:

    $ python
    Python 2.7.5 (default, Jun 25 2014, 10:19:55)
    [GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import git
    >>> oldcstr = "2d018c40ff73373221bc6717bfea8ac29aff9bff"
    >>> newcstr = "aa901d68d53e686428d0c58e831f4b7c8b0de40e"
    >>> repo = git.Repo(".")
    >>> oldc = repo.commit(oldcstr)
    >>> newc = repo.commit(newcstr)
    >>> oldc.diff(newc, create_patch = True)
    []
    >>> oldc.diff(newc, create_patch = False)
    [<git.diff.Diff object at 0x7fad7fee5b18>]
    >>> oldc.diff(newc, create_patch = False)[0]
    <git.diff.Diff object at 0xc9d050>
    >>> oldc.diff(newc, create_patch = False)[0].diff
    ''
    >>> import os
    >>> os.popen("git diff %s %s" % (oldcstr, newcstr))
    <open file 'git diff 2d018c40ff73373221bc6717bfea8ac29aff9bff aa901d68d53e686428d0c58e831f4b7c8b0de40e', mode 'r' at 0xbfa930>
    >>> os.popen("git diff %s %s" % (oldcstr, newcstr)).read()
    '\x1b[1mdiff --git a/requirements.txt b/requirements.txt\x1b[m\n\x1b[1mindex 4e6c95b..7455a11 100644\x1b[m\n\x1b[1m--- a/requirements.txt\x1b[m\n\x1b[1m+++ b/requirements.txt\x1b[m\n\x1b[36m@@ -1,7 +1,7 @@\x1b[m\n pbr>=0.6,!=0.7,<1.0\x1b[m\n \x1b[m\n urwid\x1b[m\n\x1b[31m-sqlalchemy\x1b[m\n\x1b[32m+\x1b[m\x1b[32msqlalchemy>=0.9.4\x1b[m\n GitPython>=0.3.2.RC1\x1b[m\n python-dateutil\x1b[m\n requests\x1b[m\n'
    >>>

Setting color.diff to "true" instead fixes this. I think GitPython should at least warn in this case (it did get garbage information back from a git command, which should _not_ be silent failure). git diff also contains an option to explicitly disable color (--no-color or --color=never); it would be even better if GitPython could pass these.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions