如何使用GitPython获取分阶段文件? [英] How to get staged files using GitPython?

查看:151
本文介绍了如何使用GitPython获取分阶段文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GitPython 来计算git中的暂存文件。



对于修改后的文件,我可以使用

  repo = git.Repo()
modified_files = len(repo.index.diff(无))

但是对于staged文件, t找到解决方案。



我知道 git status --porcelain 但我正在寻找其他解决方案,它是更好。 (我希望使用 gitpython 不是git命令,脚本会更快)




$ b

完整演示:



首先创建测试回购:

  $ cd test 
$ mkdir repo&& cd回购&&触摸b c&& git init&& git add。 &安培;&安培; git commit -minit
$ echoa>一个&&回声b> b&&回声c> c&& git add ab
$ git status
关于分支主设备
要提交的更改:
修改:a
修改:b
未针对commit进行更改:
修改:c

现在登入ipython:

  $ ipython 
In [1]:import git
In [2]:repo = git.Repo()
In [ 3]:count_modified_files = len(repo.index.diff(无))
在[4]中:count_staged_files = len(repo.index.diff(HEAD))
In [5]:print count_modified_files,count_staged_files
1 2


I'm counting staged files in git using GitPython.

For modified files, I can use

repo = git.Repo()
modified_files = len(repo.index.diff(None))

But for staged files I can't find the solution.

I know git status --porcelain but I'm looking for other solution which is better. (I hope using gitpython not git command, the script will be faster)

解决方案

You are close, use repo.index.diff("HEAD") to get files in staging area.


Full demo:

First create a test repo:

$ 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

Now check in 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

这篇关于如何使用GitPython获取分阶段文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