Clone git repositories on their own default branch

Merged
GM
3mo ago
+161-6

Why

A worker without a local copy of a repository clones it on demand. That path only carries the repository id and name, so the repository object never learns which git ref to track. It fell back to the platform default branch. Git then failed on any repository whose default branch is not main, the repository went to error, and artifact generation failed with it.

It takes more than one worker to see this. Each worker keeps its own git directory, so only the worker that added the repository has the clone.

Closes #8749. It does not close #9825: repo.default_branch is still wrong when the clone already exists.

What changed

InfrahubRepositoryBase gets one abstract method, resolve_checkout_ref. The re-clone path calls it and passes the result to create_locally. CoreRepository reads default_branch, CoreReadOnlyRepository reads ref. No schema change, no migration.

init()
  validate_local_directories()      clone missing
    ensure_location_is_defined()    -> location
    resolve_checkout_ref()          -> default_branch or ref
      create_locally()

What to look at

backend/infrahub/git/integrator.py has the behaviour change. The graph lookup sits inside the missing directory branch, so an existing clone makes no extra query. init() is on the hot path of artifacts, transforms and generators.

How to test

uv run pytest backend/tests/functional/git/test_repository_default_branch.py
uv run pytest backend/tests/integration_docker/test_artifact_composition.py

The end to end case needs two workers, so it lives in integration_docker. The artifact composition test now uses a repository on a non default branch, instead of a new test class with its own compose stack.

Checklist

  • Tests added/updated
  • Changelog entry added
  • I have reviewed AI generated content