| 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 |
|
| 4fe7d604 Andy McCall ↶ |
39 |
|
| 40 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 41 |
pub struct StateGap { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub earlier: Vec<ChangeHash>, |
| 46 |
|
| 47 |
pub lacks: usize, |
| 48 |
|
| 49 |
|
| 50 |
pub upto: Vec<ChangeHash>, |
| 51 |
} |
| 52 |
|
| 7b9809e6 Andy McCall ↶ |
53 |
impl Repo { |
| 54 |
fn workspace_bookmark_path(&self) -> PathBuf { |
| 75167672 Andy McCall |
55 |
self.oxy |
| 56 |
.join("workspaces") |
| 57 |
.join(self.workspace.to_hex()) |
| 58 |
.join("bookmark") |
| 7b9809e6 Andy McCall ↶ |
59 |
} |
| 60 |
|
| 61 |
|
| 62 |
pub fn bookmarks(&self) -> Result<Vec<BookmarkInfo>> { |
| 63 |
let view = self.view()?; |
| 64 |
let mut out = Vec::new(); |
| 65 |
for (name, st) in &view.bookmarks { |
| 66 |
let heads = self.store.get_state(st)?.heads; |
| 67 |
let (ahead, behind) = match view.remotes.get("origin").and_then(|b| b.get(name)) { |
| 68 |
Some(rs) => { |
| 69 |
let remote_heads = self.store.get_state(rs)?.heads; |
| 75167672 Andy McCall |
70 |
( |
| 71 |
self.negotiate(&remote_heads, &heads).map(|v| v.len()).ok(), |
| 72 |
self.negotiate(&heads, &remote_heads).map(|v| v.len()).ok(), |
| 73 |
) |
| 7b9809e6 Andy McCall ↶ |
74 |
} |
| 75 |
None => (None, None), |
| 76 |
}; |
| 75167672 Andy McCall |
77 |
out.push(BookmarkInfo { |
| 78 |
name: name.clone(), |
| 79 |
heads, |
| 80 |
current: *name == self.config.bookmark, |
| 81 |
ahead, |
| 82 |
behind, |
| 83 |
}); |
| 7b9809e6 Andy McCall ↶ |
84 |
} |
| 85 |
Ok(out) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 75167672 Andy McCall |
89 |
pub fn bookmark_create( |
| 90 |
&self, |
| 91 |
name: &str, |
| 92 |
heads: Option<&[ChangeHash]>, |
| 93 |
) -> Result<Vec<ChangeHash>> { |
| 7b9809e6 Andy McCall ↶ |
94 |
if !valid_reference_name(name) { |
| 95 |
return fail(format!("{name:?} is not a valid bookmark name")); |
| 96 |
} |
| 97 |
let (op_ref, op) = self.head_op()?; |
| 98 |
if op.view.bookmarks.contains_key(name) { |
| 99 |
return fail(format!("bookmark {name} already exists")); |
| 100 |
} |
| 101 |
let heads = match heads { |
| 102 |
Some(h) => self.load_state(h)?.maximal(h), |
| 103 |
None => { |
| 104 |
let (_, wc) = self.working_copy()?; |
| 105 |
self.load_state(&wc.deps)?.maximal(&wc.deps) |
| 106 |
} |
| 107 |
}; |
| 108 |
let mut view = op.view.clone(); |
| 75167672 Andy McCall |
109 |
view.bookmarks.insert( |
| 110 |
name.to_string(), |
| 111 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 112 |
); |
| 7b9809e6 Andy McCall ↶ |
113 |
self.record_op(&[op_ref], &format!("oxy bookmark create {name}"), view)?; |
| 114 |
Ok(heads) |
| 4fe7d604 Andy McCall ↶ |
115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
pub fn state_gap(&self, from: &[ChangeHash], at: &[ChangeHash]) -> Result<StateGap> { |
| 122 |
let mut all = from.to_vec(); |
| 123 |
all.extend_from_slice(at); |
| 124 |
let st = self.load_state_history(&all)?; |
| 125 |
let with_closure = |hs: &[ChangeHash]| { |
| 126 |
let mut s: HashSet<ChangeHash> = hs.iter().copied().collect(); |
| 127 |
for h in hs { |
| 128 |
s.extend(st.closure(h).iter().copied()); |
| 129 |
} |
| 130 |
s |
| 131 |
}; |
| 132 |
let from_set = with_closure(from); |
| 133 |
let at_set = with_closure(at); |
| 134 |
let key = |h: &ChangeHash| st.get(h).map(|m| (m.hlc, *h)); |
| 135 |
let last = at.iter().filter_map(key).max(); |
| 136 |
let missing: Vec<ChangeHash> = from_set |
| 137 |
.iter() |
| 138 |
.filter(|h| !at_set.contains(h)) |
| 139 |
.copied() |
| 140 |
.collect(); |
| 141 |
let mut earlier: Vec<ChangeHash> = missing |
| 142 |
.iter() |
| 143 |
.filter(|h| matches!((key(h), last), (Some(k), Some(l)) if k < l)) |
| 144 |
.copied() |
| 145 |
.collect(); |
| 146 |
earlier.sort_by_key(key); |
| 147 |
let mut candidates = at.to_vec(); |
| 148 |
candidates.extend_from_slice(&earlier); |
| 149 |
Ok(StateGap { |
| 150 |
lacks: missing.len(), |
| 151 |
upto: st.maximal(&candidates), |
| 152 |
earlier, |
| 153 |
}) |
| 7b9809e6 Andy McCall ↶ |
154 |
} |
| 155 |
|
| 156 |
|
| 157 |
pub fn bookmark_set(&self, name: &str, heads: &[ChangeHash]) -> Result<Vec<ChangeHash>> { |
| 158 |
let (op_ref, op) = self.head_op()?; |
| 159 |
if !op.view.bookmarks.contains_key(name) { |
| 75167672 Andy McCall |
160 |
return fail(format!( |
| 161 |
"no bookmark {name}; create it with `oxy bookmark create`" |
| 162 |
)); |
| 7b9809e6 Andy McCall ↶ |
163 |
} |
| 164 |
let heads = self.load_state(heads)?.maximal(heads); |
| 165 |
let mut view = op.view.clone(); |
| 75167672 Andy McCall |
166 |
view.bookmarks.insert( |
| 167 |
name.to_string(), |
| 168 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 169 |
); |
| 7b9809e6 Andy McCall ↶ |
170 |
self.record_op(&[op_ref], &format!("oxy bookmark set {name}"), view)?; |
| 171 |
if name == self.config.bookmark { |
| 172 |
self.drop_manifest(); |
| 173 |
} |
| 174 |
Ok(heads) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
pub fn bookmark_delete(&self, name: &str) -> Result<()> { |
| 179 |
if name == self.config.bookmark { |
| 75167672 Andy McCall |
180 |
return fail(format!( |
| 181 |
"{name} is this workspace's current bookmark; switch to another first" |
| 182 |
)); |
| 7b9809e6 Andy McCall ↶ |
183 |
} |
| 184 |
for w in self.workspaces()? { |
| 185 |
if !w.current && self.workspace_bookmark_of(&w.id).as_deref() == Some(name) { |
| 75167672 Andy McCall |
186 |
return fail(format!( |
| 187 |
"{name} is the current bookmark of workspace {} at {}", |
| 188 |
w.id.short(8), |
| 189 |
w.path.display() |
| 190 |
)); |
| 7b9809e6 Andy McCall ↶ |
191 |
} |
| 192 |
} |
| 193 |
let (op_ref, op) = self.head_op()?; |
| 194 |
let mut view = op.view.clone(); |
| 195 |
if view.bookmarks.remove(name).is_none() { |
| 196 |
return fail(format!("no bookmark {name}")); |
| 197 |
} |
| 198 |
self.record_op(&[op_ref], &format!("oxy bookmark delete {name}"), view)?; |
| 199 |
Ok(()) |
| 200 |
} |
| 201 |
|
| 202 |
fn workspace_bookmark_of(&self, id: &WorkspaceId) -> Option<String> { |
| 75167672 Andy McCall |
203 |
fs::read_to_string( |
| 204 |
self.oxy |
| 205 |
.join("workspaces") |
| 206 |
.join(id.to_hex()) |
| 207 |
.join("bookmark"), |
| 208 |
) |
| 209 |
.ok() |
| 210 |
.map(|s| s.trim().to_string()) |
| 211 |
.filter(|s| !s.is_empty()) |
| 7b9809e6 Andy McCall ↶ |
212 |
} |
| 213 |
|
| 214 |
|
| 215 |
pub fn bookmark_switch(&mut self, name: &str, discard: bool) -> Result<CheckoutReport> { |
| 6caacfff Andy McCall ↶ |
216 |
let _wc = self.lock_working_copy()?; |
| 7b9809e6 Andy McCall ↶ |
217 |
let view = self.view()?; |
| 75167672 Andy McCall |
218 |
let Some(st) = view.bookmarks.get(name) else { |
| 219 |
return fail(format!("no bookmark {name}")); |
| 220 |
}; |
| 7b9809e6 Andy McCall ↶ |
221 |
let heads = self.store.get_state(st)?.heads; |
| 222 |
let report = self.checkout(&heads, discard, &format!("oxy bookmark switch {name}"))?; |
| 223 |
fs::create_dir_all(self.workspace_bookmark_path().parent().unwrap())?; |
| 224 |
write_atomic(&self.workspace_bookmark_path(), name.as_bytes())?; |
| 225 |
self.config.bookmark = name.to_string(); |
| 226 |
Ok(report) |
| 227 |
} |
| 228 |
|
| 61e3bce7 Andy McCall ↶ |
229 |
|
| 1c2bd9ab Andy McCall ↶ |
230 |
|
| 231 |
|
| 61e3bce7 Andy McCall ↶ |
232 |
pub fn bookmark_switch_carry(&mut self, name: &str) -> Result<(CheckoutReport, Vec<String>)> { |
| 6caacfff Andy McCall ↶ |
233 |
let _wc = self.lock_working_copy()?; |
| 61e3bce7 Andy McCall ↶ |
234 |
let view = self.view()?; |
| 235 |
let Some(st) = view.bookmarks.get(name) else { |
| 236 |
return fail(format!("no bookmark {name}")); |
| 237 |
}; |
| 238 |
let heads = self.store.get_state(st)?.heads; |
| 1c2bd9ab Andy McCall ↶ |
239 |
let out = |
| 240 |
self.checkout_carry(&heads, name, &format!("oxy bookmark switch {name} --carry"))?; |
| 241 |
fs::create_dir_all(self.workspace_bookmark_path().parent().unwrap())?; |
| 242 |
write_atomic(&self.workspace_bookmark_path(), name.as_bytes())?; |
| 243 |
self.config.bookmark = name.to_string(); |
| 244 |
self.snapshot(&format!("oxy bookmark switch {name} --carry"))?; |
| 245 |
Ok(out) |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
pub fn checkout_carry( |
| 255 |
&self, |
| 256 |
heads: &[ChangeHash], |
| 257 |
target: &str, |
| 258 |
command: &str, |
| 259 |
) -> Result<(CheckoutReport, Vec<String>)> { |
| 6caacfff Andy McCall ↶ |
260 |
let _wc = self.lock_working_copy()?; |
| 1c2bd9ab Andy McCall ↶ |
261 |
let snap = self.snapshot(command)?; |
| 61e3bce7 Andy McCall ↶ |
262 |
|
| 263 |
|
| 264 |
let mut touched: BTreeSet<String> = snap.statuses.iter().map(|s| s.path.clone()).collect(); |
| 265 |
for (p, f) in &snap.base_snapshot.files { |
| 266 |
if f.kind != NodeKind::Dir |
| 267 |
&& f.kind != NodeKind::Subrepo |
| 268 |
&& fs::symlink_metadata(self.root.join(p)).is_err() |
| 269 |
{ |
| 270 |
touched.insert(p.clone()); |
| 271 |
} |
| 272 |
} |
| 1c2bd9ab Andy McCall ↶ |
273 |
let target_state = self.load_state(heads)?.materialize(); |
| 61e3bce7 Andy McCall ↶ |
274 |
let same = |p: &String| { |
| 275 |
let a = snap.base_snapshot.files.get(p); |
| 1c2bd9ab Andy McCall ↶ |
276 |
let b = target_state.files.get(p); |
| 61e3bce7 Andy McCall ↶ |
277 |
match (a, b) { |
| 278 |
(None, None) => true, |
| 279 |
(Some(a), Some(b)) => { |
| 280 |
a.kind == b.kind && a.mode == b.mode && a.content == b.content |
| 281 |
} |
| 282 |
_ => false, |
| 283 |
} |
| 284 |
}; |
| 285 |
let differ: Vec<&String> = touched.iter().filter(|p| !same(p)).collect(); |
| 286 |
if !differ.is_empty() { |
| 287 |
let list: Vec<&str> = differ.iter().take(10).map(|p| p.as_str()).collect(); |
| 288 |
return fail(format!( |
| 1c2bd9ab Andy McCall ↶ |
289 |
"{target} has other versions of {} you changed ({}{}); commit first, or drop the changes with --discard", |
| 61e3bce7 Andy McCall ↶ |
290 |
if differ.len() == 1 { "a file" } else { "files" }, |
| 291 |
list.join(", "), |
| 292 |
if differ.len() > 10 { ", …" } else { "" } |
| 293 |
)); |
| 294 |
} |
| 1a3b6203 Andy McCall ↶ |
295 |
|
| 296 |
|
| 297 |
let keep: BTreeSet<String> = touched |
| 298 |
.iter() |
| 299 |
.filter(|p| { |
| 300 |
snap.base_snapshot |
| 301 |
.files |
| 302 |
.get(*p) |
| 303 |
.or_else(|| target_state.files.get(*p)) |
| 304 |
.is_some_and(|f| f.kind == NodeKind::Subrepo) |
| 305 |
}) |
| 306 |
.cloned() |
| 307 |
.collect(); |
| 61e3bce7 Andy McCall ↶ |
308 |
|
| 309 |
let mut carried: Vec<(String, Carried)> = Vec::new(); |
| 310 |
for p in &touched { |
| 1a3b6203 Andy McCall ↶ |
311 |
if keep.contains(p) { |
| 312 |
carried.push((p.clone(), Carried::Subrepo)); |
| 313 |
continue; |
| 314 |
} |
| 61e3bce7 Andy McCall ↶ |
315 |
let full = self.root.join(p); |
| 316 |
let item = match fs::symlink_metadata(&full) { |
| 317 |
Ok(m) if m.file_type().is_symlink() => { |
| 318 |
Carried::Symlink(fs::read_link(&full)?.to_string_lossy().as_bytes().to_vec()) |
| 319 |
} |
| 320 |
Ok(m) if m.is_file() => Carried::File(is_executable(&m), fs::read(&full)?), |
| 321 |
Ok(_) => continue, |
| 322 |
Err(_) => Carried::Absent, |
| 323 |
}; |
| 324 |
carried.push((p.clone(), item)); |
| 325 |
} |
| 1a3b6203 Andy McCall ↶ |
326 |
let report = self.checkout_keeping(heads, true, command, &keep)?; |
| 61e3bce7 Andy McCall ↶ |
327 |
for (p, item) in &carried { |
| 328 |
match item { |
| 329 |
Carried::Symlink(target) => self.write_symlink(p, target)?, |
| 330 |
Carried::File(exec, bytes) => self.write_file(p, bytes, *exec)?, |
| 331 |
Carried::Absent => { |
| 332 |
let _ = fs::remove_file(self.root.join(p)); |
| 333 |
} |
| 1a3b6203 Andy McCall ↶ |
334 |
Carried::Subrepo => {} |
| 61e3bce7 Andy McCall ↶ |
335 |
} |
| 336 |
} |
| 337 |
self.drop_manifest(); |
| 1c2bd9ab Andy McCall ↶ |
338 |
self.snapshot(command)?; |
| 61e3bce7 Andy McCall ↶ |
339 |
Ok((report, carried.into_iter().map(|(p, _)| p).collect())) |
| 340 |
} |
| 341 |
|
| 7b9809e6 Andy McCall ↶ |
342 |
|
| 343 |
pub(crate) fn set_workspace_bookmark(&self, id: &WorkspaceId, name: &str) -> Result<()> { |
| 344 |
let dir = self.oxy.join("workspaces").join(id.to_hex()); |
| 345 |
fs::create_dir_all(&dir)?; |
| 346 |
write_atomic(&dir.join("bookmark"), name.as_bytes()) |
| 347 |
} |
| 348 |
|
| 349 |
|
| 1c2bd9ab Andy McCall ↶ |
350 |
|
| 7b9809e6 Andy McCall ↶ |
351 |
pub fn merge(&self, name: &str) -> Result<MergeReport> { |
| 6caacfff Andy McCall ↶ |
352 |
let _wc = self.lock_working_copy()?; |
| e266b7d4 Andy McCall ↶ |
353 |
let (_, op) = self.head_op()?; |
| 354 |
if name == self.config.bookmark { |
| 7b9809e6 Andy McCall ↶ |
355 |
return fail(format!("{name} is the current bookmark")); |
| 356 |
} |
| 75167672 Andy McCall |
357 |
let Some(st) = op.view.bookmarks.get(name) else { |
| 358 |
return fail(format!("no bookmark {name}")); |
| 359 |
}; |
| 7b9809e6 Andy McCall ↶ |
360 |
let other = self.store.get_state(st)?.heads; |
| e266b7d4 Andy McCall ↶ |
361 |
self.merge_heads(other, &format!("oxy merge {name}")) |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
pub fn rebase_into_working_copy( |
| 370 |
&self, |
| 371 |
c: &ChangeHash, |
| 372 |
onto: &[ChangeHash], |
| 373 |
) -> Result<MergeReport> { |
| 374 |
let _wc = self.lock_working_copy()?; |
| 375 |
let (_, op) = self.head_op()?; |
| 376 |
let current = self.config.bookmark.clone(); |
| 377 |
let local = self.heads_of(&op.view, ¤t)?; |
| 378 |
if !self.load_state_indexed(&local)?.contains(c) { |
| 379 |
return fail(format!( |
| 380 |
"#{} is not on the current bookmark {current}; switch to the bookmark \ |
| 381 |
holding it first", |
| 382 |
c.short(12) |
| 383 |
)); |
| 384 |
} |
| 385 |
let named: Vec<String> = onto.iter().map(|h| h.short(12)).collect(); |
| 386 |
let label = format!( |
| 387 |
"oxy rebase {} --onto {} --into-working-copy", |
| 388 |
c.short(12), |
| 389 |
named.join(" ") |
| 390 |
); |
| 391 |
self.merge_heads(onto.to_vec(), &label) |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
fn merge_heads(&self, other: Vec<ChangeHash>, label: &str) -> Result<MergeReport> { |
| 397 |
let (op_ref, op) = self.head_op()?; |
| 398 |
let current = self.config.bookmark.clone(); |
| 7b9809e6 Andy McCall ↶ |
399 |
let local = self.heads_of(&op.view, ¤t)?; |
| 400 |
let mut all = local.clone(); |
| 401 |
all.extend(other); |
| 402 |
let mut heads = self.load_state(&all)?.maximal(&all); |
| 403 |
heads.sort(); |
| 404 |
let mut sorted_local = local.clone(); |
| 405 |
sorted_local.sort(); |
| 406 |
if heads == sorted_local { |
| 75167672 Andy McCall |
407 |
return Ok(MergeReport { |
| 408 |
heads, |
| 409 |
changed: false, |
| 410 |
checked_out: false, |
| 411 |
}); |
| 7b9809e6 Andy McCall ↶ |
412 |
} |
| 413 |
let mut view = op.view.clone(); |
| 75167672 Andy McCall |
414 |
view.bookmarks.insert( |
| 415 |
current, |
| 416 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 417 |
); |
| e266b7d4 Andy McCall ↶ |
418 |
self.record_op(&[op_ref], label, view)?; |
| 419 |
let snap = self.snapshot(label)?; |
| 7b9809e6 Andy McCall ↶ |
420 |
let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty(); |
| 421 |
let mut base = snap.base.maximal(&snap.wc.deps); |
| 422 |
base.sort(); |
| 423 |
let mut checked_out = false; |
| 1c2bd9ab Andy McCall ↶ |
424 |
|
| 425 |
|
| 426 |
if clean { |
| e266b7d4 Andy McCall ↶ |
427 |
self.checkout(&heads, false, &format!("{label} (checkout)"))?; |
| 7b9809e6 Andy McCall ↶ |
428 |
checked_out = true; |
| 429 |
} |
| 75167672 Andy McCall |
430 |
Ok(MergeReport { |
| 431 |
heads, |
| 432 |
changed: true, |
| 433 |
checked_out, |
| 434 |
}) |
| 7b9809e6 Andy McCall ↶ |
435 |
} |
| 61e3bce7 Andy McCall ↶ |
436 |
} |
| 437 |
|
| 438 |
|
| 439 |
enum Carried { |
| 1a3b6203 Andy McCall ↶ |
440 |
|
| 441 |
Subrepo, |
| 61e3bce7 Andy McCall ↶ |
442 |
Symlink(Vec<u8>), |
| 443 |
|
| 444 |
File(bool, Vec<u8>), |
| 445 |
Absent, |
| 7b9809e6 Andy McCall ↶ |
446 |
} |