Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

feat(config): Add command to refresh default branch cache

Maximilian Roosd8799ff64553
diff --git a/src/commands/config.rs b/src/commands/config.rs
index c44cac7d9..edc002eee 100644
--- a/src/commands/config.rs
+++ b/src/commands/config.rs
@@ -156,6 +156,26 @@ fn get_global_config_path() -> Option<PathBuf> {
Some(strategy.config_dir().join("worktrunk").join("config.toml"))
}
+/// Handle the config refresh-cache command
+pub fn handle_config_refresh_cache() -> Result<(), GitError> {
+ use anstyle::{AnsiColor, Color};
+
+ let repo = Repository::current();
+
+ // Display progress message
+ let cyan = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)));
+ println!("🔄 {cyan}Querying remote for default branch...{cyan:#}");
+
+ // Refresh the cache (this will make a network call)
+ let branch = repo.refresh_default_branch()?;
+
+ // Display success message
+ let green_bold = GREEN.bold();
+ println!("{SUCCESS_EMOJI} {GREEN}Cache refreshed: {green_bold}{branch}{green_bold:#}{GREEN:#}");
+
+ Ok(())
+}
+
/// Handle the config help command - show LLM setup guide
pub fn handle_config_help() -> Result<(), GitError> {
let bold = AnstyleStyle::new().bold();
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 5ab9aadf9..1b40d9132 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -11,7 +11,9 @@ pub mod process;
pub mod worktree;
pub use completion::{handle_complete, handle_completion};
-pub use config::{handle_config_help, handle_config_init, handle_config_list};
+pub use config::{
+ handle_config_help, handle_config_init, handle_config_list, handle_config_refresh_cache,
+};
pub use configure_shell::{ConfigAction, handle_configure_shell};
pub use dev::handle_dev_run_hook;
pub use init::handle_init;
diff --git a/src/git.rs b/src/git.rs
index f521331f6..b42e2f69e 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -391,9 +391,17 @@ impl Repository {
/// Get the default branch name for the repository.
///
+ /// **Performance note:** This method may trigger a network call on first invocation
+ /// if the remote HEAD is not cached locally. The result is then cached in git's
+ /// config for subsequent calls. To minimize latency:
+ /// - Defer calling this until after fast, local checks (see e497f0f for example)
+ /// - Consider passing the result as a parameter if needed multiple times
+ /// - For optional operations, provide a fallback (e.g., `.unwrap_or("main")`)
+ ///
/// Uses a hybrid approach:
- /// 1. Try local cache (remote/HEAD) first for speed
- /// 2. If not cached, query the remote and cache the result
+ /// 1. Try local cache (`git rev-parse origin/HEAD`) first - fast, no network
+ /// 2. If not cached, query remote (`git ls-remote`) - may take 100ms-2s depending on network
+ /// 3. Cache the result (`git remote set-head`) for future invocations
pub fn default_branch(&self) -> Result<String, GitError> {
let remote = self.primary_remote()?;
// Try local cache first (fast path)
@@ -685,6 +693,21 @@ impl Repository {
Ok(())
}
+ /// Refresh the default branch cache by querying the remote.
+ ///
+ /// This forces a network call to `git ls-remote` to fetch the current default
+ /// branch from the remote, then updates the local cache. Use this when you
+ /// suspect the cached default branch is stale (e.g., after a repository's
+ /// default branch has been changed on the remote).
+ ///
+ /// Returns the refreshed default branch name.
+ pub fn refresh_default_branch(&self) -> Result<String, GitError> {
+ let remote = self.primary_remote()?;
+ let branch = self.query_remote_default_branch(&remote)?;
+ self.cache_default_branch(&remote, &branch)?;
+ Ok(branch)
+ }
+
// Private helper methods for default_branch()
fn get_local_default_branch(&self, remote: &str) -> Result<String, GitError> {
diff --git a/src/main.rs b/src/main.rs
index 682e819cf..6499ac6f4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,8 +12,9 @@ mod output;
use commands::{
ConfigAction, Shell, handle_complete, handle_completion, handle_config_help,
- handle_config_init, handle_config_list, handle_configure_shell, handle_dev_run_hook,
- handle_init, handle_list, handle_merge, handle_push, handle_remove, handle_switch,
+ handle_config_init, handle_config_list, handle_config_refresh_cache, handle_configure_shell,
+ handle_dev_run_hook, handle_init, handle_list, handle_merge, handle_push, handle_remove,
+ handle_switch,
};
use output::{handle_remove_output, handle_switch_output};
@@ -51,6 +52,8 @@ enum ConfigCommand {
List,
/// Show setup guide for AI-generated commit messages
Help,
+ /// Refresh the cached default branch by querying the remote
+ RefreshCache,
/// Configure shell by writing to config files
Shell {
/// Specific shell to configure (default: all shells with existing config files)
@@ -336,6 +339,7 @@ fn main() {
ConfigCommand::Init => handle_config_init(),
ConfigCommand::List => handle_config_list(),
ConfigCommand::Help => handle_config_help(),
+ ConfigCommand::RefreshCache => handle_config_refresh_cache(),
ConfigCommand::Shell { shell, force } => {
handle_configure_shell(shell, force)
.map(|results| {
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index d38eae5f6..e418c3349 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: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpiGClns/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpmGRD0M/test-config.toml
---
success: true
exit_code: 0
@@ -196,6 +196,9 @@ _wt() {
wt__config,list)
cmd="wt__config__list"
;;
+ wt__config,refresh-cache)
+ cmd="wt__config__refresh__cache"
+ ;;
wt__config,shell)
cmd="wt__config__shell"
;;
@@ -251,7 +254,7 @@ _wt() {
return 0
;;
wt__config)
- opts="-v -h --verbose --help init list help shell"
+ opts="-v -h --verbose --help init list help refresh-cache shell"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
@@ -306,6 +309,20 @@ _wt() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
+ wt__config__refresh__cache)
+ opts="-v -h --verbose --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__shell)
opts="-f -v -h --shell --force --verbose --help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 0acac10df..b7888287b 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -12,10 +12,11 @@ info:
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/.tmpjo4ARF/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpDEGyoG/test-config.toml
---
success: true
exit_code: 0
@@ -216,18 +217,21 @@ complete -c wt -n "__fish_wt_needs_command" -f -a "push" -d 'Push changes betwee
complete -c wt -n "__fish_wt_needs_command" -f -a "merge" -d 'Merge worktree into target branch'
complete -c wt -n "__fish_wt_using_subcommand init" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
complete -c wt -n "__fish_wt_using_subcommand init" -s h -l help -d 'Print help'
-complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help shell" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
-complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help shell" -s h -l help -d 'Print help'
-complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help shell" -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 help shell" -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 help shell" -f -a "help" -d 'Show setup guide for AI-generated commit messages'
-complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help shell" -f -a "shell" -d 'Configure shell by writing to config files'
+complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help refresh-cache shell" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
+complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help refresh-cache shell" -s h -l help -d 'Print help'
+complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help refresh-cache shell" -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 help refresh-cache shell" -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 help refresh-cache shell" -f -a "help" -d 'Show setup guide for AI-generated commit messages'
+complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help refresh-cache shell" -f -a "refresh-cache" -d 'Refresh the cached default branch by querying the remote'
+complete -c wt -n "__fish_wt_using_subcommand config; and not __fish_seen_subcommand_from init list help refresh-cache shell" -f -a "shell" -d 'Configure shell by writing to config files'
complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from init" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
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 v -l verbose -d 'Enable verbose output (show git commands and debug info)'
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 config; and __fish_seen_subcommand_from help" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from help" -s h -l help -d 'Print help'
+complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from refresh-cache" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
+complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from refresh-cache" -s h -l help -d 'Print help'
complete -c wt -n "__fish_wt_using_subcommand config; and __fish_seen_subcommand_from shell" -l shell -d 'Specific shell to configure (default: all shells with existing config files)' -r -f -a "bash\t''
elvish\t''
fish\t''
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 20c07a05c..fde52f051 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: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpbhsdNa/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpdLMByB/test-config.toml
---
success: true
exit_code: 0
@@ -201,6 +201,14 @@ _arguments "${_arguments_options[@]}" : \
'--help[Print help]' \
&& ret=0
;;
+(refresh-cache)
+_arguments "${_arguments_options[@]}" : \
+'-v[Enable verbose output (show git commands and debug info)]' \
+'--verbose[Enable verbose output (show git commands and debug info)]' \
+'-h[Print help]' \
+'--help[Print help]' \
+&& ret=0
+;;
(shell)
_arguments "${_arguments_options[@]}" : \
'--shell=[Specific shell to configure (default\: all shells with existing config files)]:SHELL:(bash elvish fish nushell oil powershell xonsh zsh)' \
@@ -366,6 +374,7 @@ _wt__config_commands() {
'init:Initialize global configuration file with examples' \
'list:List all configuration files and their locations' \
'help:Show setup guide for AI-generated commit messages' \
+'refresh-cache:Refresh the cached default branch by querying the remote' \
'shell:Configure shell by writing to config files' \
)
_describe -t commands 'wt config commands' commands "$@"
@@ -385,6 +394,11 @@ _wt__config__list_commands() {
local commands; commands=()
_describe -t commands 'wt config list commands' commands "$@"
}
+(( $+functions[_wt__config__refresh-cache_commands] )) ||
+_wt__config__refresh-cache_commands() {
+ local commands; commands=()
+ _describe -t commands 'wt config refresh-cache commands' commands "$@"
+}
(( $+functions[_wt__config__shell_commands] )) ||
_wt__config__shell_commands() {
local commands; commands=()