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