CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
GITv is self-hosted Git hosting: pushes over OpenSSH, a read-only web browser, and read-only HTTPS cloning of public repositories. It runs on one Fedora droplet (git.um9i.dev) behind Caddy.
Commands
Run everything from the workspace root. The web tests run the gitv binary, which only a workspace build produces.
# One test, by package and target:
Run locally against a scratch tree (admin commands skip the root check when GITV_REPOS is set):
&&
GIT_SSH_VARIANT=simple GIT_SSH_COMMAND=/scripts/fake-ssh
When dependencies change, run scripts/third-party-licenses (needs cargo install cargo-about --locked --features cli) and commit the regenerated THIRD-PARTY-LICENSES. CI fails if it is stale, and it also runs cargo audit. The GitHub Actions in .github/workflows/ci.yml are pinned to commit hashes with version comments; update the hash and comment together.
Architecture
Three crates. gitv depends only on gitv-core and a few small crates, so building it never compiles the web stack.
gitv-core: everything both binaries share:- configuration (
config.rs) - name rules (
valid_name) - working through directory handles
- running git with a deadline and an output cap
publish_file, which writes a file atomically
- configuration (
gitv: the SSH forced command (gitv serve <owner>) and the root admin CLI.gitv-web: the axum browser and the smart-HTTP clone service. Its library exposes onlyauthandrender(for tests and the CLI). The router, views and everything else live in the binary (main.rsplus modules).
Invariants that span the codebase
- Directory handles, never paths. Repositories live at
<root>/<owner>/<name>.git.- Code that finds, creates, deletes or lists repositories opens directories with
O_NOFOLLOWthroughgitv_core(open_owner,repo_dir/repo_in,open_dir/open_dir_at,try_entries,remove_tree_at,renameat2withRENAME_NOREPLACE). - It keeps a
RepoDir, and git runs inside that open directory (fchdirinpre_exec). RepoDir.pathis for messages and cache keys only; never reopen it.- Symlinked owner or repository folders are refused.
- Code that finds, creates, deletes or lists repositories opens directories with
- All git goes through core.
git_command/git/git_capped(orconfigureforgit-receive-pack/git-upload-packrun directly) give a cleared environment with no user or system config and literal pathspecs.- A watchdog kills any command past
GIT_TIMEOUT. A page-wide deadline is set withRepoDir::limit_until. - Web pages read git output through
Reader::git_bytes, capped atPAGE_OUTPUT_LIMIT(8 MiB) because the service runs underMemoryMax=640M.
- Paths and ref names are bytes.
- Git allows any bytes but
/and NUL in names. Query values decode toVec<u8>(urls::parse_query), are checked withclean_path, and reach git asOsStr. - Only display uses
lossy(). - Plain names that could be misread (40/64-hex,
refs/…,HEAD) are linked as qualified refs (Reader::branch_ref/tag_ref).Reader::resolvelooks ids up as objects only.
- Git allows any bytes but
- Visibility is a
gitv-publicmarker file in the repository directory. For a signed-out visitor, a private repository and a missing one give the same answer (View::LoginRequired), so existence never leaks. Smart HTTP serves public repositories only, read-only. The description is git'sdescriptionfile, read and replaced through the directory handle (RepoDir::description/set_description); git'sUnnamed repositoryplaceholder counts as none. - No scripts, anywhere.
- Every HTML response sends
SECURITY(CSPdefault-src 'none', noscript-src). - Raw files get
RAW_SECURITY, which addssandbox. - Templates are askama and auto-escape.
|safeis only for HTML produced by the templates themselves or byrender.rs, which escapes raw HTML in Markdown and rewrites links. - Don't add JavaScript or inline styles to pages.
- Every HTML response sends
gitv (SSH and admin)
- Access.
gitv add-keywritesrestrict,command="<gitv> serve <owner>"lines to/etc/gitv/authorized_keys. A key reaches only its owner's repositories, andserveparsesSSH_ORIGINAL_COMMAND.gitv keysandremove-keylist and revoke keys by SHA-256 fingerprint; every change to the keys file goes throughedit_keys(thekeys.lockflock, thenpublish_file, thenrestorecon). - Pushes.
- Pushing to a missing name creates the repository. It is built in a private staging folder and published by rename.
- A push holds the repository's
flockshared. The backup takes it exclusive. - A push runs with
PUSH_CONFIG(no hooks, reflogs on every ref, unreachable objects kept 30 days), and appends ref changes togitv-push.log. A repository with agitv-protectedmarker (gitv protect) also getsPROTECTED_CONFIG(no non-fast-forward or deleting branch updates; git applies neither to tags).
- Deletion moves a repository to
<owner>/.trash/<name>.<unix time>.gitv purge, run daily by a timer, removes entries older than 30 days. After that,gitv-restore <snapshot> DIR OWNER/NAMEcopies one repository out of a backup, andgitv importpublishes it through the same staging and no-replace rename ascreate(Contents::CopyOf). - Logging. SSH users only ever see generic errors. Details go to syslog, or to
$GITV_LOGwhen it is set.
gitv-web
- Routing (
main.rs). The routes form four groups:- Public, exact paths: the assets and
/login. - Smart HTTP:
info/refsandgit-upload-pack. - Browse: the
login::browsemiddleware puts anExtension<bool>"signed in" on each request. - Private: behind
login::require_login.
- Public, exact paths: the assets and
- Rendering and capacity.
- Pages render on the blocking pool via
blocking(). Renders, downloads and transfers each draw on acapacity::Pool, which reserves slots for signed-in sessions and caps each anonymous client. - The client is an IPv4 address or IPv6 /64, taken from
X-Forwarded-Foronly on loopback connections (throttle::Client).
- Pages render on the blocking pool via
- Views (
views/). Each returns aView.Repoholds aReader(git queries, one ref listing per request) and builds URLs and shared fragments. - Cache (
cache.rs). An LRU keyed by immutable object ids (plus the repository, when the content contains links). Concurrent requests for a missing key make it once. - Sign-in. One account file (
$GITV_WEB_AUTH, default/var/lib/gitv-web/login) holds an Argon2id password hash and a TOTP secret.auth.rs: credentials, the record of used codes, and the trusted-device token.login.rs: handlers, in-memory sessions, throttling.setup.rs: thesetupandpasswordcommands.- Sessions remember the account's fingerprint, so changing the password ends every other session.
- The trusted-device cookie lets a browser bypass only the global cap of 60 failed sign-ins an hour (
throttle.rs).
Configuration and environment
/etc/gitv/gitv.conf($GITV_CONFIG) holdshostandcommand. Parsing is strict: an unknown key is an error, and both binaries load it at startup.- Overrides used by local runs and tests:
GITV_REPOS,GITV_KEYS,GITV_WEB_AUTH,GITV_HOST,GITV_LOG. - The backup scripts also read
GITV_BACKUP_DIR,GITV_BACKUP_SSH_DIR,GITV_BACKUP_REQUIRE_MOUNTandGITV_BACKUP_LOCK_WAIT.
Tests
- Helpers:
testing/host.rsis shared by both packages' integration tests through#[path]includes intests/common/mod.rs. It builds a scratch installation (Host) and a stand-in for ssh.crates/gitv-web/tests/common/web.rsstarts a realgitv-webon port 0 and speaks raw HTTP/1.1. - Scratch folders: integration tests write under
CARGO_TARGET_TMPDIR. Unit tests use the system temporary folder.
Deployment
The files in deploy/ are the source copies of the host's configuration: systemd units and timers, the Caddyfile, the sshd drop-in, gitv.conf, and the backup and restore scripts. README.md has where each one goes, plus the backup and disaster-recovery procedures.
Pushing master to origin (GitHub) and gitv (the server itself) publishes source only; deploying is a separate step, over SSH as root@git.um9i.dev. The git@ account has a forced command and can't deploy.
- Check access:
ssh -o BatchMode=yes root@git.um9i.dev 'id -un && systemctl is-active gitv-web'should printroot, thenactive. - Build locally:
cargo build --locked --release --bins.- The droplet has no Rust toolchain.
- Its glibc is older than this machine's, so check with
objdump -T target/release/<bin> | grep -o 'GLIBC_[0-9.]*' | sort -uV | tail -1.
- Stage the files:
- Copy the binaries (and any changed
deploy/files) to a staging folder on the droplet. - Verify their checksums there.
- Keep the previous versions for rollback.
- Copy the binaries (and any changed
- Replace atomically:
- Write each file beside its target, then rename it over the target (
install -m 0755 … .name.new && mv -f), so a push mid-deploy never findsgitvmissing. - Run
restoreconon the replaced files.
- Write each file beside its target, then rename it over the target (
- Restart and verify:
- Run
systemctl restart gitv-web. - Check that the HTTPS homepage loads, and that
git ls-remote gitv HEADmatchesmaster. - A binary-only release needs no sshd restart.
- Restarting
gitv-websigns everyone out, since sessions are held in memory.
- Run
If a release changes units, timers, backup scripts or the Caddyfile, install those explicitly too (and systemctl daemon-reload for units).
Conventions
- Commit messages: a plain imperative subject saying what changed for the user or operator, then a body explaining why.
- Code comments explain why, in plain sentences, especially for security- and race-related choices; keep that style.