我想从GitPython中的repo(项目)获取目录(称为模块)的提交次数。
> print("before",modulePath)
> repo = Repo(modulePath)
> print(len(list(repo.iter_commits())))
当我试图打印目录提交量时,它说repo不是有效的git Repo。
- 在/ home / user / project / module之前
- git.exc.InvalidGitRepositoryError:/ home / user / project / module
欢迎任何帮助或想法:)谢谢
这是我的一个旧项目的示例代码(不是打开的,所以没有存储库链接):
def parse_commit_log(repo, *params):
commit = {}
try:
log = repo.git.log(*params).split("\n")
except git.GitCommandError:
return
for line in log:
if line.startswith(" "):
if not 'message' in commit:
commit['message'] = ""
else:
commit['message'] += "\n"
commit['message'] += line[4:]
elif line:
if 'message' in commit:
yield commit
commit = {}
else:
field, value = line.split(None, 1)
commit[field.strip(":")] = value
if commit:
yield commit
说明:
该函数需要Repo
的实例以及传递给git log
命令的相同参数。因此,您的案例中的用法可能如下所示:
repo = git.Repo('project_path')
commits = list(parse_commit_log(repo, 'module_dir'))
在内部,repo.git.log
正在调用git log
命令。它的输出看起来像这样:
commit <commit1 sha>
Author: User <[email protected]>
Date: Sun Apr 7 17:08:31 2019 -0400
Commit1 message
commit <commit2 sha>
Author: User2 <[email protected]>
Date: Sun Apr 7 17:08:31 2019 -0400
Commit2 message
parse_commit_log
解析此输出并生成提交消息。您需要添加更多行来获取提交sha,作者和日期,但这不应该太难。