2023-03-26 11:44:07 +02:00
|
|
|
use anyhow::{bail, Context, Result};
|
2023-03-10 23:42:42 +01:00
|
|
|
use arc_swap::ArcSwap;
|
2024-02-03 19:23:33 +01:00
|
|
|
use gix::filter::plumbing::driver::apply::Delay;
|
|
|
|
use std::io::Read;
|
2022-12-01 09:35:23 +01:00
|
|
|
use std::path::Path;
|
2023-03-10 23:42:42 +01:00
|
|
|
use std::sync::Arc;
|
2022-12-01 09:35:23 +01:00
|
|
|
|
2023-12-13 02:29:43 +01:00
|
|
|
use gix::objs::tree::EntryKind;
|
2023-02-20 16:48:13 +01:00
|
|
|
use gix::sec::trust::DefaultForLevel;
|
|
|
|
use gix::{Commit, ObjectId, Repository, ThreadSafeRepository};
|
2022-12-01 09:35:23 +01:00
|
|
|
|
|
|
|
use crate::DiffProvider;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
|
|
|
|
|
|
|
pub struct Git;
|
|
|
|
|
|
|
|
impl Git {
|
2023-03-26 11:44:07 +02:00
|
|
|
fn open_repo(path: &Path, ceiling_dir: Option<&Path>) -> Result<ThreadSafeRepository> {
|
2022-12-01 09:35:23 +01:00
|
|
|
// custom open options
|
2023-02-20 16:48:13 +01:00
|
|
|
let mut git_open_opts_map = gix::sec::trust::Mapping::<gix::open::Options>::default();
|
2022-12-01 09:35:23 +01:00
|
|
|
|
2022-12-05 14:46:59 +01:00
|
|
|
// On windows various configuration options are bundled as part of the installations
|
|
|
|
// This path depends on the install location of git and therefore requires some overhead to lookup
|
|
|
|
// This is basically only used on windows and has some overhead hence it's disabled on other platforms.
|
|
|
|
// `gitoxide` doesn't use this as default
|
2023-04-27 07:38:07 +02:00
|
|
|
let config = gix::open::permissions::Config {
|
2022-12-05 14:46:59 +01:00
|
|
|
system: true,
|
|
|
|
git: true,
|
|
|
|
user: true,
|
2022-12-01 09:35:23 +01:00
|
|
|
env: true,
|
|
|
|
includes: true,
|
2022-12-05 14:46:59 +01:00
|
|
|
git_binary: cfg!(windows),
|
2022-12-01 09:35:23 +01:00
|
|
|
};
|
|
|
|
// change options for config permissions without touching anything else
|
2023-04-27 07:38:07 +02:00
|
|
|
git_open_opts_map.reduced = git_open_opts_map
|
|
|
|
.reduced
|
|
|
|
.permissions(gix::open::Permissions {
|
|
|
|
config,
|
|
|
|
..gix::open::Permissions::default_for_level(gix::sec::Trust::Reduced)
|
|
|
|
});
|
|
|
|
git_open_opts_map.full = git_open_opts_map.full.permissions(gix::open::Permissions {
|
2022-12-01 09:35:23 +01:00
|
|
|
config,
|
2023-04-27 07:38:07 +02:00
|
|
|
..gix::open::Permissions::default_for_level(gix::sec::Trust::Full)
|
2022-12-01 09:35:23 +01:00
|
|
|
});
|
|
|
|
|
2023-04-27 07:38:07 +02:00
|
|
|
let open_options = gix::discover::upwards::Options {
|
|
|
|
ceiling_dirs: ceiling_dir
|
|
|
|
.map(|dir| vec![dir.to_owned()])
|
|
|
|
.unwrap_or_default(),
|
|
|
|
dot_git_only: true,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-12-01 09:35:23 +01:00
|
|
|
|
2023-03-26 11:44:07 +02:00
|
|
|
let res = ThreadSafeRepository::discover_with_environment_overrides_opts(
|
2022-12-01 09:35:23 +01:00
|
|
|
path,
|
|
|
|
open_options,
|
|
|
|
git_open_opts_map,
|
2023-03-26 11:44:07 +02:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(res)
|
2022-12-01 09:35:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiffProvider for Git {
|
2023-03-26 11:44:07 +02:00
|
|
|
fn get_diff_base(&self, file: &Path) -> Result<Vec<u8>> {
|
2022-12-01 09:35:23 +01:00
|
|
|
debug_assert!(!file.exists() || file.is_file());
|
|
|
|
debug_assert!(file.is_absolute());
|
|
|
|
|
|
|
|
// TODO cache repository lookup
|
2023-03-26 11:44:07 +02:00
|
|
|
|
|
|
|
let repo_dir = file.parent().context("file has no parent directory")?;
|
|
|
|
let repo = Git::open_repo(repo_dir, None)
|
|
|
|
.context("failed to open git repo")?
|
|
|
|
.to_thread_local();
|
|
|
|
let head = repo.head_commit()?;
|
2022-12-01 09:35:23 +01:00
|
|
|
let file_oid = find_file_in_commit(&repo, &head, file)?;
|
|
|
|
|
2023-03-26 11:44:07 +02:00
|
|
|
let file_object = repo.find_object(file_oid)?;
|
2024-02-03 19:23:33 +01:00
|
|
|
let data = file_object.detach().data;
|
|
|
|
// Get the actual data that git would make out of the git object.
|
|
|
|
// This will apply the user's git config or attributes like crlf conversions.
|
|
|
|
if let Some(work_dir) = repo.work_dir() {
|
|
|
|
let rela_path = file.strip_prefix(work_dir)?;
|
|
|
|
let rela_path = gix::path::try_into_bstr(rela_path)?;
|
|
|
|
let (mut pipeline, _) = repo.filter_pipeline(None)?;
|
|
|
|
let mut worktree_outcome =
|
|
|
|
pipeline.convert_to_worktree(&data, rela_path.as_ref(), Delay::Forbid)?;
|
|
|
|
let mut buf = Vec::with_capacity(data.len());
|
|
|
|
worktree_outcome.read_to_end(&mut buf)?;
|
|
|
|
Ok(buf)
|
|
|
|
} else {
|
|
|
|
Ok(data)
|
2022-12-05 14:46:59 +01:00
|
|
|
}
|
2022-12-01 09:35:23 +01:00
|
|
|
}
|
2023-03-10 23:42:42 +01:00
|
|
|
|
2023-03-26 11:44:07 +02:00
|
|
|
fn get_current_head_name(&self, file: &Path) -> Result<Arc<ArcSwap<Box<str>>>> {
|
2023-03-10 23:42:42 +01:00
|
|
|
debug_assert!(!file.exists() || file.is_file());
|
|
|
|
debug_assert!(file.is_absolute());
|
2023-03-26 11:44:07 +02:00
|
|
|
let repo_dir = file.parent().context("file has no parent directory")?;
|
|
|
|
let repo = Git::open_repo(repo_dir, None)
|
|
|
|
.context("failed to open git repo")?
|
|
|
|
.to_thread_local();
|
|
|
|
let head_ref = repo.head_ref()?;
|
|
|
|
let head_commit = repo.head_commit()?;
|
2023-03-10 23:42:42 +01:00
|
|
|
|
|
|
|
let name = match head_ref {
|
|
|
|
Some(reference) => reference.name().shorten().to_string(),
|
|
|
|
None => head_commit.id.to_hex_with_len(8).to_string(),
|
|
|
|
};
|
|
|
|
|
2023-03-26 11:44:07 +02:00
|
|
|
Ok(Arc::new(ArcSwap::from_pointee(name.into_boxed_str())))
|
2023-03-10 23:42:42 +01:00
|
|
|
}
|
2022-12-01 09:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the object that contains the contents of a file at a specific commit.
|
2023-03-26 11:44:07 +02:00
|
|
|
fn find_file_in_commit(repo: &Repository, commit: &Commit, file: &Path) -> Result<ObjectId> {
|
|
|
|
let repo_dir = repo.work_dir().context("repo has no worktree")?;
|
|
|
|
let rel_path = file.strip_prefix(repo_dir)?;
|
|
|
|
let tree = commit.tree()?;
|
|
|
|
let tree_entry = tree
|
2023-08-09 01:13:57 +02:00
|
|
|
.lookup_entry_by_path(rel_path, &mut Vec::new())?
|
2023-03-26 11:44:07 +02:00
|
|
|
.context("file is untracked")?;
|
2023-12-13 02:29:43 +01:00
|
|
|
match tree_entry.mode().kind() {
|
2022-12-01 09:35:23 +01:00
|
|
|
// not a file, everything is new, do not show diff
|
2023-12-13 02:29:43 +01:00
|
|
|
mode @ (EntryKind::Tree | EntryKind::Commit | EntryKind::Link) => {
|
2023-03-26 11:44:07 +02:00
|
|
|
bail!("entry at {} is not a file but a {mode:?}", file.display())
|
|
|
|
}
|
2022-12-01 09:35:23 +01:00
|
|
|
// found a file
|
2023-12-13 02:29:43 +01:00
|
|
|
EntryKind::Blob | EntryKind::BlobExecutable => Ok(tree_entry.object_id()),
|
2022-12-01 09:35:23 +01:00
|
|
|
}
|
|
|
|
}
|