Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templater: add commit.diff().<format>() methods #4099

Merged
merged 3 commits into from
Jul 17, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* In git diffs, word-level hunks are now highlighted with underline. See [diff
colors and styles](docs/config.md#diff-colors-and-styles) for customization.

* New `.diff().<format>()` commit template methods are added. They can be used
in order to show diffs conditionally. For example,
`if(current_working_copy, diff.summary())`.

* `jj git clone` and `jj git init` with an existing git repository adds the
default branch of the remote as repository settings for
`revset-aliases."trunk()"`.`
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ name = "runner"
cargo_metadata = { workspace = true }

[dependencies]
bstr = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true }
clap-markdown = { workspace = true }
Expand Down
12 changes: 9 additions & 3 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::sync::Arc;
use std::time::SystemTime;
use std::{fs, str};

use bstr::ByteVec as _;
use clap::builder::{
MapValueParser, NonEmptyStringValueParser, TypedValueParser, ValueParserFactory,
};
Expand Down Expand Up @@ -1063,6 +1064,7 @@ impl WorkspaceCommandHelper {
pub fn commit_template_language(&self) -> Result<CommitTemplateLanguage<'_>, CommandError> {
Ok(CommitTemplateLanguage::new(
self.repo().as_ref(),
&self.path_converter,
self.workspace_id(),
self.revset_parse_context(),
self.id_prefix_context()?,
Expand All @@ -1084,7 +1086,8 @@ impl WorkspaceCommandHelper {
let mut output = Vec::new();
self.write_commit_summary(&mut PlainTextFormatter::new(&mut output), commit)
.expect("write() to PlainTextFormatter should never fail");
String::from_utf8(output).expect("template output should be utf-8 bytes")
// Template output is usually UTF-8, but it can contain file content.
output.into_string_lossy()
}

/// Writes one-line summary of the given `commit`.
Expand Down Expand Up @@ -1637,7 +1640,8 @@ impl WorkspaceCommandTransaction<'_> {
let mut output = Vec::new();
self.write_commit_summary(&mut PlainTextFormatter::new(&mut output), commit)
.expect("write() to PlainTextFormatter should never fail");
String::from_utf8(output).expect("template output should be utf-8 bytes")
// Template output is usually UTF-8, but it can contain file content.
output.into_string_lossy()
}

pub fn write_commit_summary(
Expand All @@ -1651,6 +1655,7 @@ impl WorkspaceCommandTransaction<'_> {
.expect("parse error should be confined by WorkspaceCommandHelper::new()");
let language = CommitTemplateLanguage::new(
self.tx.repo(),
&self.helper.path_converter,
self.helper.workspace_id(),
self.helper.revset_parse_context(),
&id_prefix_context,
Expand Down Expand Up @@ -2667,7 +2672,8 @@ pub fn format_template<C: Clone>(ui: &Ui, arg: &C, template: &TemplateRenderer<C
template
.format(arg, ui.new_formatter(&mut output).as_mut())
.expect("write() to vec backed formatter should never fail");
String::from_utf8(output).expect("template output should be utf-8 bytes")
// Template output is usually UTF-8, but it can contain file content.
output.into_string_lossy()
}

/// CLI command builder and runner.
Expand Down
Loading