chore: upgrade gpui-component to 0.7 and gix to 0.88
This commit is contained in:
@@ -29,8 +29,8 @@ impl GitCache {
|
||||
let path = self.repo_path(addr);
|
||||
match gix::open(&path) {
|
||||
Ok(repo) => Ok(Some(Repo { inner: repo })),
|
||||
Err(gix::open::Error::NotARepository { .. }) => Ok(None),
|
||||
Err(gix::open::Error::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
|
||||
// A missing path and a directory that is no repository are both classified `NotFound`.
|
||||
Err(e) if e.is_not_found() => Ok(None),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use anyhow::Result;
|
||||
use gix::diff::blob::unified_diff::{ConsumeHunk, DiffLineKind as GixLineKind, HunkHeader};
|
||||
|
||||
use crate::GixResultExt as _;
|
||||
use crate::repo::Repo;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -164,7 +165,7 @@ impl Repo {
|
||||
.resource_cache
|
||||
.options
|
||||
.skip_internal_diff_if_external_is_configured = true;
|
||||
let outcome = platform.resource_cache.prepare_diff()?;
|
||||
let outcome = platform.resource_cache.prepare_diff().into_anyhow()?;
|
||||
|
||||
let (binary, hunks, insertions, deletions) = match outcome.operation {
|
||||
Operation::InternalDiff { algorithm } => {
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::GixResultExt as _;
|
||||
use crate::repo::Repo;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -29,8 +30,8 @@ impl FileCommit {
|
||||
commit: &gix::Commit<'_>,
|
||||
include_description: bool,
|
||||
) -> Result<FileCommit> {
|
||||
let author = commit.author()?;
|
||||
let message = commit.message()?;
|
||||
let author = commit.author().into_anyhow()?;
|
||||
let message = commit.message().into_anyhow()?;
|
||||
|
||||
Ok(FileCommit {
|
||||
id: commit.id().shorten_or_id().to_string(),
|
||||
@@ -44,7 +45,7 @@ impl FileCommit {
|
||||
None
|
||||
},
|
||||
author: String::from_utf8_lossy(author.name).trim().to_string(),
|
||||
time: author.time()?.seconds,
|
||||
time: author.time().into_anyhow()?.seconds,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,19 @@ pub use repo::{Repo, RepoRefState};
|
||||
pub use scan::{LocalRepo, find_git_repos};
|
||||
pub use worktree::WorktreeSnapshot;
|
||||
|
||||
pub(crate) trait GixResultExt<T> {
|
||||
fn into_anyhow(self) -> anyhow::Result<T>;
|
||||
}
|
||||
|
||||
impl<T, E> GixResultExt<T> for Result<T, gix::Exn<E>>
|
||||
where
|
||||
E: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
fn into_anyhow(self) -> anyhow::Result<T> {
|
||||
self.map_err(|exn| anyhow::Error::from(exn.into_error()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn git_in(dir: &std::path::Path, args: &[&str]) -> anyhow::Result<String> {
|
||||
let output = Repo::run_git(dir, args, "git")?;
|
||||
|
||||
@@ -2,6 +2,7 @@ use anyhow::Result;
|
||||
use gix::bstr::ByteSlice;
|
||||
use nostr::prelude::*;
|
||||
|
||||
use crate::GixResultExt as _;
|
||||
use crate::repo::Repo;
|
||||
|
||||
/// The kind of NIP-34 relationship a local repository has on disk.
|
||||
@@ -186,7 +187,7 @@ impl Repo {
|
||||
|
||||
pub fn set_nostr_repo(&self, naddr: &str) -> Result<()> {
|
||||
self.edit_local_config(|config| {
|
||||
config.set_raw_value("nostr.repo", naddr)?;
|
||||
config.set_raw_value("nostr.repo", naddr).into_anyhow()?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ use crate::repo::Repo;
|
||||
pub struct PatchParser;
|
||||
|
||||
impl PatchParser {
|
||||
// A malformed input yields one element covering it.
|
||||
pub fn split_patch_series(patch: &str) -> Vec<&str> {
|
||||
Self::envelopes(patch)
|
||||
.into_iter()
|
||||
@@ -20,8 +19,6 @@ impl PatchParser {
|
||||
.collect()
|
||||
}
|
||||
|
||||
// Backed by `diffy::patch_set`, which implements git's extended diff format:
|
||||
// rename and copy detection, binary detection, quoted/escaped paths.
|
||||
pub fn patch_diffs(patch: &str) -> Result<CommitDiff> {
|
||||
if !patch.lines().any(|line| line.starts_with("diff --git ")) {
|
||||
return Ok(CommitDiff { files: Vec::new() });
|
||||
@@ -36,8 +33,6 @@ impl PatchParser {
|
||||
Ok(CommitDiff { files })
|
||||
}
|
||||
|
||||
// Entries appear in patch order, oldest first as `git format-patch`
|
||||
// produces them.
|
||||
pub fn patch_commits(patch: &str) -> Vec<FileCommit> {
|
||||
Self::envelopes(patch)
|
||||
.into_iter()
|
||||
@@ -62,8 +57,6 @@ impl PatchParser {
|
||||
.collect()
|
||||
}
|
||||
|
||||
// The one parser `split_patch_series` and `patch_commits` share.
|
||||
// A malformed input yields one message covering the whole input.
|
||||
fn envelopes(patch: &str) -> Vec<Envelope<'_>> {
|
||||
let mut messages: Vec<Envelope<'_>> = Vec::new();
|
||||
let mut current: Option<(usize, &str, Vec<&str>)> = None;
|
||||
@@ -136,7 +129,7 @@ impl PatchParser {
|
||||
}
|
||||
}
|
||||
|
||||
// Matches `[PATCH]`, `[PATCH 1/2]`, `[RFC PATCH]`, etc.
|
||||
/// Matches `[PATCH]`, `[PATCH 1/2]`, `[RFC PATCH]`, etc.
|
||||
fn strip_patch_prefix(subject: &str) -> String {
|
||||
let trimmed = subject.trim();
|
||||
let Some(rest) = trimmed.strip_prefix('[') else {
|
||||
@@ -153,9 +146,6 @@ impl PatchParser {
|
||||
}
|
||||
|
||||
fn file_diff(file: FilePatch<'_, str>) -> Result<FileDiff> {
|
||||
// The `---`/`+++` paths carry the `a/`/`b/` prefix, dropped the same way
|
||||
// `git apply -p1` does; rename and copy paths come from their own
|
||||
// headers, unprefixed.
|
||||
let stripped;
|
||||
let operation = match file.operation() {
|
||||
operation @ (FileOperation::Rename { .. } | FileOperation::Copy { .. }) => operation,
|
||||
@@ -213,8 +203,6 @@ impl PatchParser {
|
||||
})
|
||||
}
|
||||
|
||||
// `diffy` reports only the hunk header ranges; the per-line numbers are
|
||||
// counted from them the way the header encodes them.
|
||||
fn hunk_diff(hunk: &Hunk<'_, str>) -> DiffHunk {
|
||||
let old_range = hunk.old_range();
|
||||
let new_range = hunk.new_range();
|
||||
@@ -272,7 +260,7 @@ impl PatchParser {
|
||||
}
|
||||
}
|
||||
|
||||
// A `git format-patch` mbox message, split on its `From <40-hex> <date>` envelope.
|
||||
/// A `git format-patch` mbox message, split on its `From <40-hex> <date>` envelope.
|
||||
struct Envelope<'a> {
|
||||
text: &'a str,
|
||||
id: &'a str,
|
||||
@@ -290,8 +278,6 @@ impl Envelope<'_> {
|
||||
}
|
||||
|
||||
impl Repo {
|
||||
// The git CLI handles the mbox format natively.
|
||||
// TODO: replace with a pure-Rust implementation later without changing callers.
|
||||
pub fn apply_patch(&self, patch: &str) -> Result<()> {
|
||||
let workdir = self
|
||||
.inner
|
||||
|
||||
@@ -6,6 +6,7 @@ use anyhow::{Context, Result, bail};
|
||||
use gix::interrupt::IS_INTERRUPTED;
|
||||
use gix::progress::Discard;
|
||||
|
||||
use crate::GixResultExt as _;
|
||||
use crate::repo::Repo;
|
||||
|
||||
impl Repo {
|
||||
@@ -15,7 +16,8 @@ impl Repo {
|
||||
gix::refspec::parse(
|
||||
gix::bstr::BStr::new("+refs/nostr/*:refs/nostr/*"),
|
||||
gix::refspec::parse::Operation::Fetch,
|
||||
)?
|
||||
)
|
||||
.into_anyhow()?
|
||||
.to_owned(),
|
||||
],
|
||||
..Default::default()
|
||||
@@ -87,10 +89,6 @@ impl Repo {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// The convergence probe for a push that lost the compare-and-swap race to
|
||||
// the grasp server's own background ref alignment. Extra advertised refs
|
||||
// are ignored: the question is whether the pushed data is already there,
|
||||
// not whether the remote is an exact mirror.
|
||||
pub fn remote_has_refs(&self, url: &str, expected: &[(String, String)]) -> Result<bool> {
|
||||
if expected.is_empty() {
|
||||
return Ok(true);
|
||||
@@ -98,8 +96,6 @@ impl Repo {
|
||||
|
||||
let url = Self::transport_url(url);
|
||||
|
||||
// A URL-created remote has no configured fetch refspecs, and `ref_map`
|
||||
// only keeps refs matching one; match each expected ref by exact name.
|
||||
let refspecs = expected
|
||||
.iter()
|
||||
.map(|(name, _)| {
|
||||
@@ -110,6 +106,7 @@ impl Repo {
|
||||
.map(|spec| spec.to_owned())
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.into_anyhow()
|
||||
.context("invalid refspec")?;
|
||||
|
||||
let options = gix::remote::ref_map::Options {
|
||||
@@ -126,8 +123,8 @@ impl Repo {
|
||||
.ref_map(Discard, options)
|
||||
.with_context(|| format!("listing refs of {url} failed"))?;
|
||||
|
||||
// Peeled tag entries carry the tag object in their direct oid, matching
|
||||
// `git ls-remote` while skipping the duplicated `^{}` lines.
|
||||
// Peeled tag entries carry the tag object in their direct oid,
|
||||
// matching `git ls-remote` while skipping the duplicated `^{}` lines.
|
||||
let advertised: HashMap<String, String> = refs
|
||||
.remote_refs
|
||||
.iter()
|
||||
@@ -149,23 +146,30 @@ impl Repo {
|
||||
|
||||
// `git remote add` also configures the default fetch refspec.
|
||||
self.edit_local_config(|config| {
|
||||
config.set_raw_value("remote.origin.url", url)?;
|
||||
config.set_raw_value("remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")?;
|
||||
config
|
||||
.set_raw_value("remote.origin.url", url)
|
||||
.into_anyhow()?;
|
||||
config
|
||||
.set_raw_value("remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
|
||||
.into_anyhow()?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
// A working copy cloned from a local mirror is re-targeted at the
|
||||
// grasp server; a pre-existing fetch refspec is left untouched.
|
||||
// A working copy cloned from a local mirror is re-targeted at the grasp server,
|
||||
// a pre-existing fetch refspec is left untouched.
|
||||
pub fn set_origin(&self, url: &str) -> Result<()> {
|
||||
let had_origin = self.inner.find_remote("origin").is_ok();
|
||||
|
||||
self.edit_local_config(|config| {
|
||||
config.set_raw_value("remote.origin.url", url)?;
|
||||
config
|
||||
.set_raw_value("remote.origin.url", url)
|
||||
.into_anyhow()?;
|
||||
|
||||
if !had_origin {
|
||||
config
|
||||
.set_raw_value("remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")?;
|
||||
.set_raw_value("remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
|
||||
.into_anyhow()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -182,13 +186,12 @@ impl Repo {
|
||||
.map(|url| url.to_string()))
|
||||
}
|
||||
|
||||
// Never touches the checked-out refs or the worktree. The last error is
|
||||
// returned when no URL works.
|
||||
pub fn fetch_refs<U: AsRef<str>>(&self, urls: &[U], refspec: &str) -> Result<()> {
|
||||
let refspec = gix::refspec::parse(
|
||||
gix::bstr::BStr::new(refspec),
|
||||
gix::refspec::parse::Operation::Fetch,
|
||||
)
|
||||
.into_anyhow()
|
||||
.context("invalid fetch refspec")?
|
||||
.to_owned();
|
||||
|
||||
@@ -236,18 +239,19 @@ impl Repo {
|
||||
gix::lock::acquire::Fail::Immediately,
|
||||
None,
|
||||
)
|
||||
.into_anyhow()
|
||||
.context("failed to lock repository config")?;
|
||||
|
||||
let mut config =
|
||||
match gix::config::File::from_path_no_includes(config_path, gix::config::Source::Local)
|
||||
{
|
||||
Ok(config) => config,
|
||||
Err(gix::config::file::init::from_paths::Error::Io { source, .. })
|
||||
if source.kind() == std::io::ErrorKind::NotFound =>
|
||||
{
|
||||
gix::config::File::default()
|
||||
Err(error) if error.is_not_found() => gix::config::File::default(),
|
||||
Err(error) => {
|
||||
return Err(error)
|
||||
.into_anyhow()
|
||||
.context("failed to read repository config");
|
||||
}
|
||||
Err(error) => return Err(error).context("failed to read repository config"),
|
||||
};
|
||||
|
||||
edit(&mut config)?;
|
||||
|
||||
@@ -2,11 +2,23 @@ use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
// History walks re-decode the same commit objects without one; sized
|
||||
// generously, a walk can cover a large portion of the history.
|
||||
use crate::GixResultExt as _;
|
||||
|
||||
const OBJECT_CACHE_BYTES: usize = 64 * 1024 * 1024;
|
||||
|
||||
// One `open` per operation instead of every helper re-opening by path.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct RepoRefState {
|
||||
pub refs: Vec<(String, String)>,
|
||||
pub head: Option<String>,
|
||||
}
|
||||
|
||||
impl RepoRefState {
|
||||
fn new(refs: Vec<(String, String)>, head: Option<String>) -> Self {
|
||||
Self { refs, head }
|
||||
}
|
||||
}
|
||||
|
||||
/// One `open` per operation instead of every helper re-opening by path.
|
||||
pub struct Repo {
|
||||
pub(crate) inner: gix::Repository,
|
||||
}
|
||||
@@ -22,8 +34,6 @@ impl Repo {
|
||||
Self::open(workdir).ok()
|
||||
}
|
||||
|
||||
// Only history walks benefit from the object cache, they re-decode the
|
||||
// same commit objects repeatedly; single-object reads open plain.
|
||||
pub fn open_cached(workdir: &Path) -> Result<Self> {
|
||||
let mut repo = gix::open(workdir)?;
|
||||
repo.object_cache_size_if_unset(OBJECT_CACHE_BYTES);
|
||||
@@ -41,8 +51,6 @@ impl Repo {
|
||||
let (signature, mut time_buf) = Self::repository_signature();
|
||||
let signature = signature.to_ref(&mut time_buf);
|
||||
|
||||
// The initial branch is `main` regardless of `init.defaultBranch`:
|
||||
// point the unborn HEAD there.
|
||||
let head = gix::refs::FullName::try_from("HEAD")
|
||||
.map_err(|e| anyhow::anyhow!("invalid ref name: {e}"))?;
|
||||
|
||||
@@ -98,12 +106,14 @@ impl Repo {
|
||||
// Populate the index so the fresh repository is clean, as `git add`
|
||||
// and `git commit` would leave it.
|
||||
let mut index = repo.index_from_tree(&tree)?;
|
||||
index.write(gix::index::write::Options::default())?;
|
||||
index
|
||||
.write(gix::index::write::Options::default())
|
||||
.into_anyhow()?;
|
||||
|
||||
Ok(commit.to_string())
|
||||
}
|
||||
|
||||
// Not kept in any cache, unlike `GitCache::ensure_clone`.
|
||||
/// Not kept in any cache, unlike `GitCache::ensure_clone`.
|
||||
pub fn clone<U: AsRef<str>>(clone_urls: &[U], path: &Path) -> Result<Self> {
|
||||
if path.exists() {
|
||||
anyhow::bail!("destination {} already exists", path.display());
|
||||
@@ -130,7 +140,9 @@ impl Repo {
|
||||
|
||||
fn clone_from(url: &str, path: &Path) -> Result<Self> {
|
||||
let url = Self::transport_url(url);
|
||||
let url = gix::url::parse(url).context("invalid clone URL")?;
|
||||
let url = gix::url::parse(url)
|
||||
.into_anyhow()
|
||||
.context("invalid clone URL")?;
|
||||
|
||||
let mut prepare = gix::prepare_clone(url, path)?;
|
||||
let (mut checkout, _fetch) =
|
||||
@@ -149,24 +161,20 @@ impl Repo {
|
||||
self.inner.workdir()
|
||||
}
|
||||
|
||||
// `None` for an unborn HEAD.
|
||||
/// `None` for an unborn HEAD.
|
||||
pub fn head(&self) -> Option<String> {
|
||||
self.inner.head_id().ok().map(|id| id.to_string())
|
||||
}
|
||||
|
||||
// `Ok(None)` when the revisions share no common ancestor — a valid
|
||||
// outcome for a proposal; unresolvable revisions are errors.
|
||||
pub fn merge_base(&self, a: &str, b: &str) -> Result<Option<String>> {
|
||||
let a = self.inner.rev_parse_single(a.as_bytes())?;
|
||||
let b = self.inner.rev_parse_single(b.as_bytes())?;
|
||||
match self.inner.merge_base(a, b) {
|
||||
Ok(id) => Ok(Some(id.to_string())),
|
||||
Err(gix::repository::merge_base::Error::NotFound { .. }) => Ok(None),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
// `merge_base` reports a missing base as an unclassified error, while the
|
||||
// many-bases variant keeps the distinction as an empty result.
|
||||
let bases = self.inner.merge_bases_many(a.detach(), &[b.detach()])?;
|
||||
Ok(bases.first().map(|id| id.to_string()))
|
||||
}
|
||||
|
||||
// Oldest first: the order `git am` creates them.
|
||||
pub fn commits_since(&self, base: Option<&str>) -> Result<Vec<String>> {
|
||||
let head = match self.inner.head_id() {
|
||||
Ok(head) => head,
|
||||
@@ -198,7 +206,6 @@ impl Repo {
|
||||
Ok(commits)
|
||||
}
|
||||
|
||||
// The NIP-34 announcement's `euc` marker; `None` without commits.
|
||||
pub fn root_commit(&self) -> Result<Option<String>> {
|
||||
let Ok(head) = self.inner.head_id() else {
|
||||
return Ok(None);
|
||||
@@ -331,11 +338,9 @@ impl Repo {
|
||||
Err(_) => None,
|
||||
};
|
||||
|
||||
Ok(RepoRefState { refs, head })
|
||||
Ok(RepoRefState::new(refs, head))
|
||||
}
|
||||
|
||||
// Fast-forward only: local-only commits or diverged history must never
|
||||
// be rewritten by a refresh. Returns whether any branch moved.
|
||||
pub fn fast_forward_branches(&self) -> Result<bool> {
|
||||
use gix::refs::transaction::{Change, LogChange, PreviousValue, RefEdit, RefLog};
|
||||
|
||||
@@ -437,8 +442,6 @@ impl Repo {
|
||||
Ok(moved)
|
||||
}
|
||||
|
||||
// Like `git -c user.name=… -c user.email=…` per invocation: the repository
|
||||
// works without a global git identity, and `gix` runs no hooks and never signs.
|
||||
pub(crate) fn repository_signature() -> (gix::actor::Signature, gix::date::parse::TimeBuf) {
|
||||
let seconds = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
@@ -454,17 +457,9 @@ impl Repo {
|
||||
(signature, gix::date::parse::TimeBuf::default())
|
||||
}
|
||||
|
||||
// GRASP servers announce `grasp://` clone URLs but the transport is git
|
||||
// smart HTTP, so the scheme is rewritten for gix.
|
||||
pub(crate) fn transport_url(url: &str) -> String {
|
||||
url.strip_prefix("grasp://")
|
||||
.map(|rest| format!("https://{rest}"))
|
||||
.unwrap_or_else(|| url.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct RepoRefState {
|
||||
pub refs: Vec<(String, String)>,
|
||||
pub head: Option<String>,
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use ignore::WalkBuilder;
|
||||
use crate::nip34::Nip34Binding;
|
||||
use crate::repo::Repo;
|
||||
|
||||
// Caps nesting so pathological trees can't stall the scan.
|
||||
/// Caps nesting so pathological trees can't stall the scan.
|
||||
const SCAN_MAX_DEPTH: usize = 12;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
@@ -4,21 +4,51 @@ use std::path::{Path, PathBuf};
|
||||
use anyhow::{Context, Result};
|
||||
use gix::progress::Discard;
|
||||
|
||||
use crate::GixResultExt as _;
|
||||
use crate::history::FileCommit;
|
||||
use crate::repo::Repo;
|
||||
|
||||
pub struct WorktreeSnapshot {
|
||||
pub entries: Vec<PathBuf>,
|
||||
pub readme_path: Option<PathBuf>,
|
||||
pub readme: Option<Vec<u8>>,
|
||||
// `None` when detached, for example on a tag.
|
||||
pub current_branch: Option<String>,
|
||||
pub head_commit: Option<FileCommit>,
|
||||
pub branches: Vec<String>,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
impl WorktreeSnapshot {
|
||||
fn new(
|
||||
entries: Vec<PathBuf>,
|
||||
readme_path: Option<PathBuf>,
|
||||
readme: Option<Vec<u8>>,
|
||||
current_branch: Option<String>,
|
||||
head_commit: Option<FileCommit>,
|
||||
branches: Vec<String>,
|
||||
tags: Vec<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
entries,
|
||||
readme_path,
|
||||
readme,
|
||||
current_branch,
|
||||
head_commit,
|
||||
branches,
|
||||
tags,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Repo {
|
||||
// Best-effort: any read failure is reported as clean.
|
||||
pub fn is_dirty(&self) -> bool {
|
||||
// Tracked files, staged or not; untracked files are handled below.
|
||||
match self.inner.is_dirty() {
|
||||
Ok(true) => return true,
|
||||
Ok(false) => {}
|
||||
Err(_) => return false,
|
||||
}
|
||||
|
||||
// Untracked files surface as `DirectoryContents` items of the
|
||||
// index-vs-worktree walk; tracked files only appear there when modified.
|
||||
let Ok(platform) = self.inner.status(Discard) else {
|
||||
return false;
|
||||
};
|
||||
@@ -39,7 +69,6 @@ impl Repo {
|
||||
false
|
||||
}
|
||||
|
||||
// Best-effort: 0 when the range cannot be computed.
|
||||
pub fn commits_ahead(&self, base: &str, branch: &str) -> u32 {
|
||||
let (Some(base), Some(branch)) = (self.resolve_commit(base), self.resolve_commit(branch))
|
||||
else {
|
||||
@@ -53,13 +82,10 @@ impl Repo {
|
||||
walk.filter_map(Result::ok).count().min(u32::MAX as usize) as u32
|
||||
}
|
||||
|
||||
// Accepts full refs or the bare branch names callers pass; `gix`'s
|
||||
// revision parser already applies git's ref DWIM.
|
||||
fn resolve_commit<'a>(&'a self, rev: &str) -> Option<gix::Id<'a>> {
|
||||
self.inner.rev_parse_single(rev.as_bytes()).ok()
|
||||
}
|
||||
|
||||
// The `.git` directory is skipped.
|
||||
pub fn entries(&self) -> Result<Vec<PathBuf>> {
|
||||
let workdir = self.inner.workdir().context("repository has no worktree")?;
|
||||
|
||||
@@ -74,7 +100,6 @@ impl Repo {
|
||||
Ok(entries.into_iter().map(|(path, _)| path).collect())
|
||||
}
|
||||
|
||||
// `Ok(None)` when the path is missing or not a regular file.
|
||||
pub fn read(&self, rel: &Path) -> Result<Option<Vec<u8>>> {
|
||||
let workdir = self.inner.workdir().context("repository has no worktree")?;
|
||||
let path = workdir.join(rel);
|
||||
@@ -87,7 +112,6 @@ impl Repo {
|
||||
}
|
||||
}
|
||||
|
||||
// Falls back to any other file whose name starts with `readme`.
|
||||
pub fn find_readme(&self) -> Result<Option<PathBuf>> {
|
||||
let Some(workdir) = self.inner.workdir() else {
|
||||
return Ok(None);
|
||||
@@ -129,18 +153,17 @@ impl Repo {
|
||||
Some(path) => self.read(path)?,
|
||||
None => None,
|
||||
};
|
||||
Ok(WorktreeSnapshot {
|
||||
entries: self.entries()?,
|
||||
Ok(WorktreeSnapshot::new(
|
||||
self.entries()?,
|
||||
readme_path,
|
||||
readme,
|
||||
current_branch: self.current_branch(),
|
||||
head_commit: self.head_commit().unwrap_or(None),
|
||||
branches: self.branches().unwrap_or_default(),
|
||||
tags: self.tags().unwrap_or_default(),
|
||||
})
|
||||
self.current_branch(),
|
||||
self.head_commit().unwrap_or(None),
|
||||
self.branches().unwrap_or_default(),
|
||||
self.tags().unwrap_or_default(),
|
||||
))
|
||||
}
|
||||
|
||||
// HEAD stays attached to the branch.
|
||||
pub fn checkout_branch(&self, name: &str) -> Result<()> {
|
||||
let full = format!("refs/heads/{name}");
|
||||
|
||||
@@ -164,7 +187,6 @@ impl Repo {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// HEAD becomes detached at the tagged commit.
|
||||
pub fn checkout_tag(&self, name: &str) -> Result<()> {
|
||||
let full = format!("refs/tags/{name}");
|
||||
|
||||
@@ -196,8 +218,6 @@ impl Repo {
|
||||
|
||||
let mut index = self.inner.index_from_tree(tree)?;
|
||||
|
||||
// Files the previous index tracked but `tree` no longer contains are
|
||||
// removed, like git deleting files that vanish between branches.
|
||||
if let Ok(previous) = self.inner.index_or_empty() {
|
||||
let keep: HashSet<PathBuf> = index
|
||||
.entries()
|
||||
@@ -244,9 +264,12 @@ impl Repo {
|
||||
&bytes,
|
||||
&gix::interrupt::IS_INTERRUPTED,
|
||||
options,
|
||||
)?;
|
||||
)
|
||||
.into_anyhow()?;
|
||||
|
||||
index.write(gix::index::write::Options::default())?;
|
||||
index
|
||||
.write(gix::index::write::Options::default())
|
||||
.into_anyhow()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -301,14 +324,3 @@ impl Repo {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WorktreeSnapshot {
|
||||
pub entries: Vec<PathBuf>,
|
||||
pub readme_path: Option<PathBuf>,
|
||||
pub readme: Option<Vec<u8>>,
|
||||
// `None` when detached, for example on a tag.
|
||||
pub current_branch: Option<String>,
|
||||
pub head_commit: Option<FileCommit>,
|
||||
pub branches: Vec<String>,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user