Skip to main content

Lektor type to deduce page modification time from git

Project description

Lektor-Git-Timestamp

PyPI version PyPI Supported Python Versions GitHub license GitHub Actions (Tests)

This Lektor plugin implements a new datetime-like type, gittimestamp, which gets it's default value from git timestamps. This can be used to implement auto-updating pub_date and last_mod fields in Lektor datamodels.

Description

The gittimestamp type behaves just like the built-in datetime type, except that if the field is left blank in contents.lr a default value will be deduced from git timestamps for the file (or possibly the file’s filesystem mtime.)

If an explicit value for the field is not found, the git log for the source file (typically contents.lr) is searched using git log --follow --remove-empty -- <source_filename>, and the author timestamp of all matching commits are considered. Additionally, if the source file is dirty with respect to git’s HEAD, or if the file is not checked into the git tree, the file’s mtime is prepended to that list of timestamps. That list of timestamps is filtered based on the ignore_commits and skip_first_commit options (see below); then, finally, a timestamp is selected from those that remain based on the setting of the strategy option.

Options

The gittimestamp type supports the following options.

ignore_commits

This can be set to a string, which is interpreted as a regular expression. Any git commits whose commit message matches this pattern are ignored when computing a default timestamp value for the field. (The matching is performed using re.search.)

skip_first_commit

If this boolean option is set, the first commit in the git log for the source file will be ignored. This is useful for implementing a last_mod field which has a defined value only if the source file has actually been modified since the initial commit.

strategy

This option determines which timestamp is selected from the git log (and/or the file mtime). This can be set to one of four values:

  • last: If the source file is dirty (with respect to the git HEAD tree), the mtime of the file is used. Otherwise, the timestamp of the last (nominally the most recent) non-ignored git commit is used. This is the default strategy.

  • first: The timestamp of the first (nominally the earliest) commit is used.

  • latest: The latest timestamp is used. Normally this produces the same result at last, however due to rebasing, cherry-picking, etc. the git timestamps may not be monotonically increasing, in which case this option causes the greatest (most recent) timestamp remaining after any filtering to be selected.

  • earliest: The earliest timestamp is used. Normally this produces the same result at first, but if the timestamps in the git log are not monotonic, this will select the minimum of all the timestamps remaining after any filtering.

Examples

Here is a simple example excerpt from a datamodel file:

<...>

[fields.last_mod]
label = Time last modified
type = gittimestamp

On a page using the above datamodel, so long as the last_mod field is left blank in the contents.lr file, the page modification time will be deduced from timestamp of the most recent git commit which affected that contents.lr. (Or if that file is dirty, the value of last_mod will be taken from the file’s filesystem mtime.)


Here is a more complicated example which demonstrates the use of all the options.

<...>

[fields.pub_date]
label = Time first published
type = gittimestamp
strategy = first

[fields.last_mod]
label = Time last modified
type = gittimestamp
ignore_commits = \[nochange\]
skip_first_commit = true

This will get the default value of the pub_date field from the timestamp of the first (earliest) git commit for the source file.

The default value for last_mod will, as in the previous example, be taken from the most recent commit for the file, except that:

  • any commits whose commit message include the tag [nochange] will be ignored
  • the first commit (the one whose timestamp is used for pub_date) is ignored

If there has only been one commit of the source file, last_mod will not have a default value. (It will evaluate to a jinja2 Undefined instance.)

Author

Jeff Dairiki dairiki@dairiki.org

Changelog

Release 0.1a2 (2021-02-03)

Bugs Fixed

Fixed attrocious typo which prevented the use of anything other than the default strategy=last for picking timestamps.

Release 0.1a1 (2020-06-16)

Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lektor-git-timestamp-0.1a2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lektor_git_timestamp-0.1a2-py2.py3-none-any.whl (6.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file lektor-git-timestamp-0.1a2.tar.gz.

File metadata

  • Download URL: lektor-git-timestamp-0.1a2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for lektor-git-timestamp-0.1a2.tar.gz
Algorithm Hash digest
SHA256 b5572fd7f0e6e1fa31fbb142449b2c87e5e2595038b01b84f3dbf39d5b8895c3
MD5 0b08cbd87fd6b95408bcca693d922431
BLAKE2b-256 a084e43a1ec59c57b2c545c5ac19aa07f0ee8db0917039640463b5af76edf5b7

See more details on using hashes here.

File details

Details for the file lektor_git_timestamp-0.1a2-py2.py3-none-any.whl.

File metadata

  • Download URL: lektor_git_timestamp-0.1a2-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for lektor_git_timestamp-0.1a2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bde69999fa9b63b0283e3871e087d7ba912b7385f5759de5cc56bc584d007adc
MD5 be3465be47ca099144d02c4001ef02f7
BLAKE2b-256 35f8743b0a5001528aabf5d5623aabd7c690cb90ab4783da6de484993e8a9dbf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page