Skip to content
Source
Sign inSign up

Set up the git servers

Site administration

Set up the git servers

The git servers keep the git data of every repository: real git on three servers of your own. The Worker sends each git request and each git read of a page there. Every repository is on them since 2026-11-27, and a new repository starts there.

33 min read

There is one server in each region: us, eu and asia. One server is the primary, which takes every push and write. The other two are replicas, which serve reads. You set up the servers once. This page tells how, and how they work.

Other tasks:

You need:

  • The Source repository with its packages (pnpm install), and wrangler signed in to the Cloudflare account of the deploy.

  • Go 1.27 or later on your computer. install.sh builds gitd with it.

  • An SSH key at ~/.ssh/source_git_ed25519. To use another key, set GITD_SSH_KEY to its path.

  • An account at a cloud provider, for example Hetzner Cloud.

  • To be signed in to the analytics server. See Sign in to the server.

  • psql, and neonctl signed in to the Neon organization of the forge. The rows of the forge are in Postgres on Neon (README.md, "Database"). The steps that read or change a row of a repository use it. Put the URL of its database in PG_URL. The URL holds a password, so keep it in your shell only:

    PG_URL="$(neonctl connection-string main --project-id tiny-unit-38538247 --role-name app --database-name source)"

Run the commands for your computer from the root of the Source repository, in Git Bash or a Linux shell.

1. Make the shared secret

gitd and the Worker use one secret: the Worker sends it to gitd, and gitd sends it back to the Worker. Keep it in a file that only you can read:

(umask 077 && openssl rand -hex 32 > ~/gitd-secret)

Keep this file out of the repository. The next steps read it.

2. Create the three servers

  1. Create one Linux server in each region, for example gitd-us-1, gitd-eu-1 and gitd-asia-1. Use Debian 13. For example, use a Hetzner Cloud server with dedicated vCPU.
  2. When you create each server, add your SSH key, and give the file apps/gitd/deploy/cloud-init.yaml as its user data. On the first boot, it installs git and cloudflared, and makes the user git that owns the repositories.
  3. Give each server a firewall that lets in only SSH (TCP port 22). The primary sends the repositories to the replicas over SSH.
  4. Write down the IPv4 address of each server.

install.sh has the addresses of the first three servers as defaults. If your servers have other addresses, set them in your shell before each install.sh command:

export GITD_IP_US=<us IPv4> GITD_IP_EU=<eu IPv4> GITD_IP_ASIA=<asia IPv4>

3. Create a tunnel and a VPC service for each server

The Worker reaches gitd through a Cloudflare Tunnel, so the servers need no open HTTP port. The wrangler login cannot create tunnels. For this step, make an account API token with these permissions:

  • Cloudflare Tunnel > Edit, to create the tunnels.
  • Connectivity Directory > Admin, to create the VPC services.

Then do these steps once for each region: us, eu and asia.

  1. Give the token to wrangler, in this terminal only:

    export CLOUDFLARE_API_TOKEN=<API token>
  2. Create the tunnel. The command prints the id of the tunnel:

    pnpm -C apps/web exec wrangler tunnel create gitd-<region>

    The command also tells you to add ingress rules. The tunnel needs none: the VPC service of the next step sends the requests.

  3. Create the VPC service. It sends the requests of the Worker through the tunnel to port 8420 of the server, where gitd listens. The command prints the id of the service:

    pnpm -C apps/web exec wrangler vpc service create gitd-<region> --type http --tunnel-id <tunnel id> --ipv4 127.0.0.1 --http-port 8420
  4. Save the tunnel token in a file that only you can read. <account id> is the account_id in apps/web/wrangler.jsonc:

    (umask 077 && curl -s "https://api.cloudflare.com/client/v4/accounts/<account id>/cfd_tunnel/<tunnel id>/token" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | sed -n 's/.*"result":"\([^"]*\)".*/\1/p' > ~/gitd-<region>.tunnel)

Write down the service id of each region. Then close this terminal. In a new terminal, wrangler uses your wrangler login again.

4. Give the web Worker the secret and the bindings

