简体   繁体   English

如何从上次提交中导出文件名?

[英]How to export file names from last commit?

I managed to find out the last commit id using python:我设法使用 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_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()

But now I don't know how to transform this git command in python: git ls-tree --name-only -r last_commit ->files.txt , so I can get file names in text file.但是现在我不知道如何在 python 中转换这个 git 命令: git ls-tree --name-only -r last_commit ->files.txt文件中。

You are too close, you just need to get the files first then write them to a file:你太接近了,你只需要先获取文件然后将它们写入文件:

>>> 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)

After this, a file named 'files.txt.'在此之后,一个名为'files.txt.' should appear in the working directory with desired content.应该出现在具有所需内容的工作目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM