Handling Events
A Git instance is an EventEmitter. Every git operation emits an event with a request
object you must accept or reject.
The events
| Event | Emitted when |
|---|---|
push | somebody runs git push to a branch |
tag | somebody runs git push --tags |
fetch | somebody runs git fetch, git pull or git clone |
info | the repo is queried for refs before any other command |
head | the repo's HEAD is queried |
Accept or reject
Exactly one listener must call .accept() or .reject(). If there are no listeners for
an event, it is accepted automatically.
repos.on("push", (push) => {
if (push.branch === "main") {
push.reject(403, "pushing to main is not allowed")
} else {
push.accept()
}
})reject(code, message) sends the HTTP status code and message back to the git client.
What's on a push
repos.on("push", (push) => {
push.repo // "my-project" — repository name
push.commit // "9c1d9f2…" — commit sha being pushed
push.branch // "main" — branch name
push.headers // request headers
push.method // "POST"
push.url // "/my-project/git-receive-pack"
push.accept()
})Tags
A tag event is emitted for git push --tags. It carries version instead of branch:
repos.on("tag", (tag) => {
console.log(`tag ${tag.version} on ${tag.repo}`)
tag.accept()
})Fetch
repos.on("fetch", (fetch) => {
console.log(`fetch ${fetch.repo}/${fetch.commit}`)
fetch.accept()
})info and head
info and head fire before the underlying command runs, letting you gate access or
lazily create repositories:
repos.on("info", (info) => {
console.log(`info for ${info.repo} (exists: ${info.exists})`)
info.accept()
})See Request Objects for the full list of properties available on each event.