Only the web Worker has the secret and a binding for each region. The runner Worker has no secrets, so it reaches gitd through the web Worker: its GITD_PROXY binding (in wrangler.runner.jsonc) calls the GitdProxy entrypoint, which adds the secret. While the seam of the CI jobs is on (live.seam lists ci_job), the git requests of a CI job's container go to the web Worker's RunnerGitHost entrypoint instead (its WEB_GIT_HOST binding), which checks the job token and calls gitd itself. Neither needs a step here: both bindings are in wrangler.runner.jsonc.

  1. Set the secret on the web Worker. Paste the content of ~/gitd-secret when the command asks for the value:

    pnpm -C apps/web exec wrangler secret put GITD_SECRET
  2. Put the three bindings in vpc_services in apps/web/wrangler.jsonc, next to CLICKHOUSE. The file has the bindings of the first three servers: change their service ids if your services have other ids.

    "vpc_services": [
      { "binding": "CLICKHOUSE", "service_id": "<ClickHouse service id>" },
      { "binding": "GITD_US", "service_id": "<us service id>" },
      { "binding": "GITD_EU", "service_id": "<eu service id>" },
      { "binding": "GITD_ASIA", "service_id": "<asia service id>" }
    ]
  3. Merge this change to main. The ci/deploy job deploys it. See "Deploying" in README.md.

5. Install gitd on each server

Install the replicas first, then the primary. Each server lets in the SSH keys of the two other servers, so that each of them can become the primary in a failover. A server reaches its peers only after they are installed too.

sh apps/gitd/deploy/install.sh install eu "$GITD_IP_EU" ~/gitd-secret ~/gitd-eu.tunnel
sh apps/gitd/deploy/install.sh install asia "$GITD_IP_ASIA" ~/gitd-secret ~/gitd-asia.tunnel
sh apps/gitd/deploy/install.sh install us "$GITD_IP_US" ~/gitd-secret ~/gitd-us.tunnel
  • install.sh builds gitd, copies it to the server with its systemd units, and starts it and the tunnel.
  • Each command ends with a healthz: line, for example healthz: {"region":"us","role":"primary","behind":false,"version":0,"term":0,"primary":"us","draining":false,"kept":0}. A replica says "behind":true until the primary runs and sends it heartbeats.
  • Each server gets GITD_BUNDLE_BASE, https://source.tanstack.com/_gitbundle by default. The server that is the primary writes the bundles of the default branch, which clones download from the CDN.
  • The first primary is us. To make another region the first primary, set GITD_PRIMARY to it for every install.sh command. After a failover, an install keeps the role of each server: the role file of gitd (/srv/git/repos/gitd-role) wins.

Then delete the tunnel tokens:

rm ~/gitd-us.tunnel ~/gitd-eu.tunnel ~/gitd-asia.tunnel

6. Run the smoke test

sh apps/gitd/deploy/install.sh smoke

The test pushes to a new test repository on the primary, and checks that each replica gets it. It also stops the heartbeats to one replica for 12 seconds, and checks that this replica then refuses reads. At the end, it deletes the test repository and prints smoke test passed.

7. Make sure that the Worker gets the calls of gitd

  1. Make a header file with the secret, so that the secret is not on the command line:

    (umask 077 && printf 'x-gitd-secret: %s\n' "$(tr -d '\r\n' < ~/gitd-secret)" > ~/gitd-header)
  2. Ask the Worker if it takes the calls of gitd. It answers {"ok":true}:

    curl -sS -H @"$HOME/gitd-header" https://source.tanstack.com/_gitsrv/health

    A 403 answer means that the secret of the Worker is not the secret of ~/gitd-secret.

  3. On the analytics server, count the host samples of each server in the last 10 minutes. gitd sends one every 10 seconds, so each region has about 60:

    clickhouse-client --password "$(cat /root/.clickhouse-admin)" --query "SELECT JSONExtractString(properties, 'region') AS region, count() FROM analytics.events WHERE event = 'gitd.host' AND timestamp >= now() - INTERVAL 10 MINUTE GROUP BY region"

The git servers are ready.

