Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Jekyll Git Status

Installing this plugin

  1. Create a _plugins directory in the root of your Jekyll site.
  2. Place the ruby script into that directory. This can be done by either:
    1. Downloading the "raw" version of the jekyll-git-status.rb into that directory.
    2. Cloning this gist into that directory as a submodule.
    3. Creating a new file, with the extension .rb in that path, and pasting in the content below.
  3. If you are already running jekyll serve or bundle exec jekyll serve then stop and restart it.

Using this plugin

This plugin adds some new "data" for your Jekyll site, which you can use as follows:

  • Add content if this is part of a git repo:
{% if site.data.git.is_git_repo %}Some Content{% endif %}
  • Add content if this repo is part of a git repo, but has had no commits:
{% if site.data.git.is_git_repo and not site.data.git.is_empty_repo %}Some Content{% endif %}
  • Describe the last commit:
This is commit {{ site.data.git.last_hash_long }} AKA {{ site.data.git.last_hash }}.
It was tagged {{ site.data.git.commits_since_last_tag }} commits ago as {{ site.data.git.tag }}.
The first commit was on {{ site.data.git.first_commit_date }} at {{ site.data.git.first_commit_time }}.
{{ site.data.git.first_commit_tz }}
The last commit was on {{ site.data.git.last_commit_date }} at {{ site.data.git.last_commit_time }}.
{{ site.data.git.last_commit_tz }}
The content in this repo is (c) My Org Name {{ site.data.git.first_commit_year }}-{{ site.data.git.last_commit_year }}.

Sample output:

This is commit a1b2c3d4e5f6a7b8c9d0e1f2a3c4d5e6f7a8b9c0 AKA a1b2c3d.
It was tagged 0 commits ago as 0.0.1.
The first commit was on 1970-01-01 at 00:00:00 +0000.
The last commit was on 1971-01-01 at 00:00:00 +0000.
The content in this repo is (c) My Org Name 1970-1971.

As with all things metadata, values around first and last commit dates can be skewed or even purposefully falsified by incorrect clocks. DO NOT RELY ON THESE FOR ANYTHING SENSITIVE!

License

Released under the MIT license

Pre-History

This plugin for Jekyll is based loosely on three things;

  1. Jekyll Version Plugin
  2. Jekyll Git Hash plugin
  3. __git_ps1

Jekyll Version Plugin showed me how to check whether this was a valid Git Repo path, and some of the initial checks to make, however, by forcing it as a command, meant that I was basically asking the renderer to re-run the series of git commands each pass. Jekyll Git Hash Plugin showed me how to turn this set of commands into a blob of data I could add to my article. __git_ps1 showed me how to check more "git" things that I wanted to find.

# Copyright (c) 2021 Jonathan Spriggs
# Copyright (c) 2014 Robert Murray (for portions of the code in "Jekyll Version Plugin")
# Copyright (c) Yegor Bugayenko, 2014 (for portions of the code in "Jekyll Git Hash")
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module Jekyll
class GitStatusGenerator < Generator
priority :high
safe true
def is_git_repo?
system("git rev-parse")
end
def repo_clean?
system("git diff --no-ext-diff --quiet && git diff --no-ext-diff --cached --quiet")
end
def generate(site)
is_git_repo = false
is_empty_repo = true
last_hash = "Empty repo"
last_tag = ""
commits_since_last_tag = 0
repo_dirty = false
first_commit_date = "0000-00-00"
first_commit_year = "0000"
first_commit_time = "00:00:00"
first_commit_tz = "+0000"
last_commit_date = "0000-00-00"
last_commit_year = "0000"
last_commit_time = "00:00:00"
last_commit_tz = "+0000"
if is_git_repo?
is_git_repo = true
last_hash = %x( git rev-parse --short HEAD 2>/dev/null || true ).strip
if last_hash == ""
is_empty_repo = true
else
is_empty_repo = false
branch = %x( git branch | grep -e '^\*' | cut -d" " -f 2 ).strip
last_hash_long = %x( git rev-parse HEAD 2>/dev/null || true ).strip
last_tag_long = %x( git describe --tags --always --long ).strip
if last_tag_data = last_tag_long.match(/^(.*)-(\d+)-(\w+)$/)
last_tag = last_tag_data[1]
commits_since_last_tag = last_tag_data[2]
end
first_commit = %x( git rev-list --max-parents=0 HEAD ).strip
first_commit_date_long = %x( git show -s --format=%cd --date=format:'%Y-%m-%d %H:%M:%S %z%Z' #{first_commit}).strip
if first_commit_date_data = first_commit_date_long.match(/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) ([-+]?\d{4})$/)
first_commit_date = first_commit_date_data[1]
first_commit_time = first_commit_date_data[2]
first_commit_tz = first_commit_date_data[3]
if first_commit_date_data = first_commit_date_long.match(/^(\d{4})-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-+]?\d{4}$/)
first_commit_year = first_commit_date_data[1]
end
end
last_commit_date_long = %x( git show -s --format=%cd --date=format:'%Y-%m-%d %H:%M:%S %z%Z').strip
if last_commit_date_data = last_commit_date_long.match(/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}) ([-+]?\d{4})$/)
last_commit_date = last_commit_date_data[1]
last_commit_time = last_commit_date_data[2]
last_commit_tz = last_commit_date_data[3]
if last_commit_date_data = last_commit_date_long.match(/^(\d{4})-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-+]?\d{4}$/)
last_commit_year = last_commit_date_data[1]
end
end
end
if repo_clean?
repo_dirty = false
else
repo_dirty = true
end
end
site.data["git"] = {}
site.data["git"]["is_git_repo"] = is_git_repo
site.data["git"]["is_empty_repo"] = is_empty_repo
site.data["git"]["branch"] = branch
site.data["git"]["last_hash"] = last_hash
site.data["git"]["last_hash_long"] = last_hash_long
site.data["git"]["last_tag"] = last_tag
site.data["git"]["commits_since_last_tag"] = commits_since_last_tag
site.data["git"]["repo_dirty"] = repo_dirty
site.data["git"]["tag"] = last_tag
site.data["git"]["last_commit_date"] = last_commit_date
site.data["git"]["last_commit_year"] = last_commit_year
site.data["git"]["last_commit_time"] = last_commit_time
site.data["git"]["last_commit_tz"] = last_commit_tz
site.data["git"]["first_commit_date"] = first_commit_date
site.data["git"]["first_commit_year"] = first_commit_year
site.data["git"]["first_commit_time"] = first_commit_time
site.data["git"]["first_commit_tz"] = first_commit_tz
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment