Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Add `config init` command to create example config
Maximilian Roos53a348529280
diff --git a/config.example.toml b/config.example.tomlnew file mode 100644index 000000000..586985545--- /dev/null+++ b/config.example.toml@@ -0,0 +1,38 @@+# Worktrunk Global Configuration+# Location (XDG Base Directory spec):+# macOS/Linux: ~/.config/worktrunk/config.toml (or $XDG_CONFIG_HOME/worktrunk/config.toml)+# Windows: %APPDATA%\worktrunk\config.toml+#+# Generate with: wt config init++# Worktree Path Template+# Variables: {repo} = repository name, {branch} = branch name+# Branch slashes are replaced with dashes (feature/auth → feature-auth)+# Paths are relative to the main worktree root (original repository directory)+worktree-path = "../{repo}.{branch}"++# Alternative: Keep worktrees inside repo (useful for bare repos)+# worktree-path = ".worktrees/{branch}"++# Alternative: Shared directory for multiple repos+# worktree-path = "../worktrees/{repo}/{branch}"++# LLM Integration (Optional)+# For generating commit messages during merge operations (wt merge)+[llm]+# Example: Simon Willison's llm CLI (https://github.com/simonw/llm)+# Install: pip install llm llm-anthropic+# command = "llm"+# args = ["-m", "claude-haiku-4.5"]++# Alternative: AIChat - Rust-based, supports 20+ providers+# Install from: https://github.com/sigoden/aichat+# command = "aichat"+# args = ["-m", "claude:claude-haiku-4.5"]++# Approved Commands+# Commands approved for automatic execution after switching worktrees+# Auto-populated when you use: wt switch --execute "command" --force+# [[approved-commands]]+# project = "github.com/user/repo"+# command = "npm install"diff --git a/src/commands/config.rs b/src/commands/config.rsindex d7403caf7..282d9a7fa 100644--- a/src/commands/config.rs+++ b/src/commands/config.rs@@ -1,8 +1,54 @@+use anstyle::{AnsiColor, Color};use etcetera::base_strategy::{BaseStrategy, choose_base_strategy};use std::path::PathBuf;use worktrunk::git::{GitError, Repository};use worktrunk::styling::{AnstyleStyle, HINT, HINT_EMOJI, println};+/// Example configuration file content+const CONFIG_EXAMPLE: &str = include_str!("../../config.example.toml");++/// Handle the config init command+pub fn handle_config_init() -> Result<(), GitError> {+ let config_path = get_global_config_path().ok_or_else(|| {+ GitError::CommandFailed("Could not determine global config path".to_string())+ })?;++ // Check if file already exists+ if config_path.exists() {+ let bold = AnstyleStyle::new().bold();+ println!(+ "Global config already exists: {bold}{}{bold:#}",+ config_path.display()+ );+ println!();+ println!("{HINT_EMOJI} {HINT}Use 'wt config list' to view existing configuration{HINT:#}");+ return Ok(());+ }++ // Create parent directory if it doesn't exist+ if let Some(parent) = config_path.parent() {+ std::fs::create_dir_all(parent).map_err(|e| {+ GitError::CommandFailed(format!("Failed to create config directory: {}", e))+ })?;+ }++ // Write the example config+ std::fs::write(&config_path, CONFIG_EXAMPLE)+ .map_err(|e| GitError::CommandFailed(format!("Failed to write config file: {}", e)))?;++ // Success message+ let green = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));+ let bold = AnstyleStyle::new().bold();+ println!("✅ {green}Created config file{green:#}");+ println!(" {bold}{}{bold:#}", config_path.display());+ println!();+ println!(+ "{HINT_EMOJI} {HINT}Edit this file to customize worktree paths and LLM settings{HINT:#}"+ );++ Ok(())+}+/// Handle the config list commandpub fn handle_config_list() -> Result<(), GitError> {// Display global config@@ -29,6 +75,7 @@ fn display_global_config() -> Result<(), GitError> {// Check if file existsif !config_path.exists() {println!(" {HINT_EMOJI} {HINT}Not found (using defaults){HINT:#}");+ println!(" {HINT_EMOJI} {HINT}Run 'wt config init' to create a config file{HINT:#}");println!();println!(" {dim}# Default configuration:{dim:#}");println!(" {dim}worktree-path = \"../{{repo}}.{{branch}}\"{dim:#}");diff --git a/src/commands/mod.rs b/src/commands/mod.rsindex e5a6d118a..8869ed6b7 100644--- a/src/commands/mod.rs+++ b/src/commands/mod.rs@@ -8,7 +8,7 @@ pub mod process;pub mod worktree;pub use completion::{handle_complete, handle_completion};-pub use config::handle_config_list;+pub use config::{handle_config_init, handle_config_list};pub use configure_shell::{ConfigAction, handle_configure_shell};pub use init::handle_init;pub use list::handle_list;diff --git a/src/main.rs b/src/main.rsindex 7d297f8b9..e633bf43e 100644--- a/src/main.rs+++ b/src/main.rs@@ -11,9 +11,9 @@ mod llm;mod output;use commands::{- ConfigAction, Shell, handle_complete, handle_completion, handle_config_list,- handle_configure_shell, handle_init, handle_list, handle_merge, handle_push, handle_remove,- handle_switch,+ ConfigAction, Shell, handle_complete, handle_completion, handle_config_init,+ handle_config_list, handle_configure_shell, handle_init, handle_list, handle_merge,+ handle_push, handle_remove, handle_switch,};use output::{handle_remove_output, handle_switch_output};@@ -37,6 +37,8 @@ struct Cli {#[derive(Subcommand)]enum ConfigCommand {+ /// Initialize global configuration file with examples+ Init,/// List all configuration files and their locationsList,}@@ -229,6 +231,7 @@ fn main() {.map_err(GitError::CommandFailed)}Commands::Config { action } => match action {+ ConfigCommand::Init => handle_config_init(),ConfigCommand::List => handle_config_list(),},Commands::List { format, branches } => handle_list(format, branches),diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex c4491b84f..b10135043 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -152,6 +152,9 @@ _wt() {wt,switch)cmd="wt__switch";;+ wt__config,init)+ cmd="wt__config__init"+ ;;wt__config,list)cmd="wt__config__list";;@@ -204,7 +207,7 @@ _wt() {return 0;;wt__config)- opts="-h --help list"+ opts="-h --help init list"if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; thenCOMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0@@ -217,6 +220,20 @@ _wt() {COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0;;+ wt__config__init)+ opts="-h --help"+ if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then+ COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )+ return 0+ fi+ case "${prev}" in+ *)+ COMPREPLY=()+ ;;+ esac+ COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )+ return 0+ ;;wt__config__list)opts="-h --help"if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; thendiff --git a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snapindex bccc4c664..62b5a8bc7 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap@@ -154,6 +154,9 @@ _wt() {wt,switch)cmd="wt__switch";;+ wt__config,init)+ cmd="wt__config__init"+ ;;wt__config,list)cmd="wt__config__list";;@@ -206,7 +209,7 @@ _wt() {return 0;;wt__config)- opts="-h --help list"+ opts="-h --help init list"if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; thenCOMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0@@ -219,6 +222,20 @@ _wt() {COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )return 0;;+ wt__config__init)+ opts="-h --help"+ if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then+ COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )+ return 0+ fi+ case "${prev}" in+ *)+ COMPREPLY=()+ ;;+ esac+ COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )+ return 0+ ;;wt__config__list)opts="-h --help"if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; thendiff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex 497b6bad8..8d61ff767 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -159,8 +159,10 @@ zsh\t''"complete -c wt -n "__fish_wt_using_subcommand configure-shell" -l cmd -d 'Command prefix (default: wt)' -rcomplete -c wt -n "__fish_wt_using_subcommand configure-shell" -s y -l yes -d 'Skip confirmation prompt'complete -c wt -n "__fish_wt_using_subcommand configure-shell" -s h -l help -d 'Print help'-complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from list" -s h -l help -d 'Print help'-complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from list" -f -a "list" -d 'List all configuration files and their locations'+complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list" -s h -l help -d 'Print help'+complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list" -f -a "init" -d 'Initialize global configuration file with examples'+complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list" -f -a "list" -d 'List all configuration files and their locations'+complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from init" -s h -l help -d 'Print help'complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from list" -s h -l help -d 'Print help'complete -c wt -n "__fish_wt_using_subcommand list" -l format -d 'Output format' -r -f -a "table\t'Human-readable table format'json\t'JSON format'"diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex a428af151..a877a2d51 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -166,7 +166,13 @@ _arguments "${_arguments_options[@]}" : \(( CURRENT += 1 ))curcontext="${curcontext%:*:*}:wt-config-command-$line[1]:"case $line[1] in- (list)+ (init)+_arguments "${_arguments_options[@]}" : \+'-h[Print help]' \+'--help[Print help]' \+&& ret=0+;;+(list)_arguments "${_arguments_options[@]}" : \'-h[Print help]' \'--help[Print help]' \@@ -276,10 +282,16 @@ _wt__completion_commands() {(( $+functions[_wt__config_commands] )) ||_wt__config_commands() {local commands; commands=(+'init:Initialize global configuration file with examples' \'list:List all configuration files and their locations' \)_describe -t commands 'wt config commands' commands "$@"}+(( $+functions[_wt__config__init_commands] )) ||+_wt__config__init_commands() {+ local commands; commands=()+ _describe -t commands 'wt config init commands' commands "$@"+}(( $+functions[_wt__config__list_commands] )) ||_wt__config__list_commands() {local commands; commands=()