8. Make sure that a repository works

  1. Clone the repository:

    git clone https://source.tanstack.com/@<owner>/<name>.git
  2. Open the code page of the repository in the browser.

  3. On the analytics server, count the git requests of the repository by backend. The clone of step 1 counts as servers. Its first request asks for the capabilities of protocol v2, which hold no ref: the Worker keeps them for 10 minutes, so that request counts as cache when the repository was fetched in the last 10 minutes:

    clickhouse-client --password "$(cat /root/.clickhouse-admin)" --query "SELECT JSONExtractString(properties, 'backend') AS backend, count() FROM analytics.events WHERE event = 'git.request' AND repo_id = '<repo id>' AND timestamp >= now() - INTERVAL 1 HOUR GROUP BY backend"
  4. If the repository has a fork, open a pull request from the fork in the browser. For the private companion, open one of its private pull requests.

Done

The git servers serve every clone, fetch and push of every repository, and the git reads of its pages. Postgres keeps the issues, pull requests and comments of each repository, and a copy of its refs, in the schema repo.

What does not work on the git servers yet

These are refused on a repository on the git servers:

  • A pull request from a fork on the git servers that brings more than 20,000 LFS pointer files: its merge, and "Make public" when it is private.
  • Making a private pull request public when it brings more than 1 GiB of new git data, or a file of more than 16 MiB.
  • A push from a shallow clone to a fork whose branches are behind its upstream, in these cases: the clone is of a branch other than the upstream's default branch, the fork is a fork of a private repository, or the fork is on a replica that became the primary in a failover. A full clone has no such limit.
  • A pull request, a comparison or a commit whose list of changed files is more than 16 MiB as JSON. That is about 70,000 files with paths of about 40 characters. The page shows it as too large, and such a pull request does not open.

These work in another way:

  • A fetch with git protocol v0 or v1 is served, but with no partial clone: git gets every object. A fetch with protocol v2, the default of git, can be a partial clone.
  • A push with a very deep history is taken. The git servers have no limit on the history walk of a push.
  • The branches page lists the newest 10,000 branches.
  • The primary writes a bundle for clones from the CDN after a push to the default branch, at most once in 10 minutes. Only a public repository serves its bundles.

