我正在使用GitPython计算git中的暂存文件.
对于修改过的文件,我可以使用
repo = git.Repo()
modified_files = len(repo.index.diff(None))
Run Code Online (Sandbox Code Playgroud)
但对于分阶段文件,我找不到解决方案.
我知道,git status --porcelain但我正在寻找更好的其他解决方案.(我希望使用gitpython不是git命令,脚本会更快)
以下是我认为应该可行的三种方法,因此按顺序尝试:
使用pygithub : (Github 的 python API) 将推送请求发送到我的存储库。失败,因为我在 API 中找不到推送功能。我可以看到编辑文件,但是当我计划经常替换文件时,这无济于事。
git push在 python 子进程 (HTTPS) 的命令行中使用:这几乎有效,但我不知道如何填写所需的用户和密码字段。试图:
import subprocess
from pexpect import popen_spawn
user = 'GithubUsername'
password = '***********'
cmd = "cd C:\\Users\Dropbox\git-test"
returned_value = subprocess.call(cmd, shell=True) # returns the exit code in unix
cmd = "git add ."
subprocess.call(cmd, shell=True)
cmd = 'git commit -m "python project update"'
subprocess.call(cmd, shell=True)
cmd = "git remote set-url origin https://github.com/Tehsurfer/git-test.git"
subprocess.call(cmd, shell=True)
cmd = "git push …Run Code Online (Sandbox Code Playgroud)