Skip to content

Add support for creating new branch in temp worktree#49

Merged
Pajn merged 1 commit into
mainfrom
branching
Mar 30, 2026
Merged

Add support for creating new branch in temp worktree#49
Pajn merged 1 commit into
mainfrom
branching

Conversation

@Pajn
Copy link
Copy Markdown
Owner

@Pajn Pajn commented Mar 30, 2026

Stack

Summary by CodeRabbit

  • New Features

    • New -b/--branch option for kin wt temp: create and check out a new local branch in one step; optional start-point can be provided.
  • Bug Fixes

    • Improved rollback and safer cleanup when errors occur during temp worktree creation; when automatic rollback is unsafe, guidance is shown and leftovers are left for manual cleanup.
  • Documentation

    • README and CLI reference updated with synopsis, examples, and behavior for the -b form.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9e4aba70-52aa-49bd-9021-2f8cea676def

📥 Commits

Reviewing files that changed from the base of the PR and between 1685333 and bf64f69.

📒 Files selected for processing (6)
  • README.md
  • docs/cli_reference.md
  • src/commands/worktree.rs
  • src/worktree/git.rs
  • src/worktree/roles.rs
  • tests/worktree_temp_tests.rs

📝 Walkthrough

Walkthrough

Adds a new -b/--branch mode to kin wt temp that creates a new local branch from an optional start-point and materializes it in a managed temporary worktree; docs, CLI args, worktree role logic, git helpers, and tests were updated to support this flow.

Changes

Cohort / File(s) Summary
Documentation
README.md, docs/cli_reference.md
Documented new kin wt temp -b <new-branch> [<start-point>] synopsis and examples; clarified positional semantics with and without -b.
Command Interface
src/commands/worktree.rs
Replaced TempArgs.branch with new_branch (-b/--branch) and target positional; dispatches to ensure_temp_new_branch() when -b present, otherwise to existing ensure_temp().
Worktree Logic
src/worktree/roles.rs
Added pub fn ensure_temp_new_branch(repo, branch, requested_start_point) implementing start-point resolution, local-branch creation, temp-path allocation, add-worktree, hook execution, and conditional rollback (remove worktree and delete branch only when tip matches).
Git helpers
src/worktree/git.rs
Added create_local_branch_from_start_point_strict() and delete_local_branch_if_tip_matches() with strict NotFound and OID checks; updated imports for ErrorCode and Oid.
Tests
tests/worktree_temp_tests.rs
Added helpers and tests covering creation from current tip, creation from remote start-point (upstream), rejection when branch exists, rollback on hook failures (including case preserving branch after diverging commit).

Sequence Diagram(s)

sequenceDiagram
    participant User as User/CLI
    participant Cmd as worktree command
    participant Roles as roles module
    participant Git as git/git2
    participant FS as Filesystem

    User->>Cmd: kin wt temp -b <new-branch> [start-point]
    Cmd->>Cmd: parse args (new_branch?, target?)
    Cmd->>Roles: ensure_temp_new_branch(repo, new_branch, target)
    Roles->>Git: resolve start-point (requested or HEAD)
    Roles->>Git: verify branch non-existence
    Roles->>Git: create local branch from start-point
    Roles->>FS: compute/verify temp worktree path
    Roles->>Git: add worktree for new branch
    Roles->>Roles: run on_create hooks
    alt hooks succeed
        Roles->>FS: write worktree metadata
        Roles-->>Cmd: return success
    else hooks fail
        Roles->>Git: attempt delete_local_branch_if_tip_matches(new_branch, expected_tip)
        Roles->>FS: remove created worktree path if present
        Roles-->>Cmd: return aggregated error (may advise manual cleanup)
    end
    Cmd-->>User: exit (success|error)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I nibble at branches, hop from HEAD to origin,
I craft a tiny branch and make a seeded lawn,
Temp trees spring up, metadata snug and bright,
If hooks frown, I tidy what I can by night,
A rabbit leaves a note: "Manual cleanup in sight."

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding -b flag support to create new branches in temp worktrees, which is the primary objective addressed across all modified files.
Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #48: adds -b <new-branch> [<start-point>] mode to kin wt temp command with proper branch creation, updates CLI reference and README documentation, and includes comprehensive integration tests for the new functionality.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue #48 requirements: documentation updates (README, CLI reference), CLI argument modifications (TempArgs), new branch creation workflow (ensure_temp_new_branch), git operations (create_local_branch_from_start_point_strict, delete_local_branch_if_tip_matches), and integration tests for the new feature.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

This was referenced Mar 30, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/cli_reference.md (1)

298-303: ⚠️ Potential issue | 🟡 Minor

Split the kin wt temp synopsis into the two supported forms.

Line 303 currently advertises [<branch-or-start-point>] unconditionally, but only the -b path treats the positional as a start point. Without -b, the implementation still interprets it as a branch name, so this synopsis is promising a mode the command does not actually support.

