move blame to repository extension trait

This commit is contained in:
Kiril Videlov 2024-06-03 01:40:41 +02:00
parent 8eb7d1d0f0
commit 8ab770b15a
3 changed files with 39 additions and 27 deletions

View File

@ -1,5 +1,5 @@
use super::{Oid, Refname, Result, Url};
use git2::{BlameOptions, Submodule};
use git2::Submodule;
use git2_hooks::HookResult;
use std::{path::Path, str};
@ -331,25 +331,6 @@ impl Repository {
Ok(())
}
pub fn blame(
&self,
path: &Path,
min_line: u32,
max_line: u32,
oldest_commit: &Oid,
newest_commit: &Oid,
) -> Result<git2::Blame> {
let mut opts = BlameOptions::new();
opts.min_line(min_line as usize)
.max_line(max_line as usize)
.newest_commit(git2::Oid::from(*newest_commit))
.oldest_commit(git2::Oid::from(*oldest_commit))
.first_parent(true);
self.0
.blame_file(path, Some(&mut opts))
.map_err(super::Error::Blame)
}
/// Returns a list of remotes
///
/// Returns `Vec<String>` instead of StringArray because StringArray cannot safly be sent between threads

View File

@ -1,6 +1,6 @@
use anyhow::{bail, Context, Result};
use git2::{Repository, Tree};
use std::{process::Stdio, str};
use git2::{BlameOptions, Repository, Tree};
use std::{path::Path, process::Stdio, str};
use tracing::instrument;
use super::Refname;
@ -35,6 +35,15 @@ pub trait RepositoryExt {
parents: &[&git2::Commit<'_>],
change_id: Option<&str>,
) -> Result<git2::Oid>;
fn blame(
&self,
path: &Path,
min_line: u32,
max_line: u32,
oldest_commit: git2::Oid,
newest_commit: git2::Oid,
) -> Result<git2::Blame, git2::Error>;
}
impl RepositoryExt for Repository {
@ -78,6 +87,23 @@ impl RepositoryExt for Repository {
}
Ok(oid)
}
fn blame(
&self,
path: &Path,
min_line: u32,
max_line: u32,
oldest_commit: git2::Oid,
newest_commit: git2::Oid,
) -> Result<git2::Blame, git2::Error> {
let mut opts = BlameOptions::new();
opts.min_line(min_line as usize)
.max_line(max_line as usize)
.newest_commit(newest_commit)
.oldest_commit(oldest_commit)
.first_parent(true);
self.blame_file(path, Some(&mut opts))
}
}
/// takes raw commit data and commits it to the repository

View File

@ -1664,16 +1664,21 @@ fn compute_locks(
for (path, hunks) in base_diffs.clone().into_iter() {
for hunk in hunks {
let blame = match project_repository.git_repository.blame(
let blame = match project_repository.repo().blame(
&path,
hunk.old_start,
(hunk.old_start + hunk.old_lines).saturating_sub(1),
&merge_base,
integration_commit,
merge_base.into(),
(*integration_commit).into(),
) {
Ok(blame) => blame,
Err(git::Error::Blame(err)) if err.code() == ErrorCode::NotFound => continue,
Err(err) => return Err(err.into()),
Err(error) => {
if error.code() == ErrorCode::NotFound {
continue;
} else {
return Err(error.into());
}
}
};
for blame_hunk in blame.iter() {