Skip to content

Diff fails on empty repository. #41

Description

@nemec

If you have an empty repository (git init in an empty folder), then add files through GitPython, the repository isn't registered as "dirty" and diffs come back empty. For example:

>>> r = Repo('.')
>>> r.index.add(["README"])
>>> r.is_dirty
False
>>> r.index.diff(None)
[]

The index.entries property shows the newly added file, but there doesn't seem to be a way to turn it into "this file has been added, but not committed".

Activity

  1. fcelda commented on Mar 21, 2012

    @fcelda
    Contributor

    Actually, diff() works just fine. If you add some files into the index, you have to append staged (or cached) parameter to make diff to show the changes.

    >>> r.index.diff(None, staged=True)
    [<git.diff.Diff object at 0x2a86c80>]

    But is_dirty() behaves inconsistently. It should indeed return False. My patch (1253036) fixes it. But I'm unable to run your nose tests and therefore I cannot update them. Hope this helps:

    #!/usr/bin/python
    
    import git
    import os
    
    os.system("rm -rf repo/")
    r = git.Repo.init("repo")
    os.system("echo foo > repo/foo")
    os.system("echo bar > repo/bar")
    
    assert not r.is_dirty()
    
    r.index.add(["foo"])
    assert r.is_dirty()
    
    r.index.commit("foo messsage")
    assert not r.is_dirty()
    
    r.index.add(["bar"])
    assert r.is_dirty()
  2. nemec commented on Mar 22, 2012

    @nemec
    Author

    Ah, I must have missed out on the staged keyword in the docs. Thanks.

  3. added 2 commits that reference this issue on Sep 29, 2014
  4. added 2 commits that reference this issue on Nov 14, 2014
  5. added this to the v0.3.5 - bugfixes milestone on Nov 19, 2014
  6. Byron commented on Nov 19, 2014

    @Byron
    Member

    Even though plenty of work has been done on this already, there is no test-case yet to truly verify this works. It should be done when working on 0.3.5.

  7. Byron commented on Jan 8, 2015

    @Byron
    Member

    A lot of time has passed, and plenty of changes were applied to is_dirty(). Additionally, there are plenty of tests checking all aspects of it.
    Therefore I believe this issue can be closed for now.

  8. added 2 commits that reference this issue on Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions