Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Add `^` symbol for main/default branch
Maximilian Roosb7a8b8172e88
diff --git a/.claude-plugin/skills/worktrunk/README.md b/.claude-plugin/skills/worktrunk/README.mdindex b12aa6ecb..ae9bd8e5b 100644--- a/.claude-plugin/skills/worktrunk/README.md+++ b/.claude-plugin/skills/worktrunk/README.md@@ -257,9 +257,9 @@ Now `wsl new-feature` creates a branch, sets up the worktree, runs initializatio## All Commands-- `wt switch [branch]` - Switch to existing worktree-- `wt switch --create [branch]` - Create and switch (supports `--base=@` to branch from current HEAD)-- `wt remove [branch]` - Remove worktree (use `@` for current)+- `wt switch [branch]` - Switch to existing worktree (use `@` for current, `-` for previous, `^` for main)+- `wt switch --create [branch]` - Create and switch (use `--base=^` for main, `--base=@` for current HEAD)+- `wt remove [branch]` - Remove worktree (use `@` for current, `-` for previous, `^` for main)- `wt merge [target]` - Merge, push, cleanup- `wt list` - Show all worktrees- `wt config` - Manage configurationdiff --git a/CLAUDE.md b/CLAUDE.mdindex f77a7a8e5..3ecf92d58 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -39,8 +39,8 @@ Keep flag and argument descriptions brief—aim for 3-5 words maximum. Use paren**Good examples:**- `/// Skip approval prompts`- `/// Show CI, conflicts, and full diffs`-- `/// Target branch (default: default branch)`-- `/// Branch, path, '@' (HEAD), or '-' (previous)`+- `/// Target branch (defaults to default branch)`+- `/// Branch, path, '@' (HEAD), '-' (previous), or '^' (main)`**Bad examples (too verbose):**- `/// Auto-approve project commands without saving approvals.`diff --git a/README.md b/README.mdindex b12aa6ecb..ae9bd8e5b 100644--- a/README.md+++ b/README.md@@ -257,9 +257,9 @@ Now `wsl new-feature` creates a branch, sets up the worktree, runs initializatio## All Commands-- `wt switch [branch]` - Switch to existing worktree-- `wt switch --create [branch]` - Create and switch (supports `--base=@` to branch from current HEAD)-- `wt remove [branch]` - Remove worktree (use `@` for current)+- `wt switch [branch]` - Switch to existing worktree (use `@` for current, `-` for previous, `^` for main)+- `wt switch --create [branch]` - Create and switch (use `--base=^` for main, `--base=@` for current HEAD)+- `wt remove [branch]` - Remove worktree (use `@` for current, `-` for previous, `^` for main)- `wt merge [target]` - Merge, push, cleanup- `wt list` - Show all worktrees- `wt config` - Manage configurationdiff --git a/src/cli.rs b/src/cli.rsindex 271bef576..449ac483a 100644--- a/src/cli.rs+++ b/src/cli.rs@@ -138,7 +138,7 @@ pub enum StandaloneCommand {/// Squash commits with LLM-generated messageSquash {- /// Target branch (default: default branch)+ /// Target branch (defaults to default branch)#[arg(add = crate::completion::branch_value_completer())]target: Option<String>,@@ -156,7 +156,7 @@ pub enum StandaloneCommand {/// Automatically stashes non-conflicting edits in the target worktree before/// the push and restores them afterward so other agents' changes stay intact.Push {- /// Target branch (default: default branch)+ /// Target branch (defaults to default branch)#[arg(add = crate::completion::branch_value_completer())]target: Option<String>,@@ -167,7 +167,7 @@ pub enum StandaloneCommand {/// Rebase current branch onto target branchRebase {- /// Target branch (default: default branch)+ /// Target branch (defaults to default branch)#[arg(add = crate::completion::branch_value_completer())]target: Option<String>,},@@ -376,12 +376,14 @@ Skip hooks during creation:SHORTCUTS:-Use '@' to refer to your current HEAD (following git's convention):+Use '@' for current HEAD, '-' for previous, '^' for main:wt switch @ # Switch to current branch's worktree- wt switch --create new-feature --base=@ # Branch from current HEAD+ wt switch - # Switch to previous worktree+ wt switch --create new-feature --base=^ # Branch from main (default)+ wt switch --create bugfix --base=@ # Branch from current HEADwt remove @ # Remove current worktree"#)]Switch {- /// Branch, path, '@' (HEAD), or '-' (previous)+ /// Branch, path, '@' (HEAD), '-' (previous), or '^' (main)#[arg(add = crate::completion::worktree_branch_completer())]branch: String,@@ -389,7 +391,7 @@ Use '@' to refer to your current HEAD (following git's convention):#[arg(short = 'c', long)]create: bool,- /// Base branch (with --create)+ /// Base branch (defaults to default branch)#[arg(short = 'b', long, add = crate::completion::branch_value_completer())]base: Option<String>,@@ -515,7 +517,7 @@ Keep worktree after merging:Skip pre-merge commands:wt merge --no-verify"#)]Merge {- /// Target branch (default: default branch)+ /// Target branch (defaults to default branch)#[arg(add = crate::completion::branch_value_completer())]target: Option<String>,diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex 679e5421a..d15438b0f 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -283,18 +283,20 @@ impl Repository {})}- /// Resolve a worktree name, expanding "@" to the current branch and "-" to the previous branch.+ /// Resolve a worktree name, expanding "@" to current, "-" to previous, and "^" to main.////// # Arguments/// * `name` - The worktree name to resolve:/// - "@" for current HEAD/// - "-" for previous branch (via worktrunk.history)+ /// - "^" for main/default branch/// - any other string is returned as-is////// # Returns- /// - `Ok(name)` if not "@" or "-"+ /// - `Ok(name)` if not a special symbol/// - `Ok(current_branch)` if "@" and on a branch/// - `Ok(previous_branch)` if "-" and worktrunk.history has a previous branch+ /// - `Ok(default_branch)` if "^"/// - `Err(DetachedHead)` if "@" and in detached HEAD state/// - `Err` if "-" but no previous branch in historypub fn resolve_worktree_name(&self, name: &str) -> Result<String, GitError> {@@ -311,6 +313,7 @@ impl Repository {)})}+ "^" => self.default_branch(),_ => Ok(name.to_string()),}}diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rsindex 7dd0009fb..5af8c23ab 100644--- a/tests/integration_tests/switch.rs+++ b/tests/integration_tests/switch.rs@@ -500,3 +500,38 @@ fn test_switch_previous_branch_with_worktrunk_history() {&["-"],);}++#[test]+fn test_switch_main_branch() {+ use std::process::Command;++ let repo = TestRepo::new();+ repo.commit("Initial commit");++ // Create a feature branch+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["branch", "feature-a"])+ .current_dir(repo.root_path())+ .output()+ .expect("Failed to create feature-a");++ // Switch to feature-a first+ snapshot_switch("switch_main_branch_to_feature", &repo, &["feature-a"]);++ // Now wt switch ^ should resolve to main+ snapshot_switch("switch_main_branch", &repo, &["^"]);+}++#[test]+fn test_create_with_base_main() {+ let repo = TestRepo::new();+ repo.commit("Initial commit");++ // Create new branch from main using ^+ snapshot_switch(+ "create_with_base_main",+ &repo,+ &["--create", "new-feature", "--base", "^"],+ );+}diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 12e3d8a5b..bd01af1e4 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp7bMDFm/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpp7A70A/test-config.toml---success: trueexit_code: 0@@ -273,7 +273,7 @@ complete -c wt -n "__fish_wt_using_subcommand list" -l branches -d 'Include brancomplete -c wt -n "__fish_wt_using_subcommand list" -l full -d 'Show CI, conflicts, and full diffs'complete -c wt -n "__fish_wt_using_subcommand list" -s v -l verbose -d 'Show git commands and debug info'complete -c wt -n "__fish_wt_using_subcommand list" -s h -l help -d 'Print help (see more with \'--help\')'-complete -c wt -n "__fish_wt_using_subcommand switch" -s b -l base -f -d 'Base branch (with --create)' -r+complete -c wt -n "__fish_wt_using_subcommand switch" -s b -l base -f -d 'Base branch (defaults to default branch)' -rcomplete -c wt -n "__fish_wt_using_subcommand switch" -s x -l execute -d 'Execute command after switching' -rcomplete -c wt -n "__fish_wt_using_subcommand switch" -s C -d 'Change working directory' -r -Fcomplete -c wt -n "__fish_wt_using_subcommand switch" -s c -l create -d 'Create a new branch'diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 2b1e45c18..d759b19ad 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp2UKDFi/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmptJsgAq/test-config.toml---success: trueexit_code: 0@@ -297,7 +297,7 @@ _arguments "${_arguments_options[@]}" : \'--verbose[Show git commands and debug info]' \'-h[Print help]' \'--help[Print help]' \-'::target -- Target branch (default\: default branch):_default' \+'::target -- Target branch (defaults to default branch):_default' \&& ret=0;;(push)@@ -308,7 +308,7 @@ _arguments "${_arguments_options[@]}" : \'--verbose[Show git commands and debug info]' \'-h[Print help (see more with '\''--help'\'')]' \'--help[Print help (see more with '\''--help'\'')]' \-'::target -- Target branch (default\: default branch):_default' \+'::target -- Target branch (defaults to default branch):_default' \&& ret=0;;(rebase)@@ -318,7 +318,7 @@ _arguments "${_arguments_options[@]}" : \'--verbose[Show git commands and debug info]' \'-h[Print help]' \'--help[Print help]' \-'::target -- Target branch (default\: default branch):_default' \+'::target -- Target branch (defaults to default branch):_default' \&& ret=0;;(ask-approvals)@@ -372,8 +372,8 @@ json\:"JSON output"))' \;;(switch)_arguments "${_arguments_options[@]}" : \-'-b+[Base branch (with --create)]:BASE:_default' \-'--base=[Base branch (with --create)]:BASE:_default' \+'-b+[Base branch (defaults to default branch)]:BASE:_default' \+'--base=[Base branch (defaults to default branch)]:BASE:_default' \'-x+[Execute command after switching]:EXECUTE:_default' \'--execute=[Execute command after switching]:EXECUTE:_default' \'-C+[Change working directory]:path:_files' \@@ -386,7 +386,7 @@ _arguments "${_arguments_options[@]}" : \'--verbose[Show git commands and debug info]' \'-h[Print help]' \'--help[Print help]' \-':branch -- Branch, path, '\''@'\'' (HEAD), or '\''-'\'' (previous):_default' \+':branch -- Branch, path, '\''@'\'' (HEAD), '\''-'\'' (previous), or '\''^'\'' (main):_default' \&& ret=0;;(remove)@@ -414,7 +414,7 @@ _arguments "${_arguments_options[@]}" : \'--verbose[Show git commands and debug info]' \'-h[Print help (see more with '\''--help'\'')]' \'--help[Print help (see more with '\''--help'\'')]' \-'::target -- Target branch (default\: default branch):_default' \+'::target -- Target branch (defaults to default branch):_default' \&& ret=0;;esacdiff --git a/tests/snapshots/integration__integration_tests__switch__create_with_base_main.snap b/tests/snapshots/integration__integration_tests__switch__create_with_base_main.snapnew file mode 100644index 000000000..1a04c950e--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch__create_with_base_main.snap@@ -0,0 +1,30 @@+---+source: tests/integration_tests/switch.rs+info:+ program: wt+ args:+ - switch+ - "--create"+ - new-feature+ - "--base"+ - ^+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: /dev/null+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1761609600"+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpmv5V3E/test-config.toml+---+success: true+exit_code: 0+----- stdout -----+✅ [32mCreated new worktree for [1m[32mnew-feature[0m[32m from [1m[32mmain[0m[32m at [1m[32m[REPO].new-feature[0m[0m+💡 [2mTo enable automatic cd, run: wt config shell[0m++----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__switch__switch_main_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_main_branch.snapnew file mode 100644index 000000000..837073e38--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch__switch_main_branch.snap@@ -0,0 +1,27 @@+---+source: tests/integration_tests/switch.rs+info:+ program: wt+ args:+ - switch+ - ^+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: /dev/null+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1761609600"+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp67bHYd/test-config.toml+---+success: true+exit_code: 0+----- stdout -----+⚪ Already on worktree for [1mmain[0m at [1m[REPO][0m+💡 [2mTo enable automatic cd, run: wt config shell[0m++----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__switch__switch_main_branch_to_feature.snap b/tests/snapshots/integration__integration_tests__switch__switch_main_branch_to_feature.snapnew file mode 100644index 000000000..99e764840--- /dev/null+++ b/tests/snapshots/integration__integration_tests__switch__switch_main_branch_to_feature.snap@@ -0,0 +1,27 @@+---+source: tests/integration_tests/switch.rs+info:+ program: wt+ args:+ - switch+ - feature-a+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: /dev/null+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1761609600"+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp48ZhIy/test-config.toml+---+success: true+exit_code: 0+----- stdout -----+✅ [32mSwitched to worktree for [1m[32mfeature-a[0m[32m at [1m[32m[REPO].feature-a[0m[0m+💡 [2mTo enable automatic cd, run: wt config shell[0m++----- stderr -----