ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: add commit as its own command (not an alias for close)

It seems very likely that we're going to remove support for open
commits, but it's still useful to have a `commit` command that lets
the user enter a description and starts a new change. Calling it
`commit` seems good to make the transition from other VCSs simpler.
This commit is contained in:
Martin von Zweigbergk 2022-10-29 14:48:13 -07:00 committed by Martin von Zweigbergk
parent ea576a8327
commit f02d92a3fe
3 changed files with 134 additions and 1 deletions

View file

@ -13,6 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`committer(needle)`, `merges()` revsets. Use `x & description(needle)`
instead.
* `jj commit` is now a separate command from `jj close` (which is deprecated).
The behavior has changed slightly. It now always asks for a description, even
if there already was a description set. It now also only works on the
working-copy commit (there's no `-r` argument).
### New features
* The new `jj git remote rename` command allows git remotes to be renamed

View file

@ -78,6 +78,7 @@ enum Commands {
Obslog(ObslogArgs),
Interdiff(InterdiffArgs),
Describe(DescribeArgs),
Commit(CommitArgs),
Close(CloseArgs),
Open(OpenArgs),
Duplicate(DuplicateArgs),
@ -349,12 +350,21 @@ struct DescribeArgs {
stdin: bool,
}
/// Update the description and create a new change on top.
#[derive(clap::Args, Clone, Debug)]
#[command(hide = true)]
struct CommitArgs {
/// The change description to use (don't open editor)
#[arg(long, short)]
message: Option<String>,
}
/// Mark a revision closed
///
/// For information about open/closed revisions, see
/// https://github.com/martinvonz/jj/blob/main/docs/working-copy.md.
#[derive(clap::Args, Clone, Debug)]
#[command(visible_alias = "commit", hide = true)]
#[command(hide = true)]
struct CloseArgs {
/// The revision to close
#[arg(default_value = "@")]
@ -2438,6 +2448,45 @@ fn cmd_describe(
Ok(())
}
fn cmd_commit(ui: &mut Ui, command: &CommandHelper, args: &CommitArgs) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let commit_id = workspace_command
.repo()
.view()
.get_wc_commit_id(&workspace_command.workspace_id())
.ok_or_else(|| UserError("This command requires a working copy".to_string()))?;
let commit = workspace_command.repo().store().get_commit(commit_id)?;
let mut commit_builder =
CommitBuilder::for_rewrite_from(ui.settings(), &commit).set_open(false);
let description = if let Some(message) = &args.message {
message.to_string()
} else {
edit_description(ui, workspace_command.repo(), commit.description())?
};
commit_builder = commit_builder.set_description(description);
let mut tx = workspace_command.start_transaction(&format!("commit {}", commit.id().hex()));
let new_commit = commit_builder.write_to_repo(tx.mut_repo());
let workspace_ids = tx
.mut_repo()
.view()
.workspaces_for_wc_commit_id(commit.id());
if !workspace_ids.is_empty() {
let new_checkout = CommitBuilder::for_open_commit(
ui.settings(),
new_commit.id().clone(),
new_commit.tree_id().clone(),
)
.write_to_repo(tx.mut_repo());
for workspace_id in workspace_ids {
tx.mut_repo().edit(workspace_id, &new_checkout).unwrap();
}
}
workspace_command.finish_transaction(ui, tx)?;
Ok(())
}
fn cmd_open(ui: &mut Ui, command: &CommandHelper, args: &OpenArgs) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let commit = workspace_command.resolve_single_rev(&args.revision)?;
@ -4527,6 +4576,7 @@ pub fn run_command(
Commands::Interdiff(sub_args) => cmd_interdiff(ui, command_helper, sub_args),
Commands::Obslog(sub_args) => cmd_obslog(ui, command_helper, sub_args),
Commands::Describe(sub_args) => cmd_describe(ui, command_helper, sub_args),
Commands::Commit(sub_args) => cmd_commit(ui, command_helper, sub_args),
Commands::Close(sub_args) => cmd_close(ui, command_helper, sub_args),
Commands::Open(sub_args) => cmd_open(ui, command_helper, sub_args),
Commands::Duplicate(sub_args) => cmd_duplicate(ui, command_helper, sub_args),

View file

@ -0,0 +1,78 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::path::Path;
use crate::common::TestEnvironment;
pub mod common;
#[test]
fn test_commit_with_description_from_cli() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let workspace_path = test_env.env_root().join("repo");
// Description applies to the current working-copy (not the new one)
test_env.jj_cmd_success(&workspace_path, &["commit", "-m=first"]);
insta::assert_snapshot!(get_log_output(&test_env, &workspace_path), @r###"
@ 69e88fe3e63b (no description set)
o 85a1e2839620 first
o 000000000000 (no description set)
"###);
}
#[test]
fn test_commit_with_editor() {
let mut test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let workspace_path = test_env.env_root().join("repo");
// Check that the text file gets initialized with the current description and
// set a new one
test_env.jj_cmd_success(&workspace_path, &["describe", "-m=initial"]);
let edit_script = test_env.set_up_fake_editor();
std::fs::write(
&edit_script,
"expect
initial
JJ: Lines starting with \"JJ: \" (like this one) will be removed.
\0write
modified",
)
.unwrap();
test_env.jj_cmd_success(&workspace_path, &["commit"]);
insta::assert_snapshot!(get_log_output(&test_env, &workspace_path), @r###"
@ 3ea3453a773f (no description set)
o 792a60936c42 modified
o 000000000000 (no description set)
"###);
}
#[test]
fn test_commit_without_working_copy() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let workspace_path = test_env.env_root().join("repo");
test_env.jj_cmd_success(&workspace_path, &["workspace", "forget"]);
let stderr = test_env.jj_cmd_failure(&workspace_path, &["commit", "-m=first"]);
insta::assert_snapshot!(stderr, @r###"
Error: This command requires a working copy
"###);
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(cwd, &["log", "-T", r#"commit_id.short() " " description"#])
}