Skip to content

Instantly share code, notes, and snippets.

@mattyclarkson
Created January 26, 2021 15:12
Show Gist options
  • Save mattyclarkson/5ad1e21b97f48b3481f7d2c75af00bb4 to your computer and use it in GitHub Desktop.
Save mattyclarkson/5ad1e21b97f48b3481f7d2c75af00bb4 to your computer and use it in GitHub Desktop.
A husky hook to add `Change-Id` to commit messages
/*
"husky": {
"hooks": {
"commit-msg": "node change-id.js -E HUSKY_GIT_PARAMS"
}
},
*/
const [,, arg, key] = process.argv
const { equal: assert } = require('assert').strict
assert(arg, '-E')
assert(key, 'HUSKY_GIT_PARAMS')
const filepath = process.env[key]
assert(filepath, '.git/COMMIT_EDITMSG')
const { existsSync: exists } = require('fs')
const { execSync: run } = require('child_process')
const gerrit = '.git/hooks/gerrit-commit-msg'
if (!exists(gerrit)) {
const { repository: { url: remote } } = require(process.env.npm_package_json)
const { URL } = require('url')
const url = new URL(remote)
const { port } = url
assert(url.protocol, 'ssh:')
url.port = ''
url.pathname = ''
const host = `${url}`.replace('ssh://', '')
console.error('Downloading Gerrit `Change-Id` commit message hook')
run(`scp -p -P ${port} ${host}:hooks/commit-msg ${gerrit}`)
}
run(`${gerrit} ${filepath}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment