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