From 183f8414624eb37174344dc8849a56c619f0c9ae Mon Sep 17 00:00:00 2001 From: Benjamin Tan Date: Fri, 26 Apr 2024 21:56:55 +0800 Subject: [PATCH] backout: add `--template` option --- cli/src/commands/backout.rs | 27 ++++++++ cli/src/config/templates.toml | 2 + cli/tests/cli-reference@.md.snap | 1 + cli/tests/runner.rs | 1 + cli/tests/test_backout_command.rs | 102 ++++++++++++++++++++++++++++++ lib/src/rewrite.rs | 6 +- 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 cli/tests/test_backout_command.rs diff --git a/cli/src/commands/backout.rs b/cli/src/commands/backout.rs index 1538b848e5..a2cc011ac3 100644 --- a/cli/src/commands/backout.rs +++ b/cli/src/commands/backout.rs @@ -18,6 +18,8 @@ use tracing::instrument; use crate::cli_util::{CommandHelper, RevisionArg}; use crate::command_error::CommandError; +use crate::commit_templater::CommitTemplateLanguage; +use crate::formatter::PlainTextFormatter; use crate::ui::Ui; /// Apply the reverse of a revision on top of another revision @@ -31,6 +33,11 @@ pub(crate) struct BackoutArgs { // copy should be rebased on top? #[arg(long, short, default_value = "@")] destination: Vec, + /// Template used to generate the commit message + /// + /// For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md + #[arg(long, short = 'T')] + template: Option, } #[instrument(skip_all)] @@ -47,11 +54,31 @@ pub(crate) fn cmd_backout( parents.push(destination); } let mut tx = workspace_command.start_transaction(); + let commit_description = { + let language = tx.base_workspace_helper().commit_template_language()?; + let template_string = match &args.template { + Some(value) => value.to_string(), + None => command + .settings() + .config() + .get_string("templates.backout")?, + }; + let template = tx.base_workspace_helper().parse_template( + &language, + &template_string, + CommitTemplateLanguage::wrap_commit, + )?; + let mut output = Vec::new(); + let mut formatter = PlainTextFormatter::new(&mut output); + template.format(&commit_to_back_out, &mut formatter)?; + String::from_utf8(output).expect("template output should be utf-8 bytes") + }; back_out_commit( command.settings(), tx.mut_repo(), &commit_to_back_out, &parents, + Some(commit_description), )?; tx.finish( ui, diff --git a/cli/src/config/templates.toml b/cli/src/config/templates.toml index f188f43b2e..706895ca2a 100644 --- a/cli/src/config/templates.toml +++ b/cli/src/config/templates.toml @@ -1,4 +1,6 @@ [templates] +backout = '"backout of commit " ++ commit_id' + commit_summary = 'format_commit_summary_with_refs(self, branches)' commit_summary_no_branches = 'format_commit_summary_with_refs(self, "")' diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index 76bc688160..9f7ce7f550 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -217,6 +217,7 @@ Apply the reverse of a revision on top of another revision * `-d`, `--destination ` — The revision to apply the reverse changes on top of Default value: `@` +* `-T`, `--template