Repository navigation
Description
Split from #6340.
Problem: On 2026-11-18 a run of devops repos worktrees add on the clone of dan-petty/homelab left the clone in detached HEAD. The body records no code path in src/devops_cli/git/worktree_add.py or the reclaim that detaches HEAD. The cause is unknown. Why deferred: The main change stops a failed origin read before any worktree removal and asserts that HEAD is unchanged on that path. A detachment on a successful run has no evidence, so this change cannot state a test or a fix. Acceptance criteria: A test runs devops repos worktrees add on a clone of a private repository and shows the clone stays on its branch after the command exits 0. The test fails on current main if the defect is present, or the issue records the evidence that the detached HEAD came from outside the command. A fix keeps the clone on its branch after a successful worktrees add run.
Proposed design
Problem statement
Add a postcondition guard that restores the original branch and an offline regression test for devops repos worktrees add. A successful run may leave the clone in detached HEAD, and the cause is unknown. The add paths in worktree_add.py do not change the main worktree HEAD. The regression test covers the create, take-over and reclaim paths. The private-repository live check is deferred because it needs credentials.
Acceptance criteria
Requirement: Create path keeps the clone branch
The devops repos worktrees add command SHALL leave the clone on its original branch after it creates a worktree.
Scenario: Successful create keeps HEAD
- WHEN A test runs
devops repos worktrees addon a temporary clone with a local bare origin and a stubbed claim, and the command exits 0. - THEN The test asserts that
git -C <clone> symbolic-ref --short -q HEADreturns the original branch.
Requirement: Take-over path keeps the clone branch
The devops repos worktrees add command SHALL leave the clone on its original branch after it takes over a pushed branch.
Scenario: Successful take-over keeps HEAD
- WHEN A test runs
devops repos worktrees addon a temporary clone with a local bare origin and an App-authored branch, and the command exits 0. - THEN The test asserts that
git -C <clone> symbolic-ref --short -q HEADreturns the original branch.
Requirement: Reclaim keeps the clone branch
The reclaim of merged worktrees SHALL leave the clone on its original branch.
Scenario: Reclaim keeps HEAD
- WHEN A test calls
reclaim_merged_worktreeswith a test double forprune_worktreesthat detaches HEAD and returns an empty report. - THEN The test asserts that
git -C <clone> symbolic-ref --short -q HEADreturns the original branch.
Requirement: Restore failure is a typed error
The command MUST raise GitOperationError when it cannot restore the original branch.
Scenario: Failed restore reports a typed error
- WHEN A test calls
add_worktreewith a test double that detaches HEAD and a restore helper that fails. - THEN The test asserts that
GitOperationErroris raised and its message names the original branch.
Existing Tool Considered
No alternatives check has run. The Git CLI covers the need: git symbolic-ref --short -q HEAD reads the current branch and git checkout <branch> restores it. Git lacks an automated regression test, so the change adds a pytest test around the existing command. No new dependency is needed.
Key questions
- Which code path in
src/devops_cli/git/worktree_add.pychanges the HEAD of the main worktree?: The create and take-over paths do not change it._createrunsgit worktree add --no-track -b <branch> <directory> <start>, and_take_overrunsgit worktree add --track -b <branch> <directory> origin/<branch>. Both create a separate worktree and run nogit checkoutorgit switchin the clone. [sources: src/devops_cli/git/worktree_add.py] - How should the regression test provide an origin without a private repository?: Use a temporary clone with a local bare repository as
origin. TheOriginReadsprotocol and_CloneOriginreadoriginwith Git commands, so a local bare origin exercises the same code. Keep the private-repository run as a separate live check. (best practice) [sources: src/devops_cli/git/worktree_add.py]- Chosen: local bare origin. It runs offline with no secret and exercises the same Git reads.
- Rejected: private repository clone. It needs network, credentials and a private repository, so it cannot run in CI.
- What should the command do when a successful add leaves HEAD detached?: Read the original branch before the command changes any worktree, and restore it before the command returns. If the restore fails, raise
GitOperationErrorthat names the branch. A test double that detaches HEAD proves the guard. (best practice) [sources: src/devops_cli/git/worktree_add.py]- Chosen: postcondition guard. It keeps the clone on its branch and gives a testable failure path.
- Rejected: test only. It records the behavior but does not keep the clone on its branch when the defect occurs.
- Where should the postcondition guard run?: In a context manager in
src/devops_cli/git/worktree_add.pythat wrapsreclaim_merged_worktreesandadd_worktree. Both public operations then keep HEAD, and the dry-run path ofadd_worktreereturns inside the guard without a change. (best practice) [sources: src/devops_cli/git/worktree_add.py]- Chosen: context manager around both public functions. It covers the reclaim and the add, and every caller uses the same guard.
- Rejected: inside
add_worktreeonly. The reclaim runs beforeadd_worktree, so a detach there would be missed.
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
devops-cli-service commentedon Nov 19, 2026
Both target 'devops repos worktrees add' and the same 2026-11-18 incident on dan-petty/homelab, but #6340 fixes the failed-credential path while the candidate investigates detached HEAD on the success path.devops-cli-service commentedon Nov 19, 2026
Next step: the alternatives check records a verdict. See the section Proposed design of the body.
fix(repos): preserve clone branch after worktrees add (#6354)
devops-cli-service commentedon Nov 24, 2026
Released in v0.211.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.211.0
devops-cli-service commentedon Nov 24, 2026
Live since 2026-11-24 20:09 UTC