| 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 |
pub remote: Option<String>, |
| 19 |
} |
| 20 |
|
| 21 |
#[derive(Clone, Debug)] |
| 22 |
pub struct MergeReport { |
| 23 |
pub heads: Vec<ChangeHash>, |
| 24 |
pub changed: bool, |
| 25 |
pub checked_out: bool, |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
pub fn valid_reference_name(name: &str) -> bool { |
| 31 |
!name.is_empty() |
| 32 |
&& name.len() <= 255 |
| 33 |
&& !name.starts_with('-') |
| 34 |
&& !name.chars().any(|c| c.is_control()) |
| 35 |
&& !name.contains("..") |
| 36 |
&& !name.contains("//") |
| 37 |
&& !name.contains("@{") |
| 38 |
&& name != "policy" |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pub fn remote_bookmark<'a>(view: &'a View, spec: &str) -> Option<(&'a str, &'a str, ObjectRef)> { |
| 45 |
view.remotes |
| 46 |
.iter() |
| 47 |
.filter_map(|(remote, bookmarks)| { |
| 48 |
let rest = spec.strip_prefix(remote.as_str())?.strip_prefix('/')?; |
| 49 |
let (name, state) = bookmarks.get_key_value(rest)?; |
| 50 |
Some((remote.as_str(), name.as_str(), *state)) |
| 51 |
}) |
| 52 |
.max_by_key(|(remote, _, _)| remote.len()) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
#[derive(Clone, Debug)] |
| 57 |
pub struct RemoteOnlyBookmark { |
| 58 |
pub remote: String, |
| 59 |
pub name: String, |
| 60 |
pub heads: Vec<ChangeHash>, |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 65 |
pub struct StateGap { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub earlier: Vec<ChangeHash>, |
| 70 |
|
| 71 |
pub lacks: usize, |
| 72 |
|
| 73 |
|
| 74 |
pub upto: Vec<ChangeHash>, |
| 75 |
} |
| 76 |
|
| 77 |
impl Repo { |
| 78 |
pub(crate) fn workspace_bookmark_path(&self) -> PathBuf { |
| 79 |
self.oxy |
| 80 |
.join("workspaces") |
| 81 |
.join(self.workspace.to_hex()) |
| 82 |
.join("bookmark") |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
pub fn default_remote(&self) -> String { |
| 89 |
if let Some(r) = &self.config.default_remote { |
| 90 |
return r.clone(); |
| 91 |
} |
| 92 |
match self.config.remotes.keys().collect::<Vec<_>>().as_slice() { |
| 93 |
[only] if *only != "origin" => (*only).clone(), |
| 94 |
_ => "origin".into(), |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
fn tracking_remote(&self, view: &View, name: &str) -> Option<String> { |
| 102 |
|
| 103 |
let configured = |r: &str| self.config.remotes.contains_key(r); |
| 104 |
let default = self.default_remote(); |
| 105 |
if configured(&default) |
| 106 |
&& view |
| 107 |
.remotes |
| 108 |
.get(&default) |
| 109 |
.is_some_and(|b| b.contains_key(name)) |
| 110 |
{ |
| 111 |
return Some(default); |
| 112 |
} |
| 113 |
view.remotes |
| 114 |
.iter() |
| 115 |
.find(|(r, b)| configured(r) && b.contains_key(name)) |
| 116 |
.map(|(r, _)| r.clone()) |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
pub fn tracking(&self, remote: &str, name: &str) -> Result<Option<(usize, usize)>> { |
| 123 |
let view = self.view()?; |
| 124 |
let (Some(st), Some(rs)) = ( |
| 125 |
view.bookmarks.get(name), |
| 126 |
view.remotes.get(remote).and_then(|b| b.get(name)), |
| 127 |
) else { |
| 128 |
return Ok(None); |
| 129 |
}; |
| 130 |
let heads = self.store.get_state(st)?.heads; |
| 131 |
let remote_heads = self.store.get_state(rs)?.heads; |
| 132 |
Ok(Some(( |
| 133 |
self.negotiate(&remote_heads, &heads)?.len(), |
| 134 |
self.negotiate(&heads, &remote_heads)?.len(), |
| 135 |
))) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
pub fn bookmarks(&self) -> Result<Vec<BookmarkInfo>> { |
| 141 |
let view = self.view()?; |
| 142 |
let mut out = Vec::new(); |
| 143 |
for (name, st) in &view.bookmarks { |
| 144 |
let heads = self.store.get_state(st)?.heads; |
| 145 |
let remote = self.tracking_remote(&view, name); |
| 146 |
let (ahead, behind) = match remote |
| 147 |
.as_ref() |
| 148 |
.and_then(|r| view.remotes.get(r)) |
| 149 |
.and_then(|b| b.get(name)) |
| 150 |
{ |
| 151 |
Some(rs) => { |
| 152 |
let remote_heads = self.store.get_state(rs)?.heads; |
| 153 |
( |
| 154 |
self.negotiate(&remote_heads, &heads).map(|v| v.len()).ok(), |
| 155 |
self.negotiate(&heads, &remote_heads).map(|v| v.len()).ok(), |
| 156 |
) |
| 157 |
} |
| 158 |
None => (None, None), |
| 159 |
}; |
| 160 |
out.push(BookmarkInfo { |
| 161 |
name: name.clone(), |
| 162 |
heads, |
| 163 |
current: *name == self.config.bookmark, |
| 164 |
ahead, |
| 165 |
behind, |
| 166 |
remote, |
| 167 |
}); |
| 168 |
} |
| 169 |
Ok(out) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
pub fn catch_up_bookmark(&self, name: &str) -> Result<Option<usize>> { |
| 176 |
let (op_ref, op) = self.head_op()?; |
| 177 |
let remote = self.default_remote(); |
| 178 |
let (Some(local), Some(rs)) = ( |
| 179 |
op.view.bookmarks.get(name).copied(), |
| 180 |
op.view |
| 181 |
.remotes |
| 182 |
.get(&remote) |
| 183 |
.and_then(|b| b.get(name)) |
| 184 |
.copied(), |
| 185 |
) else { |
| 186 |
return Ok(None); |
| 187 |
}; |
| 188 |
if local == rs { |
| 189 |
return Ok(None); |
| 190 |
} |
| 191 |
let heads = self.store.get_state(&local)?.heads; |
| 192 |
let remote_heads = self.store.get_state(&rs)?.heads; |
| 193 |
if !self.negotiate(&remote_heads, &heads)?.is_empty() { |
| 194 |
return Ok(None); |
| 195 |
} |
| 196 |
let behind = self.negotiate(&heads, &remote_heads)?.len(); |
| 197 |
if behind == 0 { |
| 198 |
return Ok(None); |
| 199 |
} |
| 200 |
let mut view = op.view.clone(); |
| 201 |
view.bookmarks.insert(name.to_string(), rs); |
| 202 |
self.record_op( |
| 203 |
&[op_ref], |
| 204 |
&format!("oxy bookmark switch {name} (catch up with {remote})"), |
| 205 |
view, |
| 206 |
)?; |
| 207 |
Ok(Some(behind)) |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
pub fn remote_only_bookmarks(&self) -> Result<Vec<RemoteOnlyBookmark>> { |
| 213 |
let view = self.view()?; |
| 214 |
let mut out = Vec::new(); |
| 215 |
for (remote, bookmarks) in &view.remotes { |
| 216 |
for (name, st) in bookmarks { |
| 217 |
if view.bookmarks.contains_key(name) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
out.push(RemoteOnlyBookmark { |
| 221 |
remote: remote.clone(), |
| 222 |
name: name.clone(), |
| 223 |
heads: self.store.get_state(st)?.heads, |
| 224 |
}); |
| 225 |
} |
| 226 |
} |
| 227 |
Ok(out) |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
pub fn local_bookmark_for(&self, spec: &str) -> Result<(String, Option<String>)> { |
| 237 |
let (op_ref, op) = self.head_op()?; |
| 238 |
if op.view.bookmarks.contains_key(spec) { |
| 239 |
return Ok((spec.to_string(), None)); |
| 240 |
} |
| 241 |
let (remote, name, state) = match remote_bookmark(&op.view, spec) { |
| 242 |
Some((remote, name, state)) => (remote.to_string(), name.to_string(), state), |
| 243 |
None => { |
| 244 |
let mut having: Vec<&String> = op |
| 245 |
.view |
| 246 |
.remotes |
| 247 |
.iter() |
| 248 |
.filter(|(_, b)| b.contains_key(spec)) |
| 249 |
.map(|(r, _)| r) |
| 250 |
.collect(); |
| 251 |
if let Some(i) = having.iter().position(|r| *r == "origin") { |
| 252 |
having = vec![having[i]]; |
| 253 |
} |
| 254 |
match having.as_slice() { |
| 255 |
[] => return fail(format!("no bookmark {spec}, here or on a remote")), |
| 256 |
[r] => { |
| 257 |
let state = op.view.remotes[*r][spec]; |
| 258 |
(r.to_string(), spec.to_string(), state) |
| 259 |
} |
| 260 |
several => { |
| 261 |
let names: Vec<String> = |
| 262 |
several.iter().map(|r| format!("{r}/{spec}")).collect(); |
| 263 |
return fail(format!( |
| 264 |
"several remotes have {spec}: name one, as {}", |
| 265 |
names.join(" or ") |
| 266 |
)); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
}; |
| 271 |
if op.view.bookmarks.contains_key(&name) { |
| 272 |
|
| 273 |
return Ok((name, None)); |
| 274 |
} |
| 275 |
let mut view = op.view.clone(); |
| 276 |
view.bookmarks.insert(name.clone(), state); |
| 277 |
self.record_op( |
| 278 |
&[op_ref], |
| 279 |
&format!("oxy bookmark track {remote}/{name}"), |
| 280 |
view, |
| 281 |
)?; |
| 282 |
Ok((name, Some(remote))) |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
pub fn bookmark_create( |
| 287 |
&self, |
| 288 |
name: &str, |
| 289 |
heads: Option<&[ChangeHash]>, |
| 290 |
) -> Result<Vec<ChangeHash>> { |
| 291 |
if !valid_reference_name(name) { |
| 292 |
return fail(format!("{name:?} is not a valid bookmark name")); |
| 293 |
} |
| 294 |
let (op_ref, op) = self.head_op()?; |
| 295 |
if op.view.bookmarks.contains_key(name) { |
| 296 |
return fail(format!("bookmark {name} already exists")); |
| 297 |
} |
| 298 |
let heads = match heads { |
| 299 |
Some(h) => self.load_state_history(h)?.maximal(h), |
| 300 |
None => { |
| 301 |
let (_, wc) = self.working_copy()?; |
| 302 |
self.load_state_history(&wc.deps)?.maximal(&wc.deps) |
| 303 |
} |
| 304 |
}; |
| 305 |
let mut view = op.view.clone(); |
| 306 |
view.bookmarks.insert( |
| 307 |
name.to_string(), |
| 308 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 309 |
); |
| 310 |
self.record_op(&[op_ref], &format!("oxy bookmark create {name}"), view)?; |
| 311 |
Ok(heads) |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
pub fn state_gap(&self, from: &[ChangeHash], at: &[ChangeHash]) -> Result<StateGap> { |
| 319 |
let mut all = from.to_vec(); |
| 320 |
all.extend_from_slice(at); |
| 321 |
let st = self.load_state_history(&all)?; |
| 322 |
let with_closure = |hs: &[ChangeHash]| { |
| 323 |
let mut s: HashSet<ChangeHash> = hs.iter().copied().collect(); |
| 324 |
for h in hs { |
| 325 |
s.extend(st.closure(h).iter().copied()); |
| 326 |
} |
| 327 |
s |
| 328 |
}; |
| 329 |
let from_set = with_closure(from); |
| 330 |
let at_set = with_closure(at); |
| 331 |
let key = |h: &ChangeHash| st.get(h).map(|m| (m.hlc, *h)); |
| 332 |
let last = at.iter().filter_map(key).max(); |
| 333 |
let missing: Vec<ChangeHash> = from_set |
| 334 |
.iter() |
| 335 |
.filter(|h| !at_set.contains(h)) |
| 336 |
.copied() |
| 337 |
.collect(); |
| 338 |
let mut earlier: Vec<ChangeHash> = missing |
| 339 |
.iter() |
| 340 |
.filter(|h| matches!((key(h), last), (Some(k), Some(l)) if k < l)) |
| 341 |
.copied() |
| 342 |
.collect(); |
| 343 |
earlier.sort_by_key(key); |
| 344 |
let mut candidates = at.to_vec(); |
| 345 |
candidates.extend_from_slice(&earlier); |
| 346 |
Ok(StateGap { |
| 347 |
lacks: missing.len(), |
| 348 |
upto: st.maximal(&candidates), |
| 349 |
earlier, |
| 350 |
}) |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
pub fn bookmark_set(&self, name: &str, heads: &[ChangeHash]) -> Result<Vec<ChangeHash>> { |
| 355 |
let (op_ref, op) = self.head_op()?; |
| 356 |
if !op.view.bookmarks.contains_key(name) { |
| 357 |
return fail(format!( |
| 358 |
"no bookmark {name}; create it with `oxy bookmark create`" |
| 359 |
)); |
| 360 |
} |
| 361 |
let heads = self.load_state_history(heads)?.maximal(heads); |
| 362 |
let mut view = op.view.clone(); |
| 363 |
view.bookmarks.insert( |
| 364 |
name.to_string(), |
| 365 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 366 |
); |
| 367 |
self.record_op(&[op_ref], &format!("oxy bookmark set {name}"), view)?; |
| 368 |
if name == self.config.bookmark { |
| 369 |
self.drop_manifest(); |
| 370 |
} |
| 371 |
Ok(heads) |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
pub fn bookmark_delete(&self, name: &str) -> Result<()> { |
| 376 |
if name == self.config.bookmark { |
| 377 |
return fail(format!( |
| 378 |
"{name} is this workspace's current bookmark; switch to another first" |
| 379 |
)); |
| 380 |
} |
| 381 |
for w in self.workspaces()? { |
| 382 |
if !w.current && self.workspace_bookmark_of(&w.id).as_deref() == Some(name) { |
| 383 |
return fail(format!( |
| 384 |
"{name} is the current bookmark of workspace {} at {}", |
| 385 |
w.id.short(8), |
| 386 |
w.path.display() |
| 387 |
)); |
| 388 |
} |
| 389 |
} |
| 390 |
let (op_ref, op) = self.head_op()?; |
| 391 |
let mut view = op.view.clone(); |
| 392 |
if view.bookmarks.remove(name).is_none() { |
| 393 |
return fail(format!("no bookmark {name}")); |
| 394 |
} |
| 395 |
self.record_op(&[op_ref], &format!("oxy bookmark delete {name}"), view)?; |
| 396 |
Ok(()) |
| 397 |
} |
| 398 |
|
| 399 |
fn workspace_bookmark_of(&self, id: &WorkspaceId) -> Option<String> { |
| 400 |
fs::read_to_string( |
| 401 |
self.oxy |
| 402 |
.join("workspaces") |
| 403 |
.join(id.to_hex()) |
| 404 |
.join("bookmark"), |
| 405 |
) |
| 406 |
.ok() |
| 407 |
.map(|s| s.trim().to_string()) |
| 408 |
.filter(|s| !s.is_empty()) |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
pub fn bookmark_switch(&mut self, name: &str, discard: bool) -> Result<CheckoutReport> { |
| 413 |
let _wc = self.lock_working_copy()?; |
| 414 |
let (name, _) = self.local_bookmark_for(name)?; |
| 415 |
let name = name.as_str(); |
| 416 |
let view = self.view()?; |
| 417 |
let Some(st) = view.bookmarks.get(name) else { |
| 418 |
return fail(format!("no bookmark {name}")); |
| 419 |
}; |
| 420 |
let heads = self.store.get_state(st)?.heads; |
| 421 |
let report = self.checkout(&heads, discard, &format!("oxy bookmark switch {name}"))?; |
| 422 |
fs::create_dir_all(self.workspace_bookmark_path().parent().unwrap())?; |
| 423 |
write_atomic(&self.workspace_bookmark_path(), name.as_bytes())?; |
| 424 |
self.config.bookmark = name.to_string(); |
| 425 |
Ok(report) |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
pub fn bookmark_switch_carry(&mut self, name: &str) -> Result<(CheckoutReport, Vec<String>)> { |
| 432 |
let _wc = self.lock_working_copy()?; |
| 433 |
let (name, _) = self.local_bookmark_for(name)?; |
| 434 |
let name = name.as_str(); |
| 435 |
let view = self.view()?; |
| 436 |
let Some(st) = view.bookmarks.get(name) else { |
| 437 |
return fail(format!("no bookmark {name}")); |
| 438 |
}; |
| 439 |
let heads = self.store.get_state(st)?.heads; |
| 440 |
let out = |
| 441 |
self.checkout_carry(&heads, name, &format!("oxy bookmark switch {name} --carry"))?; |
| 442 |
fs::create_dir_all(self.workspace_bookmark_path().parent().unwrap())?; |
| 443 |
write_atomic(&self.workspace_bookmark_path(), name.as_bytes())?; |
| 444 |
self.config.bookmark = name.to_string(); |
| 445 |
self.snapshot(&format!("oxy bookmark switch {name} --carry"))?; |
| 446 |
Ok(out) |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
pub fn checkout_carry( |
| 456 |
&self, |
| 457 |
heads: &[ChangeHash], |
| 458 |
target: &str, |
| 459 |
command: &str, |
| 460 |
) -> Result<(CheckoutReport, Vec<String>)> { |
| 461 |
let _wc = self.lock_working_copy()?; |
| 462 |
let snap = self.snapshot(command)?; |
| 463 |
|
| 464 |
|
| 465 |
let mut touched: BTreeSet<String> = snap.statuses.iter().map(|s| s.path.clone()).collect(); |
| 466 |
for (p, f) in &snap.base_snapshot.files { |
| 467 |
if f.kind != NodeKind::Dir |
| 468 |
&& f.kind != NodeKind::Subrepo |
| 469 |
&& fs::symlink_metadata(self.root.join(p)).is_err() |
| 470 |
{ |
| 471 |
touched.insert(p.clone()); |
| 472 |
} |
| 473 |
} |
| 474 |
let target_state = self.load_state(heads)?.materialize(); |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
let at_target_pin = |p: &String| -> bool { |
| 479 |
let Some(f) = target_state.files.get(p) else { |
| 480 |
return false; |
| 481 |
}; |
| 482 |
if f.kind != NodeKind::Subrepo { |
| 483 |
return false; |
| 484 |
} |
| 485 |
let FileContent::Opaque { blobs } = &f.content else { |
| 486 |
return false; |
| 487 |
}; |
| 488 |
let Some(want) = blobs |
| 489 |
.first() |
| 490 |
.and_then(|b| self.read_blob(b).ok()) |
| 491 |
.and_then(|b| Pin::decode(&b).ok()) |
| 492 |
else { |
| 493 |
return false; |
| 494 |
}; |
| 495 |
let hint = self |
| 496 |
.modules_on_disk() |
| 497 |
.entries |
| 498 |
.get(p) |
| 499 |
.and_then(|m| m.bookmark.clone()); |
| 500 |
super::subrepos::read_pin(&self.root.join(p), hint.as_deref()) |
| 501 |
.is_ok_and(|have| have.same_state(&want)) |
| 502 |
}; |
| 503 |
touched.retain(|p| !at_target_pin(p)); |
| 504 |
let same = |p: &String| { |
| 505 |
let a = snap.base_snapshot.files.get(p); |
| 506 |
let b = target_state.files.get(p); |
| 507 |
match (a, b) { |
| 508 |
(None, None) => true, |
| 509 |
(Some(a), Some(b)) => { |
| 510 |
a.kind == b.kind && a.mode == b.mode && a.content == b.content |
| 511 |
} |
| 512 |
_ => false, |
| 513 |
} |
| 514 |
}; |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
let already = |p: &String| -> bool { |
| 520 |
let full = self.root.join(p); |
| 521 |
match (fs::symlink_metadata(&full), target_state.files.get(p)) { |
| 522 |
(Err(_), None) => true, |
| 523 |
(Ok(m), Some(f)) if m.is_file() && f.kind == NodeKind::File => { |
| 524 |
if is_executable(&m) != f.mode.exec { |
| 525 |
return false; |
| 526 |
} |
| 527 |
let want = match &f.content { |
| 528 |
FileContent::Text { bytes, .. } => Some(bytes.clone()), |
| 529 |
FileContent::Opaque { blobs } if blobs.len() == 1 => { |
| 530 |
self.read_blob(&blobs[0]).ok() |
| 531 |
} |
| 532 |
_ => None, |
| 533 |
}; |
| 534 |
want.is_some_and(|w| fs::read(&full).is_ok_and(|have| have == w)) |
| 535 |
} |
| 536 |
_ => false, |
| 537 |
} |
| 538 |
}; |
| 539 |
touched.retain(|p| same(p) || !already(p)); |
| 540 |
let differ: Vec<&String> = touched.iter().filter(|p| !same(p)).collect(); |
| 541 |
if !differ.is_empty() { |
| 542 |
let list: Vec<&str> = differ.iter().take(10).map(|p| p.as_str()).collect(); |
| 543 |
return fail(format!( |
| 544 |
"{target} has other versions of {} you changed ({}{}); commit first, or drop the changes with --discard", |
| 545 |
if differ.len() == 1 { "a file" } else { "files" }, |
| 546 |
list.join(", "), |
| 547 |
if differ.len() > 10 { ", …" } else { "" } |
| 548 |
)); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
let keep: BTreeSet<String> = touched |
| 553 |
.iter() |
| 554 |
.filter(|p| { |
| 555 |
snap.base_snapshot |
| 556 |
.files |
| 557 |
.get(*p) |
| 558 |
.or_else(|| target_state.files.get(*p)) |
| 559 |
.is_some_and(|f| f.kind == NodeKind::Subrepo) |
| 560 |
}) |
| 561 |
.cloned() |
| 562 |
.collect(); |
| 563 |
|
| 564 |
let mut carried: Vec<(String, Carried)> = Vec::new(); |
| 565 |
for p in &touched { |
| 566 |
if keep.contains(p) { |
| 567 |
carried.push((p.clone(), Carried::Subrepo)); |
| 568 |
continue; |
| 569 |
} |
| 570 |
let full = self.root.join(p); |
| 571 |
let item = match fs::symlink_metadata(&full) { |
| 572 |
Ok(m) if m.file_type().is_symlink() => { |
| 573 |
Carried::Symlink(fs::read_link(&full)?.to_string_lossy().as_bytes().to_vec()) |
| 574 |
} |
| 575 |
Ok(m) if m.is_file() => Carried::File(is_executable(&m), fs::read(&full)?), |
| 576 |
Ok(_) => continue, |
| 577 |
Err(_) => Carried::Absent, |
| 578 |
}; |
| 579 |
carried.push((p.clone(), item)); |
| 580 |
} |
| 581 |
let mut report = self.checkout_keeping(heads, true, command, &keep)?; |
| 582 |
for (p, item) in &carried { |
| 583 |
match item { |
| 584 |
Carried::Symlink(target) => self.write_symlink(p, target)?, |
| 585 |
Carried::File(exec, bytes) => self.write_file(p, bytes, *exec)?, |
| 586 |
Carried::Absent => { |
| 587 |
let _ = fs::remove_file(self.root.join(p)); |
| 588 |
} |
| 589 |
Carried::Subrepo => {} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
let back: BTreeSet<&str> = carried |
| 596 |
.iter() |
| 597 |
.filter(|(p, item)| { |
| 598 |
matches!(item, Carried::File(..) | Carried::Symlink(_)) |
| 599 |
&& !target_state.files.contains_key(p) |
| 600 |
}) |
| 601 |
.map(|(p, _)| p.as_str()) |
| 602 |
.collect(); |
| 603 |
report.removed = report.removed.saturating_sub(back.len()); |
| 604 |
report.removed_paths.retain(|p| !back.contains(p.as_str())); |
| 605 |
self.drop_manifest(); |
| 606 |
self.snapshot(command)?; |
| 607 |
Ok((report, carried.into_iter().map(|(p, _)| p).collect())) |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
pub(crate) fn set_workspace_bookmark(&self, id: &WorkspaceId, name: &str) -> Result<()> { |
| 612 |
let dir = self.oxy.join("workspaces").join(id.to_hex()); |
| 613 |
fs::create_dir_all(&dir)?; |
| 614 |
write_atomic(&dir.join("bookmark"), name.as_bytes()) |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
pub fn merge(&self, name: &str) -> Result<MergeReport> { |
| 622 |
let _wc = self.lock_working_copy()?; |
| 623 |
let (_, op) = self.head_op()?; |
| 624 |
if name == self.config.bookmark { |
| 625 |
return fail(format!("{name} is the current bookmark")); |
| 626 |
} |
| 627 |
let here = op.view.bookmarks.get(name).copied(); |
| 628 |
let Some(state) = here.or_else(|| remote_bookmark(&op.view, name).map(|(_, _, st)| st)) |
| 629 |
else { |
| 630 |
return fail(format!( |
| 631 |
"no bookmark {name}, here or at a remote (oxy bookmark list --all lists them)" |
| 632 |
)); |
| 633 |
}; |
| 634 |
let other = self.store.get_state(&state)?.heads; |
| 635 |
self.merge_heads(other, &format!("oxy merge {name}")) |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
pub fn rebase_into_working_copy( |
| 644 |
&self, |
| 645 |
c: &ChangeHash, |
| 646 |
onto: &[ChangeHash], |
| 647 |
) -> Result<MergeReport> { |
| 648 |
let _wc = self.lock_working_copy()?; |
| 649 |
let (_, op) = self.head_op()?; |
| 650 |
let current = self.config.bookmark.clone(); |
| 651 |
let local = self.heads_of(&op.view, ¤t)?; |
| 652 |
if !self.load_state_indexed(&local)?.contains(c) { |
| 653 |
return fail(format!( |
| 654 |
"#{} is not on the current bookmark {current}; switch to the bookmark \ |
| 655 |
holding it first", |
| 656 |
c.short(12) |
| 657 |
)); |
| 658 |
} |
| 659 |
let named: Vec<String> = onto.iter().map(|h| h.short(12)).collect(); |
| 660 |
let label = format!( |
| 661 |
"oxy rebase {} --onto {} --into-working-copy", |
| 662 |
c.short(12), |
| 663 |
named.join(" ") |
| 664 |
); |
| 665 |
self.merge_heads(onto.to_vec(), &label) |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
fn merge_heads(&self, other: Vec<ChangeHash>, label: &str) -> Result<MergeReport> { |
| 671 |
let (op_ref, op) = self.head_op()?; |
| 672 |
let current = self.config.bookmark.clone(); |
| 673 |
let local = self.heads_of(&op.view, ¤t)?; |
| 674 |
let mut all = local.clone(); |
| 675 |
all.extend(other); |
| 676 |
let mut heads = self.load_state_history(&all)?.maximal(&all); |
| 677 |
heads.sort(); |
| 678 |
let mut sorted_local = local.clone(); |
| 679 |
sorted_local.sort(); |
| 680 |
if heads == sorted_local { |
| 681 |
return Ok(MergeReport { |
| 682 |
heads, |
| 683 |
changed: false, |
| 684 |
checked_out: false, |
| 685 |
}); |
| 686 |
} |
| 687 |
let mut view = op.view.clone(); |
| 688 |
view.bookmarks.insert( |
| 689 |
current, |
| 690 |
self.store.put_state(&StateObject::new(heads.clone()))?, |
| 691 |
); |
| 692 |
self.record_op(&[op_ref], label, view)?; |
| 693 |
let snap = self.snapshot(label)?; |
| 694 |
let clean = snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty(); |
| 695 |
let mut base = snap.base.maximal(&snap.wc.deps); |
| 696 |
base.sort(); |
| 697 |
let mut checked_out = false; |
| 698 |
|
| 699 |
|
| 700 |
if clean { |
| 701 |
self.checkout(&heads, false, &format!("{label} (checkout)"))?; |
| 702 |
checked_out = true; |
| 703 |
} |
| 704 |
Ok(MergeReport { |
| 705 |
heads, |
| 706 |
changed: true, |
| 707 |
checked_out, |
| 708 |
}) |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
enum Carried { |
| 714 |
|
| 715 |
Subrepo, |
| 716 |
Symlink(Vec<u8>), |
| 717 |
|
| 718 |
File(bool, Vec<u8>), |
| 719 |
Absent, |
| 720 |
} |