Skip to content

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

Add logs cache type to config cache clear command

Maximilian Roose25cbf09c072
diff --git a/src/cli.rs b/src/cli.rs
index e09fc7299..265a11af5 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -396,8 +396,8 @@ CI cache entries show status, age, and the commit SHA they were fetched for."#)]
/// Clear cached data
Clear {
- /// Cache type: 'ci' or 'default-branch' (omit for all)
- #[arg(value_parser = ["ci", "default-branch"])]
+ /// Cache type: 'ci', 'default-branch', or 'logs' (default: all)
+ #[arg(value_parser = ["ci", "default-branch", "logs"])]
cache_type: Option<String>,
},
diff --git a/src/commands/config.rs b/src/commands/config.rs
index da25db60e..7e1598852 100644
--- a/src/commands/config.rs
+++ b/src/commands/config.rs
@@ -688,26 +688,65 @@ pub fn handle_cache_clear(cache_type: Option<String>) -> anyhow::Result<()> {
crate::output::info("No default branch cache to clear")?;
}
}
+ Some("logs") => {
+ let cleared = clear_logs(&repo)?;
+ if cleared == 0 {
+ crate::output::info("No logs to clear")?;
+ } else {
+ crate::output::success(cformat!(
+ "Cleared <bold>{cleared}</> log file{}",
+ if cleared == 1 { "" } else { "s" }
+ ))?;
+ }
+ }
None => {
let cleared_default = repo
.run_command(&["config", "--unset", "worktrunk.defaultBranch"])
.is_ok();
let cleared_ci = CachedCiStatus::clear_all(&repo) > 0;
+ let cleared_logs = clear_logs(&repo)? > 0;
- if cleared_default || cleared_ci {
+ if cleared_default || cleared_ci || cleared_logs {
crate::output::success("Cleared all caches")?;
} else {
crate::output::info("No caches to clear")?;
}
}
Some(unknown) => {
- anyhow::bail!("Unknown cache type: {unknown}. Valid types: ci, default-branch");
+ anyhow::bail!("Unknown cache type: {unknown}. Valid types: ci, default-branch, logs");
}
}
Ok(())
}
+/// Clear all log files from the wt-logs directory
+fn clear_logs(repo: &Repository) -> anyhow::Result<usize> {
+ let git_common_dir = repo.git_common_dir()?;
+ let log_dir = git_common_dir.join("wt-logs");
+
+ if !log_dir.exists() {
+ return Ok(0);
+ }
+
+ let mut cleared = 0;
+ for entry in std::fs::read_dir(&log_dir)? {
+ let entry = entry?;
+ let path = entry.path();
+ if path.is_file() && path.extension().is_some_and(|ext| ext == "log") {
+ std::fs::remove_file(&path)?;
+ cleared += 1;
+ }
+ }
+
+ // Remove the directory if empty
+ if std::fs::read_dir(&log_dir)?.next().is_none() {
+ let _ = std::fs::remove_dir(&log_dir);
+ }
+
+ Ok(cleared)
+}
+
/// Handle the cache refresh command (refreshes default branch)
pub fn handle_cache_refresh() -> anyhow::Result<()> {
let repo = Repository::current();
diff --git a/tests/integration_tests/config_cache.rs b/tests/integration_tests/config_cache.rs
index 833861694..6794a3b69 100644
--- a/tests/integration_tests/config_cache.rs
+++ b/tests/integration_tests/config_cache.rs
@@ -133,8 +133,79 @@ fn test_config_cache_clear_unknown_type() {
assert!(!output.status.success());
assert_snapshot!(String::from_utf8_lossy(&output.stderr), @r"
error: invalid value 'unknown' for '[CACHE_TYPE]'
- [possible values: ci, default-branch]
+ [possible values: ci, default-branch, logs]
For more information, try '--help'.
");
}
+
+#[test]
+fn test_config_cache_clear_logs_empty() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ let output = wt_config_cache_cmd(&repo, &["clear", "logs"])
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ assert_snapshot!(String::from_utf8_lossy(&output.stderr), @"⚪ No logs to clear");
+}
+
+#[test]
+fn test_config_cache_clear_logs_with_files() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create wt-logs directory with some log files
+ let git_dir = repo.root_path().join(".git");
+ let log_dir = git_dir.join("wt-logs");
+ std::fs::create_dir_all(&log_dir).unwrap();
+ std::fs::write(log_dir.join("feature-post-start-npm.log"), "npm output").unwrap();
+ std::fs::write(log_dir.join("bugfix-remove.log"), "remove output").unwrap();
+
+ let output = wt_config_cache_cmd(&repo, &["clear", "logs"])
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ assert_snapshot!(String::from_utf8_lossy(&output.stderr), @"✅ Cleared 2 log files");
+
+ // Verify logs are gone
+ assert!(!log_dir.exists());
+}
+
+#[test]
+fn test_config_cache_clear_logs_single_file() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create wt-logs directory with one log file
+ let git_dir = repo.root_path().join(".git");
+ let log_dir = git_dir.join("wt-logs");
+ std::fs::create_dir_all(&log_dir).unwrap();
+ std::fs::write(log_dir.join("feature-remove.log"), "remove output").unwrap();
+
+ let output = wt_config_cache_cmd(&repo, &["clear", "logs"])
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ assert_snapshot!(String::from_utf8_lossy(&output.stderr), @"✅ Cleared 1 log file");
+}
+
+#[test]
+fn test_config_cache_clear_all_with_logs() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create wt-logs directory with a log file
+ let git_dir = repo.root_path().join(".git");
+ let log_dir = git_dir.join("wt-logs");
+ std::fs::create_dir_all(&log_dir).unwrap();
+ std::fs::write(log_dir.join("feature-remove.log"), "remove output").unwrap();
+
+ let output = wt_config_cache_cmd(&repo, &["clear"]).output().unwrap();
+ assert!(output.status.success());
+ assert_snapshot!(String::from_utf8_lossy(&output.stderr), @"✅ Cleared all caches");
+
+ // Verify logs are gone
+ assert!(!log_dir.exists());
+}