jj/cli/tests/test_file_annotate_command.rs
Bryce Berger 3bc111e60e annotate: make AnnotationLine template type
Allows:
* self.commit()
* self.line_number()
* self.first_line_in_hunk()

Certain pagers (like `delta`), when used for `git blame`, only show the
commit information for the first line in a hunk. This would be a nice
addition to `jj file annotate`.

`jj file annotate` already uses a template to control the rendering of
commit information --- `templates.annotate_commit_summary`. Instead of
a custom CLI flag, the tools necessary to do this should be available in
the template language.

If `1 % 2` or `1.is_even()` was available in the template language, this
would also allow alternating colors (using `raw_escape_sequence`).

Example:

```toml
[templates]
# only show commit info for the first line of each hunk
annotate_commit_summary = '''
if(first_line_in_hunk,
  show_commit_info(commit),
  pad_end(20, " "),
)
'''
```
2025-02-15 02:34:38 +00:00

206 lines
7.9 KiB
Rust

// Copyright 2024 The Jujutsu Authors
//
// 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::fs::OpenOptions;
use std::io::Write;
use std::path::Path;
use crate::common::TestEnvironment;
fn append_to_file(file_path: &Path, contents: &str) {
let mut options = OpenOptions::new();
options.append(true);
let mut file = options.open(file_path).unwrap();
writeln!(file, "{contents}").unwrap();
}
#[test]
fn test_annotate_linear() {
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("file.txt"), "line1\n").unwrap();
test_env.jj_cmd_ok(
&repo_path,
&["describe", "-m=initial", "--author=Foo <foo@example.org>"],
);
test_env.jj_cmd_ok(&repo_path, &["new", "-m=next"]);
append_to_file(&repo_path.join("file.txt"), "new text from new commit");
let stdout = test_env.jj_cmd_success(&repo_path, &["file", "annotate", "file.txt"]);
insta::assert_snapshot!(stdout, @r"
qpvuntsm foo 2001-02-03 08:05:08 1: line1
kkmpptxz test.use 2001-02-03 08:05:10 2: new text from new commit
");
}
#[test]
fn test_annotate_merge() {
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("file.txt"), "line1\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["describe", "-m=initial"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "initial"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m=commit1"]);
append_to_file(&repo_path.join("file.txt"), "new text from new commit 1");
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "commit1"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m=commit2", "initial"]);
append_to_file(&repo_path.join("file.txt"), "new text from new commit 2");
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "commit2"]);
// create a (conflicted) merge
test_env.jj_cmd_ok(&repo_path, &["new", "-m=merged", "commit1", "commit2"]);
// resolve conflicts
std::fs::write(
repo_path.join("file.txt"),
"line1\nnew text from new commit 1\nnew text from new commit 2\n",
)
.unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["file", "annotate", "file.txt"]);
insta::assert_snapshot!(stdout, @r"
qpvuntsm test.use 2001-02-03 08:05:08 1: line1
zsuskuln test.use 2001-02-03 08:05:11 2: new text from new commit 1
royxmykx test.use 2001-02-03 08:05:13 3: new text from new commit 2
");
}
#[test]
fn test_annotate_conflicted() {
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("file.txt"), "line1\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["describe", "-m=initial"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "initial"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m=commit1"]);
append_to_file(&repo_path.join("file.txt"), "new text from new commit 1");
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "commit1"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m=commit2", "initial"]);
append_to_file(&repo_path.join("file.txt"), "new text from new commit 2");
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "commit2"]);
// create a (conflicted) merge
test_env.jj_cmd_ok(&repo_path, &["new", "-m=merged", "commit1", "commit2"]);
test_env.jj_cmd_ok(&repo_path, &["new"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["file", "annotate", "file.txt"]);
insta::assert_snapshot!(stdout, @r"
qpvuntsm test.use 2001-02-03 08:05:08 1: line1
yostqsxw test.use 2001-02-03 08:05:15 2: <<<<<<< Conflict 1 of 1
yostqsxw test.use 2001-02-03 08:05:15 3: %%%%%%% Changes from base to side #1
yostqsxw test.use 2001-02-03 08:05:15 4: +new text from new commit 1
yostqsxw test.use 2001-02-03 08:05:15 5: +++++++ Contents of side #2
royxmykx test.use 2001-02-03 08:05:13 6: new text from new commit 2
yostqsxw test.use 2001-02-03 08:05:15 7: >>>>>>> Conflict 1 of 1 ends
");
}
#[test]
fn test_annotate_merge_one_sided_conflict_resolution() {
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("file.txt"), "line1\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["describe", "-m=initial"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "initial"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m=commit1"]);
append_to_file(&repo_path.join("file.txt"), "new text from new commit 1");
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "commit1"]);
test_env.jj_cmd_ok(&repo_path, &["new", "-m=commit2", "initial"]);
append_to_file(&repo_path.join("file.txt"), "new text from new commit 2");
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "-r@", "commit2"]);
// create a (conflicted) merge
test_env.jj_cmd_ok(&repo_path, &["new", "-m=merged", "commit1", "commit2"]);
// resolve conflicts
std::fs::write(
repo_path.join("file.txt"),
"line1\nnew text from new commit 1\n",
)
.unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["file", "annotate", "file.txt"]);
insta::assert_snapshot!(stdout, @r"
qpvuntsm test.use 2001-02-03 08:05:08 1: line1
zsuskuln test.use 2001-02-03 08:05:11 2: new text from new commit 1
");
}
#[test]
fn test_annotate_with_template() {
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("file.txt"), "line1\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["commit", "-m=initial"]);
append_to_file(
&repo_path.join("file.txt"),
"new text from new commit 1\nthat splits into multiple lines",
);
test_env.jj_cmd_ok(&repo_path, &["commit", "-m=commit1"]);
append_to_file(
&repo_path.join("file.txt"),
"new text from new commit 2\nalso continuing on a second line\nand a third!",
);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m=commit2"]);
let template = indoc::indoc! {r#"
if(first_line_in_hunk, "\n" ++ separate("\n",
commit.change_id().shortest(8)
++ " "
++ commit.description().first_line(),
commit_timestamp(commit).local().format('%Y-%m-%d %H:%M:%S')
++ " "
++ commit.author(),
) ++ "\n") ++ pad_start(4, line_number) ++ ": "
"#};
let stdout = test_env.jj_cmd_success(
&repo_path,
&["file", "annotate", "file.txt", "-T", template],
);
insta::assert_snapshot!(stdout, @r#"
qpvuntsm initial
2001-02-03 08:05:08 Test User <test.user@example.com>
1: line1
rlvkpnrz commit1
2001-02-03 08:05:09 Test User <test.user@example.com>
2: new text from new commit 1
3: that splits into multiple lines
kkmpptxz commit2
2001-02-03 08:05:10 Test User <test.user@example.com>
4: new text from new commit 2
5: also continuing on a second line
6: and a third!
"#);
}