使用 GitPython,我尝试列出给定提交时目录的内容(即当时目录的“快照”)。
在终端中,我要做的是:
git ls-tree --name-only 4b645551aa82ec55d1794d0bae039dd28e6c5704
我怎样才能在 GitPython 中做同样的事情?
根据我找到的类似问题的答案(GitPython 通过 sha 获取树和 blob 对象 https://stackoverflow.com/q/10717790/222529)我尝试过递归遍历base_commit.tree
和它的.trees
,但我似乎没有到达任何地方。
有任何想法吗?
事实上,遍历树/子树是正确的方法。然而,内置traverse
方法可能会出现子模块问题。相反,我们可以自己迭代地进行遍历并找到所有 blob 对象(其中包含给定提交时存储库中的文件)。没有必要使用execute
.
def list_files_in_commit(commit):
"""
Lists all the files in a repo at a given commit
:param commit: A gitpython Commit object
"""
file_list = []
dir_list = []
stack = [commit.tree]
while len(stack) > 0:
tree = stack.pop()
# enumerate blobs (files) at this level
for b in tree.blobs:
file_list.append(b.path)
for subtree in tree.trees:
stack.append(subtree)
# you can return dir_list if you want directories too
return file_list
如果您希望文件受到给定提交的影响,可以通过commit.stats.files
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)