# Host a main branch without a forge

> Keep main in a plain bare Git repository over SSH, and let the host itself refuse any rewrite of main.

Source: https://getbay.io/docs/host (HTML).

`bay host` creates a bare Git repository to serve as your remote. Its main is append-only, and Git on the host enforces that on every push. With a key registry, the host also refuses any main commit that is not a runner's merge. Use it when you have no forge, or no route to one.

It is plain Git, not a server. bay adds no protocol, no accounts and no daemon. Git over SSH carries every push and fetch. Who may push at all stays with SSH and the host's file permissions.

## When to host a main branch

- **No forge**: you keep this repository on no GitHub, GitLab or other forge.
- **Air-gapped machines**: the machines have no route out. Move changes with `bay share --bundle`.
- **A bare repository over SSH**: you already keep repositories on a server you run.
- **Rules on the host**: you want the host itself to refuse a rewrite of main.

## Set up the host

1. **Check Git** The host needs Git 2.54 or newer. Below that, Git ignores hooks set in configuration. Below 2.54, `bay host` refuses with `TOOL_BELOW_FLOOR` and creates nothing.

2. **Create the repository** Run `bay host` on the host. The path must be empty or absent. `--main` names main, `main` by default. Without `--object-format`, it takes the object format of the repository you run it in, else Git's default. Clones must match the host's format, because SHA-1 and SHA-256 repositories cannot share objects. With `--upstream <url>`, the host becomes an origin of a hosted remote, as Serve your machines from an origin explains.

```sh
bay host /srv/git/app.git --main main
```

3. **Check the host** Run `bay doctor` in the new repository. Its `trunk-hook` row must be green.

```sh
cd /srv/git/app.git && bay doctor
```

4. **Point your clones at it** Add the host as a remote. Commit `.bay/config.toml` and `.bay/policy.toml`, then push main. A clone that added the remote by hand records main with `bay install --main main --remote origin`. A fresh `git clone` of the host needs no step: `bay host` made main its default branch.

```sh
git remote add origin ssh://git.example.com/srv/git/app.git
git push origin main
bay install --main main --remote origin
```

5. **Add the registry** Push the first main before the first registry. With a registry, each new main commit must be a runner's merge, the first ones too. Before it holds a registry, the host judges only main and promotion targets. So anyone who can push may create the first registry. So create the registry yourself, right after the first main, with `bay enrol`. Keys and the registry explains the classes and the rules.

```sh
bay enrol registry admin ~/.ssh/bay-registry.pub   # with bay.registryKey set to its private half
```

## What `bay host` sets

The hooks call the bay that ran `bay host`, by its full path. Keep bay at that path on the host. Update bay here before the runners that push to it: a hook older than 0.6.104 refuses their lease refs.

| Setting | What it does |
|---|---|
| `--ref-format=reftable` | Stores refs in reftable, whose updates of several refs in one push are atomic. |
| `hook.bay-scope` on `pre-receive` | Keeps main append-only and judges each `[[target]]` ref, on every host. It also judges every bay ref once the host holds a key registry. |
| `bay.main` | The main branch name that the hook reads. Without it, they guard the branch the host's `HEAD` names. |
| `core.logAllRefUpdates = always` | Keeps a reflog for every ref. The hook reads it to judge an adoption by the host's clock. |
| `receive.advertiseAtomic = true` | Lets a merge move main and its merged marker in one atomic push. |
| `core.commitGraph = true` and `gc.writeCommitGraph = true` | Git reads a commit-graph to walk history faster, and `git gc` writes one. |
| `commitGraph.changedPaths = true` and `commitGraph.changedPathsVersion = 2` | Each graph Git writes carries changed-path Bloom filters, version 2. |
| `maintenance.commit-graph.enabled = true` | Git's own maintenance writes and refreshes the graph after a push. Git's default waits until 100 commits are outside it. bay runs no maintenance itself. |
| With `--upstream` | Also `bay.upstream`, `bay.upstreamMode = through`, `hook.bay-origin` on `post-receive`, `uploadpack.allowFilter = true`, `uploadpack.allowAnySHA1InWant = true`, `core.fsync = committed` and `reftable.lockTimeout = 5000`. The any-object setting lets a blob-less runner fetch a delta base by its id, over SSH too. `bay host serve` sets it on an origin made before. |

