gnostr_asyncgit/sync/
repository.rs

1use std::{
2	cell::RefCell,
3	path::{Path, PathBuf},
4};
5
6use git2::{Repository, RepositoryOpenFlags};
7
8use crate::error::Result;
9
10///
11pub type RepoPathRef = RefCell<RepoPath>;
12
13///
14#[derive(Clone, Debug)]
15pub enum RepoPath {
16	///
17	Path(PathBuf),
18	///
19	Workdir {
20		///
21		gitdir: PathBuf,
22		///
23		workdir: PathBuf,
24	},
25}
26
27impl RepoPath {
28	///
29	pub fn gitpath(&self) -> &Path {
30		match self {
31			Self::Path(p) => p.as_path(),
32			Self::Workdir { gitdir, .. } => gitdir.as_path(),
33		}
34	}
35
36	///
37	pub fn workdir(&self) -> Option<&Path> {
38		match self {
39			Self::Path(_) => None,
40			Self::Workdir { workdir, .. } => Some(workdir.as_path()),
41		}
42	}
43}
44
45impl From<&str> for RepoPath {
46	fn from(p: &str) -> Self {
47		Self::Path(PathBuf::from(p))
48	}
49}
50
51pub fn repo(repo_path: &RepoPath) -> Result<Repository> {
52	let repo = Repository::open_ext(
53		repo_path.gitpath(),
54		RepositoryOpenFlags::empty(),
55		Vec::<&Path>::new(),
56	)?;
57
58	if let Some(workdir) = repo_path.workdir() {
59		repo.set_workdir(workdir, false)?;
60	}
61
62	Ok(repo)
63}