Skip to content

remove --delete-branch assumes worktree directory name matches git branch name #80

Description

@bjester

Observed Behavior

Executing treefort remove <worktree_name> --delete-branch passes worktree_name directly to git.delete_branch(worktree_name, force=force). If the worktree's checked-out branch name does not match its directory name (for instance, when checked out into a custom folder or when the branch name was normalized), git attempts to delete a branch named after the directory rather than the actual branch. This either fails or risks deleting an unrelated branch.

Expected Behavior

RemoveCommand should inspect the worktree before deleting it (e.g. via git.list_worktrees() or querying HEAD in the worktree directory) to determine the actual branch associated with the worktree, and delete that branch.

Steps to Reproduce

  1. In an initialized repository, add a worktree with a directory name different from the branch name:
    git worktree add custom-dir -b my-feature
  2. Run treefort remove custom-dir --delete-branch.
  3. git worktree remove successfully deletes custom-dir.
  4. The command then runs git.delete_branch("custom-dir"), which fails because no branch named custom-dir exists (my-feature is left undeleted).

Minimal Reproduction Environment / Conditions

  • Version / Branch / Commit: release-v0.5.x@6d07c0c
  • Component: treefort.commands.remove.RemoveCommand

Errors, Logs & Telemetry

WARNING: Failed to delete branch 'custom-dir': Command failed, with exit status 1: git branch -d -- custom-dir
error: branch 'custom-dir' not found.

Code location in src/treefort/commands/remove.py:50-54:

if delete_branch:
    try:
        self._runtime.git.delete_branch(worktree_name, force=force)
    except CommandError as e:
        self._logger.warning(f"Failed to delete branch '{worktree_name}': {e}")

Deliverables & Testing Requirements

  • Determine the actual target branch from the worktree before removing the directory.
  • Delete the resolved branch name rather than blindly using the directory name.
  • Regression Safety: Add a unit test in tests/commands/test_remove.py verifying that when directory name and branch name differ, the correct branch name is deleted.

Technical Pointers & Root Cause Clues

  • Suspected Components: src/treefort/commands/remove.py:50-54
  • Related Commits: c953c29 (Update remove command to have option of deleting branch)

Notes & Edge Cases

  • If the worktree is in a detached HEAD state, branch deletion should be skipped gracefully with an informative log message.

Metadata

Severity / Priority: P2 (Normal)
Area: bug, remove, git

AI Usage

Drafted with Gemini following the DEEP framework based on code review findings against release-v0.5.x. Reproduction conditions and code references were verified in the codebase.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2 - NormalShould fix but can waitbugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions