Skip to content

Commit

Permalink
Expose diff format options to describe-like commands
Browse files Browse the repository at this point in the history
This implements some more powerful version of `git commit --verbose`,
which lets the user get more context in the editor buffer they use to
write a commit message.

We default to `DiffFormat::Summary`, which was the previously hardcoded
value.
  • Loading branch information
cbarrete committed Apr 7, 2024
1 parent feaaa48 commit 900a192
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::command_error::{user_error, CommandError};
use crate::description_util::{
description_template_for_commit, edit_description, join_message_paragraphs,
};
use crate::diff_util::DiffFormatArgs;
use crate::ui::Ui;

/// Update the description and create a new change on top.
Expand All @@ -40,6 +41,8 @@ pub(crate) struct CommitArgs {
/// Put these paths in the first commit
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
#[command(flatten)]
diff_format: DiffFormatArgs,
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -94,6 +97,7 @@ new working-copy commit.
commit.description(),
&base_tree,
&middle_tree,
&args.diff_format,
)?;

let description = if !args.message_paragraphs.is_empty() {
Expand Down
12 changes: 10 additions & 2 deletions cli/src/commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::command_error::CommandError;
use crate::description_util::{
description_template_for_describe, edit_description, join_message_paragraphs,
};
use crate::diff_util::DiffFormatArgs;
use crate::ui::Ui;

/// Update the change description or other metadata
Expand Down Expand Up @@ -58,6 +59,8 @@ pub(crate) struct DescribeArgs {
/// $ JJ_USER='Foo Bar' [email protected] jj describe --reset-author
#[arg(long)]
reset_author: bool,
#[command(flatten)]
diff_format: DiffFormatArgs,
}

#[instrument(skip_all)]
Expand All @@ -78,8 +81,13 @@ pub(crate) fn cmd_describe(
} else if args.no_edit {
commit.description().to_owned()
} else {
let template =
description_template_for_describe(ui, command.settings(), &workspace_command, &commit)?;
let template = description_template_for_describe(
ui,
command.settings(),
&workspace_command,
&commit,
&args.diff_format,
)?;
edit_description(workspace_command.repo(), &template, command.settings())?
};
if description == *commit.description() && !args.reset_author {
Expand Down
5 changes: 5 additions & 0 deletions cli/src/commands/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::cli_util::{CommandHelper, RevisionArg};
use crate::command_error::CommandError;
use crate::commands::rebase::rebase_descendants;
use crate::description_util::{description_template_for_commit, edit_description};
use crate::diff_util::DiffFormatArgs;
use crate::ui::Ui;

/// Split a revision in two
Expand Down Expand Up @@ -57,6 +58,8 @@ pub(crate) struct SplitArgs {
/// Put these paths in the first commit
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
#[command(flatten)]
diff_format: DiffFormatArgs,
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -119,6 +122,7 @@ the operation will be aborted.
commit.description(),
&base_tree,
&selected_tree,
&args.diff_format,
)?;
let first_description = edit_description(tx.base_repo(), &first_template, command.settings())?;
let first_commit = tx
Expand Down Expand Up @@ -155,6 +159,7 @@ the operation will be aborted.
commit.description(),
second_base_tree,
&second_tree,
&args.diff_format,
)?;
edit_description(tx.base_repo(), &second_template, command.settings())?
};
Expand Down
9 changes: 6 additions & 3 deletions cli/src/description_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use jj_lib::settings::UserSettings;

use crate::cli_util::{edit_temp_file, WorkspaceCommandHelper};
use crate::command_error::CommandError;
use crate::diff_util::{self, DiffFormat};
use crate::diff_util::{self, diff_formats_for_describe, DiffFormatArgs};
use crate::formatter::PlainTextFormatter;
use crate::text_util;
use crate::ui::Ui;
Expand Down Expand Up @@ -95,6 +95,7 @@ pub fn description_template_for_describe(
settings: &UserSettings,
workspace_command: &WorkspaceCommandHelper,
commit: &Commit,
diff_format: &DiffFormatArgs,
) -> Result<String, CommandError> {
let mut diff_summary_bytes = Vec::new();
diff_util::show_patch(
Expand All @@ -103,7 +104,7 @@ pub fn description_template_for_describe(
workspace_command,
commit,
&EverythingMatcher,
&[DiffFormat::Summary],
&diff_formats_for_describe(settings, diff_format)?,
)?;
let description = if commit.description().is_empty() {
settings.default_description()
Expand All @@ -117,6 +118,7 @@ pub fn description_template_for_describe(
}
}

#[allow(clippy::too_many_arguments)]
pub fn description_template_for_commit(
ui: &Ui,
settings: &UserSettings,
Expand All @@ -125,6 +127,7 @@ pub fn description_template_for_commit(
overall_commit_description: &str,
from_tree: &MergedTree,
to_tree: &MergedTree,
diff_format: &DiffFormatArgs,
) -> Result<String, CommandError> {
let mut diff_summary_bytes = Vec::new();
diff_util::show_diff(
Expand All @@ -134,7 +137,7 @@ pub fn description_template_for_commit(
from_tree,
to_tree,
&EverythingMatcher,
&[DiffFormat::Summary],
&diff_formats_for_describe(settings, diff_format)?,
)?;
let mut template_chunks = Vec::new();
if !intro.is_empty() {
Expand Down
13 changes: 13 additions & 0 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ pub fn diff_formats_for_log(
Ok(formats)
}

/// Returns a list of requested diff formats for describe-like commands, which
/// will never be empty.
pub fn diff_formats_for_describe(
settings: &UserSettings,
args: &DiffFormatArgs,
) -> Result<Vec<DiffFormat>, config::ConfigError> {
let mut formats = diff_formats_from_args(settings, args)?;
if formats.is_empty() {
formats.push(DiffFormat::Summary);
}
Ok(formats)
}

fn diff_formats_from_args(
settings: &UserSettings,
args: &DiffFormatArgs,
Expand Down

0 comments on commit 900a192

Please sign in to comment.