diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b62d6d..11e2875 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - The `auto` color mode respects the `NO_COLOR` environment variable (see https://no-color.org/). +- `--svg` uses `--format` for the commit text when it is given. Multi-line formats are joined into one line. ### Changed diff --git a/src/main.rs b/src/main.rs index a25c492..99a00e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,6 +62,7 @@ fn from_args() -> Result<(), String> { ses.repository.unwrap(), Rc::new(ses.settings.unwrap()), ses.svg, + ses.svg_use_format, ses.commit_limit, ses.refspecs, ) @@ -108,7 +109,11 @@ fn build_cli() -> Command { .arg( Arg::new("svg") .long("svg") - .help("Render graph as SVG instead of text-based.") + .help( + "Render graph as SVG instead of text-based.\n\ + Commit text follows --format if given, otherwise the summary.\n\ + Multi-line formats are joined into one line.", + ) .required(false) .num_args(0), ) @@ -179,6 +184,8 @@ fn configure_session(ses: &mut Session, matches: &ArgMatches) -> Result, pub svg: bool, + pub svg_use_format: bool, pub commit_limit: Option, pub refspecs: Vec, } @@ -249,6 +257,7 @@ impl Session { // Other fields repository: None, svg: false, + svg_use_format: false, commit_limit: None, refspecs: Vec::new(), } @@ -636,6 +645,7 @@ fn run( repository: Repository, settings: Rc, svg: bool, + svg_use_format: bool, max_commits: Option, refspecs: Vec, ) -> Result<(), String> { @@ -684,7 +694,7 @@ fn run( let stdout = io::stdout(); let mut out = stdout.lock(); if svg { - let svg_lines = print_svg(&graph, settings)?; + let svg_lines = print_svg(&graph, settings, svg_use_format)?; io_err_to_string(writeln!(out, "{}", svg_lines))?; } else { let (g_lines, t_lines, _indices) = print_unicode(&graph, settings)?; diff --git a/src/print/svg.rs b/src/print/svg.rs index 0cd4556..db7b29f 100644 --- a/src/print/svg.rs +++ b/src/print/svg.rs @@ -5,13 +5,21 @@ use itertools::enumerate; use gleisbau::graph::CommitInfo; use gleisbau::graph::GitGraph; use gleisbau::layout::get_deviate_index; +use gleisbau::print::format::format_commit_metadata; use gleisbau::settings::Settings; use svg::node::element::path::Data; use svg::node::element::{Circle, Group, Line, Path, Text, Title}; use svg::Document; /// Creates a SVG visual representation of a graph. -pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result { +/// +/// With `use_format`, the text next to each commit follows `settings.format`, +/// joined into one line. Otherwise it is the commit summary. +pub fn print_svg( + graph: &GitGraph, + settings: &Settings, + use_format: bool, +) -> Result { let tracks = &graph.tracks; let layout = &graph.layout; let mut document = Document::new(); @@ -58,10 +66,23 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result>() + .join(" ") + } else { + commit + .summary() + .map_err(|err| err.message().to_string())? + .unwrap_or("") + .to_string() + }; + let commit_summary = commit_summary.as_str(); document = document.add(draw_summary(row(idx), max_column, commit_summary)); @@ -372,12 +393,20 @@ mod tests { BranchOrder, BranchSettings, BranchSettingsDef, Characters, MergePatterns, Settings, }; use std::rc::Rc; + use std::sync::atomic::{AtomicUsize, Ordering}; /// Renders a repository with the commits "first" and then "second". fn render(rotate: bool) -> String { + render_with(rotate, CommitFormat::OneLine, false) + } + + fn render_with(rotate: bool, format: CommitFormat, use_format: bool) -> String { + // Tests run in parallel, so every repository gets its own folder + static RUNS: AtomicUsize = AtomicUsize::new(0); let dir = std::env::temp_dir().join(format!( - "git-graph-svg-test-{}-{rotate}", - std::process::id() + "git-graph-svg-test-{}-{}", + std::process::id(), + RUNS.fetch_add(1, Ordering::Relaxed) )); let repo = Repository::init(&dir).unwrap(); let signature = Signature::now("Test", "test@example.com").unwrap(); @@ -405,7 +434,7 @@ mod tests { compact: true, colored: false, include_remote: false, - format: CommitFormat::OneLine, + format, wrapping: None, characters: Characters::thin(), branch_order: BranchOrder::ShortestFirst(true), @@ -417,7 +446,7 @@ mod tests { .with_settings(settings.clone()) .build() .unwrap(); - let svg = print_svg(&graph, &settings).unwrap(); + let svg = print_svg(&graph, &settings, use_format).unwrap(); std::fs::remove_dir_all(&dir).unwrap(); svg } @@ -445,4 +474,21 @@ mod tests { // Commit dots and lines are mirrored around the same rows as the text. assert!(svg.contains("transform=\"matrix(1 0 0 -1 0 45)\"")); } + + #[test] + fn svg_uses_format_when_asked() { + let format = || CommitFormat::Format("%s by %an".to_string()); + let svg = render_with(false, format(), true); + assert!(svg.contains(">\nsecond by Test")); + // Without an explicit --format the summary is shown as before. + let svg = render_with(false, format(), false); + assert!(svg.contains(">\nsecond\n")); + assert!(!svg.contains("by Test")); + } + + #[test] + fn svg_joins_multi_line_format_into_one_line() { + let svg = render_with(false, CommitFormat::Short, true); + assert!(svg.contains("Author: Test <test@example.com> second")); + } }