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