Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(merge): Add --tracked-only flag and warn for untracked
Maximilian Roosea2e3736647b
diff --git a/README.md b/README.mdindex 8a5d7acc5..78f6c0de5 100644--- a/README.md+++ b/README.md@@ -14,7 +14,7 @@ Running ten agents on different features? `wt switch --create feature-a`, `wt sw## What It Does-Automates the full lifecycle: create worktree, work, merge back to local main, remove worktree.+Automates the full lifecycle: create worktree, work, merge back, remove worktree.```bash$ wt switch --create fix-auth@@ -22,21 +22,15 @@ $ wt switch --create fix-auth# Agent works, makes changes, then:$ wt merge main --squash-π Rebasing onto main...-π Merging 1 commit to main @ a1b2c3d--* a1b2c3d (HEAD -> fix-auth) Add authentication-- auth.js | 45 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+)--β Merged to main (1 commit, 1 file, +45)-π Cleaning up worktree...-β Returned to primary at ../repo+π Switching to main...+π Staging changes (3 modified)...+π Merging fix-auth β main...+β Merged and pushed+β Removed worktree# Shell back in main```-Shell integration means directories change automatically. Merge handles staging, committing, merging to main, cleanup. One command.+Shell integration means directories change automatically. Merge handles staging, committing, merging, pushing, cleanup. One command.## Installation@@ -54,9 +48,8 @@ wt switch --create feature-name**Finish and merge:**```bash-wt merge main --squash # Rebases onto main, merges to main, removes worktree+wt merge main --squash```-Merges your changes into main: rebases your branch onto the latest main, merges the result into your local main branch, and cleans up the worktree. Use `--squash` to combine all commits into one.**See active worktrees:**```bash@@ -84,39 +77,20 @@ wt config help # Setup guideWorktrunk is opinionated. The choices optimize for AI agent workflows:-1. **Merge does everything** - Staging, committing, merging to main, cleanup in one command+1. **Merge does everything** - Staging, committing all changes, merging, pushing, cleanup in one command2. **Squash by default** - Linear history, configurable3. **Automatic shell navigation** - No manual `cd` commands4. **Fail-fast hooks** - Tests block bad mergesThese trade manual control for automation. For fine-grained control, use `git worktree` directly.-## Local Workflow Model--Worktrunk manages **local git worktrees** with **local operations**:--- **Local operations only** - `wt merge` merges commits to your local main branch, not to remote repositories-- **Fast-forward only** - No force pushes or commit rewriting; main branch advances cleanly forward-- **Remote sync separate** - You manage `git push`/`git pull` to remotes yourself, worktrunk focuses on local branch workflow--**Example workflow:**-```bash-wt switch --create feature # Create feature worktree-# ... make changes ...-wt merge main # Rebase and merge to local main branch-cd ../repo # Switch to main worktree-git push origin main # Push to remote (standard git)-```--Worktrunk doesn't replace gitβit automates the worktree lifecycle on top of it.-## All Commands- `wt switch [branch]` - Switch to existing worktree- `wt switch --create [branch]` - Create and switch- `wt remove` - Remove current, return to main-- `wt merge [target]` - Rebase onto target, merge to target, and cleanup worktree-- `wt push [target]` - Fast-forward merge to another branch (no rebase)+- `wt merge [target]` - Merge, push, cleanup+- `wt push [target]` - Move changes to another branch- `wt list` - Show all worktrees- `wt config` - Manage configuration@@ -293,7 +267,7 @@ Automate common tasks by creating `.config/wt.toml` in your repository root. Run| **pre-commit-command** | Before committing changes during `wt merge` (when not squashing) | Sequential, blocking, fail-fast | Terminates merge immediately || **pre-squash-command** | Before squashing commits during `wt merge --squash` | Sequential, blocking, fail-fast | Terminates merge immediately || **pre-merge-command** | Before any commits/rebasing during `wt merge` | Sequential, blocking, fail-fast | Terminates merge immediately |-| **post-merge-command** | After successful merge to target branch, before cleanup | Sequential, blocking | Logs warning, continues with remaining commands |+| **post-merge-command** | After successful merge and push to target branch, before cleanup | Sequential, blocking | Logs warning, continues with remaining commands |**Template variables:** `{repo}`, `{branch}`, `{worktree}`, `{repo_root}`, `{target}`@@ -325,14 +299,14 @@ worktree-path = "../worktrees/{main-worktree}/{branch}"### Fast Branch Switching-Move changes from the current worktree directly to another branch without committing or merging. Useful for moving work-in-progress code.+Push changes from the current worktree directly to another branch without committing or merging. Useful for moving work-in-progress code.```bash-# Move current changes to another branch+# Push current changes to another branch$ wt push feature-experiment```-Worktrunk stages the changes, creates a commit, and merges it to the target branch.+Worktrunk stages the changes, creates a commit, and pushes it to the target branch's worktree if it exists.### Shell Integration Detailsdiff --git a/src/commands/merge.rs b/src/commands/merge.rsindex d70867684..064226bd0 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -1,19 +1,57 @@use worktrunk::config::{ProjectConfig, WorktrunkConfig};use worktrunk::git::{GitError, GitResultExt, Repository};use worktrunk::styling::{- AnstyleStyle, CYAN, CYAN_BOLD, HINT, HINT_EMOJI, format_bash_with_gutter, format_with_gutter,+ AnstyleStyle, CYAN, CYAN_BOLD, HINT, HINT_EMOJI, WARNING, WARNING_EMOJI,+ format_bash_with_gutter, format_with_gutter,};use super::command_executor::{CommandContext, prepare_project_commands};use super::worktree::{handle_push, handle_remove, parse_diff_shortstat};use crate::output::execute_command_in_worktree;+/// Extract untracked files from git status --porcelain output+fn get_untracked_files(status_output: &str) -> Vec<String> {+ let mut untracked = Vec::new();++ for line in status_output.lines() {+ // Git status --porcelain format: XY filename+ // Untracked files have "??" status+ if let Some(filename) = line.strip_prefix("?? ") {+ untracked.push(filename.to_string());+ }+ }++ untracked+}++/// Warn about untracked files being auto-staged+fn show_untracked_warning(repo: &Repository) -> Result<(), GitError> {+ let status = repo+ .run_command(&["status", "--porcelain"])+ .git_context("Failed to get status")?;+ let untracked = get_untracked_files(&status);++ if untracked.is_empty() {+ return Ok(());+ }++ // Format file list (comma-separated)+ let file_list = untracked.join(", ");++ crate::output::progress(format!(+ "{WARNING_EMOJI} {WARNING}Auto-staging untracked files: {file_list}{WARNING:#}"+ ))?;++ Ok(())+}+pub fn handle_merge(target: Option<&str>,squash_enabled: bool,keep: bool,no_hooks: bool,force: bool,+ tracked_only: bool,) -> Result<(), GitError> {let repo = Repository::current();@@ -39,12 +77,27 @@ pub fn handle_merge(// Handle uncommitted changes depending on whether we're squashingif repo.is_dirty()? {if squash_enabled {- // Just stage - squash will handle committing- repo.run_command(&["add", "-A"])- .git_context("Failed to stage changes")?;+ // Warn about untracked files before staging+ if !tracked_only {+ show_untracked_warning(&repo)?;+ }++ if tracked_only {+ repo.run_command(&["add", "-u"])+ .git_context("Failed to stage tracked changes")?;+ } else {+ repo.run_command(&["add", "-A"])+ .git_context("Failed to stage changes")?;+ }} else {// Commit immediately when not squashing- handle_commit_changes(&config.commit_generation, ¤t_branch, no_hooks, force)?;+ handle_commit_changes(+ &config.commit_generation,+ ¤t_branch,+ no_hooks,+ force,+ tracked_only,+ )?;}}@@ -291,6 +344,7 @@ fn handle_commit_changes(current_branch: &str,no_hooks: bool,force: bool,+ tracked_only: bool,) -> Result<(), GitError> {let repo = Repository::current();let config = WorktrunkConfig::load().git_context("Failed to load config")?;@@ -309,9 +363,19 @@ fn handle_commit_changes()?;}- // Stage all changes including untracked files- repo.run_command(&["add", "-A"])- .git_context("Failed to stage changes")?;+ // Warn about untracked files before staging (only if using git add -A)+ if !tracked_only {+ show_untracked_warning(&repo)?;+ }++ // Stage changes+ if tracked_only {+ repo.run_command(&["add", "-u"])+ .git_context("Failed to stage tracked changes")?;+ } else {+ repo.run_command(&["add", "-A"])+ .git_context("Failed to stage changes")?;+ }commit_with_generated_message("Committing changes...", commit_generation_config)}diff --git a/src/main.rs b/src/main.rsindex b9fabf860..98d3b2882 100644--- a/src/main.rs+++ b/src/main.rs@@ -255,6 +255,10 @@ Skip pre-merge commands:/// Skip approval prompts for commands#[arg(short, long)]force: bool,++ /// Only stage tracked files (git add -u) instead of all files (git add -A)+ #[arg(long)]+ tracked_only: bool,},/// Generate shell completion script (deprecated - use init instead)@@ -509,7 +513,15 @@ fn main() {keep,no_hooks,force,- } => handle_merge(target.as_deref(), squash_enabled, keep, no_hooks, force),+ tracked_only,+ } => handle_merge(+ target.as_deref(),+ squash_enabled,+ keep,+ no_hooks,+ force,+ tracked_only,+ ),Commands::Completion { shell } => {let mut cli_cmd = Cli::command();handle_completion(shell, &mut cli_cmd);diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex 1baf34546..23c211801 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -880,6 +880,56 @@ fn test_merge_auto_commit_and_squash() {);}+#[test]+fn test_merge_with_untracked_files() {+ let mut repo = TestRepo::new();+ repo.commit("Initial commit");+ repo.setup_remote("main");++ // Create a worktree for main+ let main_wt = repo.root_path().parent().unwrap().join("test-repo.main-wt");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["worktree", "add", main_wt.to_str().unwrap(), "main"])+ .current_dir(repo.root_path())+ .output()+ .expect("Failed to add worktree");++ // Create a feature worktree with one commit+ let feature_wt = repo.add_worktree("feature", "feature");+ std::fs::write(feature_wt.join("file1.txt"), "content 1").expect("Failed to write file");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["add", "file1.txt"])+ .current_dir(&feature_wt)+ .output()+ .expect("Failed to add file");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["commit", "-m", "feat: add file 1"])+ .current_dir(&feature_wt)+ .output()+ .expect("Failed to commit");++ // Add untracked files+ std::fs::write(feature_wt.join("untracked1.txt"), "untracked content 1")+ .expect("Failed to write file");+ std::fs::write(feature_wt.join("untracked2.txt"), "untracked content 2")+ .expect("Failed to write file");++ // Merge - should show warning about untracked files+ snapshot_merge_with_env(+ "merge_with_untracked_files",+ &repo,+ &["main"],+ Some(&feature_wt),+ &[+ ("WORKTRUNK_COMMIT_GENERATION__COMMAND", "echo"),+ ("WORKTRUNK_COMMIT_GENERATION__ARGS", "fix: commit changes"),+ ],+ );+}+#[test]fn test_merge_pre_merge_command_success() {let mut repo = TestRepo::new();diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex 2ab913d4d..38783f861 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpsmlUHy/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmptSlwtE/test-config.toml---success: trueexit_code: 0@@ -402,7 +402,7 @@ _wt() {return 0;;wt__merge)- opts="-k -f -v -h --no-squash --keep --no-hooks --force --verbose --help [TARGET]"+ opts="-k -f -v -h --no-squash --keep --no-hooks --force --tracked-only --verbose --help [TARGET]"if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; thenCOMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 02f59870e..dd7ab1751 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/.tmpDaJquk/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpEtNUfC/test-config.toml---success: trueexit_code: 0@@ -271,6 +271,7 @@ complete -c wt -n "__fish_wt_using_subcommand merge" -l no-squash -d 'Disable sqcomplete -c wt -n "__fish_wt_using_subcommand merge" -s k -l keep -d 'Keep worktree after merging (don\'t remove)'complete -c wt -n "__fish_wt_using_subcommand merge" -l no-hooks -d 'Skip all project hooks (pre-merge-command)'complete -c wt -n "__fish_wt_using_subcommand merge" -s f -l force -d 'Skip approval prompts for commands'+complete -c wt -n "__fish_wt_using_subcommand merge" -l tracked-only -d 'Only stage tracked files (git add -u) instead of all files (git add -A)'complete -c wt -n "__fish_wt_using_subcommand merge" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'complete -c wt -n "__fish_wt_using_subcommand merge" -s h -l help -d 'Print help (see more with \'--help\')'complete -c wt -n "__fish_wt_using_subcommand completion" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 68dceb336..70367a81a 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/.tmplCNLKR/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpG3qBik/test-config.toml---success: trueexit_code: 0@@ -312,6 +312,7 @@ _arguments "${_arguments_options[@]}" : \'--no-hooks[Skip all project hooks (pre-merge-command)]' \'-f[Skip approval prompts for commands]' \'--force[Skip approval prompts for commands]' \+'--tracked-only[Only stage tracked files (git add -u) instead of all files (git add -A)]' \'-v[Enable verbose output (show git commands and debug info)]' \'--verbose[Enable verbose output (show git commands and debug info)]' \'-h[Print help (see more with '\''--help'\'')]' \diff --git a/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap b/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snapindex 134bbe24e..2f9b324b8 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_dirty_working_tree.snap@@ -16,11 +16,12 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpc0fetn/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpB0fxlV/test-config.toml---success: trueexit_code: 0----- stdout -----+π‘ [33mAuto-staging untracked files: dirty.txt[0mπ [36mCommitting changes...[0m (1 file, [32m+1[0m)π [36mGenerating commit message...[0mπ‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snapindex d6760b558..201c21bf4 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap@@ -17,11 +17,12 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpB1r44t/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpDPSCsN/test-config.toml---success: trueexit_code: 0----- stdout -----+π‘ [33mAuto-staging untracked files: feature.txt[0mπ [36mCommitting changes...[0m (1 file, [32m+1[0m)π [36mGenerating commit message...[0mπ‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snapindex bd7ac10ce..3e1d6f00a 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap@@ -17,11 +17,12 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp6V1hsj/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpboobtT/test-config.toml---success: trueexit_code: 0----- stdout -----+π‘ [33mAuto-staging untracked files: feature.txt[0mπ [36mCommitting changes...[0m (1 file, [32m+1[0m)π [36mGenerating commit message...[0mπ‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snapnew file mode 100644index 000000000..326095b95--- /dev/null+++ b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap@@ -0,0 +1,51 @@+---+source: tests/integration_tests/merge.rs+info:+ program: wt+ args:+ - merge+ - main+ 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_COMMIT_GENERATION__ARGS: "fix: commit changes"+ WORKTRUNK_COMMIT_GENERATION__COMMAND: echo+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpf9g0jQ/test-config.toml+---+success: true+exit_code: 0+----- stdout -----+π‘ [33mAuto-staging untracked files: untracked1.txt, untracked2.txt[0m+π [36mSquashing 1 commits into 1[0m (1 file, [32m+1[0m)...+π [36mGenerating squash commit message...[0m+π‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0m+[40m [0m [1mSquash commits from main[0m+[40m [0m+[40m [0m Combined commits:+[40m [0m - feat: add file 1++β [32mSquashed 1 commits into 1[0m @ [SHA]+π [36mRebasing onto [0m[1m[36mmain[0m[36m...[0m+π [36mMerging 1 commit to [1m[36mmain[0m[0m @ [SHA]++* [SHA][33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m Squash commits from main++ file1.txt | 1 [32m+[m+ untracked1.txt | 1 [32m+[m+ untracked2.txt | 1 [32m+[m+ 3 files changed, 3 insertions(+)++β [32mMerged to [1m[32mmain[0m (1 commit, 3 files, [32m+3[0m) [0m+π [36mCleaning up worktree...[0m++β [32mReturned to primary at [2m[REPO][0m[0m++----- stderr -----