2022-09-28 00:06:18 +00:00
|
|
|
use anyhow::Result;
|
2022-09-30 19:50:55 +00:00
|
|
|
use collections::HashMap;
|
2023-07-22 18:53:26 +00:00
|
|
|
use git2::{BranchType, StatusShow};
|
2022-09-22 23:55:24 +00:00
|
|
|
use parking_lot::Mutex;
|
2023-05-11 03:09:37 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2022-10-01 00:33:34 +00:00
|
|
|
use std::{
|
2023-05-12 15:37:07 +00:00
|
|
|
cmp::Ordering,
|
2023-05-09 17:02:58 +00:00
|
|
|
ffi::OsStr,
|
|
|
|
os::unix::prelude::OsStrExt,
|
2023-03-16 20:04:13 +00:00
|
|
|
path::{Component, Path, PathBuf},
|
2023-05-09 17:02:58 +00:00
|
|
|
sync::Arc,
|
2023-07-22 00:51:00 +00:00
|
|
|
time::SystemTime,
|
2022-10-01 00:33:34 +00:00
|
|
|
};
|
2023-05-12 15:37:07 +00:00
|
|
|
use sum_tree::{MapSeekTarget, TreeMap};
|
2023-05-03 15:51:58 +00:00
|
|
|
use util::ResultExt;
|
2022-10-01 00:33:34 +00:00
|
|
|
|
|
|
|
pub use git2::Repository as LibGitRepository;
|
2022-09-22 23:55:24 +00:00
|
|
|
|
2023-06-27 11:28:50 +00:00
|
|
|
#[derive(Clone, Debug, Hash, PartialEq)]
|
|
|
|
pub struct Branch {
|
|
|
|
pub name: Box<str>,
|
|
|
|
/// Timestamp of most recent commit, normalized to Unix Epoch format.
|
|
|
|
pub unix_timestamp: Option<i64>,
|
|
|
|
}
|
2023-10-06 20:14:53 +00:00
|
|
|
|
2022-09-28 18:42:22 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-09-30 22:25:25 +00:00
|
|
|
pub trait GitRepository: Send {
|
2022-10-05 20:28:01 +00:00
|
|
|
fn reload_index(&self);
|
|
|
|
fn load_index_text(&self, relative_file_path: &Path) -> Option<String>;
|
2023-05-03 15:51:58 +00:00
|
|
|
fn branch_name(&self) -> Option<String>;
|
2023-07-22 18:53:26 +00:00
|
|
|
|
|
|
|
/// Get the statuses of all of the files in the index that start with the given
|
|
|
|
/// path and have changes with resepect to the HEAD commit. This is fast because
|
|
|
|
/// the index stores hashes of trees, so that unchanged directories can be skipped.
|
2023-07-22 00:51:00 +00:00
|
|
|
fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus>;
|
2023-07-22 18:53:26 +00:00
|
|
|
|
|
|
|
/// Get the status of a given file in the working directory with respect to
|
|
|
|
/// the index. In the common case, when there are no changes, this only requires
|
|
|
|
/// an index lookup. The index stores the mtime of each file when it was added,
|
|
|
|
/// so there's no work to do if the mtime matches.
|
2023-07-22 00:51:00 +00:00
|
|
|
fn unstaged_status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus>;
|
2023-07-22 18:53:26 +00:00
|
|
|
|
|
|
|
/// Get the status of a given file in the working directory with respect to
|
|
|
|
/// the HEAD commit. In the common case, when there are no changes, this only
|
|
|
|
/// requires an index lookup and blob comparison between the index and the HEAD
|
|
|
|
/// commit. The index stores the mtime of each file when it was added, so there's
|
|
|
|
/// no need to consider the working directory file if the mtime matches.
|
|
|
|
fn status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus>;
|
|
|
|
|
2023-07-20 20:22:36 +00:00
|
|
|
fn branches(&self) -> Result<Vec<Branch>>;
|
|
|
|
fn change_branch(&self, _: &str) -> Result<()>;
|
|
|
|
fn create_branch(&self, _: &str) -> Result<()>;
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
2022-09-22 23:55:24 +00:00
|
|
|
|
2023-05-01 22:35:22 +00:00
|
|
|
impl std::fmt::Debug for dyn GitRepository {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("dyn GitRepository<...>").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 22:25:25 +00:00
|
|
|
impl GitRepository for LibGitRepository {
|
2022-10-05 20:28:01 +00:00
|
|
|
fn reload_index(&self) {
|
|
|
|
if let Ok(mut index) = self.index() {
|
|
|
|
_ = index.read(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_index_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-29 17:10:39 +00:00
|
|
|
const STAGE_NORMAL: i32 = 0;
|
|
|
|
let index = repo.index()?;
|
2023-03-16 20:04:13 +00:00
|
|
|
|
|
|
|
// This check is required because index.get_path() unwraps internally :(
|
|
|
|
check_path_to_repo_path_errors(relative_file_path)?;
|
|
|
|
|
|
|
|
let oid = match index.get_path(&relative_file_path, STAGE_NORMAL) {
|
2022-09-29 17:10:39 +00:00
|
|
|
Some(entry) => entry.id,
|
2022-09-28 00:06:18 +00:00
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
2022-09-29 17:10:39 +00:00
|
|
|
let content = repo.find_blob(oid)?.content().to_owned();
|
2022-10-03 19:11:06 +00:00
|
|
|
Ok(Some(String::from_utf8(content)?))
|
2022-09-28 00:06:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 22:25:25 +00:00
|
|
|
match logic(&self, 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
|
|
|
|
}
|
2023-05-03 15:51:58 +00:00
|
|
|
|
|
|
|
fn branch_name(&self) -> Option<String> {
|
|
|
|
let head = self.head().log_err()?;
|
|
|
|
let branch = String::from_utf8_lossy(head.shorthand_bytes());
|
|
|
|
Some(branch.to_string())
|
|
|
|
}
|
2023-05-09 15:36:43 +00:00
|
|
|
|
2023-07-22 00:51:00 +00:00
|
|
|
fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus> {
|
2023-05-10 15:49:30 +00:00
|
|
|
let mut map = TreeMap::default();
|
2023-07-23 00:47:36 +00:00
|
|
|
|
2023-07-22 00:08:31 +00:00
|
|
|
let mut options = git2::StatusOptions::new();
|
|
|
|
options.pathspec(path_prefix);
|
2023-07-23 00:53:58 +00:00
|
|
|
options.show(StatusShow::Index);
|
2023-07-23 00:47:36 +00:00
|
|
|
|
2023-07-22 00:08:31 +00:00
|
|
|
if let Some(statuses) = self.statuses(Some(&mut options)).log_err() {
|
2023-07-23 00:53:58 +00:00
|
|
|
for status in statuses.iter() {
|
2023-07-20 20:22:36 +00:00
|
|
|
let path = RepoPath(PathBuf::from(OsStr::from_bytes(status.path_bytes())));
|
2023-07-23 00:53:58 +00:00
|
|
|
let status = status.status();
|
|
|
|
if !status.contains(git2::Status::IGNORED) {
|
|
|
|
if let Some(status) = read_status(status) {
|
|
|
|
map.insert(path, status)
|
|
|
|
}
|
|
|
|
}
|
2023-07-20 20:22:36 +00:00
|
|
|
}
|
2023-05-09 15:36:43 +00:00
|
|
|
}
|
2023-07-20 20:22:36 +00:00
|
|
|
map
|
2023-05-09 15:36:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 00:51:00 +00:00
|
|
|
fn unstaged_status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus> {
|
2023-07-23 00:47:36 +00:00
|
|
|
// If the file has not changed since it was added to the index, then
|
|
|
|
// there can't be any changes.
|
|
|
|
if matches_index(self, path, mtime) {
|
|
|
|
return None;
|
2023-07-22 00:51:00 +00:00
|
|
|
}
|
2023-07-23 00:47:36 +00:00
|
|
|
|
|
|
|
let mut options = git2::StatusOptions::new();
|
|
|
|
options.pathspec(&path.0);
|
|
|
|
options.disable_pathspec_match(true);
|
|
|
|
options.include_untracked(true);
|
|
|
|
options.recurse_untracked_dirs(true);
|
|
|
|
options.include_unmodified(true);
|
|
|
|
options.show(StatusShow::Workdir);
|
|
|
|
|
|
|
|
let statuses = self.statuses(Some(&mut options)).log_err()?;
|
|
|
|
let status = statuses.get(0).and_then(|s| read_status(s.status()));
|
|
|
|
status
|
2023-07-22 00:51:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 18:53:26 +00:00
|
|
|
fn status(&self, path: &RepoPath, mtime: SystemTime) -> Option<GitFileStatus> {
|
|
|
|
let mut options = git2::StatusOptions::new();
|
|
|
|
options.pathspec(&path.0);
|
|
|
|
options.disable_pathspec_match(true);
|
2023-07-23 00:47:36 +00:00
|
|
|
options.include_untracked(true);
|
|
|
|
options.recurse_untracked_dirs(true);
|
|
|
|
options.include_unmodified(true);
|
2023-07-22 18:53:26 +00:00
|
|
|
|
|
|
|
// If the file has not changed since it was added to the index, then
|
|
|
|
// there's no need to examine the working directory file: just compare
|
|
|
|
// the blob in the index to the one in the HEAD commit.
|
|
|
|
if matches_index(self, path, mtime) {
|
|
|
|
options.show(StatusShow::Index);
|
2023-06-05 22:19:59 +00:00
|
|
|
}
|
2023-07-22 18:53:26 +00:00
|
|
|
|
|
|
|
let statuses = self.statuses(Some(&mut options)).log_err()?;
|
|
|
|
let status = statuses.get(0).and_then(|s| read_status(s.status()));
|
|
|
|
status
|
2023-05-10 16:55:10 +00:00
|
|
|
}
|
2023-07-22 00:05:42 +00:00
|
|
|
|
2023-06-27 11:28:50 +00:00
|
|
|
fn branches(&self) -> Result<Vec<Branch>> {
|
2023-06-27 10:02:30 +00:00
|
|
|
let local_branches = self.branches(Some(BranchType::Local))?;
|
|
|
|
let valid_branches = local_branches
|
|
|
|
.filter_map(|branch| {
|
2023-06-27 11:28:50 +00:00
|
|
|
branch.ok().and_then(|(branch, _)| {
|
|
|
|
let name = branch.name().ok().flatten().map(Box::from)?;
|
2023-06-27 12:20:36 +00:00
|
|
|
let timestamp = branch.get().peel_to_commit().ok()?.time();
|
|
|
|
let unix_timestamp = timestamp.seconds();
|
|
|
|
let timezone_offset = timestamp.offset_minutes();
|
|
|
|
let utc_offset =
|
|
|
|
time::UtcOffset::from_whole_seconds(timezone_offset * 60).ok()?;
|
|
|
|
let unix_timestamp =
|
|
|
|
time::OffsetDateTime::from_unix_timestamp(unix_timestamp).ok()?;
|
2023-06-27 11:28:50 +00:00
|
|
|
Some(Branch {
|
|
|
|
name,
|
2023-06-27 12:20:36 +00:00
|
|
|
unix_timestamp: Some(unix_timestamp.to_offset(utc_offset).unix_timestamp()),
|
2023-06-27 11:28:50 +00:00
|
|
|
})
|
|
|
|
})
|
2023-06-27 10:02:30 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Ok(valid_branches)
|
|
|
|
}
|
|
|
|
fn change_branch(&self, name: &str) -> Result<()> {
|
|
|
|
let revision = self.find_branch(name, BranchType::Local)?;
|
|
|
|
let revision = revision.get();
|
|
|
|
let as_tree = revision.peel_to_tree()?;
|
|
|
|
self.checkout_tree(as_tree.as_object(), None)?;
|
|
|
|
self.set_head(
|
|
|
|
revision
|
|
|
|
.name()
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Branch name could not be retrieved"))?,
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-07 16:36:55 +00:00
|
|
|
fn create_branch(&self, name: &str) -> Result<()> {
|
|
|
|
let current_commit = self.head()?.peel_to_commit()?;
|
|
|
|
self.branch(name, ¤t_commit, false)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-10 16:55:10 +00:00
|
|
|
}
|
2023-05-09 15:36:43 +00:00
|
|
|
|
2023-07-22 18:53:26 +00:00
|
|
|
fn matches_index(repo: &LibGitRepository, path: &RepoPath, mtime: SystemTime) -> bool {
|
|
|
|
if let Some(index) = repo.index().log_err() {
|
|
|
|
if let Some(entry) = index.get_path(&path, 0) {
|
|
|
|
if let Some(mtime) = mtime.duration_since(SystemTime::UNIX_EPOCH).log_err() {
|
|
|
|
if entry.mtime.seconds() == mtime.as_secs() as i32
|
|
|
|
&& entry.mtime.nanoseconds() == mtime.subsec_nanos()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2023-05-10 16:55:10 +00:00
|
|
|
fn read_status(status: git2::Status) -> Option<GitFileStatus> {
|
|
|
|
if status.contains(git2::Status::CONFLICTED) {
|
|
|
|
Some(GitFileStatus::Conflict)
|
2023-05-15 20:40:55 +00:00
|
|
|
} else if status.intersects(
|
|
|
|
git2::Status::WT_MODIFIED
|
|
|
|
| git2::Status::WT_RENAMED
|
|
|
|
| git2::Status::INDEX_MODIFIED
|
|
|
|
| git2::Status::INDEX_RENAMED,
|
|
|
|
) {
|
2023-05-10 16:55:10 +00:00
|
|
|
Some(GitFileStatus::Modified)
|
2023-05-15 19:00:12 +00:00
|
|
|
} else if status.intersects(git2::Status::WT_NEW | git2::Status::INDEX_NEW) {
|
2023-05-10 16:55:10 +00:00
|
|
|
Some(GitFileStatus::Added)
|
|
|
|
} else {
|
|
|
|
None
|
2023-05-09 15:36:43 +00:00
|
|
|
}
|
2022-09-22 23:55:24 +00:00
|
|
|
}
|
2022-09-28 18:42:22 +00:00
|
|
|
|
2022-10-01 00:33:34 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2022-09-28 18:42:22 +00:00
|
|
|
pub struct FakeGitRepository {
|
2022-09-30 19:50:55 +00:00
|
|
|
state: Arc<Mutex<FakeGitRepositoryState>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct FakeGitRepositoryState {
|
|
|
|
pub index_contents: HashMap<PathBuf, String>,
|
2023-05-10 16:55:10 +00:00
|
|
|
pub worktree_statuses: HashMap<RepoPath, GitFileStatus>,
|
2023-05-04 13:41:11 +00:00
|
|
|
pub branch_name: Option<String>,
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FakeGitRepository {
|
2022-10-01 00:33:34 +00:00
|
|
|
pub fn open(state: Arc<Mutex<FakeGitRepositoryState>>) -> Arc<Mutex<dyn GitRepository>> {
|
|
|
|
Arc::new(Mutex::new(FakeGitRepository { state }))
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl GitRepository for FakeGitRepository {
|
2022-10-05 20:28:01 +00:00
|
|
|
fn reload_index(&self) {}
|
|
|
|
|
|
|
|
fn load_index_text(&self, path: &Path) -> Option<String> {
|
2022-09-30 19:50:55 +00:00
|
|
|
let state = self.state.lock();
|
|
|
|
state.index_contents.get(path).cloned()
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
2023-05-03 15:51:58 +00:00
|
|
|
|
|
|
|
fn branch_name(&self) -> Option<String> {
|
2023-05-04 13:41:11 +00:00
|
|
|
let state = self.state.lock();
|
|
|
|
state.branch_name.clone()
|
2023-05-03 15:51:58 +00:00
|
|
|
}
|
2023-05-09 15:36:43 +00:00
|
|
|
|
2023-07-22 00:51:00 +00:00
|
|
|
fn staged_statuses(&self, path_prefix: &Path) -> TreeMap<RepoPath, GitFileStatus> {
|
2023-05-10 15:49:30 +00:00
|
|
|
let mut map = TreeMap::default();
|
2023-07-20 20:22:36 +00:00
|
|
|
let state = self.state.lock();
|
2023-05-10 16:55:10 +00:00
|
|
|
for (repo_path, status) in state.worktree_statuses.iter() {
|
2023-07-22 00:08:31 +00:00
|
|
|
if repo_path.0.starts_with(path_prefix) {
|
|
|
|
map.insert(repo_path.to_owned(), status.to_owned());
|
|
|
|
}
|
2023-05-10 02:29:45 +00:00
|
|
|
}
|
2023-07-20 20:22:36 +00:00
|
|
|
map
|
2023-05-09 17:02:58 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 00:51:00 +00:00
|
|
|
fn unstaged_status(&self, _path: &RepoPath, _mtime: SystemTime) -> Option<GitFileStatus> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-07-22 18:53:26 +00:00
|
|
|
fn status(&self, path: &RepoPath, _mtime: SystemTime) -> Option<GitFileStatus> {
|
2023-05-10 02:29:45 +00:00
|
|
|
let state = self.state.lock();
|
2023-07-22 18:53:26 +00:00
|
|
|
state.worktree_statuses.get(path).cloned()
|
2023-05-09 15:36:43 +00:00
|
|
|
}
|
2023-07-20 20:22:36 +00:00
|
|
|
|
|
|
|
fn branches(&self) -> Result<Vec<Branch>> {
|
|
|
|
Ok(vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change_branch(&self, name: &str) -> Result<()> {
|
|
|
|
let mut state = self.state.lock();
|
|
|
|
state.branch_name = Some(name.to_owned());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_branch(&self, name: &str) -> Result<()> {
|
|
|
|
let mut state = self.state.lock();
|
|
|
|
state.branch_name = Some(name.to_owned());
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-09-28 18:42:22 +00:00
|
|
|
}
|
2023-03-16 20:04:13 +00:00
|
|
|
|
|
|
|
fn check_path_to_repo_path_errors(relative_file_path: &Path) -> Result<()> {
|
|
|
|
match relative_file_path.components().next() {
|
|
|
|
None => anyhow::bail!("repo path should not be empty"),
|
|
|
|
Some(Component::Prefix(_)) => anyhow::bail!(
|
|
|
|
"repo path `{}` should be relative, not a windows prefix",
|
|
|
|
relative_file_path.to_string_lossy()
|
|
|
|
),
|
|
|
|
Some(Component::RootDir) => {
|
|
|
|
anyhow::bail!(
|
|
|
|
"repo path `{}` should be relative",
|
|
|
|
relative_file_path.to_string_lossy()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Some(Component::CurDir) => {
|
|
|
|
anyhow::bail!(
|
|
|
|
"repo path `{}` should not start with `.`",
|
|
|
|
relative_file_path.to_string_lossy()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Some(Component::ParentDir) => {
|
|
|
|
anyhow::bail!(
|
|
|
|
"repo path `{}` should not start with `..`",
|
|
|
|
relative_file_path.to_string_lossy()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
2023-05-09 17:02:58 +00:00
|
|
|
|
2023-05-11 19:01:42 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
2023-05-10 16:55:10 +00:00
|
|
|
pub enum GitFileStatus {
|
2023-05-09 17:02:58 +00:00
|
|
|
Added,
|
|
|
|
Modified,
|
|
|
|
Conflict,
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:02:59 +00:00
|
|
|
impl GitFileStatus {
|
2023-06-01 23:51:34 +00:00
|
|
|
pub fn merge(
|
|
|
|
this: Option<GitFileStatus>,
|
|
|
|
other: Option<GitFileStatus>,
|
2023-06-02 21:51:40 +00:00
|
|
|
prefer_other: bool,
|
2023-06-01 23:51:34 +00:00
|
|
|
) -> Option<GitFileStatus> {
|
2023-06-02 21:51:40 +00:00
|
|
|
if prefer_other {
|
|
|
|
return other;
|
|
|
|
} else {
|
|
|
|
match (this, other) {
|
|
|
|
(Some(GitFileStatus::Conflict), _) | (_, Some(GitFileStatus::Conflict)) => {
|
|
|
|
Some(GitFileStatus::Conflict)
|
|
|
|
}
|
|
|
|
(Some(GitFileStatus::Modified), _) | (_, Some(GitFileStatus::Modified)) => {
|
|
|
|
Some(GitFileStatus::Modified)
|
|
|
|
}
|
|
|
|
(Some(GitFileStatus::Added), _) | (_, Some(GitFileStatus::Added)) => {
|
|
|
|
Some(GitFileStatus::Added)
|
|
|
|
}
|
|
|
|
_ => None,
|
2023-06-01 23:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:02:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:02:58 +00:00
|
|
|
#[derive(Clone, Debug, Ord, Hash, PartialOrd, Eq, PartialEq)]
|
2023-06-02 06:27:49 +00:00
|
|
|
pub struct RepoPath(pub PathBuf);
|
2023-05-09 17:02:58 +00:00
|
|
|
|
|
|
|
impl RepoPath {
|
2023-05-10 23:07:41 +00:00
|
|
|
pub fn new(path: PathBuf) -> Self {
|
2023-05-09 17:02:58 +00:00
|
|
|
debug_assert!(path.is_relative(), "Repo paths must be relative");
|
|
|
|
|
|
|
|
RepoPath(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&Path> for RepoPath {
|
|
|
|
fn from(value: &Path) -> Self {
|
|
|
|
RepoPath::new(value.to_path_buf())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PathBuf> for RepoPath {
|
|
|
|
fn from(value: PathBuf) -> Self {
|
|
|
|
RepoPath::new(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RepoPath {
|
|
|
|
fn default() -> Self {
|
|
|
|
RepoPath(PathBuf::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<Path> for RepoPath {
|
|
|
|
fn as_ref(&self) -> &Path {
|
|
|
|
self.0.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Deref for RepoPath {
|
|
|
|
type Target = PathBuf;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 15:37:07 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RepoPathDescendants<'a>(pub &'a Path);
|
|
|
|
|
|
|
|
impl<'a> MapSeekTarget<RepoPath> for RepoPathDescendants<'a> {
|
|
|
|
fn cmp_cursor(&self, key: &RepoPath) -> Ordering {
|
|
|
|
if key.starts_with(&self.0) {
|
|
|
|
Ordering::Greater
|
|
|
|
} else {
|
|
|
|
self.0.cmp(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|