From 077bac8be10ca58545f8eb16da4670934565120d Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 31 Oct 2024 17:53:13 +0900 Subject: [PATCH] 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. --- lib/src/annotate.rs | 44 ++++++++++++++++++++++++++++++++------ lib/tests/test_annotate.rs | 33 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/lib/src/annotate.rs b/lib/src/annotate.rs index 4bc57c9e2a..c8e14830ec 100644 --- a/lib/src/annotate.rs +++ b/lib/src/annotate.rs @@ -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 { 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, file_path: &RepoPath, ) -> Result { - 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, + file_path: &RepoPath, + starting_text: impl Into>, +) -> Result { + 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, + file_path: &RepoPath, + mut source: Source, +) -> Result { 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 }) } diff --git a/lib/tests/test_annotate.rs b/lib/tests/test_annotate.rs index faef109b5a..d0cc459e12 100644 --- a/lib/tests/test_annotate.rs +++ b/lib/tests/test_annotate.rs @@ -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]