fix(repository): detect existing repo when .git is a worktree gitlink#1029
Open
petercrocker wants to merge 1 commit into
Open
fix(repository): detect existing repo when .git is a worktree gitlink#1029petercrocker wants to merge 1 commit into
petercrocker wants to merge 1 commit into
Conversation
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.
Deploying infrahub-sdk-python with
|
| 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 |
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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GitRepoManager.initialize_repo()checked(root / ".git").is_dir(), which is False in a git worktree where.gitis a gitlink file (gitdir: /path/to/main/.git/worktrees/<name>).Repo.initthen crashed withFileExistsError: ./.gitwhen dulwich tried tomkdirover the gitlink file. This madeinfrahubctl transform(and anything else relying on branch auto-detection) unusable from a worktree..exists()— dulwich'sRepo(path)already resolves the gitlink to the real controldir, andporcelain.active_branchreturns the worktree's branch correctly.Test plan
test_initialize_repo_uses_existing_repo_in_worktreeconstructs a real gitlink.gitfile pointing to a fresh dulwich repo and assertsGitRepoManageropens 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).GitRepoManager('.').active_branchnow returns the worktree branch instead of crashing.Summary by cubic
Fix repository detection in Git worktrees by treating
.gitas existing even when it is a gitlink file. This prevents init crashes and restores branch auto-detection for commands likeinfrahubctl transform..gitcheck from.is_dir()to.exists()so worktrees open viadulwichRepo(path)and return the correct branch.test_initialize_repo_uses_existing_repo_in_worktreeto ensure we open the existing repo instead of re-initializing.Written for commit 05d636b. Summary will update on new commits. Review in cubic