2022-09-28 00:06:18 +00:00
|
|
|
use anyhow::Result;
|
2022-09-28 15:43:33 +00:00
|
|
|
use git2::Repository as LibGitRepository;
|
2022-09-22 23:55:24 +00:00
|
|
|
use parking_lot::Mutex;
|
2022-09-28 15:43:33 +00:00
|
|
|
use std::{path::Path, sync::Arc};
|
2022-09-22 23:55:24 +00:00
|
|
|
use util::ResultExt;
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
pub trait GitRepository: Send + Sync + std::fmt::Debug {
|
|
|
|
fn manages(&self, path: &Path) -> bool;
|
|
|
|
|
|
|
|
fn in_dot_git(&self, path: &Path) -> bool;
|
|
|
|
|
|
|
|
fn content_path(&self) -> &Path;
|
|
|
|
|
|
|
|
fn git_dir_path(&self) -> &Path;
|
|
|
|
|
|
|
|
fn scan_id(&self) -> usize;
|
|
|
|
|
|
|
|
fn set_scan_id(&mut self, scan_id: usize);
|
|
|
|
|
|
|
|
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>>;
|
|
|
|
|
|
|
|
fn boxed_clone(&self) -> Box<dyn GitRepository>;
|
|
|
|
|
|
|
|
async fn load_head_text(&self, relative_file_path: &Path) -> Option<String>;
|
|
|
|
}
|
|
|
|
|
2022-09-22 23:55:24 +00:00
|
|
|
#[derive(Clone)]
|
2022-09-28 18:42:22 +00:00
|
|
|
pub struct RealGitRepository {
|
2022-09-22 23:55:24 +00:00
|
|
|
// Path to folder containing the .git file or directory
|
|
|
|
content_path: Arc<Path>,
|
|
|
|
// Path to the actual .git folder.
|
|
|
|
// Note: if .git is a file, this points to the folder indicated by the .git file
|
|
|
|
git_dir_path: Arc<Path>,
|
2022-09-27 18:37:33 +00:00
|
|
|
scan_id: usize,
|
2022-09-28 00:06:18 +00:00
|
|
|
libgit_repository: Arc<Mutex<LibGitRepository>>,
|
2022-09-22 23:55:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
impl RealGitRepository {
|
|
|
|
pub fn open(dotgit_path: &Path) -> Option<Box<dyn GitRepository>> {
|
2022-09-28 00:06:18 +00:00
|
|
|
LibGitRepository::open(&dotgit_path)
|
2022-09-22 23:55:24 +00:00
|
|
|
.log_err()
|
2022-09-28 18:42:22 +00:00
|
|
|
.and_then::<Box<dyn GitRepository>, _>(|libgit_repository| {
|
|
|
|
Some(Box::new(Self {
|
2022-09-26 20:57:31 +00:00
|
|
|
content_path: libgit_repository.workdir()?.into(),
|
|
|
|
git_dir_path: dotgit_path.canonicalize().log_err()?.into(),
|
2022-09-27 18:37:33 +00:00
|
|
|
scan_id: 0,
|
2022-09-22 23:55:24 +00:00
|
|
|
libgit_repository: Arc::new(parking_lot::Mutex::new(libgit_repository)),
|
2022-09-28 18:42:22 +00:00
|
|
|
}))
|
2022-09-22 23:55:24 +00:00
|
|
|
})
|
|
|
|
}
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
2022-09-22 23:55:24 +00:00
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl GitRepository for RealGitRepository {
|
|
|
|
fn manages(&self, path: &Path) -> bool {
|
2022-09-26 20:57:31 +00:00
|
|
|
path.canonicalize()
|
|
|
|
.map(|path| path.starts_with(&self.content_path))
|
|
|
|
.unwrap_or(false)
|
2022-09-22 23:55:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
fn in_dot_git(&self, path: &Path) -> bool {
|
2022-09-26 20:57:31 +00:00
|
|
|
path.canonicalize()
|
|
|
|
.map(|path| path.starts_with(&self.git_dir_path))
|
|
|
|
.unwrap_or(false)
|
2022-09-22 23:55:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
fn content_path(&self) -> &Path {
|
2022-09-22 23:55:24 +00:00
|
|
|
self.content_path.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
fn git_dir_path(&self) -> &Path {
|
2022-09-22 23:55:24 +00:00
|
|
|
self.git_dir_path.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
fn scan_id(&self) -> usize {
|
2022-09-27 18:37:33 +00:00
|
|
|
self.scan_id
|
2022-09-22 23:55:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
async fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
|
2022-09-28 15:43:33 +00:00
|
|
|
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
|
2022-09-28 00:06:18 +00:00
|
|
|
let object = repo
|
|
|
|
.head()?
|
|
|
|
.peel_to_tree()?
|
2022-09-28 15:43:33 +00:00
|
|
|
.get_path(relative_file_path)?
|
2022-09-28 00:06:18 +00:00
|
|
|
.to_object(&repo)?;
|
|
|
|
|
|
|
|
let content = match object.as_blob() {
|
|
|
|
Some(blob) => blob.content().to_owned(),
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let head_text = String::from_utf8(content.to_owned())?;
|
|
|
|
Ok(Some(head_text))
|
|
|
|
}
|
|
|
|
|
2022-09-28 15:43:33 +00:00
|
|
|
match logic(&self.libgit_repository.as_ref().lock(), relative_file_path) {
|
2022-09-28 00:06:18 +00:00
|
|
|
Ok(value) => return value,
|
|
|
|
Err(err) => log::error!("Error loading head text: {:?}", err),
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2022-09-28 18:42:22 +00:00
|
|
|
|
|
|
|
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>> {
|
|
|
|
self.libgit_repository.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_scan_id(&mut self, scan_id: usize) {
|
|
|
|
self.scan_id = scan_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn boxed_clone(&self) -> Box<dyn GitRepository> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
2022-09-28 00:06:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
impl std::fmt::Debug for RealGitRepository {
|
2022-09-28 00:06:18 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("GitRepository")
|
|
|
|
.field("content_path", &self.content_path)
|
|
|
|
.field("git_dir_path", &self.git_dir_path)
|
|
|
|
.field("scan_id", &self.scan_id)
|
|
|
|
.field("libgit_repository", &"LibGitRepository")
|
|
|
|
.finish()
|
|
|
|
}
|
2022-09-22 23:55:24 +00:00
|
|
|
}
|
2022-09-28 18:42:22 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct FakeGitRepository {
|
|
|
|
content_path: Arc<Path>,
|
|
|
|
git_dir_path: Arc<Path>,
|
|
|
|
scan_id: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FakeGitRepository {
|
|
|
|
pub fn open(dotgit_path: &Path, scan_id: usize) -> Box<dyn GitRepository> {
|
|
|
|
Box::new(FakeGitRepository {
|
|
|
|
content_path: dotgit_path.parent().unwrap().into(),
|
|
|
|
git_dir_path: dotgit_path.into(),
|
|
|
|
scan_id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl GitRepository for FakeGitRepository {
|
|
|
|
fn manages(&self, path: &Path) -> bool {
|
|
|
|
path.starts_with(self.content_path())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn in_dot_git(&self, path: &Path) -> bool {
|
|
|
|
path.starts_with(self.git_dir_path())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn content_path(&self) -> &Path {
|
|
|
|
&self.content_path
|
|
|
|
}
|
|
|
|
|
|
|
|
fn git_dir_path(&self) -> &Path {
|
|
|
|
&self.git_dir_path
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scan_id(&self) -> usize {
|
|
|
|
self.scan_id
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn load_head_text(&self, _: &Path) -> Option<String> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn git_repo(&self) -> Arc<Mutex<LibGitRepository>> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_scan_id(&mut self, scan_id: usize) {
|
|
|
|
self.scan_id = scan_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn boxed_clone(&self) -> Box<dyn GitRepository> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
|
|
|
}
|