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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::path::{Path, PathBuf};
use crate::{
bstr::{BStr, BString, ByteSlice},
worktree::Proxy,
Repository, ThreadSafeRepository,
};
#[allow(missing_docs)]
pub mod into_repo {
use std::path::PathBuf;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Open(#[from] crate::open::Error),
#[error("Worktree at '{}' is inaccessible", .base.display())]
MissingWorktree { base: PathBuf },
#[error(transparent)]
MissingGitDirFile(#[from] std::io::Error),
}
}
impl<'repo> Proxy<'repo> {
pub(crate) fn new(parent: &'repo Repository, git_dir: impl Into<PathBuf>) -> Self {
Proxy {
parent,
git_dir: git_dir.into(),
}
}
}
impl<'repo> Proxy<'repo> {
pub fn base(&self) -> std::io::Result<PathBuf> {
let git_dir = self.git_dir.join("gitdir");
let base_dot_git = git_discover::path::from_plain_file(&git_dir).ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Required file '{}' does not exist", git_dir.display()),
)
})??;
Ok(git_discover::path::without_dot_git_dir(base_dot_git))
}
pub fn git_dir(&self) -> &Path {
&self.git_dir
}
pub fn id(&self) -> &BStr {
git_path::os_str_into_bstr(self.git_dir.file_name().expect("worktrees/ parent dir"))
.expect("no illformed UTF-8")
}
pub fn is_locked(&self) -> bool {
self.git_dir.join("locked").is_file()
}
pub fn lock_reason(&self) -> Option<BString> {
std::fs::read(self.git_dir.join("locked"))
.ok()
.map(|contents| contents.trim().into())
}
pub fn into_repo_with_possibly_inaccessible_worktree(self) -> Result<Repository, crate::open::Error> {
let base = self.base().ok();
let repo = ThreadSafeRepository::open_from_paths(self.git_dir, base, self.parent.options.clone())?;
Ok(repo.into())
}
pub fn into_repo(self) -> Result<Repository, into_repo::Error> {
let base = self.base()?;
if !base.is_dir() {
return Err(into_repo::Error::MissingWorktree { base });
}
let repo = ThreadSafeRepository::open_from_paths(self.git_dir, base.into(), self.parent.options.clone())?;
Ok(repo.into())
}
}