Python 如何从上次提交导出文件名?
我使用python找到了最后一个提交id:Python 如何从上次提交导出文件名?,python,git,Python,Git,我使用python找到了最后一个提交id: import subprocess def get_git_revision_hash(): full_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']) full_hash = str(full_hash, "utf-8").strip() return full_hash def get_git_revision_short_ha
import subprocess
def get_git_revision_hash():
full_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
full_hash = str(full_hash, "utf-8").strip()
return full_hash
def get_git_revision_short_hash():
short_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'])
short_hash = str(short_hash, "utf-8").strip()
return short_hash
last_commit= get_git_revision_hash()
但是现在我不知道如何在python中转换这个git命令:
git ls tree--name only-r last_commit->files.txt,这样我就可以在文本文件中获得文件名。你太接近了,你只需要先获取文件,然后将它们写入文件:
>>> files = subprocess.check_output(['git', 'ls-tree', '--name-only', '-r', last_commit]).decode('utf8')
>>> with open('files.txt', 'w') as f:
... print(files, file=f)
在此之后,名为'files.txt.
的文件应显示在工作目录中,包含所需的内容。如果您以任何频率执行此操作,让我指出,有一个Python GIT API可用:注意,这里不需要首先将HEAD
转换为修订哈希ID:您可以运行GIT ls tree--name only-r HEAD
。但是,如果出于其他一些好的原因,您已经完成了将HEAD
转换为修订哈希ID的工作,那么像这样将原始哈希ID提供给Git会稍微(非常稍微)更有效。