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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::path::{Path, PathBuf};
use gix_error::{ResultExt, bail};
use crate::{
Repository, Result, ThreadSafeRepository,
bstr::{BStr, BString, ByteSlice},
open::{RelatedRepositoryKind, related_repository_skipped},
worktree::Proxy,
};
impl<'repo> Proxy<'repo> {
pub(crate) fn new(parent: &'repo Repository, git_dir: impl Into<PathBuf>) -> Self {
Proxy {
parent,
git_dir: parent.current_dir().join(git_dir.into()),
}
}
pub(crate) fn new_if_gitdir_file_exists(parent: &'repo Repository, git_dir: impl Into<PathBuf>) -> Option<Self> {
let proxy = Proxy::new(parent, git_dir);
if proxy.git_dir.join("gitdir").is_file() {
Some(proxy)
} else {
None
}
}
}
impl Proxy<'_> {
fn dot_git(&self) -> std::io::Result<PathBuf> {
let git_dir = self.git_dir.join("gitdir");
gix_discover::path::from_plain_file_relative_to_file(&git_dir).ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Required file \"{}\" does not exist", git_dir.display()),
)
})?
}
/// Read the absolute location of the checkout, the base of the work tree.
/// Relative registrations are resolved against the private Git directory.
/// Note that the location might not exist.
pub fn base(&self) -> Result<PathBuf> {
Ok(gix_discover::path::without_dot_git_dir(self.dot_git().or_error()?))
}
/// The absolute git directory for the work tree, typically contained within the parent git dir.
/// Relative repository paths are anchored to the parent repository's captured current directory.
pub fn git_dir(&self) -> &Path {
&self.git_dir
}
/// The name of the worktree, which is derived from its folder within the `worktrees` directory within the parent `.git` folder.
/// Return an error if the directory name cannot be represented as Git bytes.
pub fn id(&self) -> Result<&BStr> {
gix_path::os_str_into_bstr(self.git_dir.file_name().expect("worktrees/ parent dir"))
}
/// Return true if the worktree cannot be pruned, moved or deleted, which is useful if it is located on an external storage device.
pub fn is_locked(&self) -> bool {
self.git_dir.join("locked").symlink_metadata().is_ok()
}
/// Return true if this worktree can be pruned without an expiry grace period.
///
/// Locked worktrees are never prunable. Otherwise, an unreadable `gitdir` file or missing target
/// makes the worktree prunable.
pub fn is_prunable(&self) -> bool {
!self.is_locked()
&& self
.dot_git()
.map_or(true, |dot_git| dot_git.symlink_metadata().is_err())
}
/// Provide a reason for the locking of this worktree, if it is locked at all.
///
/// Note that we squelch errors in case the file cannot be read in which case the
/// reason is an empty string.
pub fn lock_reason(&self) -> Option<BString> {
std::fs::read(self.git_dir.join("locked"))
.ok()
.map(|contents| contents.trim().into())
}
/// Set or clear this linked worktree's lock, even when its checkout is inaccessible.
///
/// `Some(reason)` creates or replaces the reason, including an empty reason. `None` unlocks,
/// succeeding even if already unlocked. Reasons use Git's newline-terminated encoding and are
/// trimmed when read through [`Self::lock_reason()`]. The private Git directory must already exist
/// when locking. Callers must serialize concurrent changes to the lock and its reason.
/// See [`gix_worktree::set_lock()`] for filesystem details.
#[cfg(feature = "worktree-mutation")]
pub fn set_lock(&self, reason: Option<&BStr>) -> Result<()> {
gix_worktree::set_lock(&self.git_dir, reason)
.or_raise(|| crate::error::message!("Could not update the worktree lock in \"{}\"", self.git_dir.display()))
}
/// Transform this proxy into a [`Repository`] while ignoring issues reading `base()` and ignoring that it might not exist.
///
/// Most importantly, the `Repository` might be initialized with a non-existing work tree directory as the checkout
/// was removed or moved in the mean time or is unavailable for other reasons.
/// The caller will encounter io errors if it's used like the work tree is guaranteed to be present, but can still access
/// a lot of information if work tree access is avoided.
/// The returned repository has absolute paths.
pub fn into_repo_with_possibly_inaccessible_worktree(self) -> Result<Repository> {
let base = self.base().ok();
self.open(base)
}
/// Like `into_repo_with_possibly_inaccessible_worktree()` but will fail if the `base()` cannot be read or
/// if the worktree doesn't exist.
///
/// The returned repository has absolute paths, as with [`Self::into_repo_with_possibly_inaccessible_worktree()`].
pub fn into_repo(self) -> Result<Repository> {
let base = self.base()?;
if !base.is_dir() {
bail!("Worktree at \"{}\" is inaccessible", base.display());
}
self.open(Some(base))
}
fn open(self, base: Option<PathBuf>) -> Result<Repository> {
let cwd = self.parent.current_dir();
let common_dir = cwd.join(self.parent.common_dir());
let mut options = self.parent.options.clone_for_linked_worktree();
if options.git_dir_trust.is_none() {
// The proxy opens its parent's common directory even if the private `commondir` file points elsewhere.
options = options.with_trust(gix_discover::repository::trust(
&self.git_dir,
base.as_deref(),
cwd,
(&common_dir, None),
)?);
}
let options = options
.configure_related_open(
self.parent,
RelatedRepositoryKind::LinkedWorktree,
&self.git_dir,
base.as_deref(),
)?
.continue_value()
.ok_or_else(|| related_repository_skipped(&self.git_dir))?;
ThreadSafeRepository::open_from_paths(self.git_dir, base, options, Some(common_dir)).map(Into::into)
}
}