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

cli: make log -s/--git imply -p

`log -s/--summary` and `log --git` without `-p` don't do anything. I
also don't think it's very useful to pass these flags in an alias,
where you would then sometimes also pass `-p` to see the diff summary
in the output. We already have the `diff.format` config for that use
case. So let's make both of these flags imply `-p`.

I implemented it by making the `diff_format` variable an
`Option<DiffFormat>`, which is set iff we should show a patch. That
way we have the condition in one place, and the places we use it
cannot forget to check it.
This commit is contained in:
Martin von Zweigbergk 2022-05-12 22:20:10 -07:00 committed by Martin von Zweigbergk
parent 788831fed3
commit 87ba11592b
2 changed files with 56 additions and 6 deletions

View file

@ -1287,7 +1287,7 @@ struct LogArgs {
#[clap(long, short = 'p')]
patch: bool,
#[clap(flatten)]
format: DiffFormatArgs,
diff_format: DiffFormatArgs,
}
/// Show how a change has evolved
@ -2993,7 +2993,8 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C
let checkout_id = repo.view().get_checkout(&workspace_id);
let revset = revset_expression.evaluate(repo.as_repo_ref(), Some(&workspace_id))?;
let store = repo.store();
let diff_format = diff_format_for(ui, &args.format);
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(),
@ -3053,7 +3054,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C
if !buffer.ends_with(b"\n") {
buffer.push(b'\n');
}
if args.patch {
if let Some(diff_format) = diff_format {
let writer = Box::new(&mut buffer);
let mut formatter = ui.new_formatter(writer);
show_patch(formatter.as_mut(), &workspace_command, &commit, diff_format)?;
@ -3070,9 +3071,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C
for index_entry in revset.iter() {
let commit = store.get_commit(&index_entry.commit_id())?;
template.format(&commit, formatter)?;
// TODO: should --summary (without --patch) show diff summary as in hg log
// --stat?
if args.patch {
if let Some(diff_format) = diff_format {
show_patch(formatter, &workspace_command, &commit, diff_format)?;
}
}

View file

@ -76,4 +76,55 @@ fn test_log_with_or_without_diff() {
+foo
"###);
// `-s` implies `-p`, with or without graph
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description", "-s"]);
insta::assert_snapshot!(stdout, @r###"
@ a new commit
| M file1
o add a file
| A file1
o
"###);
let stdout = test_env.jj_cmd_success(
&repo_path,
&["log", "-T", "description", "--no-graph", "-s"],
);
insta::assert_snapshot!(stdout, @r###"
a new commit
M file1
add a file
A file1
"###);
// `--git` implies `-p`, with or without graph
let stdout = test_env.jj_cmd_success(
&repo_path,
&["log", "-T", "description", "-r", "@", "--git"],
);
insta::assert_snapshot!(stdout, @r###"
@ a new commit
~ diff --git a/file1 b/file1
index 257cc5642c...3bd1f0e297 100644
--- a/file1
+++ b/file1
@@ -1,1 +1,2 @@
foo
+bar
"###);
let stdout = test_env.jj_cmd_success(
&repo_path,
&["log", "-T", "description", "-r", "@", "--no-graph", "--git"],
);
insta::assert_snapshot!(stdout, @r###"
a new commit
diff --git a/file1 b/file1
index 257cc5642c...3bd1f0e297 100644
--- a/file1
+++ b/file1
@@ -1,1 +1,2 @@
foo
+bar
"###);
}