【问题标题】:Configure GitPython to output/log commands and process output配置 GitPython 以输出/记录命令并处理输出
【发布时间】:2021-04-14 01:07:30
【问题描述】:

我正在使用 GitPython 对 repos 运行几个简单的命令。基本上大部分只是:

repo = Repo(repo_dir)
repo.git.fetch()
result = repo.git.diff("origin", name_only=True)
repo.git.merge()
# ...

有没有办法设置 GitPython 来输出/记录正在运行的命令,并显示它们产生的原始输出?

例如,我希望上面的内容类似于:

$ git fetch
$ git diff --name-only origin
path/to/differing_file
path/to/another/differing_file
$ git merge

【问题讨论】:

    标签: python git gitpython


    【解决方案1】:

    GitPython 使用模块logging。通过在代码前添加logging.basicConfig(level=logging.DEBUG),它会打印类似的日志

    DEBUG:git.cmd:Popen(['git', 'fetch'], cwd=E:\path\foo, universal_newlines=False, shell=None)
    

    如果您希望它按预期打印格式化日志,您可以修改GitPython 的源代码。我正在使用 Python 2.7,我的 GitPython 安装在 C:\Python27\lib\site-packages\git。你可以在 REPL 中运行git.__file__ 来找到你的安装目录。在文件C:\Python27\lib\site-packages\git\cmd.py 中,您可以找到方法def execute。它调用subprocess.Popen 来运行命令。您可以在Popen 之前修改log.debug 行,或者在适当的行处插入print(' '.join(cmd))。如果您使用的是 Python 3.3 或更高版本,您还可以参考this answer 了解如何使用subprocess 打印命令。

    【讨论】:

    • 这是完美的。谢谢。
    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2019-05-06
    相关资源
    最近更新 更多