Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: preserve git graph structure and align columns in wt select log preview (#557)
Maximilian Roos1bfa61d57c35
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yamlindex 1794fb34b..0f70a0b7a 100644--- a/.pre-commit-config.yaml+++ b/.pre-commit-config.yaml@@ -56,7 +56,8 @@ repos:types: ["rust"]entry: '\\x1b\['# format.rs strips ANSI codes injected by wrap_ansi crate- exclude: '^(tests/|.*_test\.rs$|src/styling/(line|mod|format)\.rs$|src/output/global\.rs$|src/cli\.rs$)'+ # select.rs tests verify ANSI dimming behavior+ exclude: '^(tests/|.*_test\.rs$|src/styling/(line|mod|format)\.rs$|src/output/global\.rs$|src/cli\.rs$|src/commands/select\.rs$)'- id: no-std-canonicalize-in-testsname: no-std-canonicalize-in-testsdescription: Use dunce::canonicalize in tests to avoid Windows \\?\ prefix issuesdiff --git a/src/commands/select.rs b/src/commands/select.rsindex 98c65b7df..7b36c7d07 100644--- a/src/commands/select.rs+++ b/src/commands/select.rs@@ -2,6 +2,7 @@ use anyhow::Context;use color_print::cformat;use skim::prelude::*;use std::borrow::Cow;+use std::collections::{HashMap, HashSet};use std::fs;use std::path::PathBuf;use std::process::{Command, Stdio};@@ -647,172 +648,318 @@ impl WorktreeSkimItem {// Format strings for git log// Without timestamps: hash (colored/dimmed), then message- // Dim format: only hash is dimmed, message stays normal (matches upstream style)- let no_timestamp_format = "--format=%C(auto)%h%C(auto)%d%C(reset) %s";- let dim_no_timestamp_format = "--format=%C(dim)%h%C(reset) %s";-- // With timestamps and diffstat, we use field delimiter and --numstat for predictable parsing:- // - \x1f (unit separator) separates fields within a commit line- // - --numstat gives tab-separated "added\tdeleted\tfilename" lines we sum- // Format: graph + hash \x1f timestamp \x1f message (followed by numstat lines)+ // Format includes full hash (for matching) between SOH and NUL delimiters.+ // Display content uses \x1f to separate fields for timestamp parsing.+ // Format: SOH full_hash NUL short_hash \x1f timestamp \x1f decorations+message+ // Using delimiters allows parsing without assuming fixed hash length (SHA-256 safe)+ // Note: Use %x01/%x00 (git's hex escapes) to avoid embedding control chars in argvlet timestamp_format = format!(- "--format=%C(auto)%h{}%ct{}%C(auto)%d%C(reset) %s",- FIELD_DELIM, FIELD_DELIM- );- // Dim format: hash is dimmed, timestamp will also be dimmed- let dim_timestamp_format = format!(- "--format=%C(dim)%h%C(reset){}%ct{} %s",+ "--format=%x01%H%x00%C(auto)%h{}%ct{}%C(auto)%d%C(reset) %s",FIELD_DELIM, FIELD_DELIM);+ let no_timestamp_format = "--format=%x01%H%x00%C(auto)%h%C(auto)%d%C(reset) %s";let log_limit_str = log_limit.to_string();- if is_default_branch {- // Viewing default branch itself - show history without dimming- let format: &str = if show_timestamps {- ×tamp_format- } else {- no_timestamp_format- };- let mut args = vec!["log", "--graph", format, "--color=always"];- if show_timestamps {- args.push("--numstat");- }- args.extend_from_slice(&["-n", &log_limit_str, head]);- if let Ok(log_output) = repo.run_command(&args) {- if show_timestamps {- output.push_str(&format_log_output(&log_output));- } else {- output.push_str(&log_output);- }- }++ // Get commits after merge-base (for dimming logic)+ // These are commits reachable from HEAD but not from merge-base, shown bright.+ // Commits before merge-base (shared with default branch) are shown dimmed.+ // Bounded to log_limit since we only need to check displayed commits.+ let unique_commits: Option<HashSet<String>> = if is_default_branch {+ // On default branch: no dimming (None means show everything bright)+ None} else {- // Not on default branch - show bright commits unique to this branch, dimmed commits on default- // Total commits shown is capped at log_limit (based on preview height)+ // On feature branch: get commits unique to this branch+ // rev-list A...B --right-only gives commits reachable from B but not A+ let range = format!("{}...{}", merge_base, head);+ let commits = repo+ .run_command(&["rev-list", &range, "--right-only", "-n", &log_limit_str])+ .map(|out| out.lines().map(String::from).collect())+ .unwrap_or_default();+ Some(commits) // Some(empty) means dim everything+ };- // Part 1: Bright commits (merge-base..HEAD)- let range = format!("{}..{}", merge_base, head);- let format: &str = if show_timestamps {- ×tamp_format- } else {- no_timestamp_format- };- let mut args = vec!["log", "--graph", format, "--color=always"];+ // Get graph output (no --numstat to avoid blank continuation lines)+ let format: &str = if show_timestamps {+ ×tamp_format+ } else {+ no_timestamp_format+ };+ let args = vec![+ "log",+ "--graph",+ format,+ "--color=always",+ "-n",+ &log_limit_str,+ head,+ ];++ if let Ok(log_output) = repo.run_command(&args) {+ let (processed, hashes) =+ process_log_with_dimming(&log_output, unique_commits.as_ref());if show_timestamps {- args.push("--numstat");- }- args.push(&range);- let mut bright_count = 0;- if let Ok(log_output) = repo.run_command(&args)- && !log_output.is_empty()- {- // Count commit lines (those with field delimiter, not numstat lines)- bright_count = log_output- .lines()- .filter(|l| l.contains(FIELD_DELIM))- .count();- if show_timestamps {- output.push_str(&format_log_output(&log_output));- } else {- output.push_str(&log_output);- }- // Ensure newline between bright and dim sections- if !output.ends_with('\n') {- output.push('\n');- }+ // Batch fetch stats for all commits+ let stats = batch_fetch_stats(&repo, &hashes);+ output.push_str(&format_log_output(&processed, &stats));+ } else {+ // Strip hash markers (SOH...NUL) since we're not using format_log_output+ output.push_str(&strip_hash_markers(&processed));}+ }- // Part 2: Dimmed commits on default branch (history before merge-base)- // Only show enough to reach LOG_LIMIT total- let dim_limit = log_limit.saturating_sub(bright_count);- if dim_limit > 0 {- let format: &str = if show_timestamps {- &dim_timestamp_format- } else {- dim_no_timestamp_format- };- let dim_limit_str = dim_limit.to_string();- let mut args = vec!["log", "--graph", format, "--color=always"];- if show_timestamps {- args.push("--numstat");- }- args.extend_from_slice(&["-n", &dim_limit_str, merge_base]);- if let Ok(log_output) = repo.run_command(&args) {- if show_timestamps {- output.push_str(&format_log_output(&log_output));- } else {- output.push_str(&log_output);- }- }+ output+ }+}++/// Batch fetch diffstats for multiple commits using git diff-tree --stdin.+/// Returns a map of full_hash -> (insertions, deletions).+///+/// Note: This bypasses shell_exec::run() because it requires stdin piping,+/// which run() doesn't support. Failures are silent (preview context).+fn batch_fetch_stats(repo: &Repository, hashes: &[String]) -> HashMap<String, (usize, usize)> {+ use std::io::Write;++ if hashes.is_empty() {+ return HashMap::new();+ }++ let mut cmd = Command::new("git");+ // --root: include stats for root commits (no parent to diff against)+ cmd.args(["diff-tree", "--numstat", "-r", "--root", "--stdin"])+ .current_dir(repo.worktree_base().unwrap_or_else(|_| ".".into()))+ .stdin(Stdio::piped())+ .stdout(Stdio::piped())+ .stderr(Stdio::null());++ let Ok(mut child) = cmd.spawn() else {+ return HashMap::new();+ };++ // Write hashes to stdin+ if let Some(mut stdin) = child.stdin.take() {+ for hash in hashes {+ let _ = writeln!(stdin, "{}", hash);+ }+ }++ let Ok(output) = child.wait_with_output() else {+ return HashMap::new();+ };++ // Parse output: hash line followed by numstat lines+ let mut stats: HashMap<String, (usize, usize)> = HashMap::new();+ let mut current_hash: Option<String> = None;+ let mut current_stats = (0usize, 0usize);++ for line in String::from_utf8_lossy(&output.stdout).lines() {+ // Hash line (40 or 64 hex chars)+ if line.chars().all(|c| c.is_ascii_hexdigit()) && (line.len() == 40 || line.len() == 64) {+ // Save previous hash's stats+ if let Some(hash) = current_hash.take() {+ stats.insert(hash, current_stats);}+ current_hash = Some(line.to_string());+ current_stats = (0, 0);+ } else if let Some((ins, del)) = parse_numstat_line(line) {+ current_stats.0 += ins;+ current_stats.1 += del;}+ }- output+ // Don't forget the last hash+ if let Some(hash) = current_hash {+ stats.insert(hash, current_stats);}++ stats}/// Field delimiter for git log format with timestampsconst FIELD_DELIM: char = '\x1f';+/// Start delimiter for full hash (SOH - Start of Heading)+const HASH_START: char = '\x01';++/// End delimiter for full hash (NUL)+const HASH_END: char = '\x00';+/// Timestamp column width ("12mo" is the longest)const TIMESTAMP_WIDTH: usize = 4;+/// Process git log output: strip hash prefix and dim non-unique commits.+///+/// - `unique_commits = None`: show everything bright (default branch)+/// - `unique_commits = Some(set)`: bright if in set, dim otherwise+/// - Graph-only lines pass through unchanged+///+/// Returns (processed_output, list_of_full_hashes) for batch stats lookup.+fn process_log_with_dimming(+ log_output: &str,+ unique_commits: Option<&HashSet<String>>,+) -> (String, Vec<String>) {+ use ansi_str::AnsiStr;++ let dim = anstyle::Style::new().dimmed();+ let reset = anstyle::Reset;++ let mut result = String::with_capacity(log_output.len());+ let mut hashes = Vec::new();++ for (i, line) in log_output.lines().enumerate() {+ if i > 0 {+ result.push('\n');+ }++ // Parse commit line: graph_prefix + SOH + full_hash + NUL + display+ if let Some(hash_start) = line.find(HASH_START)+ && let Some(hash_end_offset) = line[hash_start + 1..].find(HASH_END)+ {+ let hash_end = hash_start + 1 + hash_end_offset;+ let graph_prefix = &line[..hash_start];+ let full_hash = &line[hash_start + 1..hash_end];+ let display = &line[hash_end + 1..];++ // Collect hash for stats lookup+ hashes.push(full_hash.to_string());++ // Bright if: no dimming (None) OR commit is in unique set+ let is_bright = match unique_commits {+ None => true, // Default branch: all bright+ Some(set) => set.contains(full_hash), // Feature branch: bright if unique+ };++ // Keep SOH hash NUL markers for format_log_output to extract hash for stats lookup+ if is_bright {+ result.push_str(graph_prefix);+ result.push(HASH_START);+ result.push_str(full_hash);+ result.push(HASH_END);+ result.push_str(display);+ } else {+ // Dim: strip colors and wrap in dim style, but keep hash markers+ result.push_str(graph_prefix);+ result.push(HASH_START);+ result.push_str(full_hash);+ result.push(HASH_END);+ result.push_str(&dim.to_string());+ result.push_str(&display.ansi_strip());+ result.push_str(&reset.to_string());+ }+ continue;+ }+ // Graph-only lines: pass through unchanged+ result.push_str(line);+ }+ (result, hashes)+}+/// Format git log output with timestamps and diffstats.///-/// Parses git log output in the format:-/// `graph_hash\x1ftimestamp\x1f decoration message`-/// followed by numstat lines (`added\tdeleted\tfilename`).+/// Takes pre-processed log output (graph + commits) and a stats map.+/// Each commit line has format: `graph_prefix short_hash \x1f timestamp \x1f decoration message`///-/// Returns formatted output with aligned timestamps and diff stats.-fn format_log_output(log_output: &str) -> String {+/// The full hash for stats lookup is embedded as: `SOH full_hash NUL` before the short hash,+/// but this was already stripped by process_log_with_dimming. We need the hash in the line+/// to look up stats - so we keep the full hash in a different delimiter for this function.+fn format_log_output(log_output: &str, stats: &HashMap<String, (usize, usize)>) -> String {use crate::display::format_relative_time_short;- format_log_output_with_formatter(log_output, format_relative_time_short)+ format_log_output_with_formatter(log_output, stats, format_relative_time_short)}/// Format git log output with a custom time formatter.////// This variant allows dependency injection for testing with deterministic timestamps.-fn format_log_output_with_formatter<F>(log_output: &str, format_time: F) -> String+fn format_log_output_with_formatter<F>(+ log_output: &str,+ stats: &HashMap<String, (usize, usize)>,+ format_time: F,+) -> StringwhereF: Fn(i64) -> String,{- // State machine: accumulate stats for each commit- let mut pending_commit: Option<&str> = None;- let mut pending_stats: (usize, usize) = (0, 0);- let mut result = Vec::new();+ use ansi_str::AnsiStr;+ use unicode_width::UnicodeWidthStr;++ // First pass: find max display width of graph+hash prefix for alignment+ let max_prefix_width = log_output+ .lines()+ .filter(|line| line.contains(FIELD_DELIM))+ .filter_map(|line| {+ let first_delim = line.find(FIELD_DELIM)?;+ let graph_hash_raw = &line[..first_delim];+ let graph_hash = strip_hash_markers(graph_hash_raw);+ // Calculate display width (strip ANSI, measure unicode width)+ Some(graph_hash.ansi_strip().width())+ })+ .max()+ .unwrap_or(0);+ // Second pass: format with alignment+ let mut result = Vec::new();for line in log_output.lines() {if line.contains(FIELD_DELIM) {- // This is a commit line - emit any pending commit first- if let Some(prev) = pending_commit.take() {- result.push(format_commit_line(prev, pending_stats, &format_time));- }- pending_commit = Some(line);- pending_stats = (0, 0);- } else if let Some((ins, del)) = parse_numstat_line(line) {- // Accumulate stats for pending commit- pending_stats.0 += ins;- pending_stats.1 += del;+ // Commit line - look up stats by hash extracted from line+ let commit_stats = extract_hash_from_line(line)+ .and_then(|h| stats.get(h))+ .copied()+ .unwrap_or((0, 0));+ result.push(format_commit_line(+ line,+ commit_stats,+ max_prefix_width,+ &format_time,+ ));+ } else {+ // Graph-only line - pass through+ result.push(line.to_string());}- // Skip empty/graph-only lines- }-- // Don't forget final commit- if let Some(last) = pending_commit {- result.push(format_commit_line(last, pending_stats, &format_time));}result.join("\n")}-/// Format a single commit line with stats+/// Extract the full hash from a commit line that still has SOH/NUL markers.+/// Returns None if not found (line already processed or malformed).+fn extract_hash_from_line(line: &str) -> Option<&str> {+ let hash_start = line.find(HASH_START)?;+ let hash_end_offset = line[hash_start + 1..].find(HASH_END)?;+ Some(&line[hash_start + 1..hash_start + 1 + hash_end_offset])+}++/// Strip SOH...NUL hash markers from output (used when not formatting with timestamps).+fn strip_hash_markers(s: &str) -> String {+ let mut result = String::with_capacity(s.len());+ let mut chars = s.chars().peekable();++ while let Some(c) = chars.next() {+ if c == HASH_START {+ // Skip until NUL+ while let Some(&next) = chars.peek() {+ chars.next();+ if next == HASH_END {+ break;+ }+ }+ } else {+ result.push(c);+ }+ }+ result+}++/// Format a single commit line with stats, padding the prefix to target_width for alignmentfn format_commit_line<F>(commit_line: &str,(insertions, deletions): (usize, usize),+ target_width: usize,format_time: &F,) -> StringwhereF: Fn(i64) -> String,{+ use ansi_str::AnsiStr;+ use unicode_width::UnicodeWidthStr;use worktrunk::styling::{ADDITION, DELETION};let dim_style = anstyle::Style::new().dimmed();@@ -821,7 +968,9 @@ whereif let Some(first_delim) = commit_line.find(FIELD_DELIM)&& let Some(second_delim) = commit_line[first_delim + 1..].find(FIELD_DELIM){- let graph_hash = &commit_line[..first_delim];+ let graph_hash_raw = &commit_line[..first_delim];+ // Strip SOH...NUL hash markers from graph_hash portion+ let graph_hash = strip_hash_markers(graph_hash_raw);let timestamp_str = &commit_line[first_delim + 1..first_delim + 1 + second_delim];let rest = &commit_line[first_delim + 1 + second_delim + 1..];@@ -839,9 +988,14 @@ where};let stat_str = format!(" {}", diff_config.format_aligned(insertions, deletions));+ // Pad graph_hash to target_width for column alignment+ let current_width = graph_hash.ansi_strip().width();+ let padding = " ".repeat(target_width.saturating_sub(current_width));+format!(- "{}{} {dim_style}{:>width$}{reset}{}",+ "{}{}{} {dim_style}{:>width$}{reset}{}",graph_hash,+ padding,stat_str,time,rest,@@ -1239,31 +1393,56 @@ mod tests {"1h".to_string() // Return a fixed time for all timestamps}+ /// Create a stats map with a single entry+ fn stats_for(+ hash: &str,+ insertions: usize,+ deletions: usize,+ ) -> HashMap<String, (usize, usize)> {+ let mut map = HashMap::new();+ map.insert(hash.to_string(), (insertions, deletions));+ map+ }++ /// Create a stats map with multiple entries+ fn multi_stats(entries: &[(&str, usize, usize)]) -> HashMap<String, (usize, usize)> {+ entries+ .iter()+ .map(|(h, i, d)| (h.to_string(), (*i, *d)))+ .collect()+ }+#[test]fn test_format_log_output_single_commit() {- // Simulate git log output: hash\x1ftimestamp\x1f message- let input = "abc1234\x1f1699999000\x1f Fix bug";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ // Simulate git log output with SOH/NUL markers: * SOH full_hash NUL short_hash \x1f timestamp \x1f message+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let input = format!("* \x01{}\x00abc1234\x1f1699999000\x1f Fix bug", full_hash);+ let stats = stats_for(full_hash, 5, 2);+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);// Should contain the hash and messageassert!(output.contains("abc1234"));assert!(output.contains("Fix bug"));// Should contain formatted timeassert!(output.contains("1h"));+ // Should contain stats+ assert!(output.contains("+5"), "expected +5 in output: {}", output);}#[test]- fn test_format_log_output_with_numstat() {- // Commit line followed by numstat lines- let input = "abc1234\x1f1699999000\x1f Add feature\n\- 10\t5\tfile1.rs\n\- 3\t0\tfile2.rs";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ fn test_format_log_output_with_stats() {+ // Commit line with pre-computed stats+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let input = format!(+ "* \x01{}\x00abc1234\x1f1699999000\x1f Add feature",+ full_hash+ );+ // Stats are pre-computed (accumulated from numstat lines)+ let stats = stats_for(full_hash, 13, 5);+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);// Should contain the hash and messageassert!(output.contains("abc1234"));- // Stats should be accumulated: 10+3=13 insertions, 5+0=5 deletions- // The output should contain the stats in the formatted lineassert!(output.contains("Add feature"));// Verify stats are present (green +13, red -5)assert!(output.contains("+13"), "expected +13 in output: {}", output);@@ -1272,12 +1451,16 @@ mod tests {#[test]fn test_format_log_output_multiple_commits() {- // Two commits, each with numstat- let input = "abc1234\x1f1699999000\x1f First commit\n\- 5\t2\tfile.rs\n\- def5678\x1f1699998000\x1f Second commit\n\- 10\t3\tother.rs";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ // Two commits with pre-computed stats+ let hash1 = "abc1234567890123456789012345678901234567ab";+ let hash2 = "def5678901234567890123456789012345678901cd";+ let input = format!(+ "* \x01{}\x00abc1234\x1f1699999000\x1f First commit\n\+ * \x01{}\x00def5678\x1f1699998000\x1f Second commit",+ hash1, hash2+ );+ let stats = multi_stats(&[(hash1, 5, 2), (hash2, 10, 3)]);+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);// Both commits should be in outputassert!(output.contains("abc1234"));@@ -1292,15 +1475,51 @@ mod tests {#[test]fn test_format_log_output_empty_input() {- let output = format_log_output_with_formatter("", fixed_time_formatter);+ let stats = HashMap::new();+ let output = format_log_output_with_formatter("", &stats, fixed_time_formatter);assert!(output.is_empty());}#[test]- fn test_format_log_output_no_numstat() {- // Commit without numstat lines- let input = "abc1234\x1f1699999000\x1f Just a commit";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ fn test_format_log_output_preserves_graph_lines() {+ // Merge commit with graph continuation line between commits+ let hash1 = "abc1234567890123456789012345678901234567ab";+ let hash2 = "def5678901234567890123456789012345678901cd";+ let input = format!(+ "* \x01{}\x00abc1234\x1f1699999000\x1f Merge branch\n\+ |\\ \n\+ | * \x01{}\x00def5678\x1f1699998000\x1f Feature commit",+ hash1, hash2+ );+ let stats = multi_stats(&[(hash1, 0, 0), (hash2, 5, 2)]);+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);++ // Graph line should be preserved between commits+ assert!(output.contains("|\\"), "graph line should be preserved");+ assert!(output.contains("abc1234"), "first commit should be present");+ assert!(+ output.contains("def5678"),+ "second commit should be present"+ );++ // Verify order: merge commit, graph line, feature commit+ let lines: Vec<&str> = output.lines().collect();+ assert_eq!(lines.len(), 3, "Expected 3 lines: {:?}", lines);+ assert!(lines[0].contains("abc1234"));+ assert!(lines[1].contains("\\"));+ assert!(lines[2].contains("def5678"));+ }++ #[test]+ fn test_format_log_output_no_stats() {+ // Commit without stats (not in stats map)+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let input = format!(+ "* \x01{}\x00abc1234\x1f1699999000\x1f Just a commit",+ full_hash+ );+ let stats = HashMap::new(); // Empty stats - shows no diff+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);assert!(output.contains("abc1234"));assert!(output.contains("Just a commit"));@@ -1309,9 +1528,13 @@ mod tests {#[test]fn test_format_log_output_with_graph_prefix() {// Git graph output includes graph characters- let input = "* abc1234\x1f1699999000\x1f Commit with graph\n\- | 5\t2\tfile.rs";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let input = format!(+ "* \x01{}\x00abc1234\x1f1699999000\x1f Commit with graph",+ full_hash+ );+ let stats = stats_for(full_hash, 5, 2);+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);assert!(output.contains("abc1234"));assert!(output.contains("Commit with graph"));@@ -1321,36 +1544,34 @@ mod tests {}#[test]- fn test_format_log_output_binary_files() {- // Binary files show "-" in numstat- let input = "abc1234\x1f1699999000\x1f Add image\n\- -\t-\timage.png\n\- 5\t0\tdocs.md";- let output = format_log_output_with_formatter(input, fixed_time_formatter);-- // Binary files treated as 0 additions/deletions- // Should still format the commit line+ fn test_format_log_output_zero_stats() {+ // Commit with zero stats (e.g., binary-only changes)+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let input = format!("* \x01{}\x00abc1234\x1f1699999000\x1f Add image", full_hash);+ let stats = stats_for(full_hash, 0, 0);+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);+assert!(output.contains("abc1234"));assert!(output.contains("Add image"));- // Verify stats: 0 (binary) + 5 = 5 insertions, 0 deletions- assert!(output.contains("+5"), "expected +5 in output: {}", output);}#[test]fn test_format_log_output_malformed_commit_line() {- // Line without proper field delimiters should be passed through+ // Line without proper field delimiters passes throughlet input = "abc1234 regular commit line";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ let stats = HashMap::new();+ let output = format_log_output_with_formatter(input, &stats, fixed_time_formatter);- // Should be empty since no valid commit lines (no FIELD_DELIM)- assert!(output.is_empty());+ // Lines without \x1f delimiter pass through unchanged+ assert!(output.contains("abc1234"));}#[test]fn test_format_log_output_commit_line_missing_second_delimiter() {// Only one delimiter - malformedlet input = "abc1234\x1f1699999000 Fix bug";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ let stats = HashMap::new();+ let output = format_log_output_with_formatter(input, &stats, fixed_time_formatter);// Should output the line as-is since it's malformed (only one \x1f)assert!(output.contains("abc1234"));@@ -1359,9 +1580,13 @@ mod tests {#[test]fn test_format_log_output_stats_only_deletions() {// Commit with only deletions (no insertions)- let input = "abc1234\x1f1699999000\x1f Remove old code\n\- 0\t50\told_file.rs";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let input = format!(+ "* \x01{}\x00abc1234\x1f1699999000\x1f Remove old code",+ full_hash+ );+ let stats = stats_for(full_hash, 0, 50);+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);assert!(output.contains("abc1234"));assert!(output.contains("Remove old code"));@@ -1372,9 +1597,13 @@ mod tests {#[test]fn test_format_log_output_large_stats() {// Commit with large stats (tests K notation)- let input = "abc1234\x1f1699999000\x1f Big refactor\n\- 1500\t800\tlarge_file.rs";- let output = format_log_output_with_formatter(input, fixed_time_formatter);+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let input = format!(+ "* \x01{}\x00abc1234\x1f1699999000\x1f Big refactor",+ full_hash+ );+ let stats = stats_for(full_hash, 1500, 800);+ let output = format_log_output_with_formatter(&input, &stats, fixed_time_formatter);assert!(output.contains("abc1234"));// Large numbers should use K notation@@ -1390,7 +1619,8 @@ mod tests {// Test the format_commit_line function directlylet commit_line = "abc1234\x1f1699999000\x1f Test commit";let stats = (10, 5);- let output = format_commit_line(commit_line, stats, &fixed_time_formatter);+ let target_width = 7; // "abc1234" is 7 chars, no padding needed+ let output = format_commit_line(commit_line, stats, target_width, &fixed_time_formatter);assert!(output.contains("abc1234"));assert!(output.contains("Test commit"));@@ -1398,4 +1628,245 @@ mod tests {assert!(output.contains("-5"), "expected -5 in output: {}", output);assert!(output.contains("1h"), "expected time in output: {}", output);}++ #[test]+ fn test_format_commit_line_with_padding() {+ // Test that padding aligns shorter hashes to target width+ let commit_line = "abc12\x1f1699999000\x1f Short hash";+ let stats = (5, 2);+ let target_width = 9; // Pad "abc12" (5 chars) to 9 chars+ let output = format_commit_line(commit_line, stats, target_width, &fixed_time_formatter);++ // Should have 4 spaces of padding after hash before stats+ assert!(output.contains("abc12 "), "expected padding: {}", output);+ }++ // Tests for process_log_with_dimming+ //+ // Input format: graph_prefix + SOH (\x01) + full_hash + NUL (\x00) + display+ // Example: "* \x01abc123...def456\x00abc1234 (HEAD) message"++ /// Parse output to determine which lines are dimmed vs bright.+ /// Returns (is_dimmed, content) for each line.+ fn parse_dimming_output(output: &str) -> Vec<(bool, String)> {+ use ansi_str::AnsiStr;+ output+ .lines()+ .map(|line| {+ // Check if line contains dim escape sequence (\x1b[2m)+ let is_dimmed = line.contains("\x1b[2m");+ let content = line.ansi_strip().to_string();+ (is_dimmed, content)+ })+ .collect()+ }++ #[test]+ fn test_process_log_with_dimming_parses_commit_line() {+ // Simulates git log output with SOH/NUL delimiters around full hash+ let hash = "abc123456789012345678901234567890123456789";+ let input = format!("* \x01{}\x00abc1234 (HEAD) Fix bug", hash);++ let unique = HashSet::from([hash.to_string()]);+ let (output, hashes) = process_log_with_dimming(&input, Some(&unique));++ // SOH/NUL markers are preserved for format_log_output to extract hashes+ assert!(+ output.contains('\x01'),+ "SOH should be preserved for format_log_output"+ );+ assert!(+ output.contains('\x00'),+ "NUL should be preserved for format_log_output"+ );+ assert!(output.contains("abc1234"), "short hash preserved");+ assert!(output.contains("Fix bug"), "message preserved");++ // Hashes should be collected for batch stats lookup+ assert_eq!(hashes.len(), 1);+ assert_eq!(hashes[0], hash);++ // Should be bright (in unique set)+ let parsed = parse_dimming_output(&output);+ assert_eq!(parsed.len(), 1);+ assert!(!parsed[0].0, "commit in unique set should be bright");+ }++ #[test]+ fn test_process_log_with_dimming_dims_non_unique() {+ let unique_hash = "abc123456789012345678901234567890123456789";+ let other_hash = "def123456789012345678901234567890123456789";++ let input = format!(+ "* \x01{}\x00abc1234 Unique commit\n\+ * \x01{}\x00def1234 Not unique",+ unique_hash, other_hash+ );++ let unique = HashSet::from([unique_hash.to_string()]);+ let (output, hashes) = process_log_with_dimming(&input, Some(&unique));++ // Both hashes should be collected+ assert_eq!(hashes.len(), 2);++ let parsed = parse_dimming_output(&output);+ assert_eq!(parsed.len(), 2);++ // First commit (unique) should be bright+ assert!(!parsed[0].0, "unique commit should be bright");+ assert!(parsed[0].1.contains("Unique commit"));++ // Second commit (not unique) should be dimmed+ assert!(parsed[1].0, "non-unique commit should be dimmed");+ assert!(parsed[1].1.contains("Not unique"));+ }++ #[test]+ fn test_process_log_with_dimming_none_means_all_bright() {+ // None = default branch, show everything bright+ let hash = "abc123456789012345678901234567890123456789";+ let input = format!("* \x01{}\x00abc1234 Some commit", hash);++ let (output, hashes) = process_log_with_dimming(&input, None);++ assert_eq!(hashes.len(), 1);+ let parsed = parse_dimming_output(&output);+ assert_eq!(parsed.len(), 1);+ assert!(!parsed[0].0, "None means default branch, all bright");+ }++ #[test]+ fn test_process_log_with_dimming_empty_set_means_all_dim() {+ // Some(empty) = feature branch with no unique commits, dim everything+ let hash = "abc123456789012345678901234567890123456789";+ let input = format!("* \x01{}\x00abc1234 Some commit", hash);++ let empty: HashSet<String> = HashSet::new();+ let (output, hashes) = process_log_with_dimming(&input, Some(&empty));++ assert_eq!(hashes.len(), 1);+ let parsed = parse_dimming_output(&output);+ assert_eq!(parsed.len(), 1);+ assert!(+ parsed[0].0,+ "Some(empty) means feature branch with no unique commits, all dim"+ );+ }++ #[test]+ fn test_process_log_with_dimming_preserves_graph_lines() {+ let hash = "abc123456789012345678901234567890123456789";+ // Git graph can have continuation lines between commits+ let input = format!(+ "* \x01{}\x00abc1234 First\n\+ |\n\+ * \x01{}\x00def1234 Second",+ hash, "def123456789012345678901234567890123456789"+ );++ let unique = HashSet::from([hash.to_string()]);+ let (output, _hashes) = process_log_with_dimming(&input, Some(&unique));++ // Graph-only line should be preserved unchanged+ assert!(output.contains("\n|\n"), "graph line should be preserved");+ }++ #[test]+ fn test_process_log_with_dimming_sha256_compatible() {+ // SHA-256 hashes are 64 characters (not 40)+ let sha256_hash = "abc1234567890123456789012345678901234567890123456789012345678901";+ assert_eq!(sha256_hash.len(), 64);++ let input = format!("* \x01{}\x00abc1234 SHA-256 repo", sha256_hash);++ let unique = HashSet::from([sha256_hash.to_string()]);+ let (output, hashes) = process_log_with_dimming(&input, Some(&unique));++ assert_eq!(hashes[0], sha256_hash);+ let parsed = parse_dimming_output(&output);+ assert!(!parsed[0].0, "SHA-256 hash should be matched correctly");+ assert!(parsed[0].1.contains("SHA-256 repo"));+ }++ #[test]+ fn test_process_log_with_dimming_strips_ansi_when_dimming() {+ let hash = "abc123456789012345678901234567890123456789";+ // Simulate colored git output+ let input = format!(+ "* \x01{}\x00\x1b[33mabc1234\x1b[m\x1b[33m (HEAD)\x1b[m message",+ hash+ );++ // Use a different hash to trigger dimming+ let other_unique = HashSet::from(["other".to_string()]);+ let (output, _hashes) = process_log_with_dimming(&input, Some(&other_unique));++ // Dimmed output should have colors stripped+ let parsed = parse_dimming_output(&output);+ assert!(parsed[0].0, "should be dimmed");+ // The ansi_strip should have removed the color codes+ assert!(parsed[0].1.contains("abc1234"));+ assert!(parsed[0].1.contains("(HEAD)"));+ }++ // Tests for strip_hash_markers++ #[test]+ fn test_strip_hash_markers_removes_soh_nul_block() {+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let input = format!("* \x01{}\x00abc1234 message", full_hash);+ let output = strip_hash_markers(&input);++ assert!(!output.contains('\x01'));+ assert!(!output.contains('\x00'));+ assert_eq!(output, "* abc1234 message");+ }++ #[test]+ fn test_strip_hash_markers_preserves_other_content() {+ // No markers - content unchanged+ let input = "* abc1234 (HEAD -> main) Initial commit";+ let output = strip_hash_markers(input);+ assert_eq!(output, input);+ }++ #[test]+ fn test_strip_hash_markers_handles_multiple_markers() {+ let input = "line1 \x01hash1\x00 content1\nline2 \x01hash2\x00 content2";+ let output = strip_hash_markers(input);+ assert_eq!(output, "line1 content1\nline2 content2");+ }++ // Tests for extract_hash_from_line++ #[test]+ fn test_extract_hash_from_line_finds_hash() {+ let full_hash = "abc1234567890123456789012345678901234567ab";+ let line = format!("* \x01{}\x00abc1234 message", full_hash);+ let extracted = extract_hash_from_line(&line);+ assert_eq!(extracted, Some(full_hash));+ }++ #[test]+ fn test_extract_hash_from_line_sha256() {+ let sha256_hash = "abc1234567890123456789012345678901234567890123456789012345678901";+ let line = format!("* \x01{}\x00abc1234 message", sha256_hash);+ let extracted = extract_hash_from_line(&line);+ assert_eq!(extracted, Some(sha256_hash));+ }++ #[test]+ fn test_extract_hash_from_line_no_markers() {+ let line = "* abc1234 message";+ let extracted = extract_hash_from_line(line);+ assert_eq!(extracted, None);+ }++ #[test]+ fn test_extract_hash_from_line_incomplete_markers() {+ // Only SOH, no NUL+ let line = "* \x01abc1234 message";+ let extracted = extract_hash_from_line(line);+ assert_eq!(extracted, None);+ }}diff --git a/tests/snapshots/integration__integration_tests__select__select_preview_log.snap b/tests/snapshots/integration__integration_tests__select__select_preview_log.snapindex 74762f291..83438ff4d 100644--- a/tests/snapshots/integration__integration_tests__select__select_preview_log.snap+++ b/tests/snapshots/integration__integration_tests__select__select_preview_log.snap@@ -10,7 +10,7 @@ expression: normalizedβ* 17c13c5 +1 1d Add file 3 with contentβ* fad018c +1 1d Add file 2 with contentβ* 8df4920 +1 1d Add file 1 with content- β* a1e809f +3 1d Initial commit+ β* a1e809f +3 1d (HEAD -> main) Initial commitβββ