Using sphinx-git

Currently, sphinx-git provides two extensions to Sphinx: the git_changelog and git_commit_detail directives.

git_changelog Directive

The git_changelog directive produces a list of commits in the repository in which the documentation build is happening.

By default, it will output the most recent 10 commits. So:

.. git_changelog::

produces:

  • Add a demo picture to README by Daniel Watkins at 2017-12-16 17:45:35

    This fixes #38.

  • Prepare for development of next release by Daniel Watkins at 2017-12-16 17:37:13
  • Prepare v10.1.1 release by Daniel Watkins at 2017-12-16 17:35:13
  • Make CHANGELOG formatting consistent by Daniel Watkins at 2017-12-16 17:34:10
  • Update CHANGELOG by Daniel Watkins at 2017-12-16 17:32:52
  • Merge pull request #47 from OddBloke/fix_rtd by Daniel Watkins at 2017-12-16 17:31:05

    Handle detached HEADs

  • Fix operating on a detached HEAD by Daniel Watkins at 2017-12-16 17:27:27

    This (a) should work, and (b) is the environment that ReadTheDocs builds in, so we need this to get updated docs.

  • Inline unnecessary method by Daniel Watkins at 2017-12-16 17:27:04
  • Prepare for development in next release by Daniel Watkins at 2017-12-16 17:12:28
  • Finalise v10.1.0 by Daniel Watkins at 2017-12-16 17:06:33

As you can see, each revision has the message, author and date output in a list. If a commit has a detailed message (i.e. any part of the commit message that is not on the first line), that will be output below the list item for that commit.

Changing Number of Revisions in Output

If you want to change the number of revisions output by git_changelog, then you can specify the :revisions: argument. So:

.. git_changelog::
    :revisions: 2

produces:

  • Add a demo picture to README by Daniel Watkins at 2017-12-16 17:45:35

    This fixes #38.

  • Prepare for development of next release by Daniel Watkins at 2017-12-16 17:37:13

If you specify more revisions than the history contains, all revisions in the history will be displayed.

Specifying Range of Revisions to Output

If you want even more control over the output of git_changelog, then you can specify precisely the revisions you want included using the :rev-list: argument. So:

.. git_changelog::
    :rev-list: v3..v4

produces a list of all the commits between the v3 and v4 tags:

  • Add feature credits to CHANGELOG. by Daniel Watkins at 2013-11-16 23:08:14
  • Add v4 changelog entry. by Daniel Watkins at 2013-11-16 23:06:36
  • Merge pull request #10 from OddBloke/rev_list by OddBloke at 2013-11-16 23:02:41

    Add the possiblity to specify commits using a rev-list parameter.

  • Make a couple of formatting changes. by Daniel Watkins at 2013-11-16 22:59:21
  • add rev-list explanation in README by Gregory Eric Sanderson at 2013-09-30 17:23:39
  • display a changelog using a range of commits by Gregory Eric Sanderson at 2013-09-30 17:06:15

    Adds a new option ‘rev-list’. rev-list lets you define which commit to start from when displaying the changelog. You can use a tag, a branch, a commit hash, an explicit range, or anything else supported by git-rev-parse. Examples: .. git_changelog:: :rev-list: v1.0..HEAD .. git_changelog:: :rev-list: master..topicbranch Consult the man pages of git-rev-parse for more details on the syntax.

  • use a version of gitpython that provides iter_commits() by Gregory Eric Sanderson at 2013-09-30 17:04:34
  • Add CHANGELOG. by Daniel Watkins at 2013-07-07 08:35:43
  • Bump to v4 (for development). by Daniel Watkins at 2013-07-07 08:35:32

and:

.. git_changelog::
    :rev-list: v1

gives you a list of all commits up to the v1 tag (most of which involved me wrestling with setuptools):

  • I despise setuptools. by Daniel Watkins at 2012-07-09 16:46:00
  • Start again. by Daniel Watkins at 2012-07-09 16:39:20
  • Fix requirements. by Daniel Watkins at 2012-07-09 16:37:48
  • Add setup.py. by Daniel Watkins at 2012-07-09 16:34:03
  • Add README. by Daniel Watkins at 2012-07-09 16:33:18
  • Initial implementation. by Daniel Watkins at 2012-07-09 16:16:13

:rev-list: lets you specify revisions using anything that git rev-parse will accept. See the man page for details.

Warning

The :revisions: argument and the :rev-list: argument don’t play nicely together. :rev-list: will always take precedence, and all commits specified by the revision specification be output regardless of the :revisions: argument [1].

Sphinx will output a warning if you specify both.

Filter Revisons to Matching Only Certain Files Based on Filenames

If you only want to see the changelog regarding certain files (eg. for devops reasons you need to have both SaSS and CSS in your repository or you only want to see the changes made to the docs directory) you can use the :filename_filter: argument with git_changelog. :filename_filter: is expecting anything that can be evaluated as a regular expression. So:

.. git_changelog::
    :filename_filter: doc/.*\.rst

will produce the list of commits that modified documentation content.

Note

The :filename_filter: argument is compatible with both :revisions: and :rev-list:. Filtering on filenames is then performed on the selected (number of) revisions.

Preformatted Output for Detailed Messages

If you would prefer for the detailed commit messages to be output as preformatted text (e.g. if you include code samples in your commit messages), then you can specify this preference using the :detailed-message-pre: argument. So:

.. git_changelog::
    :rev-list: 3669419^..3669419
    :detailed-message-pre: True

becomes:

  • display a changelog using a range of commits by Gregory Eric Sanderson at 2013-09-30 17:06:15
    Adds a new option 'rev-list'.
    rev-list lets you define which commit to start from when displaying the
    changelog. You can use a tag, a branch, a commit hash, an explicit
    range, or anything else supported by git-rev-parse. Examples:
    
    .. git_changelog::
        :rev-list: v1.0..HEAD
    
    .. git_changelog::
        :rev-list: master..topicbranch
    
    Consult the man pages of git-rev-parse for more details on the syntax.
    

Footnotes

[1]Patches welcome!

git_commit_detail Directive

The git_commit_detail directive produces information about the current commit in the repository against which the documentation is being built. The following options are available:

branch
Display the branch name.
commit
Display the commit hash.
sha_length
Set the number of characters of the hash to display.
no_github_link
By default, if the repository’s origin remote is GitHub, the commit will link to the GitHub page for the commit. Use this option to disable this.
uncommitted
Show a warning if there are uncommitted changes in the repository.
untracked
Show a warning if there are untracked files in the repository directory.

For example:

.. git_commit_detail::
    :branch:
    :commit:
    :sha_length: 10
    :uncommitted:
    :untracked:

becomes

Commit:de78781d59

Warning

There were uncommitted changes when this was compiled.