Get modified files using gitpython

I want to get a list of modified files of the current git-repo. Files that are usually listed under Changes not staged for commit:upon invocation git status.

So far I have managed to connect to the repository, pull it out and show all unsolicited files:

from git import Repo
repo = Repo(pk_repo_path)
o = self.repo.remotes.origin
o.pull()[0]
print(repo.untracked_files)

But now I want to show all files that have changes (not perfect). Can someone push me in the right direction? I looked at the names of the methods repoand experimented for a while, but I can not find the right solution.

Obviously, I could name repo.git.statusand parse the files, but this is not at all elegant. There must be something better.


Edit: Now that I think about it. More useful would be a function that tells me the status for a single file. For example:

print(repo.get_status(path_to_file))
>>untracked
print(repo.get_status(path_to_another_file))
>>not staged
+4
1
for item in repo.index.diff(None):
    print item.a_path

:

changedFiles = [ item.a_path for item in repo.index.diff(None) ]

repo.index.diff() git.diff.Diffable, http://gitpython.readthedocs.io/en/stable/reference.html#module-git.diff

, :

def get_status(repo, path):
    changed = [ item.a_path for item in repo.index.diff(None) ]
    if path in repo.untracked_files:
        return 'untracked'
    elif path in changed:
        return 'modified'
    else:
        return 'don''t care'
+3

Source: https://habr.com/ru/post/1616039/


All Articles