Skip to content

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

Use `home` crate to find home directory

Maximilian Roosbd8d50e8ba5d
diff --git a/Cargo.lock b/Cargo.lock
index f1f3232d5..cb063591f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1039,6 +1039,15 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
+[[package]]
+name = "home"
+version = "0.5.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cc627f471c528ff0c4a49e1d5e60450c8f6461dd6d10ba9dcd3a61d3dff7728d"
+dependencies = [
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "iana-time-zone"
version = "0.1.64"
@@ -2896,6 +2905,7 @@ dependencies = [
"env_logger",
"etcetera",
"fs2",
+ "home",
"indexmap",
"insta",
"insta-cmd",
diff --git a/Cargo.toml b/Cargo.toml
index b6a22bdbc..9c5cb613f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -70,6 +70,7 @@ tree-sitter-bash = { version = "0.25", optional = true }
tree-sitter-highlight = { version = "0.25", optional = true }
unicode-width = "0.2"
osc8 = "0.1.0"
+home = "0.5.12"
[target.'cfg(unix)'.dependencies]
skim = "0.20"
diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rs
index bd09e9269..c444788dd 100644
--- a/src/commands/configure_shell.rs
+++ b/src/commands/configure_shell.rs
@@ -101,7 +101,9 @@ fn scan_shell_configs(
let mut skipped = Vec::new();
for shell in shells {
- let paths = shell.config_paths(command_name);
+ let paths = shell
+ .config_paths(command_name)
+ .map_err(|e| e.to_string())?;
// Find the first existing config file
let target_path = paths.iter().find(|p| p.exists());
diff --git a/src/shell.rs b/src/shell.rs
index df07a8ad0..71886972b 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -1,6 +1,16 @@
use askama::Template;
use std::path::PathBuf;
+/// Get the user's home directory or return an error
+fn home_dir() -> Result<PathBuf, std::io::Error> {
+ home::home_dir().ok_or_else(|| {
+ std::io::Error::new(
+ std::io::ErrorKind::NotFound,
+ "Could not determine home directory",
+ )
+ })
+}
+
/// Supported shells
///
/// Currently supported: bash, fish, zsh
@@ -34,10 +44,10 @@ impl Shell {
///
/// Returns paths in order of preference. The first existing file should be used.
/// For Fish, the cmd_prefix is used to name the conf.d file.
- pub fn config_paths(&self, cmd_prefix: &str) -> Vec<PathBuf> {
- let home = PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".to_string()));
+ pub fn config_paths(&self, cmd_prefix: &str) -> Result<Vec<PathBuf>, std::io::Error> {
+ let home = home_dir()?;
- match self {
+ Ok(match self {
Self::Bash => {
// Use .bashrc - sourced by interactive shells (login shells should source .bashrc)
vec![home.join(".bashrc")]
@@ -89,7 +99,7 @@ impl Shell {
// Self::Xonsh => {
// vec![home.join(".xonshrc")]
// }
- }
+ })
}
/// Returns the line to add to the config file for shell integration
@@ -150,11 +160,11 @@ impl Shell {
///
/// This function is prefix-agnostic - it detects integration patterns regardless
/// of what cmd_prefix was used during configuration (wt, worktree, etc).
- pub fn is_integration_configured() -> Option<PathBuf> {
+ pub fn is_integration_configured() -> Result<Option<PathBuf>, std::io::Error> {
use std::fs;
use std::io::{BufRead, BufReader};
- let home = PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".to_string()));
+ let home = home_dir()?;
// Check common shell config files for integration patterns
let config_files = vec![
@@ -194,7 +204,7 @@ impl Shell {
if (trimmed.starts_with("eval \"$(") || trimmed.starts_with("eval '$("))
&& trimmed.contains(" init ")
{
- return Some(path);
+ return Ok(Some(path));
}
}
}
@@ -215,13 +225,13 @@ impl Shell {
&& content.contains("switch")
&& content.contains("__WORKTRUNK_CD__")
{
- return Some(path);
+ return Ok(Some(path));
}
}
}
}
- None
+ Ok(None)
}
/// Returns a summary of what the shell integration does for display in confirmation