| 7b9809e6 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use super::*; |
| 6 |
|
| 7 |
|
| 8 |
#[derive(Clone, Debug)] |
| 9 |
pub struct BookmarkInfo { |
| 10 |
pub name: String, |
| 11 |
pub heads: Vec<ChangeHash>, |
| 12 |
|
| 13 |
pub current: bool, |
| 14 |
|
| 15 |
pub ahead: Option<usize>, |
| 16 |
pub behind: Option<usize>, |
| 17 |
} |
| 18 |
|
| 19 |
#[derive(Clone, Debug)] |
| 20 |
pub struct MergeReport { |
| 21 |
pub heads: Vec<ChangeHash>, |
| 22 |
pub changed: bool, |
| 23 |
pub checked_out: bool, |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
pub fn valid_reference_name(name: &str) -> bool { |
| 29 |
!name.is_empty() |
| 30 |
&& name.len() <= 255 |
| 31 |
&& !name.starts_with('-') |
| 32 |
&& !name.chars().any(|c| c.is_control()) |
| 33 |
&& !name.contains("..") |
| 34 |
&& !name.contains("//") |
| 35 |
&& !name.contains("@{") |
| 36 |
&& name != "policy" |
| 37 |
} |
| 38 |
|
| 39 |
impl Repo { |
| 40 |
fn workspace_bookmark_path(&self) -> PathBuf { |
| 75167672 Andy McCall |
41 |
self.oxy |
| 42 |
.join("workspaces") |
| 43 |
.join(self.workspace.to_hex()) |
| 44 |
.join("bookmark") |
| 7b9809e6 Andy McCall ↶ |
45 |
} |
| 46 |
|
| 47 |
|
| 48 |
pub fn bookmarks(&self) -> Result<Vec<BookmarkInfo>> { |
| 49 |
let view = self.view()?; |
| 50 |
let mut out = Vec::new(); |
| 51 |
for (name, st) in &view.bookmarks { |
| 52 |
let heads = self.store.get_state(st)?.heads; |
| 53 |
let (ahead, behind) = match view.remotes.get("origin").and_then(|b| b.get(name)) { |
| 54 |
Some(rs) => { |
| 55 |
let remote_heads = self.store.get_state(rs)?.heads; |
| 75167672 Andy McCall |
56 |
( |
| 57 |
self.negotiate(&remote_heads, &heads).map(|v| v.len()).ok(), |
| 58 |
self.negotiate(&heads, &remote_heads).map(|v| v.len()).ok(), |
| 59 |
) |
| 7b9809e6 Andy McCall ↶ |
60 |
} |
| 61 |
None => (None, None), |
| 62 |
}; |
| 75167672 Andy McCall |
63 |
out.push(BookmarkInfo { |
| 64 |
name: name.clone(), |
| 65 |
heads, |
| 66 |
current: *name == self.config.bookmark, |
| 67 |
ahead, |
| 68 |
behind, |
| 69 |
}); |
| 7b9809e6 Andy McCall ↶ |
70 |
} |
| 71 |
Ok(out) |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75167672 Andy McCall |
75 |
pub fn bookmark_create( |
| 76 |
&self, |
| 77 |
name: &str, |
| 78 |
heads: Option<&[ChangeHash]>, |
| 79 |
) -> Result<Vec<ChangeHash>> { |
| 7b9809e6 Andy McCall ↶ |
80 |
if !valid_reference_name(name) { |
| 81 |
return fail(format!("{name:?} is not a valid bookmark name")); |
| 82 |
} |
| 83 |
let (op_ref, op) = self.head_op()?; |
| 84 |
if op.view.bookmarks.contains_key(name) { |
| 85 |
return fail(format!("bookmark {name} already exists")); |
| 86 |
} |
| 87 |
let heads = match heads { |
| 88 |
Some(h) => self.load_state(h)?.maximal(h), |
| 89 |
None => { |
| 90 |
let (_, wc) = self.working_copy()?; |
| 91 |
self.load_state(&wc.deps)?.maximal(&wc.deps) |
| 92 |
} |
| 93 |
}; |
| 94 |
let mut view = op.view.clone(); |
| 75167672 Andy McCall |
95 |
view.bookmarks.insert( |
| 96 |
name.to_string(), |
| 97 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 98 |
); |
| 7b9809e6 Andy McCall ↶ |
99 |
self.record_op(&[op_ref], &format!("oxy bookmark create {name}"), view)?; |
| 100 |
Ok(heads) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
pub fn bookmark_set(&self, name: &str, heads: &[ChangeHash]) -> Result<Vec<ChangeHash>> { |
| 105 |
let (op_ref, op) = self.head_op()?; |
| 106 |
if !op.view.bookmarks.contains_key(name) { |
| 75167672 Andy McCall |
107 |
return fail(format!( |
| 108 |
"no bookmark {name}; create it with `oxy bookmark create`" |
| 109 |
)); |
| 7b9809e6 Andy McCall ↶ |
110 |
} |
| 111 |
let heads = self.load_state(heads)?.maximal(heads); |
| 112 |
let mut view = op.view.clone(); |
| 75167672 Andy McCall |
113 |
view.bookmarks.insert( |
| 114 |
name.to_string(), |
| 115 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 116 |
); |
| 7b9809e6 Andy McCall ↶ |
117 |
self.record_op(&[op_ref], &format!("oxy bookmark set {name}"), view)?; |
| 118 |
if name == self.config.bookmark { |
| 119 |
self.drop_manifest(); |
| 120 |
} |
| 121 |
Ok(heads) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
pub fn bookmark_delete(&self, name: &str) -> Result<()> { |
| 126 |
if name == self.config.bookmark { |
| 75167672 Andy McCall |
127 |
return fail(format!( |
| 128 |
"{name} is this workspace's current bookmark; switch to another first" |
| 129 |
)); |
| 7b9809e6 Andy McCall ↶ |
130 |
} |
| 131 |
for w in self.workspaces()? { |
| 132 |
if !w.current && self.workspace_bookmark_of(&w.id).as_deref() == Some(name) { |
| 75167672 Andy McCall |
133 |
return fail(format!( |
| 134 |
"{name} is the current bookmark of workspace {} at {}", |
| 135 |
w.id.short(8), |
| 136 |
w.path.display() |
| 137 |
)); |
| 7b9809e6 Andy McCall ↶ |
138 |
} |
| 139 |
} |
| 140 |
let (op_ref, op) = self.head_op()?; |
| 141 |
let mut view = op.view.clone(); |
| 142 |
if view.bookmarks.remove(name).is_none() { |
| 143 |
return fail(format!("no bookmark {name}")); |
| 144 |
} |
| 145 |
self.record_op(&[op_ref], &format!("oxy bookmark delete {name}"), view)?; |
| 146 |
Ok(()) |
| 147 |
} |
| 148 |
|
| 149 |
fn workspace_bookmark_of(&self, id: &WorkspaceId) -> Option<String> { |
| 75167672 Andy McCall |
150 |
fs::read_to_string( |
| 151 |
self.oxy |
| 152 |
.join("workspaces") |
| 153 |
.join(id.to_hex()) |
| 154 |
.join("bookmark"), |
| 155 |
) |
| 156 |
.ok() |
| 157 |
.map(|s| s.trim().to_string()) |
| 158 |
.filter(|s| !s.is_empty()) |
| 7b9809e6 Andy McCall ↶ |
159 |
} |
| 160 |
|
| 161 |
|
| 162 |
pub fn bookmark_switch(&mut self, name: &str, discard: bool) -> Result<CheckoutReport> { |
| 163 |
let view = self.view()?; |
| 75167672 Andy McCall |
164 |
let Some(st) = view.bookmarks.get(name) else { |
| 165 |
return fail(format!("no bookmark {name}")); |
| 166 |
}; |
| 7b9809e6 Andy McCall ↶ |
167 |
let heads = self.store.get_state(st)?.heads; |
| 168 |
let report = self.checkout(&heads, discard, &format!("oxy bookmark switch {name}"))?; |
| 169 |
fs::create_dir_all(self.workspace_bookmark_path().parent().unwrap())?; |
| 170 |
write_atomic(&self.workspace_bookmark_path(), name.as_bytes())?; |
| 171 |
self.config.bookmark = name.to_string(); |
| 172 |
Ok(report) |
| 173 |
} |
| 174 |
|
| 61e3bce7 Andy McCall ↶ |
175 |
|
| 1c2bd9ab Andy McCall ↶ |
176 |
|
| 177 |
|
| 61e3bce7 Andy McCall ↶ |
178 |
pub fn bookmark_switch_carry(&mut self, name: &str) -> Result<(CheckoutReport, Vec<String>)> { |
| 179 |
let view = self.view()?; |
| 180 |
let Some(st) = view.bookmarks.get(name) else { |
| 181 |
return fail(format!("no bookmark {name}")); |
| 182 |
}; |
| 183 |
let heads = self.store.get_state(st)?.heads; |
| 1c2bd9ab Andy McCall ↶ |
184 |
let out = |
| 185 |
self.checkout_carry(&heads, name, &format!("oxy bookmark switch {name} --carry"))?; |
| 186 |
fs::create_dir_all(self.workspace_bookmark_path().parent().unwrap())?; |
| 187 |
write_atomic(&self.workspace_bookmark_path(), name.as_bytes())?; |
| 188 |
self.config.bookmark = name.to_string(); |
| 189 |
self.snapshot(&format!("oxy bookmark switch {name} --carry"))?; |
| 190 |
Ok(out) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
pub fn checkout_carry( |
| 200 |
&self, |
| 201 |
heads: &[ChangeHash], |
| 202 |
target: &str, |
| 203 |
command: &str, |
| 204 |
) -> Result<(CheckoutReport, Vec<String>)> { |
| 205 |
let snap = self.snapshot(command)?; |
| 61e3bce7 Andy McCall ↶ |
206 |
|
| 207 |
|
| 208 |
let mut touched: BTreeSet<String> = snap.statuses.iter().map(|s| s.path.clone()).collect(); |
| 209 |
for (p, f) in &snap.base_snapshot.files { |
| 210 |
if f.kind != NodeKind::Dir |
| 211 |
&& f.kind != NodeKind::Subrepo |
| 212 |
&& fs::symlink_metadata(self.root.join(p)).is_err() |
| 213 |
{ |
| 214 |
touched.insert(p.clone()); |
| 215 |
} |
| 216 |
} |
| 1c2bd9ab Andy McCall ↶ |
217 |
let target_state = self.load_state(heads)?.materialize(); |
| 61e3bce7 Andy McCall ↶ |
218 |
let same = |p: &String| { |
| 219 |
let a = snap.base_snapshot.files.get(p); |
| 1c2bd9ab Andy McCall ↶ |
220 |
let b = target_state.files.get(p); |
| 61e3bce7 Andy McCall ↶ |
221 |
match (a, b) { |
| 222 |
(None, None) => true, |
| 223 |
(Some(a), Some(b)) => { |
| 224 |
a.kind == b.kind && a.mode == b.mode && a.content == b.content |
| 225 |
} |
| 226 |
_ => false, |
| 227 |
} |
| 228 |
}; |
| 229 |
let differ: Vec<&String> = touched.iter().filter(|p| !same(p)).collect(); |
| 230 |
if !differ.is_empty() { |
| 231 |
let list: Vec<&str> = differ.iter().take(10).map(|p| p.as_str()).collect(); |
| 232 |
return fail(format!( |
| 1c2bd9ab Andy McCall ↶ |
233 |
"{target} has other versions of {} you changed ({}{}); commit first, or drop the changes with --discard", |
| 61e3bce7 Andy McCall ↶ |
234 |
if differ.len() == 1 { "a file" } else { "files" }, |
| 235 |
list.join(", "), |
| 236 |
if differ.len() > 10 { ", …" } else { "" } |
| 237 |
)); |
| 238 |
} |
| 239 |
|
| 240 |
let mut carried: Vec<(String, Carried)> = Vec::new(); |
| 241 |
for p in &touched { |
| 242 |
let full = self.root.join(p); |
| 243 |
let item = match fs::symlink_metadata(&full) { |
| 244 |
Ok(m) if m.file_type().is_symlink() => { |
| 245 |
Carried::Symlink(fs::read_link(&full)?.to_string_lossy().as_bytes().to_vec()) |
| 246 |
} |
| 247 |
Ok(m) if m.is_file() => Carried::File(is_executable(&m), fs::read(&full)?), |
| 248 |
Ok(_) => continue, |
| 249 |
Err(_) => Carried::Absent, |
| 250 |
}; |
| 251 |
carried.push((p.clone(), item)); |
| 252 |
} |
| 1c2bd9ab Andy McCall ↶ |
253 |
let report = self.checkout(heads, true, command)?; |
| 61e3bce7 Andy McCall ↶ |
254 |
for (p, item) in &carried { |
| 255 |
match item { |
| 256 |
Carried::Symlink(target) => self.write_symlink(p, target)?, |
| 257 |
Carried::File(exec, bytes) => self.write_file(p, bytes, *exec)?, |
| 258 |
Carried::Absent => { |
| 259 |
let _ = fs::remove_file(self.root.join(p)); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
self.drop_manifest(); |
| 1c2bd9ab Andy McCall ↶ |
264 |
self.snapshot(command)?; |
| 61e3bce7 Andy McCall ↶ |
265 |
Ok((report, carried.into_iter().map(|(p, _)| p).collect())) |
| 266 |
} |
| 267 |
|
| 7b9809e6 Andy McCall ↶ |
268 |
|
| 269 |
pub(crate) fn set_workspace_bookmark(&self, id: &WorkspaceId, name: &str) -> Result<()> { |
| 270 |
let dir = self.oxy.join("workspaces").join(id.to_hex()); |
| 271 |
fs::create_dir_all(&dir)?; |
| 272 |
write_atomic(&dir.join("bookmark"), name.as_bytes()) |
| 273 |
} |
| 274 |
|
| 275 |
|
| 1c2bd9ab Andy McCall ↶ |
276 |
|
| 7b9809e6 Andy McCall ↶ |
277 |
pub fn merge(&self, name: &str) -> Result<MergeReport> { |
| 278 |
let (op_ref, op) = self.head_op()?; |
| 279 |
let current = self.config.bookmark.clone(); |
| 280 |
if name == current { |
| 281 |
return fail(format!("{name} is the current bookmark")); |
| 282 |
} |
| 75167672 Andy McCall |
283 |
let Some(st) = op.view.bookmarks.get(name) else { |
| 284 |
return fail(format!("no bookmark {name}")); |
| 285 |
}; |
| 7b9809e6 Andy McCall ↶ |
286 |
let other = self.store.get_state(st)?.heads; |
| 287 |
let local = self.heads_of(&op.view, ¤t)?; |
| 288 |
let mut all = local.clone(); |
| 289 |
all.extend(other); |
| 290 |
let mut heads = self.load_state(&all)?.maximal(&all); |
| 291 |
heads.sort(); |
| 292 |
let mut sorted_local = local.clone(); |
| 293 |
sorted_local.sort(); |
| 294 |
if heads == sorted_local { |
| 75167672 Andy McCall |
295 |
return Ok(MergeReport { |
| 296 |
heads, |
| 297 |
changed: false, |
| 298 |
checked_out: false, |
| 299 |
}); |
| 7b9809e6 Andy McCall ↶ |
300 |
} |
| 301 |
let mut view = op.view.clone(); |
| 75167672 Andy McCall |
302 |
view.bookmarks.insert( |
| 303 |
current, |
| 304 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 305 |
); |
| 7b9809e6 Andy McCall ↶ |
306 |
self.record_op(&[op_ref], &format!("oxy merge {name}"), view)?; |
| 307 |
let snap = self.snapshot("oxy merge")?; |
| 308 |
let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty(); |
| 309 |
let mut base = snap.base.maximal(&snap.wc.deps); |
| 310 |
base.sort(); |
| 311 |
let mut checked_out = false; |
| 1c2bd9ab Andy McCall ↶ |
312 |
|
| 313 |
|
| 314 |
if clean { |
| 7b9809e6 Andy McCall ↶ |
315 |
self.checkout(&heads, false, "oxy merge (checkout)")?; |
| 316 |
checked_out = true; |
| 317 |
} |
| 75167672 Andy McCall |
318 |
Ok(MergeReport { |
| 319 |
heads, |
| 320 |
changed: true, |
| 321 |
checked_out, |
| 322 |
}) |
| 7b9809e6 Andy McCall ↶ |
323 |
} |
| 61e3bce7 Andy McCall ↶ |
324 |
} |
| 325 |
|
| 326 |
|
| 327 |
enum Carried { |
| 328 |
Symlink(Vec<u8>), |
| 329 |
|
| 330 |
File(bool, Vec<u8>), |
| 331 |
Absent, |
| 7b9809e6 Andy McCall ↶ |
332 |
} |