我正在使用GitPython计算 git 中的暂存文件。
对于修改过的文件,我可以使用
repo = git.Repo()
modified_files = len(repo.index.diff(None))
但是对于暂存文件,我找不到解决方案。
我知道git status --porcelain
,但我正在寻找其他更好的解决方案。(我希望使用gitpython
not git命令,脚本会更快)
您很近,用于repo.index.diff("HEAD")
在暂存区获取文件。
首先创建一个测试仓库:
$ cd test
$ mkdir repo && cd repo && touch a b c && git init && git add . && git commit -m "init"
$ echo "a" > a && echo "b" > b && echo "c" > c && git add a b
$ git status
On branch master
Changes to be committed:
modified: a
modified: b
Changes not staged for commit:
modified: c
现在检查 ipython:
$ ipython
In [1]: import git
In [2]: repo = git.Repo()
In [3]: count_modified_files = len(repo.index.diff(None))
In [4]: count_staged_files = len(repo.index.diff("HEAD"))
In [5]: print count_modified_files, count_staged_files
1 2