node-git-server logonode-git-server

Git

The main class. Create an instance, attach event listeners, then listen().

import { Git } from "@batchfy/node-git-server"
 
const repos = new Git(repoDir, options)

Constructor

new Git(repoDir: string | ((repo?: string) => string | Promise<string>), options?: GitOptions)

repoDir

Either a base directory (string) that contains your bare repositories, or a resolver function that maps a repository name to a directory. The resolver may be async (return a Promise<string>).

options

OptionTypeDefaultDescription
autoCreatebooleantrueCreate the target repository on demand if it does not exist.
authenticatefunction—Gate requests. See Authentication.
checkoutbooleanfalseCreate working-tree repos (git init) instead of bare repos.

Methods

listen(port, options?, callback?)

Start the server. options.type is "http" (default) or "https"; for HTTPS also pass key and cert. Returns the Git instance.

repos.listen(7005, { type: "http" }, () => {
    console.log("listening")
})

close()

Stop the server. Returns a Promise that resolves once it has closed.

await repos.close()

list()

List the repositories in the base directory. Supports a callback or a Promise:

const repos = await git.list()
// or
git.list((err, repos) => {})

exists(repo)

Resolve whether a repository exists on disk. Returns a Promise<boolean>.

if (await git.exists("my-project")) {
    /* ... */
}

create(repo, callback?)

Create a new (bare) repository. Async; also accepts a completion callback.

await git.create("my-project")

handle(req, res)

The low-level request handler. Use it to mount the git server inside an existing http/https server:

import http from "node:http"
 
const server = http.createServer((req, res) => repos.handle(req, res))
server.listen(7005)

getType(service)

Map a git service to an operation type: "upload-pack" → "fetch", "receive-pack" → "push".

Events

Git extends EventEmitter and emits push, tag, fetch, info and head. See Handling Events and Request Objects.