Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ Every flag has an environment-variable equivalent.

Endpoints: `/healthz`, `/readyz`, `/metrics` (see [Metrics](./metrics.md)).


## Reusing a cache after changing upstream

Mirrors are keyed by repo path under `--cache-root`, not by upstream URL.
After changing `--upstream` and restarting the proxy, clone, incremental fetch,
and missing-SHA fetch use the configured upstream. Existing mirrors are reused;
their recorded `origin` URL is not rewritten. Normal fetch TTL rules still apply.

Old cached objects and local want-by-SHA pins remain and can still be served.
Changing the fetch URL is not a security-domain migration. When switching trust
domains, use a fresh or separate cache root, including for LFS objects.
11 changes: 4 additions & 7 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,19 +390,16 @@ impl GitCache {

async fn fetch(&self, repo: &RepoRef, cred: Option<&str>) -> Result<()> {
tracing::debug!(repo = %repo.name, "fetching updates from upstream");
// Run inside the bare mirror and fetch `origin`: `clone --mirror` above sets
// up exactly one remote named `origin` (git's default remote name) pointing at
// the upstream URL, with a mirror refspec that updates all refs. So `origin`
// is not an assumption about the client - it is the remote this proxy created.
// A reused mirror can still record an older upstream in origin.
let started = Instant::now();
let mut cmd = self.fetch_cmd(cred);
cmd.current_dir(&repo.cache_dir)
.arg("fetch")
.arg("--prune")
.arg("--quiet")
.arg("origin")
.arg(&repo.upstream_url)
// Mirror refspec, minus the local-only pin namespace: `--prune` would
// otherwise drop those refs as "not on origin" (see `prune_wants`).
// otherwise drop those refs as "not on upstream" (see `prune_wants`).
.arg("+refs/*:refs/*")
.arg(format!("^{PROXY_WANTS_NS}/*"));
if let Err(error) = run_upstream(&mut cmd, "fetch", &repo.name, None).await {
Expand Down Expand Up @@ -463,7 +460,7 @@ impl GitCache {
.arg("fetch")
.arg("--no-tags")
.arg("--quiet")
.arg("origin");
.arg(&repo.upstream_url);
for oid in &missing {
// Pin under a reserved ref (force: the ref name is the SHA and may
// already exist), making the object a `want` tip safe from `git gc`.
Expand Down
97 changes: 97 additions & 0 deletions tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,103 @@ use tower::ServiceExt; // for `oneshot`

mod helpers;

/// A retained mirror must fetch configured B even while origin still points to A.
#[tokio::test]
async fn reused_mirror_fetches_configured_upstream_for_refs_and_missing_sha() {
let old = tempfile::tempdir().unwrap();
git(old.path(), &["init", "-q", "-b", "main", "."]);
git(old.path(), &["commit", "-q", "--allow-empty", "-m", "old"]);
let old_sha = git(old.path(), &["rev-parse", "HEAD"]).trim().to_string();
git(old.path(), &["branch", "old-only"]);

let new = tempfile::tempdir().unwrap();
git(new.path(), &["init", "-q", "-b", "main", "."]);
git(new.path(), &["commit", "-q", "--allow-empty", "-m", "new"]);
let new_sha = git(new.path(), &["rev-parse", "HEAD"]).trim().to_string();
git(new.path(), &["tag", "new-tag"]);
git(new.path(), &["checkout", "-q", "-b", "hidden"]);
git(
new.path(),
&["commit", "-q", "--allow-empty", "-m", "hidden"],
);
let hidden_sha = git(new.path(), &["rev-parse", "HEAD"]).trim().to_string();
git(
new.path(),
&["config", "uploadpack.hideRefs", "refs/heads/hidden"],
);
git(
new.path(),
&["config", "uploadpack.allowAnySHA1InWant", "true"],
);

let cache = tempfile::tempdir().unwrap();
let metrics = Arc::new(Metrics::new());
let gitcache = GitCache::new(
GitConfig {
git_binary: "git".into(),
upstream_auth_header: None,
big_file_threshold: "8m".into(),
fetch_ttl: Duration::ZERO,
max_wants: 100,
max_upload_packs: 0,
pack_threads: None,
pack_window_memory: None,
},
metrics.clone(),
None,
);
let mut repo = git_cache_proxy::repo::RepoRef {
name: "repo.git".into(),
upstream_url: format!("file://{}", old.path().display()),
cache_dir: cache.path().join("repo.git"),
};
gitcache.ensure_fresh(&repo, true, &[], None).await.unwrap();
let pin = format!("refs/proxy-wants/{old_sha}");
git(&repo.cache_dir, &["update-ref", &pin, &old_sha]);
let origin = git(&repo.cache_dir, &["config", "remote.origin.url"]);
repo.upstream_url = format!("file://{}", new.path().display());

gitcache.ensure_fresh(&repo, true, &[], None).await.unwrap();
assert_eq!(git(&repo.cache_dir, &["rev-parse", "main"]).trim(), new_sha);
assert_eq!(
git(&repo.cache_dir, &["rev-parse", "new-tag"]).trim(),
new_sha
);
assert_eq!(git(&repo.cache_dir, &["rev-parse", &pin]).trim(), old_sha);
let refs = git(&repo.cache_dir, &["for-each-ref", "--format=%(refname)"]);
assert!(!refs.contains("refs/heads/old-only"), "{refs}");
assert!(!refs.contains("refs/heads/hidden"), "{refs}");
let missing = Command::new("git")
.current_dir(&repo.cache_dir)
.args(["cat-file", "-e", &hidden_sha])
.output()
.unwrap();
assert!(!missing.status.success());

// Skip full-ref refresh so only the SHA backfill can supply this object.
let mut body = pkt(&format!("want {hidden_sha}\n"));
body.extend_from_slice(b"0000");
gitcache
.ensure_fresh(&repo, false, &body, None)
.await
.unwrap();
let hidden_pin = format!("refs/proxy-wants/{hidden_sha}");
assert_eq!(
git(&repo.cache_dir, &["rev-parse", &hidden_pin]).trim(),
hidden_sha
);
gitcache.ensure_fresh(&repo, true, &[], None).await.unwrap();
assert_eq!(
git(&repo.cache_dir, &["rev-parse", &hidden_pin]).trim(),
hidden_sha
);
assert_eq!(
git(&repo.cache_dir, &["config", "remote.origin.url"]),
origin
);
assert_eq!(want_fetch_ok_count(&metrics.gather()), 1);
}

/// Regression test for the gzip transport bug: git's smart-HTTP client
/// compresses the `git-upload-pack` request body and sends `Content-Encoding:
/// gzip`. The proxy must decode it before handing the bytes to `git
Expand Down
Loading