如何知道git存储库是否使用Python3(pygit2)是干净的?
我试图确定git存储库中是否需要or如何知道git存储库是否使用Python3(pygit2)是干净的?,python,python-3.x,pygit2,Python,Python 3.x,Pygit2,我试图确定git存储库中是否需要orgit提交。我想出了这个对我来说很好的代码: def is_dirty(repo): import pygit2 status = repo.status() for filepath, flags in status.items(): if flags != pygit2.GIT_STATUS_CURRENT: if flags != 16384: return True return Fals
git提交
。我想出了这个对我来说很好的代码:
def is_dirty(repo):
import pygit2
status = repo.status()
for filepath, flags in status.items():
if flags != pygit2.GIT_STATUS_CURRENT:
if flags != 16384:
return True
return False;
但这是极为低效的:repo.status()花费了很长时间——至少与git status
命令行相比是如此
所以我的问题是:有没有更有效的方法来知道存储库是否干净
附言:我在用蟒蛇3。对于python2,我使用了模块
git
,它有一个is\u dirty
函数。真的很脏吗?python:使用git状态执行shell脚本
sh:
VAR= $(git status)
dump var to file
python:
meanwhile your python script is waiting for the file to be created
while(1)
if(os.path.exists(file_path))
status = read_file_function(file_path)
break
愚蠢而简单,而且可能非常明显该性能在快速测试中表现良好
典型用法:
import pygit2
from pathlib import Path
from typing import Dict, Union
repo_path: Union[Path, str] = Path("/path/to/your/repo")
repo = pygit2.Repository(pygit2.discover_repository(repo_path))
status: Dict[str, int] = repo.status()
print(status)
# {} (nothing to commit, working tree clean)
# {"myfile" : 256} (myfile is modified but not added)
我的函数版本,用于删除更改了文件模式的文件(代码16384,GIT_FILEMODE_TREE)
性能:
%timeit repo.status()
# 2.23 ms per loop
%timeit get_repo_status(repo)
# 2.28 ms per loop
%timeit subprocess.run(["git", "status"]) # yes, I know, this is not really comparable..
# 11.3 ms per loop
5年后。这就是我现在所做的
pip3 install GitPython
然后
import git
repo = git.Repo("path/to/my/repo")
if repo.is_dirty(untracked_files=True):
do_work()
我还可以执行
命令。getoutput(“git状态”)
。但是当我真的。。。嗯,你知道……特雷斯·比恩,想要另一个糟糕/美丽的主意吗?在后台运行一个守护进程,生成一个csv或纯文本文件,其中包含每个回购协议的状态,然后您就可以以“零”成本获得任何回购协议的状态……好吧。。至少你的“获取状态(回购名称)”将是零成本。。。。
import git
repo = git.Repo("path/to/my/repo")
if repo.is_dirty(untracked_files=True):
do_work()