How it works

  • A read of the web Worker goes first to us, the server next to it, wherever the caller is. The web Worker runs in AWS us-east-1, next to the database, and every read crosses from there to a server. So run the us server near us-east-1: TanStack's runs in Ashburn. When that server is behind, busy or down, the read goes on to the primary, then to the others. While git reads at the edge are on, a clone or a fetch goes first to the server of the caller's continent instead.
  • A fetch gets only what the refs of the repository reach. gitd reads the wants of each fetch before git does, and refuses a want that no ref reaches with not our ref: a commit, a file or a folder that a force push or a deleted branch removed (apps/gitd/fetch_wants.go). git's protocol v2 does not check this itself. The hidden ref of a fork at its upstream does not count, and the refs of the upstream do. The check is one git rev-list for a clone or a usual fetch, about 15 to 30 ms on a repository of 90,000 objects. A commit that no ref reaches is refused at once. A lazy fetch of a partial clone wants old blobs: gitd checks them with the bitmap that maintenance writes, against HEAD first, then the branches and tags, then every ref. On a repository of 2.7 million objects and 30,000 pull request refs, these take about 20 ms, 2 s and 10 s. git uses the bitmap only while a ref tip is in the packs that it covers, so after a push it can skip it. A blob that the bitmap does not prove reachable, in a fork, or in a repository with no bitmap yet, gets a walk of every object that the refs reach (about 10 s on that repository) before gitd refuses it. The removed objects stay on the disk: maintenance prunes nothing.
  • A replica that does not have the newest version of a repository answers that it is behind. Then the Worker asks the primary. A read by commit id is the exception: a replica that has the commit answers the read, also while it is behind.
  • gitd lists the changed files of a comparison or a commit in one answer, with the lines that each file adds and deletes. The list has at most 200,000 files, and at most 16 MiB of them as JSON. The Worker reads the whole answer into its memory of 128 MB. A list of 16 MiB uses about 34 MiB of that memory while the Worker reads it. When gitd cuts the list, the page shows the change as too large. A merge never reads the code owners from a cut list. A branch rule that asks for code owners then refuses the merge.
  • The diffs of the files come from other gitd answers, each with at most 3,000 files and 16 MiB of patches. A page loads them in batches. REST, MCP and the factory read a comparison or a commit with its diffs in one answer. There, the files after the first 3,000 come with no diff, as the files after the budget of a diff on the Durable Object do.
  • Each server runs at most GITD_MAX_FETCHES clones and fetches at the same time. The default is 4 for each CPU of the server (16 on a server with 4 CPUs).
    • A clone or a fetch waits only for a place of the clones and fetches. Pushes, writes and the copies to the replicas never wait. A clone of a CI job waits as any other clone.
    • A new clone or fetch waits up to 5 seconds for a free place. Then the server answers that it is busy, and the Worker asks the next region.
    • If no server takes the fetch and one of them is busy, git prints TanStack Source: this repository is busy with big fetches. Try again in a minute. A Durable Object gives the same answer when its repository has too many big fetches.
    • To change the number, set GITD_MAX_FETCHES in your shell before you run install.sh install for the server. Each install writes the setting again, so set it at each install. If the value is not a whole number of 1 or more, gitd does not start, and install.sh stops at its health check.
    • Each gitd.host sample has the number (fetchPlaces), the clones and fetches that run now (fetchPlacesUsed), and the busy answers since the last sample (fetchBusy).
  • A clone or a shallow clone that has no objects yet, such as the checkout of a CI job, keeps its pack for an hour in the repository's folder (gitd-pack-cache). Every job of a CI run asks for the same commit, so the run builds that pack once: the jobs that come while it builds wait for it and then read it. A push gives the repository a new version, and a pack of the old refs is never served after it. A fetch that already has objects always gets a pack of its own. Every 10 minutes gitd deletes the packs older than an hour in every repository, and a pack over 1 GiB is never kept.
    • To turn the cache off, set GITD_PACK_CACHE=off in your shell before you run install.sh install for the server. Each install writes the setting again, so set it at each install.
  • Every hour, each server runs git maintenance on each of its repositories (gitd-maintenance.timer). The commit-graph of a repository has changed-path Bloom filters, so the history of a path reads fast on a long history: on a repository of 200,000 commits, the latest commit of a file, the count of its commits and the last commits of a folder are 2 to 5 times faster. A repository whose commit-graph has no filters gets them at the next maintenance. The filters are of version 2 (commitGraph.changedPathsVersion in the system git config, which install.sh sets).
  • Each server also runs at most GITD_MAX_READS reads of the pages at the same time, such as a folder, a file, a history or a diff. The default is 2 for each CPU of the server (8 on a server with 4 CPUs).
    • A read waits up to 5 seconds for a free place, as a clone does. Then the server answers that it is busy, and the Worker asks the next region.
    • If no server takes the read and one of them is busy, the read fails with GitdUnavailable (busy), as a read fails when no server answers.
    • The read have does not wait, because the Worker asks it right before a write. A read that comes before a write, such as the compare before a merge, waits as any other read.
    • A lower number keeps more CPU for pushes and fetches. A higher number runs more reads at the same time, but each read and each push takes longer when the CPUs are full.
    • To change the number, set GITD_MAX_READS in your shell before you run install.sh install for the server, as for GITD_MAX_FETCHES. Each install writes the setting again, so set it at each install. If the value is not a whole number of 1 or more, gitd does not start, and install.sh stops at its health check.
    • Each gitd.host sample has the number (readPlaces), the reads that run now (readPlacesUsed), and the busy answers since the last sample (readBusy).
  • Each server also makes at most GITD_MAX_ARCHIVES archives at the same time: the ZIP and tar.gz files of Download ZIP (gitd archive). The default is 2 for each CPU of the server (8 on a server with 4 CPUs).
    • An archive holds its place until its last byte reaches the Worker, at the speed of the person who downloads it, as a clone does. So archives have places of their own, and take no place of the reads: slow downloads never make the pages wait.
    • An archive waits up to 5 seconds for a free place. Then the server answers that it is busy, and the Worker asks the next region. If no server takes it, the download answers 503 with Retry-After.
    • git writes the archive in seconds, with one CPU. The whole download has 10 minutes. When git fails after the first byte, gitd cuts the connection, so the download fails and never ends as a whole file that is not.
    • To change the number, set GITD_MAX_ARCHIVES in your shell before you run install.sh install for the server, as for GITD_MAX_READS.
    • Each gitd.host sample has the number (archivePlaces), the archives that run now (archivePlacesUsed), and the busy answers since the last sample (archiveBusy).
  • Each server runs at most 2 imports into a new repository (gitd import-remote) at the same time, with places of their own. New repositories and imports gives the other limits of an import.
  • A push and a write go to the primary only. After each one, the primary sends each replica only the refs that moved, with their new objects. This copy goes to the gitd of the replica, through an SSH forward.
    • At the same time, the primary tells each replica that a new version is coming. Until a replica has the copy, it answers reads as behind.
    • The push or the write ends when one replica has the copy and each replica knows that the version is coming. It waits at most 10 seconds for the copy.
    • A copy of every ref (git push over SSH) goes only to a replica that took that notice. A replica that did not take it gets the version after a heartbeat to it works again.
    • This copy names its term and its server in git push options. The pre-receive hook of the replica lets it in only from the primary of the term in its role file. So a copy of an old primary that comes after a failover moves no ref, also on the new primary.
  • Each call of the Worker names the primary that the database names. A server that is the primary, but that the database does not name, answers reads as behind and refuses pushes and writes. So an old primary that starts again after a failover never serves its old refs.
  • The role of a server changes at run time. Each server keeps its role in its role file (/srv/git/repos/gitd-role): a term, and the region of the primary of that term. The Worker gives a new term at each failover (POST /role of gitd, which takes the shared secret).
  • Each call that the primary sends a replica names its term. A replica refuses a call of an older term. An old primary that starts again gets this refusal at its first heartbeat, and becomes a replica of the new primary.
  • A write, a push and a copy belong to the term in which they started. A server that lost its role sends no copy and no call of its old term. A write or a push that ends on it then answers not-primary, and puts its refs back, so that server never serves them. Only the Worker makes a server the primary: a replica that took a term from another server gives way to the Worker's word for that term.
  • Each minute, the web Worker checks the primary that the database names. When the primary does not answer 3 checks, 10 seconds apart, and no replica still gets its heartbeats, the Worker fails over to the newest replica. It moves git_primary of every repository in the database, and then the copy of the refs of each repository, in Postgres, follows the new primary.
  • After a failover, the new primary sends each replica every repository that it lacks. Until a replica has the version of the new term, the Worker reads that repository from another server. In this first sweep, the new primary compares the refs of each repository, not only the versions. It does not overwrite a copy that has a version that it never had, and it lists a repository that only the replica has: see Save the pushes that only one server has.
  • While the primary does not answer, a replica that is behind can answer a clone, a fetch or a page as stale, for 60 seconds after its last heartbeat. See Reads while the primary is down.
  • A hook call of a server that the database does not name as the primary is refused. So an old primary that comes back moves no ref of the Worker's copy of the refs.
  • The primary sends the whole repository with git push --mirror over SSH in these cases:
    • The primary is not sure that the replica has the previous version, for example after gitd starts on the primary.
    • A push or a write makes a new ref.
    • A write changes only the default branch.
    • The copy of the moved refs fails.
  • The git hooks of the primary ask the Worker for the push rules, and tell the Worker which refs moved. The Worker then moves the copy of the refs in Postgres, and writes the events of the push, such as CI triggers, to the outbox of the repository.
  • The hooks send these calls through gitd (POST /worker/<call> on its local address), which sends them on to the Worker over one connection that it keeps open: gitd posts its timing records to the Worker every 5 seconds over the same connection. So a push pays no DNS lookup, TCP connect or TLS handshake of its own. Cloudflare sometimes sends a new connection from the server to a far data center (from Ashburn, Paris or Stockholm, about 80 ms away), because the zone is on the Free plan. When the newest connection took more than 25 ms to connect, gitd closes it after its call, at most once every 30 seconds, and the next call connects again. A connection that reached a near data center stays. An older gitd that has no such route, or a gitd that is not running, gets the call from the hook itself, as before.
  • The pre-receive hook sends the Worker the facts that the push rules read, such as the new commits and the text of the new files. When the facts of a push do not fit one call, the hook sends them in parts first:
    • Each part holds commits, in the order of git, with at most 16 MiB of text and 100,000 changed files. The hook sends the text of a file only when the file is 1 MiB or less, so a larger file adds no text.
    • A commit that does not fit one part is cut into spans of its changed files. Each span goes in its own part, with the raw commit (its message and headers). The Worker checks each span as it checks a whole commit.
    • The Worker checks each part, and keeps what it found in Postgres until the last call. That call checks the branches and tags, with the findings of all the parts.
    • The Worker reads what all the parts found in one statement.
    • A push takes as many parts as it needs. The hook must send them all within 11 minutes, the time that it waits for the Worker. The Worker refuses a push that the hook could not send, with This push is too large for TanStack Source to check on its git servers. See Why a push was refused for the limits.
  • The pre-receive hook also sends the size of the git objects of the repository on the primary (git count-objects). The call that tells the Worker which refs moved sends this size too, after a push, a write or a fetch of GitHub sync. The size limit of the repository counts this size. For a push, it counts the size from the pre-receive hook of the push. For GitHub sync, it counts the size that the Worker got last, and at the limit sync brings no branches or tags down.
  • The Worker answers the call that tells it which refs moved as soon as the push is booked in Postgres, in one transaction that reads the records of the call and the default branch in one statement. The work after it (the repositories that watch the branches, the skills event, the CI schedules) runs after the answer, so the hook, and the person who pushed, do not wait for it. When that work never ends, the pending record of the push runs it again.
  • A fork is on the git servers too, on the primary of its upstream. So is a private companion, with only the default branch. For a pull request from such a fork, gitd reads the objects of the fork in the base repository for the compare and the merge. For "Update branch", gitd reads the objects of the base in the fork. For "Make public" of a private pull request, gitd makes the branch in the base from the objects of the companion. A merge, an update or "Make public" keeps only the objects that its new commit or branch needs. One that fails keeps none.
  • On a repository that syncs with GitHub, a person's push to a release branch goes to GitHub first. While the push waits in the pre-receive hook, the primary pushes its new commits to GitHub from the quarantine folder of the push (gitd push-incoming). The person's GitHub token is only in the body of that one call of the Worker, and the primary keeps no copy of it. The push lands here only after GitHub takes it. A push to GitHub can take minutes, so the pre-receive hook waits up to 11 minutes for the answer of the Worker.
  • A fork of a repository on the git servers is on the same primary. A fork of a public repository uses the objects of its parent there (git alternates). A fork of a private repository is a full copy. When it syncs, the primary first copies the new commits of the parent into it (gitd fetch-commit).
  • In a fork that uses the objects of its parent, the primary keeps the hidden ref refs/gitd-upstream/head. Before each push to the fork, it points this ref at the default branch of the parent. So git takes a push from a shallow clone of the parent (git clone --depth 1), also when the fork is behind its parent. No git client, Worker call or replica sees this ref.
  • When a person deletes a repository, the purge of deleted repositories asks each git server to delete its copy, within 5 minutes. While a server does not answer, the purge asks it again every 5 minutes.
  • Each minute, the web Worker compares the newest host sample of each server with the newest analytics event in ClickHouse. If a server is more than 2 minutes behind, the team gets the alert git-server-silent by email. A failover sends the alert git-failover, and a failover that cannot happen sends git-failover-stuck.

