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