|
|
@ -17,191 +17,174 @@ use gix::status::{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use gix::{Commit, ObjectId, Repository, ThreadSafeRepository};
|
|
|
|
use gix::{Commit, ObjectId, Repository, ThreadSafeRepository};
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{DiffProvider, FileChange};
|
|
|
|
use crate::FileChange;
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
|
|
|
mod test;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub fn get_diff_base(file: &Path) -> Result<Vec<u8>> {
|
|
|
|
pub struct Git;
|
|
|
|
debug_assert!(!file.exists() || file.is_file());
|
|
|
|
|
|
|
|
debug_assert!(file.is_absolute());
|
|
|
|
impl Git {
|
|
|
|
|
|
|
|
fn open_repo(path: &Path, ceiling_dir: Option<&Path>) -> Result<ThreadSafeRepository> {
|
|
|
|
// TODO cache repository lookup
|
|
|
|
// custom open options
|
|
|
|
|
|
|
|
let mut git_open_opts_map = gix::sec::trust::Mapping::<gix::open::Options>::default();
|
|
|
|
let repo_dir = file.parent().context("file has no parent directory")?;
|
|
|
|
|
|
|
|
let repo = open_repo(repo_dir, None)
|
|
|
|
// On windows various configuration options are bundled as part of the installations
|
|
|
|
.context("failed to open git repo")?
|
|
|
|
// This path depends on the install location of git and therefore requires some overhead to lookup
|
|
|
|
.to_thread_local();
|
|
|
|
// This is basically only used on windows and has some overhead hence it's disabled on other platforms.
|
|
|
|
let head = repo.head_commit()?;
|
|
|
|
// `gitoxide` doesn't use this as default
|
|
|
|
let file_oid = find_file_in_commit(&repo, &head, file)?;
|
|
|
|
let config = gix::open::permissions::Config {
|
|
|
|
|
|
|
|
system: true,
|
|
|
|
let file_object = repo.find_object(file_oid)?;
|
|
|
|
git: true,
|
|
|
|
let data = file_object.detach().data;
|
|
|
|
user: true,
|
|
|
|
// Get the actual data that git would make out of the git object.
|
|
|
|
env: true,
|
|
|
|
// This will apply the user's git config or attributes like crlf conversions.
|
|
|
|
includes: true,
|
|
|
|
if let Some(work_dir) = repo.work_dir() {
|
|
|
|
git_binary: cfg!(windows),
|
|
|
|
let rela_path = file.strip_prefix(work_dir)?;
|
|
|
|
};
|
|
|
|
let rela_path = gix::path::try_into_bstr(rela_path)?;
|
|
|
|
// change options for config permissions without touching anything else
|
|
|
|
let (mut pipeline, _) = repo.filter_pipeline(None)?;
|
|
|
|
git_open_opts_map.reduced = git_open_opts_map
|
|
|
|
let mut worktree_outcome =
|
|
|
|
.reduced
|
|
|
|
pipeline.convert_to_worktree(&data, rela_path.as_ref(), Delay::Forbid)?;
|
|
|
|
.permissions(gix::open::Permissions {
|
|
|
|
let mut buf = Vec::with_capacity(data.len());
|
|
|
|
config,
|
|
|
|
worktree_outcome.read_to_end(&mut buf)?;
|
|
|
|
..gix::open::Permissions::default_for_level(gix::sec::Trust::Reduced)
|
|
|
|
Ok(buf)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
git_open_opts_map.full = git_open_opts_map.full.permissions(gix::open::Permissions {
|
|
|
|
Ok(data)
|
|
|
|
config,
|
|
|
|
}
|
|
|
|
..gix::open::Permissions::default_for_level(gix::sec::Trust::Full)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let open_options = gix::discover::upwards::Options {
|
|
|
|
pub fn get_current_head_name(file: &Path) -> Result<Arc<ArcSwap<Box<str>>>> {
|
|
|
|
ceiling_dirs: ceiling_dir
|
|
|
|
debug_assert!(!file.exists() || file.is_file());
|
|
|
|
.map(|dir| vec![dir.to_owned()])
|
|
|
|
debug_assert!(file.is_absolute());
|
|
|
|
.unwrap_or_default(),
|
|
|
|
let repo_dir = file.parent().context("file has no parent directory")?;
|
|
|
|
dot_git_only: true,
|
|
|
|
let repo = open_repo(repo_dir, None)
|
|
|
|
..Default::default()
|
|
|
|
.context("failed to open git repo")?
|
|
|
|
};
|
|
|
|
.to_thread_local();
|
|
|
|
|
|
|
|
let head_ref = repo.head_ref()?;
|
|
|
|
|
|
|
|
let head_commit = repo.head_commit()?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let name = match head_ref {
|
|
|
|
|
|
|
|
Some(reference) => reference.name().shorten().to_string(),
|
|
|
|
|
|
|
|
None => head_commit.id.to_hex_with_len(8).to_string(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Arc::new(ArcSwap::from_pointee(name.into_boxed_str())))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let res = ThreadSafeRepository::discover_with_environment_overrides_opts(
|
|
|
|
pub fn for_each_changed_file(cwd: &Path, f: impl Fn(Result<FileChange>) -> bool) -> Result<()> {
|
|
|
|
path,
|
|
|
|
status(&open_repo(cwd, None)?.to_thread_local(), f)
|
|
|
|
open_options,
|
|
|
|
}
|
|
|
|
git_open_opts_map,
|
|
|
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
fn open_repo(path: &Path, ceiling_dir: Option<&Path>) -> Result<ThreadSafeRepository> {
|
|
|
|
}
|
|
|
|
// custom open options
|
|
|
|
|
|
|
|
let mut git_open_opts_map = gix::sec::trust::Mapping::<gix::open::Options>::default();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
let config = gix::open::permissions::Config {
|
|
|
|
|
|
|
|
system: true,
|
|
|
|
|
|
|
|
git: true,
|
|
|
|
|
|
|
|
user: true,
|
|
|
|
|
|
|
|
env: true,
|
|
|
|
|
|
|
|
includes: true,
|
|
|
|
|
|
|
|
git_binary: cfg!(windows),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// change options for config permissions without touching anything else
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
config,
|
|
|
|
|
|
|
|
..gix::open::Permissions::default_for_level(gix::sec::Trust::Full)
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let res = ThreadSafeRepository::discover_with_environment_overrides_opts(
|
|
|
|
|
|
|
|
path,
|
|
|
|
|
|
|
|
open_options,
|
|
|
|
|
|
|
|
git_open_opts_map,
|
|
|
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Emulates the result of running `git status` from the command line.
|
|
|
|
/// Emulates the result of running `git status` from the command line.
|
|
|
|
fn status(repo: &Repository, f: impl Fn(Result<FileChange>) -> bool) -> Result<()> {
|
|
|
|
fn status(repo: &Repository, f: impl Fn(Result<FileChange>) -> bool) -> Result<()> {
|
|
|
|
let work_dir = repo
|
|
|
|
let work_dir = repo
|
|
|
|
.work_dir()
|
|
|
|
.work_dir()
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("working tree not found"))?
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("working tree not found"))?
|
|
|
|
.to_path_buf();
|
|
|
|
.to_path_buf();
|
|
|
|
|
|
|
|
|
|
|
|
let status_platform = repo
|
|
|
|
let status_platform = repo
|
|
|
|
.status(gix::progress::Discard)?
|
|
|
|
.status(gix::progress::Discard)?
|
|
|
|
// Here we discard the `status.showUntrackedFiles` config, as it makes little sense in
|
|
|
|
// Here we discard the `status.showUntrackedFiles` config, as it makes little sense in
|
|
|
|
// our case to not list new (untracked) files. We could have respected this config
|
|
|
|
// our case to not list new (untracked) files. We could have respected this config
|
|
|
|
// if the default value weren't `Collapsed` though, as this default value would render
|
|
|
|
// if the default value weren't `Collapsed` though, as this default value would render
|
|
|
|
// the feature unusable to many.
|
|
|
|
// the feature unusable to many.
|
|
|
|
.untracked_files(UntrackedFiles::Files)
|
|
|
|
.untracked_files(UntrackedFiles::Files)
|
|
|
|
// Turn on file rename detection, which is off by default.
|
|
|
|
// Turn on file rename detection, which is off by default.
|
|
|
|
.index_worktree_rewrites(Some(Rewrites {
|
|
|
|
.index_worktree_rewrites(Some(Rewrites {
|
|
|
|
copies: None,
|
|
|
|
copies: None,
|
|
|
|
percentage: Some(0.5),
|
|
|
|
percentage: Some(0.5),
|
|
|
|
limit: 1000,
|
|
|
|
limit: 1000,
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
// No filtering based on path
|
|
|
|
// No filtering based on path
|
|
|
|
let empty_patterns = vec![];
|
|
|
|
let empty_patterns = vec![];
|
|
|
|
|
|
|
|
|
|
|
|
let status_iter = status_platform.into_index_worktree_iter(empty_patterns)?;
|
|
|
|
let status_iter = status_platform.into_index_worktree_iter(empty_patterns)?;
|
|
|
|
|
|
|
|
|
|
|
|
for item in status_iter {
|
|
|
|
for item in status_iter {
|
|
|
|
let Ok(item) = item.map_err(|err| f(Err(err.into()))) else {
|
|
|
|
let Ok(item) = item.map_err(|err| f(Err(err.into()))) else {
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let change = match item {
|
|
|
|
let change = match item {
|
|
|
|
Item::Modification {
|
|
|
|
Item::Modification {
|
|
|
|
rela_path, status, ..
|
|
|
|
rela_path, status, ..
|
|
|
|
} => {
|
|
|
|
} => {
|
|
|
|
let path = work_dir.join(rela_path.to_path()?);
|
|
|
|
let path = work_dir.join(rela_path.to_path()?);
|
|
|
|
match status {
|
|
|
|
match status {
|
|
|
|
EntryStatus::Conflict(_) => FileChange::Conflict { path },
|
|
|
|
EntryStatus::Conflict(_) => FileChange::Conflict { path },
|
|
|
|
EntryStatus::Change(Change::Removed) => FileChange::Deleted { path },
|
|
|
|
EntryStatus::Change(Change::Removed) => FileChange::Deleted { path },
|
|
|
|
EntryStatus::Change(Change::Modification { .. }) => {
|
|
|
|
EntryStatus::Change(Change::Modification { .. }) => {
|
|
|
|
FileChange::Modified { path }
|
|
|
|
FileChange::Modified { path }
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => continue,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Item::DirectoryContents { entry, .. } if entry.status == Status::Untracked => {
|
|
|
|
}
|
|
|
|
FileChange::Untracked {
|
|
|
|
Item::DirectoryContents { entry, .. } if entry.status == Status::Untracked => {
|
|
|
|
path: work_dir.join(entry.rela_path.to_path()?),
|
|
|
|
FileChange::Untracked {
|
|
|
|
}
|
|
|
|
path: work_dir.join(entry.rela_path.to_path()?),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Item::Rewrite {
|
|
|
|
|
|
|
|
source,
|
|
|
|
|
|
|
|
dirwalk_entry,
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
} => FileChange::Renamed {
|
|
|
|
|
|
|
|
from_path: work_dir.join(source.rela_path().to_path()?),
|
|
|
|
|
|
|
|
to_path: work_dir.join(dirwalk_entry.rela_path.to_path()?),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => continue,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if !f(Ok(change)) {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Item::Rewrite {
|
|
|
|
|
|
|
|
source,
|
|
|
|
Ok(())
|
|
|
|
dirwalk_entry,
|
|
|
|
}
|
|
|
|
..
|
|
|
|
}
|
|
|
|
} => FileChange::Renamed {
|
|
|
|
|
|
|
|
from_path: work_dir.join(source.rela_path().to_path()?),
|
|
|
|
impl Git {
|
|
|
|
to_path: work_dir.join(dirwalk_entry.rela_path.to_path()?),
|
|
|
|
pub fn get_diff_base(&self, file: &Path) -> Result<Vec<u8>> {
|
|
|
|
},
|
|
|
|
debug_assert!(!file.exists() || file.is_file());
|
|
|
|
_ => continue,
|
|
|
|
debug_assert!(file.is_absolute());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO cache repository lookup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()?;
|
|
|
|
|
|
|
|
let file_oid = find_file_in_commit(&repo, &head, file)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let file_object = repo.find_object(file_oid)?;
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_current_head_name(&self, file: &Path) -> Result<Arc<ArcSwap<Box<str>>>> {
|
|
|
|
|
|
|
|
debug_assert!(!file.exists() || file.is_file());
|
|
|
|
|
|
|
|
debug_assert!(file.is_absolute());
|
|
|
|
|
|
|
|
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()?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let name = match head_ref {
|
|
|
|
|
|
|
|
Some(reference) => reference.name().shorten().to_string(),
|
|
|
|
|
|
|
|
None => head_commit.id.to_hex_with_len(8).to_string(),
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
if !f(Ok(change)) {
|
|
|
|
Ok(Arc::new(ArcSwap::from_pointee(name.into_boxed_str())))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn for_each_changed_file(
|
|
|
|
|
|
|
|
&self,
|
|
|
|
|
|
|
|
cwd: &Path,
|
|
|
|
|
|
|
|
f: impl Fn(Result<FileChange>) -> bool,
|
|
|
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
|
|
|
Self::status(&Self::open_repo(cwd, None)?.to_thread_local(), f)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl From<Git> for DiffProvider {
|
|
|
|
Ok(())
|
|
|
|
fn from(value: Git) -> Self {
|
|
|
|
|
|
|
|
DiffProvider::Git(value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Finds the object that contains the contents of a file at a specific commit.
|
|
|
|
/// Finds the object that contains the contents of a file at a specific commit.
|
|
|
|