Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 12 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -179,6 +184,8 @@ fn configure_session(ses: &mut Session, matches: &ArgMatches) -> Result<bool, St
let reverse_commit_order = matches.get_flag("rotate");

ses.svg = matches.get_flag("svg");
// Without an explicit --format, SVG output keeps showing just the summary
ses.svg_use_format = matches.contains_id("format");
let compact = !matches.get_flag("sparse");
let debug = matches.get_flag("debug");
let style = matches
Expand Down Expand Up @@ -233,6 +240,7 @@ struct Session {
// Other fields
pub repository: Option<Repository>,
pub svg: bool,
pub svg_use_format: bool,
pub commit_limit: Option<usize>,
pub refspecs: Vec<String>,
}
Expand All @@ -249,6 +257,7 @@ impl Session {
// Other fields
repository: None,
svg: false,
svg_use_format: false,
commit_limit: None,
refspecs: Vec::new(),
}
Expand Down Expand Up @@ -636,6 +645,7 @@ fn run(
repository: Repository,
settings: Rc<Settings>,
svg: bool,
svg_use_format: bool,
max_commits: Option<usize>,
refspecs: Vec<String>,
) -> Result<(), String> {
Expand Down Expand Up @@ -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)?;
Expand Down
64 changes: 55 additions & 9 deletions src/print/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
///
/// 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<String, String> {
let tracks = &graph.tracks;
let layout = &graph.layout;
let mut document = Document::new();
Expand Down Expand Up @@ -58,10 +66,23 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
document = document.add(commit_group);

let commit = graph.repository.find_commit(info.oid).unwrap();
let commit_summary = commit
.summary()
.map_err(|err| err.message().to_string())?
.unwrap_or("");
let commit_summary = if use_format {
// Each commit gets one row, so a multi-line format is joined into one line.
// Branch names are left out because the SVG already draws them as labels.
format_commit_metadata(&commit, String::new(), &None, None, &settings.format)?
.iter()
.map(|line| line.trim())
.filter(|line| !line.is_empty())
.collect::<Vec<_>>()
.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));

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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),
Expand All @@ -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
}
Expand Down Expand Up @@ -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 &lt;test@example.com&gt; second"));
}
}
Loading