Skip to content

Commit

Permalink
cli: merge op heads and snapshot working copy by "op log" by default
Browse files Browse the repository at this point in the history
This partially reverts 543036c "cli: run 'op log' without loading repo or
merging concurrent ops." User can now get around the issue by --at-op=@
--ignore-working-copy.
  • Loading branch information
yuja committed Jul 26, 2024
1 parent 5e4c262 commit 29450a9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
6 changes: 6 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ impl CommandHelper {
.map_err(|err| map_workspace_load_error(err, self.global_args.repository.as_deref()))
}

/// Returns true if the working copy to be loaded is writable, and therefore
/// should usually be snapshotted.
pub fn is_working_copy_writable(&self) -> bool {
self.is_at_head_operation() && !self.global_args.ignore_working_copy
}

/// Returns true if the current operation is considered to be the head.
pub fn is_at_head_operation(&self) -> bool {
// TODO: should we accept --at-op=<head_id> as the head op?
Expand Down
42 changes: 15 additions & 27 deletions cli/src/commands/operation/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::slice;

use jj_lib::op_walk;

use crate::cli_util::{format_template, CommandHelper, LogContentFormat};
Expand Down Expand Up @@ -49,40 +51,26 @@ pub fn cmd_op_log(
command: &CommandHelper,
args: &OperationLogArgs,
) -> Result<(), CommandError> {
// Don't load the repo so that the operation history can be inspected even
// with a corrupted repo state. For example, you can find the first bad
// operation id to be abandoned.
let workspace = command.load_workspace()?;
let repo_loader = workspace.repo_loader();
let head_op_str = command.global_args().at_operation.as_deref().unwrap_or("@");
let head_ops = if head_op_str == "@" {
// If multiple head ops can't be resolved without merging, let the
// current op be empty. Beware that resolve_op_for_load() will eliminate
// redundant heads whereas get_current_head_ops() won't.
let current_op = op_walk::resolve_op_for_load(repo_loader, head_op_str).ok();
if let Some(op) = current_op {
vec![op]
} else {
op_walk::get_current_head_ops(
repo_loader.op_store(),
repo_loader.op_heads_store().as_ref(),
)?
}
let current_op = if command.is_working_copy_writable() {
let workspace_command = command.workspace_helper(ui)?;
workspace_command.repo().operation().clone()
} else {
vec![op_walk::resolve_op_for_load(repo_loader, head_op_str)?]
};
let current_op_id = match &*head_ops {
[op] => Some(op.id()),
_ => None,
// Don't load the repo so that the operation history can be inspected
// even with a corrupted repo state. For example, you can find the first
// bad operation id to be abandoned.
let workspace = command.load_workspace()?;
command.resolve_operation(ui, workspace.repo_loader())?
};
let op_store = current_op.op_store();

let with_content_format = LogContentFormat::new(ui, command.settings())?;

let template;
let op_node_template;
{
let language = OperationTemplateLanguage::new(
repo_loader.op_store().root_operation_id(),
current_op_id,
op_store.root_operation_id(),
Some(current_op.id()),
command.operation_template_extensions(),
);
let text = match &args.template {
Expand Down Expand Up @@ -117,7 +105,7 @@ pub fn cmd_op_log(
)?;
}
let limit = args.limit.or(args.deprecated_limit).unwrap_or(usize::MAX);
let iter = op_walk::walk_ancestors(&head_ops).take(limit);
let iter = op_walk::walk_ancestors(slice::from_ref(&current_op)).take(limit);
if !args.no_graph {
let mut graph = get_graphlog(command.settings(), formatter.raw());
for op in iter {
Expand Down
13 changes: 5 additions & 8 deletions cli/tests/test_concurrent_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@ fn test_concurrent_operation_divergence() {
&["describe", "-m", "message 2", "--at-op", "@-"],
);

// "--at-op=@" disables op heads merging
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "--at-op=@"]);
// "--at-op=@" disables op heads merging, and prints head operation ids.
let stderr = test_env.jj_cmd_failure(&repo_path, &["op", "log", "--at-op=@"]);
insta::assert_snapshot!(stderr, @r###"
Error: The "@" expression resolved to more than one operation
Hint: Try specifying one of the operations by ID: e31015019d90, 48f4a48f3f70
"###);

// "op log" doesn't merge the concurrent operations
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]);
// "op log --at-op" should work without merging the head operations
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log", "--at-op=48f4a48f3f70"]);
insta::assert_snapshot!(stdout, @r###"
48f4a48f3f70 [email protected] 2001-02-03 04:05:09.000 +07:00 - 2001-02-03 04:05:09.000 +07:00
@ 48f4a48f3f70 [email protected] 2001-02-03 04:05:09.000 +07:00 - 2001-02-03 04:05:09.000 +07:00
│ describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
│ args: jj describe -m 'message 2' --at-op @-
│ ○ e31015019d90 [email protected] 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
├─╯ describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
│ args: jj describe -m 'message 1'
○ b51416386f26 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
│ add workspace 'default'
○ 9a7d829846af [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ fn test_op_recover_from_bad_gc() {
"###);

// "op log" should still be usable.
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["op", "log"]);
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["op", "log", "--ignore-working-copy"]);
insta::assert_snapshot!(stdout, @r###"
@ 43d51d9b0c0c [email protected] 2001-02-03 04:05:12.000 +07:00 - 2001-02-03 04:05:12.000 +07:00
│ describe commit 37bb762e5dc08073ec4323bdffc023a0f0cc901e
Expand Down

0 comments on commit 29450a9

Please sign in to comment.