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

cli: teach obslog an option to show diff

This patch adds `jj obslog -p` for including the diff compared to the
predecessor (the first predecessor if there are several). If the
predecessor's parents are different, then we create a temporary tree
by rebasing the predecessor to have the same parents and we use the
result as base for the diff. That way, we avoid polluting the diff
with the changes caused by the rebase. (I don't think we currently
have any commands that can change both parents and content, so the
diff should always be empty for rewrites caused by a rebase.)

Working on this also reminded me that it'll be really nice when we
replace `jj obslog` by something based on the operation log - I really
miss seeing information about the operation in the output (like `hg
obslog` gets from its obsmarkers).
This commit is contained in:
Martin von Zweigbergk 2022-06-03 15:25:56 -07:00 committed by Martin von Zweigbergk
parent 02346de60e
commit 051c01491c
3 changed files with 230 additions and 1 deletions

View file

@ -106,6 +106,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj log` now accepts a `--reversed` option, which will show older commits
first.
* `jj obslog` now accepts `-p`/`--patch` option, which will show the diff
compared to the previous version of the change.
* The "(no name/email configured)" placeholder value for name/email will now be
replaced if once you modify a commit after having configured your name/email.

View file

@ -1322,6 +1322,15 @@ struct ObslogArgs {
/// documented and is likely to change)
#[clap(long, short = 'T')]
template: Option<String>,
/// Show patch compared to the previous version of this change
///
/// If the previous version has different parents, it will be temporarily
/// rebased to the parents of the new version, so the diff is not
/// contaminated by unrelated changes.
#[clap(long, short = 'p')]
patch: bool,
#[clap(flatten)]
diff_format: DiffFormatArgs,
}
/// Edit the change description
@ -3119,6 +3128,9 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result
let workspace_id = workspace_command.workspace_id();
let checkout_id = workspace_command.repo().view().get_checkout(&workspace_id);
let diff_format = (args.patch || args.diff_format.git || args.diff_format.summary)
.then(|| diff_format_for(ui, &args.diff_format));
let template_string = match &args.template {
Some(value) => value.to_string(),
None => log_template(ui.settings()),
@ -3142,7 +3154,7 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result
let mut graph = AsciiGraphDrawer::new(&mut formatter);
for commit in commits {
let mut edges = vec![];
for predecessor in commit.predecessors() {
for predecessor in &commit.predecessors() {
edges.push(Edge::direct(predecessor.id().clone()));
}
let mut buffer = vec![];
@ -3154,6 +3166,16 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result
if !buffer.ends_with(b"\n") {
buffer.push(b'\n');
}
if let Some(diff_format) = diff_format {
let writer = Box::new(&mut buffer);
let mut formatter = ui.new_formatter(writer);
show_predecessor_patch(
formatter.as_mut(),
&workspace_command,
&commit,
diff_format,
)?;
}
let node_symbol = if Some(commit.id()) == checkout_id {
b"@"
} else {
@ -3164,12 +3186,46 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result
} else {
for commit in commits {
template.format(&commit, formatter)?;
if let Some(diff_format) = diff_format {
show_predecessor_patch(formatter, &workspace_command, &commit, diff_format)?;
}
}
}
Ok(())
}
fn show_predecessor_patch(
formatter: &mut dyn Formatter,
workspace_command: &WorkspaceCommandHelper,
commit: &Commit,
diff_format: DiffFormat,
) -> Result<(), CommandError> {
if let Some(predecessor) = commit.predecessors().first() {
let predecessor_tree = if predecessor.parent_ids() == commit.parent_ids() {
predecessor.tree()
} else {
// Rebase the predecessor to have the current commit's parent(s) and use that
// tree as base
let new_parent_tree =
merge_commit_trees(workspace_command.repo().as_repo_ref(), &commit.parents());
let old_parent_tree = merge_commit_trees(
workspace_command.repo().as_repo_ref(),
&predecessor.parents(),
);
let rebased_tree_id =
merge_trees(&new_parent_tree, &old_parent_tree, &predecessor.tree())?;
workspace_command
.repo()
.store()
.get_tree(&RepoPath::root(), &rebased_tree_id)?
};
let diff_iterator = predecessor_tree.diff(&commit.tree(), &EverythingMatcher);
show_diff(formatter, workspace_command, diff_iterator, diff_format)?;
}
Ok(())
}
fn edit_description(
ui: &Ui,
repo: &ReadonlyRepo,

View file

@ -0,0 +1,170 @@
// 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 common::TestEnvironment;
use regex::Regex;
pub mod common;
#[test]
fn test_obslog_with_or_without_diff() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
std::fs::write(repo_path.join("file1"), "foo\n").unwrap();
test_env.jj_cmd_success(&repo_path, &["new", "-m", "my description"]);
std::fs::write(repo_path.join("file1"), "foo\nbar\n").unwrap();
std::fs::write(repo_path.join("file2"), "foo\n").unwrap();
test_env.jj_cmd_success(&repo_path, &["rebase", "-r", "@", "-d", "root"]);
std::fs::write(repo_path.join("file1"), "resolved\n").unwrap();
let stdout = get_log_output(&test_env, &repo_path, &["obslog"]);
insta::assert_snapshot!(stdout, @r###"
@ 1daafc17fefb test.user@example.com 2001-02-03 04:05:08.000 +07:00
| my description
o 813918f7b4e6 test.user@example.com 2001-02-03 04:05:08.000 +07:00 conflict
| my description
o 8f02f5470c55 test.user@example.com 2001-02-03 04:05:08.000 +07:00
| my description
o c8ceb219336b test.user@example.com 2001-02-03 04:05:08.000 +07:00
my description
"###);
// There should be no diff caused by the rebase because it was a pure rebase
// (even even though it resulted in a conflict).
let stdout = get_log_output(&test_env, &repo_path, &["obslog", "-p"]);
insta::assert_snapshot!(stdout, @r###"
@ 1daafc17fefb test.user@example.com 2001-02-03 04:05:08.000 +07:00
| my description
| Resolved conflict in file1:
| 1 1: <<<<<<<resolved
| 2 : -------
| 3 : +++++++
| 4 : +bar
| 5 : >>>>>>>
o 813918f7b4e6 test.user@example.com 2001-02-03 04:05:08.000 +07:00 conflict
| my description
o 8f02f5470c55 test.user@example.com 2001-02-03 04:05:08.000 +07:00
| my description
| Modified regular file file1:
| 1 1: foo
| 2: bar
| Added regular file file2:
| 1: foo
o c8ceb219336b test.user@example.com 2001-02-03 04:05:08.000 +07:00
my description
"###);
// Test `--no-graph`
let stdout = get_log_output(&test_env, &repo_path, &["obslog", "--no-graph"]);
insta::assert_snapshot!(stdout, @r###"
1daafc17fefb test.user@example.com 2001-02-03 04:05:08.000 +07:00
my description
813918f7b4e6 test.user@example.com 2001-02-03 04:05:08.000 +07:00 conflict
my description
8f02f5470c55 test.user@example.com 2001-02-03 04:05:08.000 +07:00
my description
c8ceb219336b test.user@example.com 2001-02-03 04:05:08.000 +07:00
my description
"###);
// Test `--git` format, and that it implies `-p`
let stdout = get_log_output(&test_env, &repo_path, &["obslog", "--no-graph", "--git"]);
insta::assert_snapshot!(stdout, @r###"
1daafc17fefb test.user@example.com 2001-02-03 04:05:08.000 +07:00
my description
diff --git a/file1 b/file1
index e155302a24...2ab19ae607 100644
--- a/file1
+++ b/file1
@@ -1,5 +1,1 @@
-<<<<<<<
--------
-+++++++
-+bar
->>>>>>>
+resolved
813918f7b4e6 test.user@example.com 2001-02-03 04:05:08.000 +07:00 conflict
my description
8f02f5470c55 test.user@example.com 2001-02-03 04:05:08.000 +07:00
my description
diff --git a/file1 b/file1
index 257cc5642c...3bd1f0e297 100644
--- a/file1
+++ b/file1
@@ -1,1 +1,2 @@
foo
+bar
diff --git a/file2 b/file2
new file mode 100644
index 0000000000..257cc5642c
--- /dev/null
+++ b/file2
@@ -1,0 +1,1 @@
+foo
c8ceb219336b test.user@example.com 2001-02-03 04:05:08.000 +07:00
my description
"###);
}
#[test]
fn test_obslog_squash() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.jj_cmd_success(&repo_path, &["describe", "-m", "first"]);
std::fs::write(repo_path.join("file1"), "foo\n").unwrap();
test_env.jj_cmd_success(&repo_path, &["new", "-m", "second"]);
std::fs::write(repo_path.join("file1"), "foo\nbar\n").unwrap();
test_env.jj_cmd_success(&repo_path, &["squash"]);
let stdout = get_log_output(&test_env, &repo_path, &["obslog", "-p", "-r", "@-"]);
insta::assert_snapshot!(stdout, @r###"
o c36a0819516d test.user@example.com 2001-02-03 04:05:07.000 +07:00
|\ first
| | Modified regular file file1:
| | 1 1: foo
| | 2: bar
o | 803a7299cb1a test.user@example.com 2001-02-03 04:05:07.000 +07:00
| | first
| | Added regular file file1:
| | 1: foo
o | 85a1e2839620 test.user@example.com 2001-02-03 04:05:07.000 +07:00
| | first
o | 230dd059e1b0 test.user@example.com 2001-02-03 04:05:07.000 +07:00
/ (no description set)
o 69231a40d60d test.user@example.com 2001-02-03 04:05:09.000 +07:00
| second
| Modified regular file file1:
| 1 1: foo
| 2: bar
o b567edda97ab test.user@example.com 2001-02-03 04:05:09.000 +07:00
second
"###);
}
fn get_log_output(test_env: &TestEnvironment, repo_path: &Path, args: &[&str]) -> String {
// Filter out the change ID since it's random
let regex = Regex::new("^([o@| ]+)?([0-9a-f]{12}) ([0-9a-f]{12}) ").unwrap();
let mut lines = vec![];
let stdout = test_env.jj_cmd_success(repo_path, args);
for line in stdout.split_inclusive('\n') {
lines.push(regex.replace(line, "$1$2 "));
}
lines.join("")
}