Get changed files using gitpython

后端 未结 1 751
-上瘾入骨i
-上瘾入骨i 2020-12-29 06:55

I want to get a list of changed files of the current git-repo. The files, that are normally listed under Changes not staged for commit: when calling git s

相关标签:
1条回答
  • 2020-12-29 07:26
    for item in repo.index.diff(None):
        print item.a_path
    

    or to get just the list:

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

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

    So function can look like this:

    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'
    
    0 讨论(0)
提交回复
热议问题