Git reads at the edge

A clone or a fetch from Asia or Europe pays for two long trips: from the caller to the web Worker in us-east-1, and from there to a git server. The edge Worker (gitflare-edge, README.md "Edge Worker") runs in the data center that takes the request. With git reads at the edge on, it answers a clone or a fetch itself, from the git server of the caller's continent: North and South America read us first, Europe and Africa read eu first, and Asia and Oceania read asia first. When that server is behind, busy or down, the read goes on to the primary, then to the others, as for the web Worker.

The edge Worker answers only these requests, for a signed-out caller, a person's token or an agent's token:

  • GET <repo>.git/info/refs?service=git-upload-pack, the start of a clone or a fetch.
  • POST <repo>.git/git-upload-pack, the clone or the fetch itself.

It passes every other request to the web Worker as before: a push, LFS, a job's token, the old dumb protocol, the git of skills, and every page and API.

The web Worker still decides who may read, with the same policy. The edge Worker asks it once (POST /_edge/git-access, which takes only the shared secret EDGE_SECRET) and keeps the yes for 60 seconds in the data center, by the SHA-256 of the caller's Authorization header and the repository. It never keeps the token itself, and it never keeps a no: a refused read goes to the web Worker, which answers it. After the answer, the edge Worker sends the web Worker the facts of a clone or a fetch (POST /_edge/git-done), which counts the traffic, gives the rank points and writes the audit row of an agent. The edge refuses the paths under /_edge/ from the internet.

