Skip to content

fix(repository): detect existing repo when .git is a worktree gitlink#1029

Open
petercrocker wants to merge 1 commit into
stablefrom
fix-worktree-git-file
Open

fix(repository): detect existing repo when .git is a worktree gitlink#1029
petercrocker wants to merge 1 commit into
stablefrom
fix-worktree-git-file

Conversation

@petercrocker
Copy link
Copy Markdown
Contributor

@petercrocker petercrocker commented May 16, 2026

Summary

  • GitRepoManager.initialize_repo() checked (root / ".git").is_dir(), which is False in a git worktree where .git is a gitlink file (gitdir: /path/to/main/.git/worktrees/<name>).
  • The existing-repo branch was skipped and Repo.init then crashed with FileExistsError: ./.git when dulwich tried to mkdir over the gitlink file. This made infrahubctl transform (and anything else relying on branch auto-detection) unusable from a worktree.
  • Fix: switch the check to .exists() — dulwich's Repo(path) already resolves the gitlink to the real controldir, and porcelain.active_branch returns the worktree's branch correctly.

Test plan

  • New unit test test_initialize_repo_uses_existing_repo_in_worktree constructs a real gitlink .git file pointing to a fresh dulwich repo and asserts GitRepoManager opens it instead of re-initializing.
  • uv run pytest tests/unit/sdk/test_repository.py — all 6 tests pass.
  • uv run invoke format lint-code — clean (ruff, ty, mypy).
  • Manual repro in a worktree: GitRepoManager('.').active_branch now returns the worktree branch instead of crashing.

Summary by cubic

Fix repository detection in Git worktrees by treating .git as existing even when it is a gitlink file. This prevents init crashes and restores branch auto-detection for commands like infrahubctl transform.

  • Bug Fixes
    • Switched .git check from .is_dir() to .exists() so worktrees open via dulwich Repo(path) and return the correct branch.
    • Added unit test test_initialize_repo_uses_existing_repo_in_worktree to ensure we open the existing repo instead of re-initializing.

Written for commit 05d636b. Summary will update on new commits. Review in cubic

GitRepoManager.initialize_repo() checked for `.git` with `.is_dir()`, which
misses git worktrees (`.git` is a file containing `gitdir: ...`). The
existing-repo branch was skipped and `Repo.init` then crashed with
`FileExistsError` trying to mkdir the gitlink file. This made `infrahubctl
transform` (and any other CLI relying on branch auto-detection) unusable from
a worktree.

Switch the check to `.exists()` — dulwich's `Repo(path)` already resolves
the gitlink to the real controldir, and `porcelain.active_branch` returns
the worktree's branch correctly.
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 05d636b
Status: ✅  Deploy successful!
Preview URL: https://56a98f73.infrahub-sdk-python.pages.dev
Branch Preview URL: https://fix-worktree-git-file.infrahub-sdk-python.pages.dev

View logs

@petercrocker petercrocker marked this pull request as ready for review May 16, 2026 14:48
@petercrocker petercrocker requested a review from a team as a code owner May 16, 2026 14:48
@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@           Coverage Diff           @@
##           stable    #1029   +/-   ##
=======================================
  Coverage   81.54%   81.54%           
=======================================
  Files         134      134           
  Lines       11467    11467           
  Branches     1735     1735           
=======================================
  Hits         9351     9351           
  Misses       1569     1569           
  Partials      547      547           
Flag Coverage Δ
integration-tests 41.71% <100.00%> (ø)
python-3.10 54.59% <100.00%> (ø)
python-3.11 54.59% <100.00%> (+0.01%) ⬆️
python-3.12 54.59% <100.00%> (+0.01%) ⬆️
python-3.13 54.57% <100.00%> (-0.02%) ⬇️
python-3.14 54.57% <100.00%> (-0.02%) ⬇️
python-filler-3.12 22.66% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/repository.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant