我们正在尝试获取Gitlab存储库中每个文件的提交。我们正在使用模块。我们可以获得存储库的提交,但不能获得存储库中单个文件的提交。有人能帮我们吗?
发布于 2022-01-19 19:45:10
单个文件的提交历史不会通过GitLab API直接公开。因此,python gitlab模块中没有这方面的直接功能。
但是,通过使用可用的API,您可以有效地获得相同的信息。具体来说,您可以使用存储库提交API和diff或文件非责任API。
使用提交API
例如,使用提交API,您可以列出所有提交及其差异,然后将每个提交的文件更改关联起来。
import gitlab
from collections import defaultdict
TOKEN = 'Your API Token'
gl = gitlab.Gitlab('https://gitlab.example.com', private_token=TOKEN)
project = gl.projects.get(1234)
commits = project.commits.list(all=True)
# file paths and a list of commits which create/modify/delete the file
file_map = defaultdict(list)
for c in commits:
diff = c.diff()
files_changed = set()
for change in diff:
files_changed.add(change['old_path'])
files_changed.add(change['new_path'])
for path in files_changed:
file_map[path].append(c)
# show list of commits which modified README.md
print(file_map['README.md'])使用指责API
使用提交API需要为每次提交获得差异,这在大型存储库上可能要花费很长时间。
如果您只对更改单个文件的提交感兴趣,则遍历指责树可能更有效。但是,请注意,使用此方法也可能会忽略提交(例如,在其他分支或发散树中提交)。
def search_blame(project, filename, base_ref=None):
if base_ref is None:
base_ref = project.default_branch
commits = set()
refs_to_check = [base_ref,]
seen = set()
while refs_to_check:
ref = refs_to_check.pop()
if ref in seen:
continue
seen.add(ref)
blame = project.files.blame(filename, ref)
for change in blame:
commit_id = change['commit']['id']
if commit_id not in seen:
refs_to_check.append(commit_id)
refs_to_check.extend(change['commit']['parent_ids'])
for c in change['commit']['parent_ids']:
commits.add(c)
commits.add(commit_id)
return commits
# show commits in blame tree for README.md
# only includes commits in the default branch
print(search_blame(project, 'README.md'))https://stackoverflow.com/questions/70773075
复制相似问题