The trade-off: a token that was revoked, a person who was removed from a repository, or a repository that was made private keeps its read access at the edge for up to 60 seconds. Writes never go through the edge.

To turn git reads at the edge on:

  1. Make the shared secret of the edge, and give it to both Workers. Run in apps/web. The secret is never printed:

    EDGE_SECRET="$(openssl rand -hex 32)"
    printf '%s' "$EDGE_SECRET" | pnpm exec wrangler secret put EDGE_SECRET --name gitflare
    printf '%s' "$EDGE_SECRET" | pnpm exec wrangler secret put EDGE_SECRET --name gitflare-edge
    unset EDGE_SECRET
  2. Give the edge Worker the secret of the git servers, the same value as GITD_SECRET of the web Worker (step 1 of this page):

    pnpm exec wrangler secret put GITD_SECRET --name gitflare-edge
  3. Turn on the site setting git.edge_reads:

    psql "$PG_URL" -c "INSERT INTO site_settings (key, value, updated_at) VALUES ('git.edge_reads', 'true', (extract(epoch from now()) * 1000)::bigint) ON CONFLICT (key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at"

    Each data center reads the setting again within about two minutes. To turn it off, set the value to 'false'. Without either secret, the edge Worker passes every request to the web Worker, whatever the setting says.

  4. Check a clone from far away, for example from the asia server: git ls-remote https://source.tanstack.com/@TanStack/form.git. The git.request event of the clone has edge: true and the region that answered (region), and its timings are the route rows that end in edge and the stream rows that end in edge <region>.

