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

Allow restricting diff/patch output to a specific revset.

This commit is contained in:
Anton Älgmyr 2024-07-06 11:37:44 +02:00
parent df021083c9
commit d1cfe93435
3 changed files with 96 additions and 3 deletions

View file

@ -70,6 +70,11 @@ pub(crate) struct LogArgs {
/// Show patch /// Show patch
#[arg(long, short = 'p')] #[arg(long, short = 'p')]
patch: bool, patch: bool,
/// Restrict diffs to the given revsets.
///
/// Defaults to all().
#[arg(long)]
diff_revisions: Vec<RevisionArg>,
#[command(flatten)] #[command(flatten)]
diff_format: DiffFormatArgs, diff_format: DiffFormatArgs,
} }
@ -102,6 +107,14 @@ pub(crate) fn cmd_log(
} }
expression expression
}; };
let diff_revset_contains = {
let diff_revset_expression = if args.diff_revisions.is_empty() {
workspace_command.attach_revset_evaluator(RevsetExpression::all())?
} else {
workspace_command.parse_union_revsets(&args.diff_revisions)?
};
diff_revset_expression.evaluate()?.containing_fn()
};
let repo = workspace_command.repo(); let repo = workspace_command.repo();
let matcher = fileset_expression.to_matcher(); let matcher = fileset_expression.to_matcher();
@ -202,8 +215,10 @@ pub(crate) fn cmd_log(
buffer.push(b'\n'); buffer.push(b'\n');
} }
if let Some(renderer) = &diff_renderer { if let Some(renderer) = &diff_renderer {
let mut formatter = ui.new_formatter(&mut buffer); if diff_revset_contains(commit.id()) {
renderer.show_patch(ui, formatter.as_mut(), &commit, matcher.as_ref())?; let mut formatter = ui.new_formatter(&mut buffer);
renderer.show_patch(ui, formatter.as_mut(), &commit, matcher.as_ref())?;
}
} }
let node_symbol = format_template(ui, &Some(commit), &node_template); let node_symbol = format_template(ui, &Some(commit), &node_template);
@ -243,7 +258,9 @@ pub(crate) fn cmd_log(
with_content_format with_content_format
.write(formatter, |formatter| template.format(&commit, formatter))?; .write(formatter, |formatter| template.format(&commit, formatter))?;
if let Some(renderer) = &diff_renderer { if let Some(renderer) = &diff_renderer {
renderer.show_patch(ui, formatter, &commit, matcher.as_ref())?; if diff_revset_contains(commit.id()) {
renderer.show_patch(ui, formatter, &commit, matcher.as_ref())?;
}
} }
} }
} }

View file

@ -1099,6 +1099,9 @@ Spans of revisions that are not included in the graph per `--revisions` are rend
For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md For the syntax, see https://github.com/martinvonz/jj/blob/main/docs/templates.md
* `-p`, `--patch` — Show patch * `-p`, `--patch` — Show patch
* `--diff-revisions <DIFF_REVISIONS>` — Restrict diffs to the given revsets.
Defaults to all().
* `-s`, `--summary` — For each path, show only whether it was modified, added, or deleted * `-s`, `--summary` — For each path, show only whether it was modified, added, or deleted
* `--stat` — Show a histogram of the changes * `--stat` — Show a histogram of the changes
* `--types` — For each path, show only its type before and after * `--types` — For each path, show only its type before and after

View file

@ -294,6 +294,79 @@ fn test_log_with_or_without_diff() {
"###); "###);
} }
#[test]
fn test_diff_revisions_restriction() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");
std::fs::write(repo_path.join("file1"), "foo\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "add a file"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m", "a new commit"]);
std::fs::write(repo_path.join("file1"), "foo\nbar\n").unwrap();
// diff-revisions doesn't affect output when summary or patch is not requested.
let stdout = test_env.jj_cmd_success(
&repo_path,
&["log", "-T", "description", "--diff-revisions=@"],
);
insta::assert_snapshot!(stdout, @r###"
@ a new commit
add a file
"###);
// `-p` for default diff output, restrict to @
let stdout = test_env.jj_cmd_success(
&repo_path,
&["log", "-T", "description", "-p", "--diff-revisions=@"],
);
insta::assert_snapshot!(stdout, @r###"
@ a new commit
Modified regular file file1:
1 1: foo
2: bar
add a file
"###);
// `-p` for default diff output, `-s` for summary, restrict to @
let stdout = test_env.jj_cmd_success(
&repo_path,
&["log", "-T", "description", "-p", "-s", "--diff-revisions=@"],
);
insta::assert_snapshot!(stdout, @r###"
@ a new commit
M file1
Modified regular file file1:
1 1: foo
2: bar
add a file
"###);
// `-s` for summary, restrict to @-
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-T",
"description",
"-p",
"-s",
"--diff-revisions=@-",
],
);
insta::assert_snapshot!(stdout, @r###"
@ a new commit
add a file
A file1
Added regular file file1:
1: foo
"###);
}
#[test] #[test]
fn test_log_null_terminate_multiline_descriptions() { fn test_log_null_terminate_multiline_descriptions() {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();