Repository.status()性能在快速测试中表现良好。
典型用法:
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)。
def get_repo_status(repo: pygit2.Repository) -> Dict[str, int]:
# get the integer representing filemode changes
changed_filemode_status_code: int = pygit2.GIT_FILEMODE_TREE
original_status_dict: Dict[str, int] = repo.status()
# transfer any non-filemode changes to a new dictionary
status_dict: Dict[str, int] = {}
for filename, code in original_status_dict.items():
if code != changed_filemode_status_code:
status_dict[filename] = code
return status_dict
表现:
%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