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
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::path::PathBuf;
pub use git_worktree::*;
use crate::{
bstr::{BStr, BString},
Repository,
};
pub(crate) type IndexStorage = git_features::threading::OwnShared<git_features::fs::MutableSnapshot<git_index::File>>;
pub type Index = git_features::fs::SharedSnapshot<git_index::File>;
#[derive(Debug, Clone)]
pub struct Proxy<'repo> {
pub(crate) parent: &'repo Repository,
pub(crate) git_dir: PathBuf,
}
impl<'repo> crate::Worktree<'repo> {
pub fn base(&self) -> &'repo std::path::Path {
self.path
}
pub fn is_main(&self) -> bool {
self.id().is_none()
}
pub fn is_locked(&self) -> bool {
Proxy::new(self.parent, self.parent.git_dir()).is_locked()
}
pub fn lock_reason(&self) -> Option<BString> {
Proxy::new(self.parent, self.parent.git_dir()).lock_reason()
}
pub fn id(&self) -> Option<&BStr> {
id(self.parent.git_dir(), self.parent.common_dir.is_some())
}
}
pub(crate) fn id(git_dir: &std::path::Path, has_common_dir: bool) -> Option<&BStr> {
if !has_common_dir {
return None;
}
let candidate = git_path::os_str_into_bstr(git_dir.file_name().expect("at least one directory level"))
.expect("no illformed UTF-8");
let maybe_worktrees = git_dir.parent()?;
(maybe_worktrees.file_name()?.to_str()? == "worktrees").then_some(candidate)
}
pub mod proxy;
pub mod open_index {
use crate::bstr::BString;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Could not interpret value '{}' as 'index.threads'", .value)]
ConfigIndexThreads {
value: BString,
#[source]
err: git_config::value::Error,
},
#[error(transparent)]
IndexFile(#[from] git_index::file::init::Error),
}
impl<'repo> crate::Worktree<'repo> {
pub fn open_index(&self) -> Result<git_index::File, Error> {
self.parent.open_index()
}
pub fn index(&self) -> Result<crate::worktree::Index, Error> {
self.parent.index()
}
}
}
pub mod excludes {
use std::path::PathBuf;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Could not read repository exclude.")]
Io(#[from] std::io::Error),
#[error(transparent)]
EnvironmentPermission(#[from] git_sec::permission::Error<PathBuf>),
#[error("The value for `core.excludesFile` could not be read from configuration")]
ExcludesFilePathInterpolation(#[from] git_config::path::interpolate::Error),
}
impl<'repo> crate::Worktree<'repo> {
pub fn excludes(
&self,
index: &git_index::State,
overrides: Option<git_attributes::MatchGroup<git_attributes::Ignore>>,
) -> Result<git_worktree::fs::Cache, Error> {
let repo = self.parent;
let case = repo
.config
.ignore_case
.then_some(git_glob::pattern::Case::Fold)
.unwrap_or_default();
let mut buf = Vec::with_capacity(512);
let excludes_file = match repo.config.excludes_file().transpose()? {
Some(user_path) => Some(user_path),
None => repo.config.xdg_config_path("ignore")?,
};
let state = git_worktree::fs::cache::State::IgnoreStack(git_worktree::fs::cache::state::Ignore::new(
overrides.unwrap_or_default(),
git_attributes::MatchGroup::<git_attributes::Ignore>::from_git_dir(
repo.git_dir(),
excludes_file,
&mut buf,
)?,
None,
case,
));
let attribute_list = state.build_attribute_list(index, index.path_backing(), case);
Ok(git_worktree::fs::Cache::new(
self.path,
state,
case,
buf,
attribute_list,
))
}
}
}