📝 Suggested wording
-kin wt temp [-b <new-branch>] [<branch-or-start-point>]
+kin wt temp [<branch>]
+kin wt temp -b <new-branch> [<start-point>]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@docs/cli_reference.md` around lines 298 - 303, The synopsis for the "kin wt
temp" command is misleading: split it into the two supported forms so the docs
reflect actual behavior—one form showing "kin wt temp -b <new-branch>
[<start-point>]" (start-point only used with -b) and the other showing "kin wt
temp <branch-or-start-point>" or "kin wt temp" (no -b, positional treated as
branch name); update the "kin wt temp" line in the CLI reference to present
these two distinct usages and clarify that the positional argument is a start
point only when -b is present (reference the "kin wt temp" synopsis in the
docs).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/worktree/roles.rs`:
- Around line 249-275: Replace the permissive
ensure_local_branch_exists_from_start_point(...) call with a strict creator that
returns whether it actually created the ref (e.g.
create_local_branch_from_start_point_strict(repo, branch, &start_point) ->
Result<bool>), use that boolean to know if we must roll back, and wrap the
subsequent steps (add_worktree, run_create_hooks, ctx.metadata.upsert,
ctx.metadata.save) in a failure path that deletes the branch if created by this
invocation; ensure rollback runs when add_worktree or run_create_hooks or
metadata save/upsert fail (and propagate the original error). Add an integration
test that runs the temp-branch flow (kin wt temp -b) with a deliberately failing
on_create hook to assert the branch is removed after the command errors.

---

Outside diff comments:
In `@docs/cli_reference.md`:
- Around line 298-303: The synopsis for the "kin wt temp" command is misleading:
split it into the two supported forms so the docs reflect actual behavior—one
form showing "kin wt temp -b <new-branch> [<start-point>]" (start-point only
used with -b) and the other showing "kin wt temp <branch-or-start-point>" or
"kin wt temp" (no -b, positional treated as branch name); update the "kin wt
temp" line in the CLI reference to present these two distinct usages and clarify
that the positional argument is a start point only when -b is present (reference
the "kin wt temp" synopsis in the docs).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 36d68a63-c7d5-4669-b0cb-b70044735e08

📥 Commits

Reviewing files that changed from the base of the PR and between 04b5caa and e55ac52.

📒 Files selected for processing (5)
  • README.md
  • docs/cli_reference.md
  • src/commands/worktree.rs
  • src/worktree/roles.rs
  • tests/worktree_temp_tests.rs

Comment thread src/worktree/roles.rs Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/worktree/roles.rs`:
- Around line 872-907: The rollback currently force-deletes the branch in
rollback_created_temp_branch by calling delete_local_branch(..., true); instead
capture the branch tip (commit OID) right after branch creation (in the code
path that sets branch_created) and store it (e.g., branch_tip_after_create),
then in rollback_created_temp_branch compare the current branch tip (resolve
branch to OID) to that saved OID and only call delete_local_branch without force
if the tip is unchanged; if the tip differs, do not delete and instead append a
rollback_errors entry instructing manual cleanup and include the current tip
info; also add a regression test that sets an on_create hook which makes a
commit then exits non-zero and assert that the branch is not force-deleted (and
that the test records the saved branch tip and reports manual cleanup when tips
differ).
- Around line 249-250: The race window lets another process create the ref after
ensure_local_branch_is_new(...) and
create_local_branch_from_start_point_strict(...) can return Ok(false), but the
current flow still proceeds to add_worktree(); change the control flow so that
create_local_branch_from_start_point_strict(repo, branch, start_point, ...)
returning Ok(false) is treated as a hard failure (return Err) instead of
continuing—do this wherever create_local_branch_from_start_point_strict is used
in this function (the block around ensure_local_branch_is_new,
resolve_requested_start_point, and the subsequent
create_local_branch.../add_worktree sequence) so we never call add_worktree()
when the branch was not actually created by this invocation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 96a42eca-d637-4062-bd87-47b738264b86

📥 Commits

Reviewing files that changed from the base of the PR and between e55ac52 and 1d60abb.

📒 Files selected for processing (6)
  • README.md
  • docs/cli_reference.md
  • src/commands/worktree.rs
  • src/worktree/git.rs
  • src/worktree/roles.rs
  • tests/worktree_temp_tests.rs

Comment thread src/worktree/roles.rs
Comment thread src/worktree/roles.rs
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/worktree/roles.rs`:
- Around line 911-918: The rollback uses delete_local_branch(repo, branch,
false) which calls the non-forced `git branch -d` and can leave branches when
the local start-point is divergent despite the OID match checked by
current_branch.get().target() == branch_tip_after_create; change the deletion to
an OID-guarded ref delete or call the force path (i.e., use
delete_local_branch(repo, branch, true) or implement a ref-delete-by-oid in
delete_local_branch that verifies branch_tip_after_create before forcing
removal) so the branch is removed when the tip matches branch_tip_after_create,
and add a regression test (based on
worktree_temp_b_failing_create_hook_rolls_back_created_branch) that simulates a
failing on_create hook with a divergent local start-point to ensure the new
behavior prevents stranded branches.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 86f20e68-c184-4311-84dd-33f59d316b63

📥 Commits

Reviewing files that changed from the base of the PR and between 1d60abb and 1685333.

📒 Files selected for processing (6)
  • README.md
  • docs/cli_reference.md
  • src/commands/worktree.rs
  • src/worktree/git.rs
  • src/worktree/roles.rs
  • tests/worktree_temp_tests.rs

Comment thread src/worktree/roles.rs Outdated
@Pajn Pajn merged commit 6f48919 into main Mar 30, 2026
2 checks passed
@Pajn Pajn deleted the branching branch March 30, 2026 20:31
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