|
| 1 | +use std::io::Write as _; |
| 2 | + |
| 3 | +use bstr::ByteVec as _; |
1 | 4 | use itertools::Itertools;
|
2 | 5 | use jj_lib::commit::Commit;
|
3 |
| -use jj_lib::matchers::EverythingMatcher; |
4 | 6 | use jj_lib::repo::ReadonlyRepo;
|
5 | 7 | use jj_lib::settings::UserSettings;
|
6 | 8 |
|
7 |
| -use crate::cli_util::{edit_temp_file, WorkspaceCommandHelper}; |
| 9 | +use crate::cli_util::{edit_temp_file, WorkspaceCommandTransaction}; |
8 | 10 | use crate::command_error::CommandError;
|
9 |
| -use crate::diff_util::DiffFormat; |
10 | 11 | use crate::formatter::PlainTextFormatter;
|
11 | 12 | use crate::text_util;
|
12 |
| -use crate::ui::Ui; |
13 | 13 |
|
14 | 14 | pub fn edit_description(
|
15 | 15 | repo: &ReadonlyRepo,
|
@@ -89,37 +89,30 @@ pub fn join_message_paragraphs(paragraphs: &[String]) -> String {
|
89 | 89 | .join("\n")
|
90 | 90 | }
|
91 | 91 |
|
| 92 | +/// Renders commit description template, which will be edited by user. |
92 | 93 | pub fn description_template(
|
93 |
| - ui: &Ui, |
94 |
| - workspace_command: &WorkspaceCommandHelper, |
| 94 | + tx: &WorkspaceCommandTransaction, |
95 | 95 | intro: &str,
|
96 | 96 | commit: &Commit,
|
97 | 97 | ) -> Result<String, CommandError> {
|
98 |
| - let mut diff_summary_bytes = Vec::new(); |
99 |
| - let diff_renderer = workspace_command.diff_renderer(vec![DiffFormat::Summary]); |
100 |
| - diff_renderer.show_patch( |
101 |
| - ui, |
102 |
| - &mut PlainTextFormatter::new(&mut diff_summary_bytes), |
103 |
| - commit, |
104 |
| - &EverythingMatcher, |
105 |
| - )?; |
106 |
| - let mut template_chunks = Vec::new(); |
| 98 | + // TODO: Should "ui.default-description" be deprecated? |
| 99 | + // We might want default description templates per command instead. For |
| 100 | + // example, "backout_description" template will be rendered against the |
| 101 | + // commit to be backed out, and the generated description could be set |
| 102 | + // without spawning editor. |
| 103 | + |
| 104 | + // Named as "draft" because the output can contain "JJ: " comment lines. |
| 105 | + let template_key = "templates.draft_commit_description"; |
| 106 | + let template_text = tx.settings().config().get_string(template_key)?; |
| 107 | + let template = tx.parse_commit_template(&template_text)?; |
| 108 | + |
| 109 | + let mut output = Vec::new(); |
107 | 110 | if !intro.is_empty() {
|
108 |
| - template_chunks.push(format!("JJ: {intro}\n")); |
| 111 | + writeln!(output, "JJ: {intro}").unwrap(); |
109 | 112 | }
|
110 |
| - template_chunks.push(commit.description().to_owned()); |
111 |
| - if !diff_summary_bytes.is_empty() { |
112 |
| - template_chunks.push("\n".to_owned()); |
113 |
| - template_chunks.push(diff_summary_to_description(&diff_summary_bytes)); |
114 |
| - } |
115 |
| - Ok(template_chunks.concat()) |
116 |
| -} |
117 |
| - |
118 |
| -pub fn diff_summary_to_description(bytes: &[u8]) -> String { |
119 |
| - let text = std::str::from_utf8(bytes).expect( |
120 |
| - "Summary diffs and repo paths must always be valid UTF8.", |
121 |
| - // Double-check this assumption for diffs that include file content. |
122 |
| - ); |
123 |
| - "JJ: This commit contains the following changes:\n".to_owned() |
124 |
| - + &textwrap::indent(text, "JJ: ") |
| 113 | + template |
| 114 | + .format(commit, &mut PlainTextFormatter::new(&mut output)) |
| 115 | + .expect("write() to vec backed formatter should never fail"); |
| 116 | + // Template output is usually UTF-8, but it can contain file content. |
| 117 | + Ok(output.into_string_lossy()) |
125 | 118 | }
|
0 commit comments