Skip to content

Commit

Permalink
cli: move join_message_paragraphs() to description_util
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Mar 6, 2024
1 parent 38d14ea commit 4dfded2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 23 deletions.
13 changes: 0 additions & 13 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2150,19 +2150,6 @@ pub struct EarlyArgs {
pub config_toml: Vec<String>,
}

/// Create a description from a list of paragraphs.
///
/// Based on the Git CLI behavior. See `opt_parse_m()` and `cleanup_mode` in
/// `git/builtin/commit.c`.
pub fn join_message_paragraphs(paragraphs: &[String]) -> String {
// Ensure each paragraph ends with a newline, then add another newline between
// paragraphs.
paragraphs
.iter()
.map(|p| text_util::complete_newline(p.as_str()))
.join("\n")
}

#[derive(Clone, Debug)]
pub struct RevisionArg(String);

Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
use jj_lib::object_id::ObjectId;
use tracing::instrument;

use crate::cli_util::{join_message_paragraphs, CommandHelper, RevisionArg};
use crate::cli_util::{CommandHelper, RevisionArg};
use crate::command_error::CommandError;
use crate::description_util::join_message_paragraphs;
use crate::ui::Ui;

/// Create a new, empty change and edit it in the working copy
Expand Down
6 changes: 4 additions & 2 deletions cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ use jj_lib::repo::Repo;
use jj_lib::rewrite::merge_commit_trees;
use tracing::instrument;

use crate::cli_util::{join_message_paragraphs, CommandHelper};
use crate::cli_util::CommandHelper;
use crate::command_error::{user_error, CommandError};
use crate::description_util::{description_template_for_commit, edit_description};
use crate::description_util::{
description_template_for_commit, edit_description, join_message_paragraphs,
};
use crate::ui::Ui;

/// Update the description and create a new change on top.
Expand Down
6 changes: 4 additions & 2 deletions cli/src/commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ use std::io::{self, Read, Write};
use jj_lib::object_id::ObjectId;
use tracing::instrument;

use crate::cli_util::{join_message_paragraphs, CommandHelper, RevisionArg};
use crate::cli_util::{CommandHelper, RevisionArg};
use crate::command_error::CommandError;
use crate::description_util::{description_template_for_describe, edit_description};
use crate::description_util::{
description_template_for_describe, edit_description, join_message_paragraphs,
};
use crate::ui::Ui;

/// Update the change description or other metadata
Expand Down
5 changes: 3 additions & 2 deletions cli/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tracing::instrument;

use crate::cli_util::{self, short_commit_hash, CommandHelper, RevisionArg};
use crate::command_error::{user_error, CommandError};
use crate::description_util::join_message_paragraphs;
use crate::ui::Ui;

/// Create a new, empty change and (by default) edit it in the working copy
Expand Down Expand Up @@ -141,7 +142,7 @@ Please use `jj new 'all:x|y'` instead of `jj new --allow-large-revsets x y`.",
new_commit = tx
.mut_repo()
.new_commit(command.settings(), new_parents_commit_id, merged_tree.id())
.set_description(cli_util::join_message_paragraphs(&args.message_paragraphs))
.set_description(join_message_paragraphs(&args.message_paragraphs))
.write()?;
num_rebased = target_ids.len();
for child_commit in target_commits {
Expand Down Expand Up @@ -173,7 +174,7 @@ Please use `jj new 'all:x|y'` instead of `jj new --allow-large-revsets x y`.",
new_commit = tx
.mut_repo()
.new_commit(command.settings(), target_ids.clone(), merged_tree.id())
.set_description(cli_util::join_message_paragraphs(&args.message_paragraphs))
.set_description(join_message_paragraphs(&args.message_paragraphs))
.write()?;
num_rebased = commits_to_rebase.len();
for child_commit in commits_to_rebase {
Expand Down
6 changes: 3 additions & 3 deletions cli/src/commands/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use jj_lib::object_id::ObjectId;
use jj_lib::revset;
use tracing::instrument;

use crate::cli_util::{self, CommandHelper, RevisionArg};
use crate::cli_util::{CommandHelper, RevisionArg};
use crate::command_error::{user_error, CommandError};
use crate::description_util::combine_messages;
use crate::description_util::{combine_messages, join_message_paragraphs};
use crate::ui::Ui;

/// Move changes from a revision into its parent
Expand Down Expand Up @@ -119,7 +119,7 @@ from the source will be moved into the parent.
// (always the case in the non-interactive case).
let abandon_child = &new_parent_tree_id == commit.tree_id();
let description = if !args.message_paragraphs.is_empty() {
cli_util::join_message_paragraphs(&args.message_paragraphs)
join_message_paragraphs(&args.message_paragraphs)
} else {
combine_messages(
tx.base_repo(),
Expand Down
13 changes: 13 additions & 0 deletions cli/src/description_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ pub fn combine_messages(
Ok(description)
}

/// Create a description from a list of paragraphs.
///
/// Based on the Git CLI behavior. See `opt_parse_m()` and `cleanup_mode` in
/// `git/builtin/commit.c`.
pub fn join_message_paragraphs(paragraphs: &[String]) -> String {
// Ensure each paragraph ends with a newline, then add another newline between
// paragraphs.
paragraphs
.iter()
.map(|p| text_util::complete_newline(p.as_str()))
.join("\n")
}

pub fn description_template_for_describe(
ui: &Ui,
settings: &UserSettings,
Expand Down

0 comments on commit 4dfded2

Please sign in to comment.