Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: preparation for commit description template #4145

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ new working-copy commit.
)?;
}

let mut commit_builder = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.detach();
commit_builder.set_tree_id(tree_id);
if args.reset_author {
commit_builder.set_author(commit_builder.committer().clone());
}

let template = description_template_for_commit(
ui,
command.settings(),
Expand All @@ -113,17 +122,9 @@ new working-copy commit.
} else {
edit_description(tx.base_repo(), &template, command.settings())?
};
commit_builder.set_description(description);
let new_commit = commit_builder.write(tx.mut_repo())?;

let mut commit_builder = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.set_tree_id(tree_id)
.set_description(description);
if args.reset_author {
let new_author = commit_builder.committer().clone();
commit_builder = commit_builder.set_author(new_author);
}
let new_commit = commit_builder.write()?;
let workspace_ids = tx
.mut_repo()
.view()
Expand Down
43 changes: 24 additions & 19 deletions cli/src/commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::{self, Read, Write};
use std::io::{self, Read};

use jj_lib::object_id::ObjectId;
use tracing::instrument;
Expand Down Expand Up @@ -69,33 +69,38 @@ pub(crate) fn cmd_describe(
let mut workspace_command = command.workspace_helper(ui)?;
let commit = workspace_command.resolve_single_rev(&args.revision)?;
workspace_command.check_rewritable([commit.id()])?;

let mut tx = workspace_command.start_transaction();
let mut commit_builder = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.detach();
if args.reset_author {
commit_builder.set_author(commit_builder.committer().clone());
}

let description = if args.stdin {
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer).unwrap();
io::stdin().read_to_string(&mut buffer)?;
buffer
} else if !args.message_paragraphs.is_empty() {
join_message_paragraphs(&args.message_paragraphs)
} else if args.no_edit {
commit.description().to_owned()
} else {
let template =
description_template_for_describe(ui, command.settings(), &workspace_command, &commit)?;
edit_description(workspace_command.repo(), &template, command.settings())?
let template = description_template_for_describe(
ui,
command.settings(),
tx.base_workspace_helper(),
&commit,
)?;
edit_description(tx.base_repo(), &template, command.settings())?
};
if description == *commit.description() && !args.reset_author {
writeln!(ui.status(), "Nothing changed.")?;
} else {
let mut tx = workspace_command.start_transaction();
let mut commit_builder = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.set_description(description);
if args.reset_author {
let new_author = commit_builder.committer().clone();
commit_builder = commit_builder.set_author(new_author);
}
commit_builder.write()?;
tx.finish(ui, format!("describe commit {}", commit.id().hex()))?;
commit_builder.set_description(description);

if commit_builder.description() != commit.description() || args.reset_author {
commit_builder.write(tx.mut_repo())?;
}
tx.finish(ui, format!("describe commit {}", commit.id().hex()))?;
Ok(())
}
110 changes: 59 additions & 51 deletions cli/src/commands/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,63 +122,71 @@ the operation will be aborted.

// Create the first commit, which includes the changes selected by the user.
let selected_tree = tx.repo().store().get_root_tree(&selected_tree_id)?;
let first_template = description_template_for_commit(
ui,
command.settings(),
tx.base_workspace_helper(),
"Enter a description for the first commit.",
commit.description(),
&base_tree,
&selected_tree,
)?;
let first_description = edit_description(tx.base_repo(), &first_template, command.settings())?;
let first_commit = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.set_tree_id(selected_tree_id)
.set_description(first_description)
.write()?;

