Skip to content

Instantly share code, notes, and snippets.

@FergusInLondon
Created April 29, 2020 13:01
Show Gist options
  • Save FergusInLondon/70abfd98133e187f728ce91df174d9ef to your computer and use it in GitHub Desktop.
Save FergusInLondon/70abfd98133e187f728ce91df174d9ef to your computer and use it in GitHub Desktop.
Get git stats for static site generation.
const run = require('util').promisify(require('child_process').exec)
async function getGitStats() {
const {stdout: refHash } = await run('git rev-parse HEAD')
const {stdout: message } = await run(`git log --format=%B -n 1 ${refHash}`)
const {stdout: branch } = await run('git rev-parse --abbrev-ref HEAD')
return { refHash, message, branch }
}
getGitStats().then(console.log)
@FergusInLondon
Copy link
Author

FergusInLondon commented Apr 29, 2020

When executed in a directory located within a git repository, an object is returned with the commit message, commit hash, and branch name. Useful for displaying meta-information in static site builds.

Requires no external dependencies - util and child_process are both core to Node.

Example Output:

➜  example git:(master) ✗ node git.js
{
  refHash: '03046e948c33f96f9ed61939c699e2dbf230bb01\n',
  message: '[ deps ] bump deps\n\n',
  branch: 'master\n'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment