Skip to content

Instantly share code, notes, and snippets.

@korzio
Created May 5, 2019 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korzio/51250cf9b26820d1d372bb0922c4d4dc to your computer and use it in GitHub Desktop.
Save korzio/51250cf9b26820d1d372bb0922c4d4dc to your computer and use it in GitHub Desktop.
import {Hook} from '@oclif/config'
import * as nodegit from 'nodegit'
import * as path from 'path'
const hook: Hook<'commit'> = async function ({id, file: fileName}) {
const directoryName = process.cwd()
const repo = await nodegit.Repository.open(path.resolve(directoryName, '.git'))
const index = await repo.refreshIndex()
await index.addByPath(fileName)
await index.write()
const oid = await index.writeTree()
const head = await nodegit.Reference.nameToId(repo, "HEAD")
const parent = await repo.getCommit(head)
const author = nodegit.Signature.now("Scott Chacon", "schacon@gmail.com")
const committer = nodegit.Signature.now("Scott A Chacon", "scott@github.com")
const commitId = await repo.createCommit("HEAD", author, committer, `${id} note ${fileName}`, oid, [parent])
console.log("New Commit: ", commitId)
}
export default hook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment