Skip to content

Commit

Permalink
description_utils: teach combine_messages() to handle more than two…
Browse files Browse the repository at this point in the history
… sources

I plan to teach `jj squash --from` to accept a revset as input.
  • Loading branch information
martinvonz committed Mar 13, 2024
1 parent 63b630d commit 86d79b9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
7 changes: 1 addition & 6 deletions cli/src/commands/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,7 @@ from the source will be moved into the destination.
Some(description) => description,
None => {
if abandon_source {
combine_messages(
tx.base_repo(),
&source,
&destination,
settings,
)?
combine_messages(tx.base_repo(), &[&source], &destination, settings)?
} else {
destination.description().to_owned()
}
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/unsquash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ aborted.
// case).
if new_parent_tree_id == parent_base_tree.id() {
tx.mut_repo().record_abandoned_commit(parent.id().clone());
let description =
combine_messages(tx.base_repo(), parent, &commit, command.settings())?;
let description = combine_messages(tx.base_repo(), &[parent], &commit, command.settings())?;
// Commit the new child on top of the parent's parents.
tx.mut_repo()
.rewrite_commit(command.settings(), &commit)
Expand Down
44 changes: 30 additions & 14 deletions cli/src/description_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,41 @@ JJ: Lines starting with "JJ: " (like this one) will be removed.
Ok(text_util::complete_newline(description.trim_matches('\n')))
}

/// Combines the descriptions from the input commits. If only one is non-empty,
/// then that one is used. Otherwise we concatenate the messages and ask the
/// user to edit the result in their editor.
pub fn combine_messages(
repo: &ReadonlyRepo,
source: &Commit,
sources: &[&Commit],
destination: &Commit,
settings: &UserSettings,
) -> Result<String, CommandError> {
let description = if source.description().is_empty() {
destination.description().to_string()
} else if destination.description().is_empty() {
source.description().to_string()
} else {
let combined = "JJ: Enter a description for the combined commit.\n".to_string()
+ "JJ: Description from the destination commit:\n"
+ destination.description()
+ "\nJJ: Description from the source commit:\n"
+ source.description();
edit_description(repo, &combined, settings)?
};
Ok(description)
let non_empty = sources
.iter()
.chain(std::iter::once(&destination))
.filter(|c| !c.description().is_empty())
.take(2)
.collect_vec();
match *non_empty.as_slice() {
[] => {
return Ok(String::new());
}
[commit] => {
return Ok(commit.description().to_owned());
}
_ => {}
}
// Produce a combined description with instructions for the user to edit.
// Include empty descriptins too, so the user doesn't have to wonder why they
// only see 2 descriptions when they combined 3 commits.
let mut combined = "JJ: Enter a description for the combined commit.".to_string();
combined.push_str("\nJJ: Description from the destination commit:\n");
combined.push_str(destination.description());
for commit in sources {
combined.push_str("\nJJ: Description from source commit:\n");
combined.push_str(commit.description());
}
edit_description(repo, &combined, settings)
}

/// Create a description from a list of paragraphs.
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_move_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ fn test_move_description() {
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
JJ: Description from source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_squash_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ fn test_squash_description() {
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
JJ: Description from source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_unsquash_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn test_unsquash_description() {
JJ: Description from the destination commit:
destination
JJ: Description from the source commit:
JJ: Description from source commit:
source
JJ: Lines starting with "JJ: " (like this one) will be removed.
Expand Down

0 comments on commit 86d79b9

Please sign in to comment.