gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.38k stars 7.49k forks source link

Add .GitInfo variable for latest commit #9738

Open hwittenborn opened 2 years ago

hwittenborn commented 2 years ago

I'd like to have the latest commit in my Git repository at the bottom of my footer that can act as a quick link to the latest change.

Unlike the current system, I don't want this to be specific to a certain page, I just want the latest commit in my current branch to be shown.

jmooring commented 2 years ago

You can do this today by setting env vars...

template code ``` {{ with site.Params.git.last_commit }}

Last commit:

  • Author: {{ .authorname }}
  • Date: {{ .date }}
  • Hash: {{ .hash }}
  • Subject: {{ .subject }}

{{ end }} ```
get-last-commit.sh ``` #!/usr/bin/env bash export HUGOxPARAMSxGITxLAST_COMMITxAUTHORNAME=$(git log -1 --format=%an) export HUGOxPARAMSxGITxLAST_COMMITxDATE=$(git log -1 --format=%cI) export HUGOxPARAMSxGITxLAST_COMMITxHASH=$(git log -1 --format=%H) export HUGOxPARAMSxGITxLAST_COMMITxSUBJECT=$(git log -1 --format=%s) ```

To build your site:

source get-last-commit.sh 
hugo server

This will show the latest commit. For example:

Author: John Doe
Date: 2022-04-02T09:03:36-07:00
Hash: 3707c2b020903d4fd7c55929c7765f7eadf0d2db
Subject: Update Node.js dependencies
hwittenborn commented 2 years ago

My only problem with that is that it doesn't feel as integrated as the current .GitInfo setup. With that, I don't need to set any environment variables, and I think it'd help if this behaved the same so I don't need to set an environment variables on every build (which could be tedious for contributors to my Hugo sites).

hpsof commented 1 year ago

@jmooring commented on Apr 2, 2022

batch-file e.g. set_git_commit_info.bat with content:

@echo off
for /f %%G in ('git rev-parse --short HEAD') do Set HUGOxPARAMSxGITxLAST_COMMITxHASH=%%G
git log -1 --format=%%%an  >> out.tmp
for /f "tokens=1 delims=:" %%G in (out.tmp) do set HUGOxPARAMSxGITxLAST_COMMITxAUTHORNAME=%%G
del out.tmp
git log -1 --format=%%%cI  >> out.tmp
for /f "tokens=1 delims=:" %%G in (out.tmp) do set HUGOxPARAMSxGITxLAST_COMMITxDATE=%%G
del out.tmp
git log -1 --format=%%%s  >> out.tmp
for /f "tokens=1 delims=:" %%G in (out.tmp) do set HUGOxPARAMSxGITxLAST_COMMITxSUBJECT=%%G
del out.tmp
echo. -----------------------------------------------------
echo. hash .....: %HUGOxPARAMSxGITxLAST_COMMITxHASH%
echo. authorname: %HUGOxPARAMSxGITxLAST_COMMITxAUTHORNAME%
echo. date .....: %HUGOxPARAMSxGITxLAST_COMMITxDATE%
echo. subject   : %HUGOxPARAMSxGITxLAST_COMMITxSUBJECT%

ouput: image

see also : https://ss64.com/nt/for_cmd.html https://teamcity-support.jetbrains.com/hc/en-us/community/posts/4414623708818-Save-output-from-a-command-to-a-temporary-environment-variable