annotate: add low-level function to specify starting file content

In "jj absorb", we'll need to calculate annotation from the parent tree. It's
usually identical to the tree of the parent commit, but this is not true for a
merge commit. Since I'm not sure how we'll process conflict trees in general,
this patch adds a minimal API to specify a single file content, not a
MergedTree.
This commit is contained in:
Yuya Nishihara 2024-10-31 17:53:13 +09:00
parent 85e0a8b068
commit 077bac8be1
2 changed files with 71 additions and 6 deletions

View file

@ -84,13 +84,17 @@ struct Source {
}
impl Source {
fn new(text: BString) -> Self {
Source {
line_map: Vec::new(),
text,
}
}
fn load(commit: &Commit, file_path: &RepoPath) -> Result<Self, BackendError> {
let tree = commit.tree()?;
let text = get_file_contents(commit.store(), file_path, &tree)?;
Ok(Source {
line_map: Vec::new(),
text: text.into(),
})
Ok(Self::new(text.into()))
}
fn fill_line_map(&mut self) {
@ -116,10 +120,38 @@ pub fn get_annotation_for_file(
domain: &Rc<ResolvedRevsetExpression>,
file_path: &RepoPath,
) -> Result<FileAnnotation, RevsetEvaluationError> {
let mut source = Source::load(starting_commit, file_path)?;
let source = Source::load(starting_commit, file_path)?;
compute_file_annotation(repo, starting_commit.id(), domain, file_path, source)
}
/// Get line by line annotations for a specific file path starting with the
/// given content.
///
/// The file content at the `starting_commit` is set to `starting_text`. This is
/// typically one of the file contents in the conflict or merged-parent tree.
///
/// See [`get_annotation_for_file()`] for the other arguments.
pub fn get_annotation_with_file_content(
repo: &dyn Repo,
starting_commit_id: &CommitId,
domain: &Rc<ResolvedRevsetExpression>,
file_path: &RepoPath,
starting_text: impl Into<Vec<u8>>,
) -> Result<FileAnnotation, RevsetEvaluationError> {
let source = Source::new(BString::new(starting_text.into()));
compute_file_annotation(repo, starting_commit_id, domain, file_path, source)
}
fn compute_file_annotation(
repo: &dyn Repo,
starting_commit_id: &CommitId,
domain: &Rc<ResolvedRevsetExpression>,
file_path: &RepoPath,
mut source: Source,
) -> Result<FileAnnotation, RevsetEvaluationError> {
source.fill_line_map();
let text = source.text.clone();
let line_map = process_commits(repo, starting_commit.id(), source, domain, file_path)?;
let line_map = process_commits(repo, starting_commit_id, source, domain, file_path)?;
Ok(FileAnnotation { line_map, text })
}

View file

@ -16,11 +16,14 @@ use std::fmt::Write as _;
use std::rc::Rc;
use jj_lib::annotate::get_annotation_for_file;
use jj_lib::annotate::get_annotation_with_file_content;
use jj_lib::annotate::FileAnnotation;
use jj_lib::backend::CommitId;
use jj_lib::backend::MergedTreeId;
use jj_lib::backend::MillisSinceEpoch;
use jj_lib::backend::Signature;
use jj_lib::backend::Timestamp;
use jj_lib::backend::TreeValue;
use jj_lib::commit::Commit;
use jj_lib::repo::MutableRepo;
use jj_lib::repo::Repo;
@ -68,6 +71,27 @@ fn annotate_within(
file_path: &RepoPath,
) -> String {
let annotation = get_annotation_for_file(repo, commit, domain, file_path).unwrap();
format_annotation(repo, &annotation)
}
fn annotate_parent_tree(repo: &dyn Repo, commit: &Commit, file_path: &RepoPath) -> String {
let tree = commit.parent_tree(repo).unwrap();
let text = match tree.path_value(file_path).unwrap().into_resolved().unwrap() {
Some(TreeValue::File { id, .. }) => {
let mut reader = repo.store().read_file(file_path, &id).unwrap();
let mut buf = Vec::new();
reader.read_to_end(&mut buf).unwrap();
buf
}
value => panic!("unexpected path value: {value:?}"),
};
let domain = RevsetExpression::all();
let annotation =
get_annotation_with_file_content(repo, commit.id(), &domain, file_path, text).unwrap();
format_annotation(repo, &annotation)
}
fn format_annotation(repo: &dyn Repo, annotation: &FileAnnotation) -> String {
let mut output = String::new();
for (commit_id, line) in annotation.lines() {
let commit = commit_id.map(|id| repo.store().get_commit(id).unwrap());
@ -328,6 +352,15 @@ fn test_annotate_merge_dup() {
commit3: 3
commit4: 4
"#);
// For example, the parent tree of commit4 doesn't contain multiple "1"s.
// If annotation were computed compared to the parent tree, not trees of the
// parent commits, "1" would be inserted at commit4.
insta::assert_snapshot!(annotate_parent_tree(tx.repo(), &commit4, file_path), @r"
commit2: 2
commit1: 1
commit3: 3
");
}
#[test]