In Git2 error fixing, opening the repo is still currently unavailable.

This commit is contained in:
2026-01-30 18:29:11 +08:00
parent 2a57946421
commit 1cbb01ccc4
10 changed files with 42 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
use anyhow::{bail, Context, Result};
use git2::{Repository, Signature, StatusOptions, DiffOptions};
use std::path::Path;
use std::path::{Path, PathBuf};
pub mod changelog;
pub mod commit;
@@ -19,13 +19,27 @@ pub struct GitRepo {
impl GitRepo {
/// Open a git repository
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let path = path.as_ref().canonicalize()
.unwrap_or_else(|_| path.as_ref().to_path_buf());
let path = path.as_ref();
if let Ok(repo) = Repository::discover(&path) {
return Ok(Self {
repo,
path: path.to_path_buf()
});
}
if let Ok(repo) = Repository::open(path) {
return Ok(Self { repo, path: path.to_path_buf() });
}
let repo = Repository::open(&path)
.with_context(|| format!("Failed to open git repository: {:?}", path))?;
Ok(Self { repo, path })
// 如果依然失败,给出明确的错误提示
bail!(
"Failed to open git repository at '{:?}'. Please ensure:\n\
1. The directory is set as safe (run: git config --global --add safe.directory \"{}\")\n\
2. The path is correct and contains a valid '.git' folder.",
path,
path.display()
)
}
/// Get repository path