Repository navigation
feat: add --branch flag to wt step commit - #1750
Conversation
Allows committing in a different worktree without changing directories: `wt step commit --branch feature-x` Adds `CommandEnv::for_branch()` which resolves the worktree path from a branch name. Updates `CommitOptions::commit()` to use the context's worktree path for all git operations instead of defaulting to cwd. Closes #1637 Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
what happens on a detached worktree? |
|
A detached worktree can't be targeted with This is consistent with the project's model of addressing worktrees by branch name. If you're in a detached worktree, Should I add an explicit note about this in the help text, or is the current error message clear enough? |
ok great — would you mind confirming? |
|
I confirmed — my earlier claim was wrong. This is because So the full picture for detached worktrees:
Both are consistent with the worktree-by-branch model. If detached HEAD support for the cwd case is wanted, |
|
I think internally we should change to supply a worktree path, especially for the external interface would remain the same |
|
Makes sense — I'll rework |
…commit `for_action()` no longer requires a branch — it resolves the worktree path from cwd and populates the branch only when available. Commands that need a branch (merge, squash) already call `require_branch()` after construction. This makes `wt step commit` work in detached HEAD worktrees, since git only needs a worktree path to stage and commit. Co-Authored-By: Claude Opus 4.6 <[email protected]>
| /// Branch to operate on | ||
| /// | ||
| /// Defaults to current worktree. |
There was a problem hiding this comment.
Done — combined into /// Branch to operate on (defaults to current worktree).
Co-Authored-By: Claude Opus 4.6 <[email protected]>
…y differs (#3970) Surfaced by tonight's nightly survey of `src/commands/context.rs`. `CommandEnv::for_action_branchless` is documented as "load the command environment without requiring a branch", but `for_action` doesn't require one either — both resolve `branch` from the same `WorkingTree::branch()` call, which returns `Ok(None)` in detached HEAD, and both leave the requirement to a later `require_branch()`. The only actual difference is where `UserConfig` comes from: `for_action` takes one from the caller, the other reads the repository's cached load. The claim has been stale since #1750, which changed `for_action`'s branch from a hard requirement to an `Option`. It survives in three places that all steer a reader the same wrong way — the constructor's name, its doc comment, and a comment at each of the two call sites (`branch-optional for CI compatibility`, `Uses branchless mode`). This renames it to `for_action_loading_config` and rewrites the three comments to state the config-source distinction. No behavior change, so there's no regression test to add: the rename is mechanical (two call sites, both in `hook_commands.rs`) and the rest is comments. `cargo clippy --all-targets --all-features` and `cargo fmt --check` are clean, and `cargo test --test integration hook_show` (21) plus `cargo test --lib --bins` pass. <details><summary>Why the two constructors are equivalent on branches</summary> Both call `repo.current_worktree().branch()`, which resolves via `rev-parse --symbolic-full-name HEAD` and returns `Ok(None)` when detached rather than erroring ([`working_tree.rs`](https://github.com/max-sixty/worktrunk/blob/40fab76812a9dd08d76b58f4ea5b8fd5bd7d54f5/src/git/repository/working_tree.rs#L435-L454)). `Repository::user_config()` is a cached `UserConfig::load_with_warnings()` on `RepoCache`, so the two paths also agree on config content — they differ only in who supplies it. Renaming rather than only correcting the doc, because a name that says "branchless" next to a sibling that is equally branchless is the part a reader trusts without opening the doc comment. </details> Co-authored-by: worktrunk-bot <[email protected]>
Summary
--branch/-bflag towt step commit, allowing commits in a different worktree withoutcd-ing there:wt step commit -b feature-xCommandEnv::for_branch()constructor that resolves a worktree path from a branch nameCommitOptions::commit()to use the context'sworktree_pathfor git operations (staging, status, commit) instead of hardcodingcurrent_worktree()— this makes it work correctly for both the default (cwd) and--branchcasesThis is the first step toward #1637. The
--branchflag can later be added to other step subcommands and eventually towt merge.Test plan
test_step_commit_branch_flag— commits to a feature worktree while cwd is the main worktree, verifies the commit lands in the correct worktreetest_step_commit_branch_flag_nonexistent— errors cleanly when the branch has no worktree🤖 Generated with Claude Code