Why a push was refused

A person tells you that the git servers refused a push. Each refused push writes one line in Workers Logs, and this line names the limit.

  1. Open the Workers Logs of the web Worker gitflare.
  2. Filter on diag = "push.refused". Add repoId to see only one repository.
  3. Read kind. If kind is facts-cut, read cut too.

The line holds ids and counts only. It never holds a ref, a path, a commit message or a name.

FieldWhat it holds
repoIdThe id of the repository.
kindWhat refused the push (see the next table).
objectsThe number of new objects of the push.
bytesThe size of the new git data of the push, in bytes.
repoBytesThe size of the git objects of the repository on the primary, or null.
pagesThe number of parts of the facts, or null when the facts came in one call.
cutWhy the pre-receive hook cut the facts, or null.
refsThe number of refs that the push changes.
refusedThe number of refs that the push rules refused, or null.

The values of kind:

kindWhat refused the push
sizeThe size limit of the repository.
objectsThe push object limit of the site.
request-bytesThe push request limit of the site.
facts-cutThe pre-receive hook cut the facts at one of its limits (see cut).
facts-missingA part of the facts is not in the Worker.
importingAn import of the repository runs.
rulesThe push rules refused one or more refs.

The git gateway refuses a push over the push request limit before it gets to the git servers. It writes the same line, with kind = "request-bytes", at = "edge-gateway", and the Content-Length of the request in bytes.

When the pre-receive hook cannot send the facts of a push, the Worker refuses the push with This push is too large for TanStack Source to check on its git servers. The hook has no limit of its own on the commits, the files, the text or the parts of a push. Each value of cut is a limit of the Worker or of the time:

