【问题标题】:How to access the Git Commit ID in Sphinx's conf.py如何在 Sphinx 的 conf.py 中访问 Git Commit ID
【发布时间】:2020-10-08 05:23:57
【问题描述】:

我正在使用 Sphinx 记录一个项目,以便在 Read the Docs 上发布。

某些内容需要链接到 GitHub 存储库中的某些文件,以便链接到的文件版本与记录的代码版本匹配。

例如,如果文档是修订版2f5dabe0,那么我可能想要链接到:

https://github.com/approvals/ApprovalTests.cpp/blob/2f5dabe0/tests/UT_Tests/UTApprovalTestTests.cpp#L58-L73

或者使用完整的提交 ID:

https://github.com/approvals/ApprovalTests.cpp/blob/2f5dabe0a533deeedc025d595d5534967737fed5/tests/UT_Tests/UTApprovalTestTests.cpp#L58-L73

在不添加任何依赖项(如GitPython)的情况下,如何获取Sphinx的conf.py中的Git Commit ID?

【问题讨论】:

    标签: github python-sphinx read-the-docs


    【解决方案1】:

    使用来自here 的想法并对此特定请求进行一些修改,以下将生成完整的提交 ID 作为字符串:

    import subprocess
    commit_id = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip().decode('ascii')
    

    如果您希望获得一个缩短的提交 ID,您可以这样做:

    commit_id = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip().decode('ascii')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 2021-06-21
      相关资源
      最近更新 更多