1

I am using Sphinx to document a project, for publication on Read the Docs.

Some of the content needs to link to some files in the GitHub repo, so that the version of file linked to matches the version of code being documented.

For example, if the documentation is of revision 2f5dabe0, then I might want to link to:

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

Or with the full commit ID:

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

Without adding any dependencies (such as GitPython), how can I obtain the Git Commit ID in Sphinx's conf.py?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Clare Macrae
  • 3,670
  • 2
  • 31
  • 45

1 Answers1

0

Using the idea from here with some modifications for this particular request, the following produces the full commit ID as a string:

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

If you'd rather get a shortened commit id, you can do this:

commit_id = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip().decode('ascii')
Moot
  • 2,195
  • 2
  • 17
  • 14