// Create the second commit, which includes everything the user didn't
// select.
let (second_tree, second_base_tree) = if args.parallel {
// Merge the original commit tree with its parent using the tree
// containing the user selected changes as the base for the merge.
// This results in a tree with the changes the user didn't select.
(end_tree.merge(&selected_tree, &base_tree)?, &base_tree)
} else {
(end_tree, &selected_tree)
};
let second_commit_parents = if args.parallel {
commit.parent_ids().to_vec()
} else {
vec![first_commit.id().clone()]
};
let second_description = if commit.description().is_empty() {
// If there was no description before, don't ask for one for the second commit.
"".to_string()
} else {
let second_template = description_template_for_commit(
let first_commit = {
let mut commit_builder = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.detach();
commit_builder.set_tree_id(selected_tree_id);
let template = description_template_for_commit(
ui,
command.settings(),
tx.base_workspace_helper(),
"Enter a description for the second commit.",
"Enter a description for the first commit.",
commit.description(),
second_base_tree,
&second_tree,
&base_tree,
&selected_tree,
)?;
edit_description(tx.base_repo(), &second_template, command.settings())?
let description = edit_description(tx.base_repo(), &template, command.settings())?;
commit_builder.set_description(description);
commit_builder.write(tx.mut_repo())?
};

// Create the second commit, which includes everything the user didn't
// select.
let second_commit = {
let (new_tree, base_tree) = if args.parallel {
// Merge the original commit tree with its parent using the tree
// containing the user selected changes as the base for the merge.
// This results in a tree with the changes the user didn't select.
(end_tree.merge(&selected_tree, &base_tree)?, &base_tree)
} else {
(end_tree, &selected_tree)
};
let parents = if args.parallel {
commit.parent_ids().to_vec()
} else {
vec![first_commit.id().clone()]
};
let mut commit_builder = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.detach();
commit_builder
.set_parents(parents)
.set_tree_id(new_tree.id())
// Generate a new change id so that the commit being split doesn't
// become divergent.
.generate_new_change_id();
let description = if commit.description().is_empty() {
// If there was no description before, don't ask for one for the
// second commit.
"".to_string()
} else {
let template = description_template_for_commit(
ui,
command.settings(),
tx.base_workspace_helper(),
"Enter a description for the second commit.",
commit.description(),
base_tree,
&new_tree,
)?;
edit_description(tx.base_repo(), &template, command.settings())?
};
commit_builder.set_description(description);
commit_builder.write(tx.mut_repo())?
};
let second_commit = tx
.mut_repo()
.rewrite_commit(command.settings(), &commit)
.set_parents(second_commit_parents)
.set_tree_id(second_tree.id())
// Generate a new change id so that the commit being split doesn't
// become divergent.
.generate_new_change_id()
.set_description(second_description)
.write()?;

// Mark the commit being split as rewritten to the second commit. As a
// result, if @ points to the commit being split, it will point to the
Expand Down
19 changes: 9 additions & 10 deletions cli/tests/test_commit_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,17 @@ fn test_commit_with_default_description() {
○ 573b6df51aea TESTED=TODO
◆ 000000000000
"###);
assert_eq!(
std::fs::read_to_string(test_env.env_root().join("editor")).unwrap(),
r#"
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor")).unwrap(), @r###"

TESTED=TODO
JJ: This commit contains the following changes:
JJ: A file1
JJ: A file2

JJ: Lines starting with "JJ: " (like this one) will be removed.
"#
);
TESTED=TODO
JJ: This commit contains the following changes:
JJ: A file1
JJ: A file2

JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
}

#[test]
Expand Down
19 changes: 9 additions & 10 deletions cli/tests/test_describe_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,17 @@ fn test_describe_default_description() {
Working copy now at: qpvuntsm 573b6df5 TESTED=TODO
Parent commit : zzzzzzzz 00000000 (empty) (no description set)
"###);
assert_eq!(
std::fs::read_to_string(test_env.env_root().join("editor")).unwrap(),
r#"
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor")).unwrap(), @r###"

TESTED=TODO
JJ: This commit contains the following changes:
JJ: A file1
JJ: A file2

JJ: Lines starting with "JJ: " (like this one) will be removed.
"#
);
TESTED=TODO
JJ: This commit contains the following changes:
JJ: A file1
JJ: A file2

JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
}

#[test]
Expand Down
Loading