## What the main hook refuses

- A push that is not a fast-forward of main.
- A deletion of main.
- A new main after the old one held merged changes.
- Any main update the hook cannot judge. An unknown outcome is a refusal.

## What stays rewritable

The main hook judges main only. `refs/bay/*` and `refs/notes/bay/*` stay rewritable, so retention cuts still work. So bay does not set `receive.denyNonFastForwards` or `receive.denyDeletes`. Those would block retention cuts too. With a key registry, an evidence record is never withdrawn. The host refuses a notes push that drops or changes an earlier record with `EVIDENCE_WITHDRAWN`.

## With a key registry

Once the host holds `refs/bay/registry`, the scoped hook judges every push. Git does not tell a hook who pushed. So the hook judges the signatures on what the push adds. Any refusal fails the whole push.

- Only a `runner` key can move main. Each new main commit must be a merge that a runner signed.
- Each merge needs a current agent-signed request and passing producer-signed evidence.
- Each merge must create its merged marker in the same push, and a change merges only once.
- Only a key in the current `registry` file can advance `refs/bay/registry`, and only by fast-forward.
- A live ref takes only a summary of its own machine, signed by the agent key it names.
- Every bay ref name must use bay's fixed alphabet.

> Without a registry, the host keeps main append-only and lets each promotion target move only to a promotable commit.
> Under `[policy] signing = "approvals"` on main, the hook checks signatures only on registry and evidence pushes, and main stays append-only.
> Anyone who can push may then add commits to main.

## Compared with a forge

| Rule | On a main branch from `bay host` | On a forge |
|---|---|---|
| Main is append-only | The main hook refuses a rewrite or a deletion. | Turn on the forge's branch protection for main. |
| Only merges reach main | With a registry, the scoped hook refuses any commit that is not a runner's merge. | Let only the runner's identity push to main. `bay status` names any other commit. |
| A removed key | Refused on every later push. | bay's own checks apply it after their next fetch. |

## Share a change

`bay share` hands a change and its evidence to another agent. It pushes `refs/bay/changes/<change>` and every evidence notes ref to a remote, all or none. `--remote` names that remote. The default is the clone's `bay.remote`, else `origin`. `--bundle` writes the same refs to one file instead, for a machine with no route.

The other agent fetches the refs with plain Git, then takes the change over with `bay start <change> --adopt`. While the sender still works on the change, the other agent can take it only after the takeover window.

```sh
bay share c7k2m4qa --bundle c7k2m4qa.bundle

# on the other machine
git fetch c7k2m4qa.bundle 'refs/bay/changes/*:refs/bay/changes/*' \
  'refs/notes/bay/evidence/*:refs/notes/bay/evidence/*'
bay start c7k2m4qa --adopt
```

## Refusals

`bay host` refuses below the Git floor. The host refuses a push with the reason on stderr.

| Code | Meaning |
|---|---|
| `TOOL_BELOW_FLOOR` | `detail.tool` is `git`: Git on the host is older than 2.54. `bay host` created nothing. |
| `HOOK_TIMEOUT` | The scoped hook ran past its time bound and refused the push. bay retries the same candidate. |
| `QUEUED` | Another operation holds the head of the queue, so this merge waits its turn. |
| `REGISTRY_CHANGED` | The registry removed a key behind this merge after the runner signed it. |
| `HOOKS_MISSING` | `bay doctor` found a bare repository with no main hook. |
| `EVIDENCE_WITHDRAWN` | With a registry, a push dropped or changed an earlier evidence record. bay never withdraws evidence. |

## Limits

- **Git on the host**: 2.54 or newer.
- **Hook time**: the scoped hook judges a push for at most 30 seconds by default. Set `bay.hookTimeoutS` on the host.
- **Live refs**: with a key registry, a host holds at most 256 live refs.
- **Operator names**: machine, runner, producer, reviewer and approver names use `a-z`, `0-9` and `-`, at most 32 characters.
