Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Show log directory path in `wt config state logs` (#267)
Maximilian Roosa65f74bb34a8
diff --git a/src/commands/config.rs b/src/commands/config.rsindex ea211e8a8..619d2b2f0 100644--- a/src/commands/config.rs+++ b/src/commands/config.rs@@ -597,6 +597,73 @@ fn clear_logs(repo: &Repository) -> anyhow::Result<usize> {Ok(cleared)}+/// Render the LOG FILES section (heading + table or "(none)") into the output buffer+fn render_log_files(out: &mut String, repo: &Repository) -> anyhow::Result<()> {+ let git_common_dir = repo.git_common_dir()?;+ let log_dir = git_common_dir.join("wt-logs");+ let log_dir_display = format_path_for_display(&log_dir);++ writeln!(+ out,+ "{}",+ format_heading("LOG FILES", Some(&format!("@ {log_dir_display}")))+ )?;++ if !log_dir.exists() {+ write!(out, "{}", format_with_gutter("(none)", "", None))?;+ return Ok(());+ }++ let mut entries: Vec<_> = std::fs::read_dir(&log_dir)?+ .filter_map(|e| e.ok())+ .filter(|e| e.path().is_file() && e.path().extension().is_some_and(|ext| ext == "log"))+ .collect();++ if entries.is_empty() {+ write!(out, "{}", format_with_gutter("(none)", "", None))?;+ return Ok(());+ }++ // Sort by modification time (newest first), then by name for stability+ entries.sort_by(|a, b| {+ let a_time = a.metadata().and_then(|m| m.modified()).ok();+ let b_time = b.metadata().and_then(|m| m.modified()).ok();+ b_time+ .cmp(&a_time)+ .then_with(|| a.file_name().cmp(&b.file_name()))+ });++ // Build table+ let mut table = String::from("| File | Size | Age |\n");+ table.push_str("|------|------|-----|\n");++ for entry in entries {+ let path = entry.path();+ let name = path.file_name().unwrap_or_default().to_string_lossy();+ let meta = entry.metadata().ok();++ let size = meta.as_ref().map(|m| m.len()).unwrap_or(0);+ let size_str = if size < 1024 {+ format!("{size}B")+ } else {+ format!("{}K", size / 1024)+ };++ let age = meta+ .and_then(|m| m.modified().ok())+ .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())+ .map(|d| format_relative_time_short(d.as_secs() as i64))+ .unwrap_or_else(|| "?".to_string());++ table.push_str(&format!("| {name} | {size_str} | {age} |\n"));+ }++ let rendered = crate::md_help::render_markdown_table(&table);+ write!(out, "{}", rendered.trim_end())?;++ Ok(())+}+/// Handle the state get commandpub fn handle_state_get(key: &str, refresh: bool, branch: Option<String>) -> anyhow::Result<()> {use super::list::ci_status::PrStatus;@@ -658,63 +725,15 @@ pub fn handle_state_get(key: &str, refresh: bool, branch: Option<String>) -> anylet status_str: &'static str = ci_status.into();crate::output::data(status_str)?;}+ // TODO: Consider simplifying to just print the path and let users run `ls -al` themselves"logs" => {- let git_common_dir = repo.git_common_dir()?;- let log_dir = git_common_dir.join("wt-logs");-- if !log_dir.exists() {- crate::output::print(info_message("No logs"))?;- return Ok(());- }-- let mut entries: Vec<_> = std::fs::read_dir(&log_dir)?- .filter_map(|e| e.ok())- .filter(|e| {- e.path().is_file() && e.path().extension().is_some_and(|ext| ext == "log")- })- .collect();+ let mut out = String::new();+ render_log_files(&mut out, &repo)?;- if entries.is_empty() {- crate::output::print(info_message("No logs"))?;- return Ok(());+ // Display through pager (fall back to stderr if pager unavailable)+ if show_help_in_pager(&out).is_err() {+ worktrunk::styling::eprintln!("{}", out);}-- // Sort by modification time (newest first), then by name for stability- entries.sort_by(|a, b| {- let a_time = a.metadata().and_then(|m| m.modified()).ok();- let b_time = b.metadata().and_then(|m| m.modified()).ok();- b_time- .cmp(&a_time)- .then_with(|| a.file_name().cmp(&b.file_name()))- });-- // Build table- let mut table = String::from("| File | Size | Age |\n");- table.push_str("|------|------|-----|\n");-- for entry in entries {- let path = entry.path();- let name = path.file_name().unwrap_or_default().to_string_lossy();- let meta = entry.metadata().ok();-- let size = meta.as_ref().map(|m| m.len()).unwrap_or(0);- let size_str = if size < 1024 {- format!("{size}B")- } else {- format!("{}K", size / 1024)- };-- let age = meta- .and_then(|m| m.modified().ok())- .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())- .map(|d| format_relative_time_short(d.as_secs() as i64))- .unwrap_or_else(|| "?".to_string());-- table.push_str(&format!("| {name} | {size_str} | {age} |\n"));- }-- let rendered = crate::md_help::render_markdown_table(&table);- crate::output::table(rendered.trim_end())?;}_ => {anyhow::bail!(@@ -1124,63 +1143,7 @@ fn handle_state_show_table(repo: &Repository) -> anyhow::Result<()> {writeln!(out)?;// Show log files- let git_common_dir = repo.git_common_dir()?;- let log_dir = git_common_dir.join("wt-logs");- writeln!(- out,- "{}",- format_heading("LOG FILES", Some("@ .git/wt-logs"))- )?;-- if !log_dir.exists() {- write!(out, "{}", format_with_gutter("(none)", "", None))?;- } else {- let mut entries: Vec<_> = std::fs::read_dir(&log_dir)?- .filter_map(|e| e.ok())- .filter(|e| e.path().is_file() && e.path().extension().is_some_and(|ext| ext == "log"))- .collect();-- if entries.is_empty() {- write!(out, "{}", format_with_gutter("(none)", "", None))?;- } else {- // Sort by modification time (newest first), then by name for stability- entries.sort_by(|a, b| {- let a_time = a.metadata().and_then(|m| m.modified()).ok();- let b_time = b.metadata().and_then(|m| m.modified()).ok();- b_time- .cmp(&a_time)- .then_with(|| a.file_name().cmp(&b.file_name()))- });-- // Build table- let mut table = String::from("| File | Size | Age |\n");- table.push_str("|------|------|-----|\n");-- for entry in entries {- let path = entry.path();- let name = path.file_name().unwrap_or_default().to_string_lossy();- let meta = entry.metadata().ok();-- let size = meta.as_ref().map(|m| m.len()).unwrap_or(0);- let size_str = if size < 1024 {- format!("{size}B")- } else {- format!("{}K", size / 1024)- };-- let age = meta- .and_then(|m| m.modified().ok())- .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())- .map(|d| format_relative_time_short(d.as_secs() as i64))- .unwrap_or_else(|| "?".to_string());-- table.push_str(&format!("| {name} | {size_str} | {age} |\n"));- }-- let rendered = crate::md_help::render_markdown_table(&table);- write!(out, "{}", rendered.trim_end())?;- }- }+ render_log_files(&mut out, repo)?;// Display through pager (fall back to stderr if pager unavailable)if let Err(e) = show_help_in_pager(&out) {diff --git a/tests/integration_tests/config_state.rs b/tests/integration_tests/config_state.rsindex d0f634ffb..9d3d575e2 100644--- a/tests/integration_tests/config_state.rs+++ b/tests/integration_tests/config_state.rs@@ -1,8 +1,17 @@use crate::common::{TEST_EPOCH, TestRepo, repo, wt_command};use insta::assert_snapshot;+use regex::Regex;use rstest::rstest;use std::process::Command;+/// Normalize LOG FILES path in output for stable snapshots (temp dir varies per test)+fn normalize_log_path(output: &str) -> String {+ // Match "LOG FILES" followed by ANSI codes, spaces, and "@ " then the path+ // Pattern: LOG FILES + optional ANSI reset + spaces + @ + path until newline+ let re = Regex::new(r"(LOG FILES\x1b\[39m\s+@ )[^\n]+").unwrap();+ re.replace_all(output, "${1}<PATH>").to_string()+}+/// Write CI status to the file-based cache at .git/wt-cache/ci-status/<branch>.jsonfn write_ci_cache(repo: &TestRepo, branch: &str, json: &str) {let git_dir = repo.root_path().join(".git");@@ -411,7 +420,17 @@ fn test_state_clear_marker_all_empty(repo: TestRepo) {fn test_state_get_logs_empty(repo: TestRepo) {let output = wt_state_cmd(&repo, "logs", "get", &[]).output().unwrap();assert!(output.status.success());- assert_snapshot!(String::from_utf8_lossy(&output.stderr), @"[2mβ[22m No logs");+ // Path is dynamic (temp dir), so check pattern instead of exact snapshot+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ stderr.contains("LOG FILES"),+ "Expected LOG FILES heading: {stderr}"+ );+ // Path separator varies by platform, so check for either / or \+ assert!(+ stderr.contains(".git/wt-logs") || stderr.contains(".git\\wt-logs"),+ "Expected .git/wt-logs or .git\\wt-logs in output: {stderr}"+ );}#[rstest]@@ -431,6 +450,15 @@ fn test_state_get_logs_with_files(repo: TestRepo) {assert!(output.status.success());// Verify we get a table with file infolet stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ stderr.contains("LOG FILES"),+ "Expected LOG FILES heading: {stderr}"+ );+ // Path separator varies by platform, so check for either / or \+ assert!(+ stderr.contains(".git/wt-logs") || stderr.contains(".git\\wt-logs"),+ "Expected .git/wt-logs or .git\\wt-logs in output: {stderr}"+ );assert!(stderr.contains("File"));assert!(stderr.contains("Size"));assert!(stderr.contains("Age"));@@ -568,7 +596,7 @@ fn test_state_clear_all_nothing_to_clear(repo: TestRepo) {fn test_state_get_empty(repo: TestRepo) {let output = wt_state_get_cmd(&repo).output().unwrap();assert!(output.status.success());- assert_snapshot!(String::from_utf8_lossy(&output.stderr), @r"+ assert_snapshot!(normalize_log_path(&String::from_utf8_lossy(&output.stderr)), @r"[36mDEFAULT BRANCH[39m[107m [0m main@@ -581,7 +609,7 @@ fn test_state_get_empty(repo: TestRepo) {[36mCI STATUS CACHE[39m[107m [0m (none)- [36mLOG FILES[39m @ .git/wt-logs+ [36mLOG FILES[39m @ <PATH>[107m [0m (none)");}@@ -613,7 +641,7 @@ fn test_state_get_with_ci_entries(repo: TestRepo) {let output = wt_state_get_cmd(&repo).output().unwrap();assert!(output.status.success());- assert_snapshot!(String::from_utf8_lossy(&output.stderr));+ assert_snapshot!(normalize_log_path(&String::from_utf8_lossy(&output.stderr)));}#[rstest]@@ -657,7 +685,7 @@ fn test_state_get_comprehensive(repo: TestRepo) {let output = wt_state_get_cmd(&repo).output().unwrap();assert!(output.status.success());- assert_snapshot!(String::from_utf8_lossy(&output.stderr));+ assert_snapshot!(normalize_log_path(&String::from_utf8_lossy(&output.stderr)));}#[rstest]diff --git a/tests/integration_tests/snapshots/integration__integration_tests__config_state__state_get_comprehensive.snap b/tests/integration_tests/snapshots/integration__integration_tests__config_state__state_get_comprehensive.snapindex 6a6633582..546d00d2f 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__config_state__state_get_comprehensive.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__config_state__state_get_comprehensive.snap@@ -1,6 +1,6 @@---source: tests/integration_tests/config_state.rs-expression: "String::from_utf8_lossy(&output.stderr)"+expression: "normalize_log_path(&String::from_utf8_lossy(&output.stderr))"---[36mDEFAULT BRANCH[39m[107m [0m main@@ -19,7 +19,7 @@ expression: "String::from_utf8_lossy(&output.stderr)"βββββββ ββββββ βββ ββββββββfeature passed now abc12345-[36mLOG FILES[39m @ .git/wt-logs+[36mLOG FILES[39m @ <PATH>File Size Ageββββββββββββββββββββββββββ ββββ ββββββbugfix-remove.log 13B futurediff --git a/tests/integration_tests/snapshots/integration__integration_tests__config_state__state_get_with_ci_entries.snap b/tests/integration_tests/snapshots/integration__integration_tests__config_state__state_get_with_ci_entries.snapindex 22dea34ec..b3981ef67 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__config_state__state_get_with_ci_entries.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__config_state__state_get_with_ci_entries.snap@@ -1,6 +1,6 @@---source: tests/integration_tests/config_state.rs-expression: "String::from_utf8_lossy(&output.stderr)"+expression: "normalize_log_path(&String::from_utf8_lossy(&output.stderr))"---[36mDEFAULT BRANCH[39m[107m [0m main@@ -18,5 +18,5 @@ expression: "String::from_utf8_lossy(&output.stderr)"feature passed now abc12345main none now deadbeef-[36mLOG FILES[39m @ .git/wt-logs+[36mLOG FILES[39m @ <PATH>[107m [0m (none)