cutLimit
bodyThe lists of the new commits of each ref do not fit the last call, whose body the Worker takes in one piece (21 MiB): more than about 500,000 new commits. Also a part of a commit with one changed file that does not fit a call.
timeThe hook did not send all the parts within 11 minutes.
newObjectsMore than 10,000,000 new objects, the highest push object limit that a site admin can set. The Worker refuses such a push for its objects first.
commitBytesA raw commit (its message and headers) of more than 16 MiB, the text of one call.
sendA part did not get to the Worker.
readGit failed while the hook read the facts.

To get such a push in, push fewer commits at a time, oldest first. See Push a long history in parts. For commitBytes, make the message of that commit shorter.

New repositories and imports

  • A new repository gets the primary of the repositories on the git servers (git_primary) in the database. Then gitd create-repo makes an empty repository on the primary, with HEAD at main.
  • The primary writes the first commit (gitd commit-files). Postgres keeps only the copy of the refs.
  • If create-repo or the first commit fails, the repository is not made, and its name stays free. Its purge is queued, and deletes what the primary made. When the primary does not answer, the person reads The git servers of this repository do not answer. Try again in a few minutes.
  • While no repository is on the git servers yet (a new deployment), a new repository takes the server whose role is primary.
  • The default branch of an empty repository moves on the primary too.

An import into a new repository:

  • The web Worker lists the branches and tags of the remote, as for any import. Then the primary fetches them in one write (gitd import-remote), and the copy of the refs in Postgres follows. Then the HEAD of the primary moves to the default branch of the remote.
  • The fetch runs on the primary apart from the request of the Worker. The import asks again every 30 seconds, until the fetch ends. One import takes at most 3 hours.
  • At most 2 imports run at the same time on a server. The other imports wait for a place, and a wait is not a failure. "Import everything" of 50 repositories fetches them 2 at a time.
  • An import starts only while the server has 20 GiB of free disk. If it has less, the import tries again 3 times in about 8 minutes, then fails.
  • git checks each object that comes (fetch.fsckObjects), and takes only the fsck messages of old habits of tools, such as bad dates and zero-padded file modes. Then git fetch makes sure that the new refs reach only objects that it has. A refused object fails the import.
  • The fetch stops when the objects of the repository take more than its size limit. Nothing of a failed import stays on the server. An import first deletes what an import that gitd stopped left.
  • git follows no redirect and uses no plain http during an import. The web Worker follows the redirects of the remote when it lists the refs, and it checks each address. The primary fetches the URL where the listing ended, so a renamed GitHub repository still imports.
  • The token of a private remote goes only in the body of that one write. gitd gives it to the one git process that fetches, and never writes it to a file or a log.
  • gitd fetches https:// URLs only. An import from an http:// URL is refused before the repository is made, with New repositories start on the git servers, which import only from https URLs. Import the https URL of the repository.
  • During a failover, the import asks the new primary. After its fetch, only the move of HEAD is tried again.
  • A gitd without import-remote answers 404, so the import fails and fetches nothing.

Local development

pnpm dev runs one git server on your computer, so making a repository, git push and pull, and the code pages work in local development (README.md, "Run it locally"). apps/web/scripts/dev-gitd.ts starts it:

  • It builds gitd from apps/gitd with the Go on your PATH when that is Go 1.27 or later. Else it downloads Go 1.27.4 from go.dev once, checks its SHA-256, and keeps it in ~/.cache/tanstack-source.
  • gitd is the primary of region us, and there is no replica. It listens on 127.0.0.1:8420 (DEV_GITD_PORT sets another port), and keeps its repositories in apps/web/.wrangler/state/gitd/repos. It makes its secret once, in apps/web/.wrangler/state/gitd/secret.
  • Its hooks call the dev server back at /_gitsrv/*, as they call the web Worker of the deploy.
  • The web Worker reaches it through GITD_US: in pnpm dev, that binding goes to a small dev-only Worker (apps/web/scripts/dev-gitd-worker.ts), which sends each call on to gitd. GITD_SECRET is the secret of the local server. GITD_EU and GITD_ASIA have no binding, so every call goes to the local server.
  • git reads none of your own git config there, as on a git server.

It needs git 2.45 or later. DEV_GITD=0 turns it off, and Windows runs none.