| 1 | |
|
| 2 | |
|
| 3 | | use super::format::{ |
| 4 | | author_name, awaits_seal, deps_line, deps_of, plural, short_id, signature_line, |
| 5 | | signature_mark_of, subject_of, via, when, |
| 6 | | }; |
| 7 | | use super::json; |
| 8 | | use super::open; |
| 9 | | use super::ui; |
| 10 | | use anyhow::Result; |
| 11 | | use oxy_core::objects::ChangeKind; |
| 12 | | use oxy_core::repo::SignatureStatus; |
| 13 | | use oxy_core::repo::{render_change, ChangeStatus, Repo}; |
| 14 | | use std::io::Write; |
| 15 | | use std::path::PathBuf; |
| 16 | |
|
| 17 | 19× | pub fn init(path: Option<PathBuf>, bare: bool, store: Option<&str>) -> Result<i32> { |
| 18 | 19× | let path = path.unwrap_or(std::env::current_dir()?); |
| 19 | 19× | let repo = Repo::init_new(&path, bare, store)?; |
| 20 | 19× | println!( |
| 21 | | "{} {}oxy repository in {}", |
| 22 | 19× | ui::done("Initialized"), |
| 23 | 19× | if bare { "bare " } else { "" }, |
| 24 | 19× | ui::path(repo.root().display()) |
| 25 | | ); |
| 26 | 19× | println!( |
| 27 | | "{}", |
| 28 | 19× | ui::kv("bookmark", ui::bookmark(&repo.config.bookmark)) |
| 29 | | ); |
| 30 | 19× | if let Some(url) = &repo.config.storage_url { |
| 31 | 0× | println!( |
| 32 | 0× | "{}", |
| 33 | 0× | ui::kv( |
| 34 | 0× | "store", |
| 35 | 0× | format!( |
| 36 | 0× | "{url} (cache in {}, up to {} MiB)", |
| 37 | 0× | ui::path(repo.root().join(".oxy/cache").display()), |
| 38 | 0× | repo.config.storage_cache >> 20 |
| 39 | 0× | ) |
| 40 | 0× | ) |
| 41 | 0× | ); |
| 42 | 19× | } |
| 43 | 19× | println!( |
| 44 | | "{}", |
| 45 | 19× | ui::kv( |
| 46 | 19× | "author", |
| 47 | 19× | format!( |
| 48 | | "{} <{}>", |
| 49 | 19× | ui::author(&repo.config.author_name), |
| 50 | | repo.config.author_email |
| 51 | | ) |
| 52 | | ) |
| 53 | | ); |
| 54 | 19× | Ok(0) |
| 55 | 19× | } |
| 56 | |
|
| 57 | 3× | fn status_tag(s: ChangeStatus) -> &'static str { |
| 58 | 3× | match s { |
| 59 | 1× | ChangeStatus::Added => "A", |
| 60 | 1× | ChangeStatus::Modified => "M", |
| 61 | 0× | ChangeStatus::Removed => "D", |
| 62 | 0× | ChangeStatus::Renamed => "R", |
| 63 | 1× | ChangeStatus::ModeChanged => "X", |
| 64 | 0× | ChangeStatus::ConflictEdited => "C!", |
| 65 | 0× | ChangeStatus::Resolved => "V", |
| 66 | | } |
| 67 | 3× | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | 46× | fn print_skipped_large(repo: &Repo) { |
| 72 | 46× | let skipped = repo.skipped_large(); |
| 73 | 46× | if skipped.is_empty() { |
| 74 | 46× | return; |
| 75 | 0× | } |
| 76 | 0× | let size = |n: u64| { |
| 77 | 0× | if n >= 1 << 30 { |
| 78 | 0× | format!("{:.1} GiB", n as f64 / (1u64 << 30) as f64) |
| 79 | | } else { |
| 80 | 0× | format!("{:.0} MiB", n as f64 / (1u64 << 20) as f64) |
| 81 | | } |
| 82 | 0× | }; |
| 83 | 0× | println!(); |
| 84 | 0× | println!( |
| 85 | | "{} {}", |
| 86 | 0× | ui::section(ui::warn("Not tracked")), |
| 87 | 0× | ui::dim(format!("new files over {}", size(repo.config.max_new_file))) |
| 88 | | ); |
| 89 | 0× | for (p, n) in &skipped { |
| 90 | 0× | println!(" {} {} {}", ui::caution(), ui::path(p), ui::dim(size(*n))); |
| 91 | 0× | } |
| 92 | 0× | println!( |
| 93 | | " {} {}", |
| 94 | 0× | ui::note(), |
| 95 | 0× | ui::dim( |
| 96 | | "to leave them out quietly, list them in .oxyignore; to track one, \ |
| 97 | | raise snapshot.max-new-file-size (oxy config snapshot.max-new-file-size 2G)" |
| 98 | | ) |
| 99 | | ); |
| 100 | 46× | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | 3× | fn print_removed(r: &oxy_core::repo::CheckoutReport) { |
| 106 | 3× | if r.removed == 0 { |
| 107 | 2× | return; |
| 108 | 1× | } |
| 109 | 1× | let more = r.removed.saturating_sub(r.removed_paths.len()); |
| 110 | 1× | let names = r.removed_paths.join(", "); |
| 111 | 1× | let tail = if more > 0 { |
| 112 | 0× | format!(" and {more} more") |
| 113 | | } else { |
| 114 | 1× | String::new() |
| 115 | | }; |
| 116 | 1× | let lead = if r.removed >= 20 { |
| 117 | 0× | ui::caution() |
| 118 | | } else { |
| 119 | 1× | ui::note() |
| 120 | | }; |
| 121 | 1× | println!( |
| 122 | | " {} {}", |
| 123 | | lead, |
| 124 | 1× | ui::dim(format!( |
| 125 | | "removed from disk (the bookmark does not have them): {names}{tail}" |
| 126 | | )) |
| 127 | | ); |
| 128 | 3× | } |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | 3× | fn print_carried(paths: &[String]) { |
| 133 | 3× | if paths.is_empty() { |
| 134 | 3× | return; |
| 135 | 0× | } |
| 136 | 0× | let shown: Vec<&str> = paths.iter().take(10).map(String::as_str).collect(); |
| 137 | 0× | let more = paths.len().saturating_sub(shown.len()); |
| 138 | 0× | println!( |
| 139 | | " {} {}", |
| 140 | 0× | ui::note(), |
| 141 | 0× | ui::dim(format!( |
| 142 | | "carried, as they were: {}{}", |
| 143 | 0× | shown.join(", "), |
| 144 | 0× | if more > 0 { |
| 145 | 0× | format!(" and {more} more") |
| 146 | | } else { |
| 147 | 0× | String::new() |
| 148 | | } |
| 149 | | )) |
| 150 | | ); |
| 151 | 3× | } |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | 46× | fn print_untracked_nested(repo: &Repo, nested: &[String]) { |
| 156 | 46× | if nested.is_empty() { |
| 157 | 44× | return; |
| 158 | 2× | } |
| 159 | 2× | println!(); |
| 160 | 2× | println!( |
| 161 | | "{} {}", |
| 162 | 2× | ui::section(ui::warn("Not tracked")), |
| 163 | 2× | ui::dim("folders holding a repository of their own") |
| 164 | | ); |
| 165 | 2× | for p in nested { |
| 166 | 2× | let kind = if repo.root().join(p).join(".git").exists() { |
| 167 | 2× | "Git" |
| 168 | | } else { |
| 169 | 0× | "Oxygen" |
| 170 | | }; |
| 171 | 2× | println!( |
| 172 | | " {} {} {}", |
| 173 | 2× | ui::caution(), |
| 174 | 2× | ui::path(format!("{p}/")), |
| 175 | 2× | ui::dim(format!("a nested {kind} repository")) |
| 176 | | ); |
| 177 | | } |
| 178 | 2× | println!( |
| 179 | | " {} {}", |
| 180 | 2× | ui::note(), |
| 181 | 2× | ui::dim( |
| 182 | | "to track one's files, remove its .git (or .oxy); to keep it a repository of its own, \ |
| 183 | | oxy submodule add URL PATH; to leave it out quietly, list it in .oxyignore" |
| 184 | | ) |
| 185 | | ); |
| 186 | 46× | } |
| 187 | |
|
| 188 | |
|
| 189 | 10× | fn print_statuses(statuses: &[oxy_core::repo::PathStatus]) { |
| 190 | 10× | if statuses.is_empty() { |
| 191 | 7× | println!( |
| 192 | | " {} {}", |
| 193 | 7× | ui::ok(ui::glyph("✔", "*")), |
| 194 | 7× | ui::dim("nothing to commit, working copy clean") |
| 195 | | ); |
| 196 | 7× | return; |
| 197 | 3× | } |
| 198 | 3× | println!(); |
| 199 | 3× | for s in statuses { |
| 200 | 3× | println!( |
| 201 | 3× | " {} {}", |
| 202 | 3× | ui::status_mark(status_tag(s.status)), |
| 203 | 3× | ui::path(&s.path) |
| 204 | 3× | ); |
| 205 | 3× | } |
| 206 | 10× | } |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | 10× | fn print_behind(repo: &Repo) { |
| 211 | 10× | if let Ok(Some(n)) = repo.working_copy_behind() { |
| 212 | 0× | println!( |
| 213 | 0× | " {} {}", |
| 214 | 0× | ui::caution(), |
| 215 | 0× | ui::warn(format!( |
| 216 | 0× | "the working copy is behind {}: it lacks {} the bookmark has; oxy checkout brings it up", |
| 217 | 0× | repo.config.bookmark, |
| 218 | 0× | plural(n, "change") |
| 219 | 0× | )) |
| 220 | 0× | ); |
| 221 | 10× | } |
| 222 | 10× | } |
| 223 | |
|
| 224 | 15× | pub fn status(json: bool) -> Result<i32> { |
| 225 | 15× | let repo = open()?; |
| 226 | | |
| 227 | | |
| 228 | 15× | let known = if json { |
| 229 | 5× | None |
| 230 | | } else { |
| 231 | 10× | repo.status_by_manifest()? |
| 232 | | }; |
| 233 | 15× | if let Some(statuses) = known { |
| 234 | 4× | let (_, wc) = repo.working_copy()?; |
| 235 | 4× | let heads = repo.bookmark_heads()?; |
| 236 | 4× | println!( |
| 237 | | "{} {}{}working copy {}{}{}", |
| 238 | 4× | ui::dim("On"), |
| 239 | 4× | ui::bookmark(&repo.config.bookmark), |
| 240 | 4× | ui::sep(), |
| 241 | 4× | ui::id(wc.change_id.short(8)), |
| 242 | 4× | ui::sep(), |
| 243 | 4× | ui::dim(plural(heads.len(), "head")) |
| 244 | | ); |
| 245 | 4× | print_behind(&repo); |
| 246 | 4× | print_statuses(&statuses); |
| 247 | 4× | print_pin_moves(&pin_moves(&repo, None)); |
| 248 | 4× | print_untracked_nested(&repo, &repo.untracked_nested()); |
| 249 | 4× | print_skipped_large(&repo); |
| 250 | 4× | return Ok(0); |
| 251 | 11× | } |
| 252 | 11× | let sp = ui::spinner("Snapshotting the working copy…"); |
| 253 | 11× | let snap = repo.snapshot_status("oxy status")?; |
| 254 | 11× | let heads = { |
| 255 | 11× | let _t = oxy_core::timing::phase("status: heads"); |
| 256 | 11× | repo.bookmark_heads()? |
| 257 | | }; |
| 258 | 11× | let _drop = oxy_core::timing::phase("status: print and drop"); |
| 259 | 11× | sp.clear(); |
| 260 | 11× | if json { |
| 261 | 5× | println!("{}", repo.status_json(&snap)?); |
| 262 | 5× | return Ok(if snap.base_snapshot.is_conflicted() { |
| 263 | 0× | 2 |
| 264 | | } else { |
| 265 | 5× | 0 |
| 266 | | }); |
| 267 | 6× | } |
| 268 | 6× | println!( |
| 269 | | "{} {}{}working copy {}{}{}", |
| 270 | 6× | ui::dim("On"), |
| 271 | 6× | ui::bookmark(&repo.config.bookmark), |
| 272 | 6× | ui::sep(), |
| 273 | 6× | ui::id(snap.wc.change_id.short(8)), |
| 274 | 6× | ui::sep(), |
| 275 | 6× | ui::dim(plural(heads.len(), "head")) |
| 276 | | ); |
| 277 | 6× | print_behind(&repo); |
| 278 | 6× | print_statuses(&snap.statuses); |
| 279 | 6× | print_pin_moves(&pin_moves(&repo, None)); |
| 280 | 6× | print_untracked_nested(&repo, &snap.untracked_nested); |
| 281 | 6× | print_skipped_large(&repo); |
| 282 | 6× | if !snap.base_snapshot.is_conflicted() { |
| 283 | 6× | return Ok(0); |
| 284 | 0× | } |
| 285 | 0× | println!(); |
| 286 | 0× | println!( |
| 287 | | "{} {}", |
| 288 | 0× | ui::section(ui::err("Conflicts")), |
| 289 | 0× | ui::dim("edit the marked files, or the .oxy-conflict-N candidates, then commit") |
| 290 | | ); |
| 291 | 0× | for c in &snap.base_snapshot.tree.conflicts { |
| 292 | 0× | println!(" {} tree: {c:?}", ui::err(ui::glyph("✱", "!"))); |
| 293 | 0× | } |
| 294 | 0× | for (p, f) in &snap.base_snapshot.files { |
| 295 | 0× | if f.is_conflicted() { |
| 296 | 0× | let state = match snap |
| 297 | 0× | .statuses |
| 298 | 0× | .iter() |
| 299 | 0× | .find(|s| s.path == *p) |
| 300 | 0× | .map(|s| s.status) |
| 301 | | { |
| 302 | | Some(ChangeStatus::Resolved) => { |
| 303 | 0× | ui::ok(format!("{} resolved in working copy", ui::glyph("✔", "*"))) |
| 304 | | } |
| 305 | | Some(ChangeStatus::ConflictEdited) => { |
| 306 | 0× | ui::warn(format!("{} markers remain", ui::glyph("‼", "!"))) |
| 307 | | } |
| 308 | 0× | _ => ui::err(format!("{} unresolved", ui::glyph("✱", "!"))), |
| 309 | | }; |
| 310 | 0× | println!(" {} {state}", ui::path(p)); |
| 311 | 0× | let resolved = matches!( |
| 312 | 0× | snap.statuses |
| 313 | 0× | .iter() |
| 314 | 0× | .find(|s| s.path == *p) |
| 315 | 0× | .map(|s| s.status), |
| 316 | | Some(ChangeStatus::Resolved) |
| 317 | | ); |
| 318 | 0× | let both = if resolved { |
| 319 | 0× | 0 |
| 320 | | } else { |
| 321 | 0× | snap.base.both_sides(&f.node).iter().flatten().count() |
| 322 | | }; |
| 323 | 0× | if both > 0 { |
| 324 | 0× | println!( |
| 325 | | " {}", |
| 326 | 0× | ui::dim(format!( |
| 327 | | "{} adjacent {}: `oxy resolve --both {p}` keeps both sides, earliest first", |
| 328 | | both, |
| 329 | 0× | if both == 1 { "insertion" } else { "insertions" } |
| 330 | | )) |
| 331 | | ); |
| 332 | 0× | } |
| 333 | 0× | } |
| 334 | | } |
| 335 | 0× | Ok(2) |
| 336 | 15× | } |
| 337 | |
|
| 338 | 36× | pub fn commit( |
| 339 | 36× | message: &str, |
| 340 | 36× | depends_on: &[String], |
| 341 | 36× | paths: &[String], |
| 342 | 36× | interactive: bool, |
| 343 | 36× | hunks: &[usize], |
| 344 | 36× | ) -> Result<i32> { |
| 345 | 36× | let repo = open()?; |
| 346 | 36× | let mut deps = Vec::new(); |
| 347 | 36× | for d in depends_on { |
| 348 | 0× | deps.push(repo.resolve_change(d)?); |
| 349 | | } |
| 350 | 36× | let selection = if !hunks.is_empty() { |
| 351 | 1× | oxy_core::repo::Selection::Hunks(hunks.to_vec()) |
| 352 | 35× | } else if interactive { |
| 353 | 0× | let (_, hunks) = repo.hunks()?; |
| 354 | 0× | if hunks.is_empty() { |
| 355 | 0× | anyhow::bail!("nothing to commit: the working copy matches its base"); |
| 356 | 0× | } |
| 357 | 0× | let hunks: Vec<_> = hunks |
| 358 | 0× | .into_iter() |
| 359 | 0× | .filter(|h| { |
| 360 | 0× | paths.is_empty() |
| 361 | 0× | || paths.iter().any(|p| { |
| 362 | 0× | h.path == *p || h.path.starts_with(&format!("{}/", p.trim_end_matches('/'))) |
| 363 | 0× | }) |
| 364 | 0× | }) |
| 365 | 0× | .collect(); |
| 366 | 0× | let mut chosen = Vec::new(); |
| 367 | 0× | let stdin = std::io::stdin(); |
| 368 | 0× | let mut take_rest = false; |
| 369 | 0× | for (n, h) in hunks.iter().enumerate() { |
| 370 | 0× | if take_rest { |
| 371 | 0× | chosen.push(h.index); |
| 372 | 0× | continue; |
| 373 | 0× | } |
| 374 | 0× | println!( |
| 375 | | "{} {} {}", |
| 376 | 0× | ui::section(format!("hunk {} of {}", n + 1, hunks.len())), |
| 377 | 0× | ui::path(&h.path), |
| 378 | 0× | ui::dim(format!("(#{})", h.index)) |
| 379 | | ); |
| 380 | 0× | for line in h.text.lines() { |
| 381 | 0× | let styled = if line.starts_with('+') { |
| 382 | 0× | ui::ok(line) |
| 383 | 0× | } else if line.starts_with('-') { |
| 384 | 0× | ui::err(line) |
| 385 | | } else { |
| 386 | 0× | ui::dim(line) |
| 387 | | }; |
| 388 | 0× | println!(" {styled}"); |
| 389 | | } |
| 390 | | loop { |
| 391 | 0× | print!( |
| 392 | | "{} ", |
| 393 | 0× | ui::warn("Commit this hunk? [y]es [n]o [a]ll remaining [q]uit:") |
| 394 | | ); |
| 395 | | use std::io::Write as _; |
| 396 | 0× | std::io::stdout().flush()?; |
| 397 | 0× | let mut answer = String::new(); |
| 398 | 0× | if stdin.read_line(&mut answer)? == 0 { |
| 399 | 0× | anyhow::bail!("aborted"); |
| 400 | 0× | } |
| 401 | 0× | match answer.trim() { |
| 402 | 0× | "y" | "Y" => { |
| 403 | 0× | chosen.push(h.index); |
| 404 | 0× | break; |
| 405 | | } |
| 406 | 0× | "n" | "N" | "" => break, |
| 407 | 0× | "a" | "A" => { |
| 408 | 0× | chosen.push(h.index); |
| 409 | 0× | take_rest = true; |
| 410 | 0× | break; |
| 411 | | } |
| 412 | 0× | "q" | "Q" => anyhow::bail!("aborted; nothing committed"), |
| 413 | 0× | _ => continue, |
| 414 | | } |
| 415 | | } |
| 416 | | } |
| 417 | 0× | if chosen.is_empty() { |
| 418 | 0× | anyhow::bail!("no hunks chosen; nothing committed"); |
| 419 | 0× | } |
| 420 | 0× | oxy_core::repo::Selection::Hunks(chosen) |
| 421 | 35× | } else if paths.is_empty() { |
| 422 | 31× | oxy_core::repo::Selection::All |
| 423 | | } else { |
| 424 | 4× | oxy_core::repo::Selection::Paths(paths.to_vec()) |
| 425 | | }; |
| 426 | | |
| 427 | | |
| 428 | 36× | let moves = match &selection { |
| 429 | 31× | oxy_core::repo::Selection::All => pin_moves(&repo, None), |
| 430 | 4× | oxy_core::repo::Selection::Paths(p) => pin_moves(&repo, Some(p)), |
| 431 | 1× | oxy_core::repo::Selection::Hunks(_) => Vec::new(), |
| 432 | | }; |
| 433 | 36× | let sp = ui::spinner("Committing…"); |
| 434 | 36× | let (h, partial) = repo.commit_selection_partial(message, &deps, &selection)?; |
| 435 | | |
| 436 | 36× | let meta = repo.store().get_change_meta(&h)?; |
| 437 | 36× | sp.done(&format!( |
| 438 | 36× | "{} {} {} {}", |
| 439 | 36× | ui::done("Committed"), |
| 440 | 36× | ui::id(meta.change_id.short(8)), |
| 441 | 36× | ui::hash(format!("#{}", h.short(12))), |
| 442 | 36× | ui::subject(subject_of(message)) |
| 443 | 36× | )); |
| 444 | 36× | for m in &moves { |
| 445 | 1× | let line = match (&m.to, m.dirty) { |
| 446 | 1× | (Some(to), _) => format!( |
| 447 | | "recorded subrepo {}'s pin moving {} {} {}: a pin bump. Meant only your own files? `oxy commit PATHS` leaves pins out", |
| 448 | | m.path, |
| 449 | | m.from, |
| 450 | 1× | ui::glyph("→", "->"), |
| 451 | | to |
| 452 | | ), |
| 453 | 0× | (None, true) => format!( |
| 454 | | "subrepo {} has uncommitted edits of its own: the pin names its last committed state; the edits stay in it", |
| 455 | | m.path |
| 456 | | ), |
| 457 | 0× | (None, false) => continue, |
| 458 | | }; |
| 459 | 1× | println!(" {} {}", ui::caution(), ui::warn(line)); |
| 460 | | } |
| 461 | 36× | print_untracked_nested(&repo, &repo.untracked_nested()); |
| 462 | 36× | print_skipped_large(&repo); |
| 463 | | |
| 464 | 36× | if partial { |
| 465 | 4× | let rest = repo.snapshot_status("oxy commit")?; |
| 466 | 4× | if !rest.statuses.is_empty() { |
| 467 | 4× | println!( |
| 468 | 4× | " {} {}", |
| 469 | 4× | ui::note(), |
| 470 | 4× | ui::dim(format!( |
| 471 | 4× | "{} left uncommitted", |
| 472 | 4× | plural(rest.statuses.len(), "path") |
| 473 | 4× | )) |
| 474 | 4× | ); |
| 475 | 4× | } |
| 476 | 32× | } |
| 477 | 36× | where_now(&repo); |
| 478 | 36× | Ok(0) |
| 479 | 36× | } |
| 480 | |
|
| 481 | |
|
| 482 | |
|
| 483 | | struct PinMove { |
| 484 | | path: String, |
| 485 | | from: String, |
| 486 | | to: Option<String>, |
| 487 | | dirty: bool, |
| 488 | | } |
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | 45× | fn pin_moves(repo: &oxy_core::repo::Repo, paths: Option<&[String]>) -> Vec<PinMove> { |
| 494 | 45× | if !repo.root().join(".oxymodules").exists() { |
| 495 | 38× | return Vec::new(); |
| 496 | 7× | } |
| 497 | 7× | let Ok(subs) = repo.subrepos() else { |
| 498 | 0× | return Vec::new(); |
| 499 | | }; |
| 500 | 7× | let chosen = |p: &str| { |
| 501 | 6× | paths.is_none_or(|ps| { |
| 502 | 1× | ps.iter().any(|x| { |
| 503 | 1× | let x = x.trim_end_matches('/'); |
| 504 | 1× | p == x || p.starts_with(&format!("{x}/")) || x.is_empty() || x == "." |
| 505 | 1× | }) |
| 506 | 1× | }) |
| 507 | 6× | }; |
| 508 | 7× | subs.into_iter() |
| 509 | 7× | .filter(|s| s.committed && chosen(&s.path)) |
| 510 | 7× | .filter_map(|s| { |
| 511 | 5× | let to = s |
| 512 | 5× | .actual |
| 513 | 5× | .as_ref() |
| 514 | 5× | .filter(|a| !a.same_state(&s.pin)) |
| 515 | 5× | .map(|a| a.summary()); |
| 516 | 5× | (to.is_some() || s.dirty).then(|| PinMove { |
| 517 | 3× | from: s.pin.summary(), |
| 518 | 3× | to, |
| 519 | 3× | dirty: s.dirty, |
| 520 | 3× | path: s.path, |
| 521 | 3× | }) |
| 522 | 5× | }) |
| 523 | 7× | .collect() |
| 524 | 45× | } |
| 525 | |
|
| 526 | |
|
| 527 | |
|
| 528 | 10× | fn print_pin_moves(moves: &[PinMove]) { |
| 529 | 10× | if moves.is_empty() { |
| 530 | 8× | return; |
| 531 | 2× | } |
| 532 | 2× | println!("{}", ui::section("Subrepo pins")); |
| 533 | 2× | for m in moves { |
| 534 | 2× | if let Some(to) = &m.to { |
| 535 | 1× | println!( |
| 536 | 1× | " {} {} {} {} {} {}", |
| 537 | 1× | ui::warn(ui::glyph("↕", "<>")), |
| 538 | 1× | ui::path(&m.path), |
| 539 | 1× | ui::dim(&m.from), |
| 540 | 1× | ui::dim(ui::glyph("→", "->")), |
| 541 | 1× | to, |
| 542 | 1× | ui::dim("pin moved: a bare `oxy commit` records it") |
| 543 | 1× | ); |
| 544 | 1× | } |
| 545 | 2× | if m.dirty { |
| 546 | 1× | println!( |
| 547 | 1× | " {} {} {}", |
| 548 | 1× | ui::warn(ui::glyph("✎", "~")), |
| 549 | 1× | ui::path(&m.path), |
| 550 | 1× | ui::dim("uncommitted edits inside it: commit them there; the parent records only its pin") |
| 551 | 1× | ); |
| 552 | 1× | } |
| 553 | | } |
| 554 | 10× | } |
| 555 | |
|
| 556 | 0× | pub fn blame(path: &str, json: bool) -> Result<i32> { |
| 557 | 0× | let repo = open()?; |
| 558 | 0× | let lines = repo.blame(path)?; |
| 559 | 0× | if json { |
| 560 | 0× | let items = lines.iter().map(|l| { |
| 561 | 0× | format!( |
| 562 | | "{{\"line\":{},\"change_id\":{},\"hash\":{},\"index\":{},\"author\":{},\"when_ms\":{}}}", |
| 563 | | l.line, |
| 564 | 0× | oxy_core::repo::json_str(&l.change_id.to_hex()), |
| 565 | 0× | oxy_core::repo::json_str(&l.hash.to_hex()), |
| 566 | | l.index, |
| 567 | 0× | oxy_core::repo::json_str(&l.author), |
| 568 | | l.when_ms |
| 569 | | ) |
| 570 | 0× | }); |
| 571 | 0× | println!("{}", oxy_core::repo::json_array(items)); |
| 572 | 0× | return Ok(0); |
| 573 | 0× | } |
| 574 | 0× | let width = lines |
| 575 | 0× | .iter() |
| 576 | 0× | .map(|l| l.author.chars().count()) |
| 577 | 0× | .max() |
| 578 | 0× | .unwrap_or(0) |
| 579 | 0× | .min(20); |
| 580 | 0× | let digits = lines.len().to_string().len(); |
| 581 | 0× | let _t = oxy_core::timing::phase("blame: print"); |
| 582 | | |
| 583 | | |
| 584 | | |
| 585 | 0× | let mut prefix: std::collections::HashMap<oxy_core::hash::ChangeHash, String> = |
| 586 | 0× | std::collections::HashMap::new(); |
| 587 | 0× | let bar = ui::dim(ui::glyph("│", "|")); |
| 588 | 0× | let mut out = std::io::BufWriter::with_capacity(1 << 16, std::io::stdout().lock()); |
| 589 | 0× | for l in &lines { |
| 590 | 0× | let p = prefix.entry(l.hash).or_insert_with(|| { |
| 591 | 0× | let author: String = l.author.chars().take(20).collect(); |
| 592 | 0× | format!( |
| 593 | | "{} {} {}", |
| 594 | 0× | ui::id(l.change_id.short(8)), |
| 595 | 0× | ui::author(format!("{author:<width$}")), |
| 596 | 0× | ui::dim(ui::ago(l.when_ms)) |
| 597 | | ) |
| 598 | 0× | }); |
| 599 | 0× | let text = String::from_utf8_lossy(&l.text); |
| 600 | 0× | if writeln!( |
| 601 | 0× | out, |
| 602 | | "{p} {:>digits$}{bar} {}", |
| 603 | | l.line, |
| 604 | 0× | text.trim_end_matches('\n') |
| 605 | | ) |
| 606 | 0× | .is_err() |
| 607 | | { |
| 608 | | |
| 609 | 0× | return Ok(0); |
| 610 | 0× | } |
| 611 | | } |
| 612 | 0× | let _ = out.flush(); |
| 613 | 0× | Ok(0) |
| 614 | 0× | } |
| 615 | |
|
| 616 | 0× | fn bisect_report(repo: &Repo, step: &oxy_core::repo::BisectStep) -> Result<i32> { |
| 617 | | use oxy_core::repo::BisectStep::*; |
| 618 | 0× | match step { |
| 619 | 0× | NeedMarks { good, bad } => { |
| 620 | 0× | println!( |
| 621 | | "{} {} {}", |
| 622 | 0× | ui::note(), |
| 623 | 0× | ui::dim(format!("{good} good, {bad} bad;")), |
| 624 | 0× | ui::dim(if *good == 0 { |
| 625 | 0× | "mark a known-good change or bookmark: oxy bisect good CHANGE" |
| 626 | | } else { |
| 627 | 0× | "mark a known-bad change or bookmark: oxy bisect bad CHANGE" |
| 628 | | }) |
| 629 | | ); |
| 630 | 0× | Ok(0) |
| 631 | | } |
| 632 | | Testing { |
| 633 | 0× | heads, |
| 634 | 0× | remaining, |
| 635 | 0× | steps_left, |
| 636 | | } => { |
| 637 | 0× | println!( |
| 638 | | "{} {} {}", |
| 639 | 0× | ui::done("Testing"), |
| 640 | 0× | ui::dim(format!( |
| 641 | | "{} in the state under test;", |
| 642 | 0× | plural(heads.len(), "head") |
| 643 | | )), |
| 644 | 0× | ui::dim(format!( |
| 645 | | "{remaining} candidates left, about {steps_left} more steps" |
| 646 | | )) |
| 647 | | ); |
| 648 | 0× | println!( |
| 649 | | " {} {}", |
| 650 | 0× | ui::note(), |
| 651 | 0× | ui::dim("run your test, then oxy bisect good | bad | skip") |
| 652 | | ); |
| 653 | 0× | Ok(0) |
| 654 | | } |
| 655 | 0× | Found { culprit } => { |
| 656 | 0× | let c = repo.store().get_change(culprit)?; |
| 657 | 0× | println!( |
| 658 | | "{} {} {} {}", |
| 659 | 0× | ui::done("Found"), |
| 660 | 0× | ui::id(short_id(&c)), |
| 661 | 0× | ui::hash(format!("#{}", culprit.short(12))), |
| 662 | 0× | ui::subject(subject_of(&c.message)) |
| 663 | | ); |
| 664 | 0× | println!(" {} {}", ui::note(), ui::dim("the first change whose inclusion makes the state bad; oxy bisect reset to return")); |
| 665 | 0× | Ok(0) |
| 666 | | } |
| 667 | 0× | Inconclusive { candidates } => { |
| 668 | 0× | println!( |
| 669 | | "{} {}", |
| 670 | 0× | ui::caution(), |
| 671 | 0× | ui::warn(format!( |
| 672 | | "every remaining candidate was skipped ({}); oxy bisect reset to return", |
| 673 | 0× | plural(candidates.len(), "change") |
| 674 | | )) |
| 675 | | ); |
| 676 | 0× | Ok(1) |
| 677 | | } |
| 678 | | } |
| 679 | 0× | } |
| 680 | |
|
| 681 | 0× | pub fn bisect_start() -> Result<i32> { |
| 682 | 0× | let repo = open()?; |
| 683 | 0× | repo.bisect_start()?; |
| 684 | 0× | println!( |
| 685 | | "{} bisect; {}", |
| 686 | 0× | ui::done("Started"), |
| 687 | 0× | ui::dim("now: oxy bisect good CHANGE and oxy bisect bad CHANGE") |
| 688 | | ); |
| 689 | 0× | Ok(0) |
| 690 | 0× | } |
| 691 | |
|
| 692 | 0× | pub fn bisect_mark(good: Option<bool>, changes: &[String]) -> Result<i32> { |
| 693 | 0× | let repo = open()?; |
| 694 | 0× | let mut heads = Vec::new(); |
| 695 | 0× | for c in changes { |
| 696 | 0× | heads.extend(repo.resolve_heads(c)?); |
| 697 | | } |
| 698 | 0× | let step = repo.bisect_mark(good, &heads)?; |
| 699 | 0× | bisect_report(&repo, &step) |
| 700 | 0× | } |
| 701 | |
|
| 702 | 0× | pub fn bisect_run(command: &[String]) -> Result<i32> { |
| 703 | 0× | let repo = open()?; |
| 704 | 0× | let step = repo.bisect_run(command)?; |
| 705 | 0× | bisect_report(&repo, &step) |
| 706 | 0× | } |
| 707 | |
|
| 708 | 1× | pub fn bisect_status(as_json: bool) -> Result<i32> { |
| 709 | 1× | let repo = open()?; |
| 710 | 1× | if as_json { |
| 711 | | use oxy_core::repo::BisectStep::*; |
| 712 | 1× | let v = match repo.bisect_status()? { |
| 713 | 1× | None => "null".to_string(), |
| 714 | 0× | Some(NeedMarks { good, bad }) => json::obj([ |
| 715 | 0× | ("state", json::s("need-marks")), |
| 716 | 0× | ("good", json::n(good)), |
| 717 | 0× | ("bad", json::n(bad)), |
| 718 | 0× | ]), |
| 719 | | Some(Testing { |
| 720 | 0× | heads, |
| 721 | 0× | remaining, |
| 722 | 0× | steps_left, |
| 723 | 0× | }) => json::obj([ |
| 724 | 0× | ("state", json::s("testing")), |
| 725 | 0× | ("heads", json::hashes(&heads)), |
| 726 | 0× | ("remaining", json::n(remaining)), |
| 727 | 0× | ("steps_left", json::n(steps_left)), |
| 728 | 0× | ]), |
| 729 | 0× | Some(Found { culprit }) => json::obj([ |
| 730 | 0× | ("state", json::s("found")), |
| 731 | 0× | ("culprit", json::s(&culprit.to_hex())), |
| 732 | 0× | ]), |
| 733 | 0× | Some(Inconclusive { candidates }) => json::obj([ |
| 734 | 0× | ("state", json::s("inconclusive")), |
| 735 | 0× | ("candidates", json::hashes(&candidates)), |
| 736 | 0× | ]), |
| 737 | | }; |
| 738 | 1× | println!("{v}"); |
| 739 | 1× | return Ok(0); |
| 740 | 0× | } |
| 741 | 0× | match repo.bisect_status()? { |
| 742 | | None => { |
| 743 | 0× | println!("{}", ui::dim("No bisect in progress.")); |
| 744 | 0× | Ok(0) |
| 745 | | } |
| 746 | 0× | Some(step) => bisect_report(&repo, &step), |
| 747 | | } |
| 748 | 1× | } |
| 749 | |
|
| 750 | 0× | pub fn bisect_reset() -> Result<i32> { |
| 751 | 0× | let repo = open()?; |
| 752 | 0× | repo.bisect_reset()?; |
| 753 | 0× | println!("{} bisect; working copy restored", ui::done("Reset")); |
| 754 | 0× | Ok(0) |
| 755 | 0× | } |
| 756 | |
|
| 757 | |
|
| 758 | |
|
| 759 | |
|
| 760 | 16× | fn signature_json(repo: &Repo, h: &oxy_core::hash::ChangeHash) -> Result<String> { |
| 761 | 16× | Ok(match repo.authenticate_change(h)? { |
| 762 | 9× | SignatureStatus::Unsigned => json::obj([("status", json::s("unsigned"))]), |
| 763 | | SignatureStatus::Valid { |
| 764 | 7× | key, |
| 765 | 7× | name, |
| 766 | 7× | email, |
| 767 | 7× | sealed, |
| 768 | 7× | revoked, |
| 769 | 7× | } => json::obj([ |
| 770 | 7× | ("status", json::s(if sealed { "sealed" } else { "signed" })), |
| 771 | 7× | ("key", json::s(&key.to_hex())), |
| 772 | 7× | ("name", json::s(&name)), |
| 773 | 7× | ("email", json::s(&email)), |
| 774 | 7× | ("revoked", json::b(revoked)), |
| 775 | | ]), |
| 776 | 0× | SignatureStatus::Invalid(why) => { |
| 777 | 0× | json::obj([("status", json::s("invalid")), ("reason", json::s(&why))]) |
| 778 | | } |
| 779 | | }) |
| 780 | 16× | } |
| 781 | |
|
| 782 | 22× | fn log_entry_json(repo: &Repo, e: &oxy_core::repo::LogEntry) -> Result<String> { |
| 783 | 22× | let c = &e.change; |
| 784 | 22× | Ok(json::obj([ |
| 785 | 22× | ("hash", json::s(&e.hash.to_hex())), |
| 786 | 22× | ("change_id", json::s(&c.change_id.to_hex())), |
| 787 | 22× | ("author", json::s(&author_name(repo, c))), |
| 788 | 22× | ("author_key", json::s(&c.author.to_hex())), |
| 789 | | ( |
| 790 | 22× | "via", |
| 791 | 22× | match via( |
| 792 | 22× | repo, |
| 793 | 22× | &repo.authenticate_change_known(&e.hash, Some(c))?, |
| 794 | 22× | &c.author, |
| 795 | | ) { |
| 796 | 0× | Some(v) => json::obj([ |
| 797 | 0× | ("name", json::s(&v.name)), |
| 798 | 0× | ("email", json::s(&v.email)), |
| 799 | 0× | ("key", json::s(&v.key.to_hex())), |
| 800 | 0× | ("scope", json::opt(v.scope.as_deref(), json::s)), |
| 801 | 0× | ("expires_ms", json::opt(v.expires, json::n)), |
| 802 | 0× | ("revoked", json::b(v.revoked)), |
| 803 | 0× | ]), |
| 804 | 22× | None => "null".to_string(), |
| 805 | | }, |
| 806 | | ), |
| 807 | 22× | ("when_ms", json::n(c.hlc.wall_ms)), |
| 808 | 22× | ("message", json::s(&c.message)), |
| 809 | 22× | ("kind", json::s(&format!("{:?}", c.kind).to_lowercase())), |
| 810 | 22× | ("deps", json::hashes(&c.deps)), |
| 811 | 22× | ("head", json::b(e.is_head)), |
| 812 | 22× | ("working_copy", json::b(e.is_working_copy)), |
| 813 | | ( |
| 814 | 22× | "signature", |
| 815 | 22× | if e.is_working_copy { |
| 816 | 6× | "null".into() |
| 817 | | } else { |
| 818 | 16× | signature_json(repo, &e.hash)? |
| 819 | | }, |
| 820 | | ), |
| 821 | | ])) |
| 822 | 22× | } |
| 823 | |
|
| 824 | |
|
| 825 | 2× | fn parse_line_range(spec: &str) -> Result<(usize, usize, &str)> { |
| 826 | 2× | let bad = || anyhow::anyhow!("-L {spec}: expected START,END:PATH, as 10,24:src/lib.rs"); |
| 827 | 2× | let (range, path) = spec.split_once(':').ok_or_else(bad)?; |
| 828 | 2× | let (a, b) = range.split_once(',').ok_or_else(bad)?; |
| 829 | | Ok(( |
| 830 | 1× | a.trim().parse().map_err(|_| bad())?, |
| 831 | 1× | b.trim().parse().map_err(|_| bad())?, |
| 832 | 1× | path, |
| 833 | | )) |
| 834 | 2× | } |
| 835 | |
|
| 836 | 10× | pub fn log( |
| 837 | 10× | revset: Option<&str>, |
| 838 | 10× | limit: Option<usize>, |
| 839 | 10× | verbose: bool, |
| 840 | 10× | path: Option<&str>, |
| 841 | 10× | lines: Option<&str>, |
| 842 | 10× | as_json: bool, |
| 843 | 10× | ) -> Result<i32> { |
| 844 | 10× | let repo = open()?; |
| 845 | | |
| 846 | | |
| 847 | | |
| 848 | | |
| 849 | | |
| 850 | 10× | let conflicted = !repo.is_at_op() && repo.recorded_conflicted()?; |
| 851 | 10× | let listing = oxy_core::timing::phase("log: entries"); |
| 852 | 10× | let entries = match (path, lines) { |
| 853 | 10× | _ if revset.is_some() => repo.log_revset(revset.unwrap_or_default())?, |
| 854 | 2× | (_, Some(spec)) => { |
| 855 | 2× | let (first, last, p) = parse_line_range(spec)?; |
| 856 | 1× | let (_, wc) = repo.working_copy()?; |
| 857 | 1× | let base = repo.load_state_history(&wc.deps)?.maximal(&wc.deps); |
| 858 | 1× | repo.line_history(&base, p, first, last)? |
| 859 | | } |
| 860 | 1× | (Some(p), None) => repo.log_for_path(p)?, |
| 861 | 7× | (None, None) => repo.log()?, |
| 862 | | }; |
| 863 | 9× | drop(listing); |
| 864 | 9× | let _print = oxy_core::timing::phase("log: print"); |
| 865 | 9× | if as_json { |
| 866 | 8× | let shown = limit.unwrap_or(entries.len()).min(entries.len()); |
| 867 | 8× | let mut items = Vec::new(); |
| 868 | 21× | for e in entries.iter().take(shown) { |
| 869 | 21× | items.push(log_entry_json(&repo, e)?); |
| 870 | | } |
| 871 | 8× | println!( |
| 872 | | "{}", |
| 873 | 8× | json::obj([ |
| 874 | 8× | ("bookmark", json::s(&repo.config.bookmark)), |
| 875 | 8× | ("total", json::n(entries.len())), |
| 876 | 8× | ("changes", json::arr(items)) |
| 877 | 8× | ]) |
| 878 | | ); |
| 879 | 8× | return Ok(0); |
| 880 | 1× | } |
| 881 | 1× | let total = entries.len(); |
| 882 | 1× | let shown = limit.unwrap_or(total).min(total); |
| 883 | | |
| 884 | 1× | let window: Vec<(ChangeHash, Vec<ChangeHash>)> = entries |
| 885 | 1× | .iter() |
| 886 | 1× | .take((shown + MAX_EDGE).min(total)) |
| 887 | 4× | .map(|e| (e.hash, e.change.deps.clone())) |
| 888 | 1× | .collect(); |
| 889 | 1× | let rows = oxy_core::lanes::graph(&window, MAX_EDGE); |
| 890 | | |
| 891 | | |
| 892 | | let everything; |
| 893 | 1× | let all = if path.is_some() || lines.is_some() || revset.is_some() { |
| 894 | 0× | everything = repo.log()?; |
| 895 | 0× | &everything |
| 896 | | } else { |
| 897 | 1× | &entries |
| 898 | | }; |
| 899 | 1× | let prefixes = oxy_core::timing::phase("log: id prefixes"); |
| 900 | 1× | let id_prefix = id_prefixes( |
| 901 | 1× | all.iter().map(|e| e.change.change_id.0).collect(), |
| 902 | 1× | entries.iter().take(shown).map(|e| e.change.change_id.0), |
| 903 | | ); |
| 904 | 1× | drop(prefixes); |
| 905 | 1× | let labels = reference_labels(&repo)?; |
| 906 | 4× | for (e, row) in entries.iter().take(shown).zip(&rows) { |
| 907 | 4× | let c = &e.change; |
| 908 | 4× | let bullet = ui::log_bullet( |
| 909 | 4× | e.is_working_copy, |
| 910 | 4× | e.is_head, |
| 911 | 4× | e.is_working_copy && conflicted, |
| 912 | | ); |
| 913 | 4× | let id = c.change_id.to_hex(); |
| 914 | 4× | let n = id_prefix.get(&c.change_id.0).copied().unwrap_or(8).min(8); |
| 915 | 4× | let mut head = vec![format!("{}{}", ui::id(&id[..n]), ui::dim(&id[n..8]))]; |
| 916 | 4× | head.push(ui::author(author_name(&repo, c))); |
| 917 | 4× | head.push(ui::dim(ui::ago(c.hlc.wall_ms))); |
| 918 | 4× | if let Some(names) = labels.get(&e.hash) { |
| 919 | 2× | head.push(names.join(" ")); |
| 920 | 2× | } |
| 921 | 4× | if c.kind == ChangeKind::Resolution { |
| 922 | 0× | head.push(ui::warn("resolution")); |
| 923 | 4× | } |
| 924 | 4× | if !e.is_working_copy { |
| 925 | 3× | head.push(ui::hash(format!("#{}", e.hash.short(8)))); |
| 926 | 3× | let status = repo.authenticate_change_known(&e.hash, Some(c))?; |
| 927 | 3× | if let Some(agent) = via(&repo, &status, &c.author) { |
| 928 | 0× | head.push(ui::dim(format!("via {}", agent.name))); |
| 929 | 3× | } |
| 930 | 3× | head.push(signature_mark_of( |
| 931 | 3× | &status, |
| 932 | 3× | awaits_seal(&repo, &status, &c.author), |
| 933 | | )); |
| 934 | 1× | } |
| 935 | 4× | println!("{}{}", node_line(row, &bullet), head.join(" ")); |
| 936 | 4× | let subject = if e.is_working_copy && c.message.is_empty() { |
| 937 | 1× | let edited = repo |
| 938 | 1× | .store() |
| 939 | 1× | .get_change(&e.hash) |
| 940 | 1× | .is_ok_and(|w| !w.tree_ops.is_empty() || !w.edits.is_empty()); |
| 941 | 1× | ui::dim(if edited { |
| 942 | 0× | "(working copy, uncommitted changes)" |
| 943 | | } else { |
| 944 | 1× | "(working copy)" |
| 945 | | }) |
| 946 | 3× | } else if c.message.trim().is_empty() { |
| 947 | 0× | ui::dim("(no description)") |
| 948 | | } else { |
| 949 | 3× | subject_of(&c.message).to_string() |
| 950 | | }; |
| 951 | 4× | println!("{}{subject}", below_line(row)); |
| 952 | 4× | if verbose { |
| 953 | 0× | let rail = continuation(row); |
| 954 | 0× | if !e.is_working_copy { |
| 955 | 0× | println!( |
| 956 | | "{rail}{}", |
| 957 | 0× | ui::dim(format!( |
| 958 | | "{} {}", |
| 959 | 0× | when(c.hlc.wall_ms), |
| 960 | 0× | signature_line(&repo, &e.hash)? |
| 961 | | )) |
| 962 | | ); |
| 963 | 0× | } |
| 964 | 0× | if !c.deps.is_empty() { |
| 965 | 0× | println!( |
| 966 | 0× | "{rail}{} {}", |
| 967 | 0× | ui::dim(ui::glyph("↳", "->")), |
| 968 | 0× | ui::dim(deps_of(&c.deps)) |
| 969 | 0× | ); |
| 970 | 0× | } |
| 971 | 0× | let body: Vec<&str> = c |
| 972 | 0× | .message |
| 973 | 0× | .lines() |
| 974 | 0× | .skip(1) |
| 975 | 0× | .skip_while(|l| l.trim().is_empty()) |
| 976 | 0× | .collect(); |
| 977 | 0× | for line in body { |
| 978 | 0× | println!("{rail} {line}"); |
| 979 | 0× | } |
| 980 | 4× | } |
| 981 | | } |
| 982 | 1× | if shown < total { |
| 983 | 0× | if let Some(last) = rows.get(shown.saturating_sub(1)) { |
| 984 | 0× | let cut: String = last |
| 985 | 0× | .below |
| 986 | 0× | .iter() |
| 987 | 0× | .map(|c| match c { |
| 988 | 0× | Cell::Blank | Cell::Elided | Cell::JoinRight => " ", |
| 989 | 0× | _ => "~ ", |
| 990 | 0× | }) |
| 991 | 0× | .collect(); |
| 992 | 0× | if cut.trim().is_empty() { |
| 993 | 0× | println!("{}", ui::dim("~")); |
| 994 | 0× | } else { |
| 995 | 0× | println!("{}", ui::dim(cut.trim_end())); |
| 996 | 0× | } |
| 997 | 0× | } |
| 998 | 0× | println!( |
| 999 | | "{}", |
| 1000 | 0× | ui::dim(format!( |
| 1001 | | "{} {} more; pass -n to see them", |
| 1002 | 0× | ui::glyph("…", "..."), |
| 1003 | 0× | total - shown |
| 1004 | | )) |
| 1005 | | ); |
| 1006 | 1× | } |
| 1007 | 1× | Ok(0) |
| 1008 | 10× | } |
| 1009 | |
|
| 1010 | |
|
| 1011 | | const MAX_EDGE: usize = 12; |
| 1012 | |
|
| 1013 | | use oxy_core::hash::ChangeHash; |
| 1014 | | use oxy_core::lanes::{Cell, GraphRow}; |
| 1015 | |
|
| 1016 | |
|
| 1017 | 6× | fn cell(c: Cell) -> &'static str { |
| 1018 | 6× | match c { |
| 1019 | 1× | Cell::Blank => " ", |
| 1020 | 3× | Cell::Line => ui::glyph("│ ", "| "), |
| 1021 | 0× | Cell::Elided => "~ ", |
| 1022 | 1× | Cell::JoinLeft => ui::glyph("├─", "|-"), |
| 1023 | 0× | Cell::JoinPass => ui::glyph("──", "--"), |
| 1024 | 0× | Cell::JoinCross => ui::glyph("┼─", "+-"), |
| 1025 | 1× | Cell::JoinRight => ui::glyph("╯ ", "' "), |
| 1026 | | } |
| 1027 | 6× | } |
| 1028 | |
|
| 1029 | |
|
| 1030 | 4× | fn node_line(row: &GraphRow, bullet: &str) -> String { |
| 1031 | 4× | let width = row |
| 1032 | 4× | .passing |
| 1033 | 4× | .iter() |
| 1034 | 4× | .copied() |
| 1035 | 4× | .max() |
| 1036 | 4× | .unwrap_or(0) |
| 1037 | 4× | .max(row.column) |
| 1038 | 4× | + 1; |
| 1039 | 4× | let mut out = String::new(); |
| 1040 | 5× | for c in 0..width { |
| 1041 | 5× | if c == row.column { |
| 1042 | 4× | out.push_str(bullet); |
| 1043 | 4× | out.push(' '); |
| 1044 | 4× | } else if row.passing.contains(&c) { |
| 1045 | 1× | out.push_str(&ui::dim(cell(Cell::Line))); |
| 1046 | 1× | } else { |
| 1047 | 0× | out.push_str(" "); |
| 1048 | 0× | } |
| 1049 | | } |
| 1050 | 4× | out.push(' '); |
| 1051 | 4× | out |
| 1052 | 4× | } |
| 1053 | |
|
| 1054 | |
|
| 1055 | |
|
| 1056 | 4× | fn below_line(row: &GraphRow) -> String { |
| 1057 | 5× | let g: String = row.below.iter().map(|c| cell(*c)).collect(); |
| 1058 | 4× | format!("{} ", ui::dim(g)) |
| 1059 | 4× | } |
| 1060 | |
|
| 1061 | |
|
| 1062 | 0× | fn continuation(row: &GraphRow) -> String { |
| 1063 | 0× | let g: String = row |
| 1064 | 0× | .below |
| 1065 | 0× | .iter() |
| 1066 | 0× | .map(|c| match c { |
| 1067 | 0× | Cell::Line | Cell::JoinLeft | Cell::JoinCross => cell(Cell::Line), |
| 1068 | 0× | _ => " ", |
| 1069 | 0× | }) |
| 1070 | 0× | .collect(); |
| 1071 | 0× | format!("{} ", ui::dim(g)) |
| 1072 | 0× | } |
| 1073 | |
|
| 1074 | |
|
| 1075 | |
|
| 1076 | |
|
| 1077 | 2× | fn id_prefixes( |
| 1078 | 2× | mut all: Vec<[u8; 16]>, |
| 1079 | 2× | wanted: impl Iterator<Item = [u8; 16]>, |
| 1080 | 2× | ) -> std::collections::HashMap<[u8; 16], usize> { |
| 1081 | 2× | all.sort_unstable(); |
| 1082 | 2× | all.dedup(); |
| 1083 | | |
| 1084 | 4004× | let common = |a: &[u8; 16], b: &[u8; 16]| { |
| 1085 | 10720× | let bytes = a.iter().zip(b).take_while(|(x, y)| x == y).count(); |
| 1086 | 4004× | match (a.get(bytes), b.get(bytes)) { |
| 1087 | 4004× | (Some(x), Some(y)) if x >> 4 == y >> 4 => 2 * bytes + 1, |
| 1088 | 1858× | _ => 2 * bytes, |
| 1089 | | } |
| 1090 | 4004× | }; |
| 1091 | 2× | let mut out = std::collections::HashMap::new(); |
| 1092 | 2004× | for id in wanted { |
| 1093 | 2004× | let Ok(i) = all.binary_search(&id) else { |
| 1094 | 0× | continue; |
| 1095 | | }; |
| 1096 | 2004× | let before = i.checked_sub(1).map_or(0, |j| common(&all[j], &id)); |
| 1097 | 2004× | let after = all.get(i + 1).map_or(0, |n| common(&id, n)); |
| 1098 | 2004× | out.insert(id, before.max(after) + 1); |
| 1099 | | } |
| 1100 | 2× | out |
| 1101 | 2× | } |
| 1102 | |
|
| 1103 | 1× | fn unique_prefixes(ids: impl Iterator<Item = String>) -> std::collections::HashMap<String, usize> { |
| 1104 | 1× | let mut v: Vec<String> = ids.collect(); |
| 1105 | 1× | v.sort(); |
| 1106 | 1× | v.dedup(); |
| 1107 | 19576× | let common = |a: &str, b: &str| a.bytes().zip(b.bytes()).take_while(|(x, y)| x == y).count(); |
| 1108 | 1× | let mut out = std::collections::HashMap::with_capacity(v.len()); |
| 1109 | 2000× | for i in 0..v.len() { |
| 1110 | 2000× | let before = if i > 0 { common(&v[i - 1], &v[i]) } else { 0 }; |
| 1111 | 2000× | let after = if i + 1 < v.len() { |
| 1112 | 1999× | common(&v[i], &v[i + 1]) |
| 1113 | | } else { |
| 1114 | 1× | 0 |
| 1115 | | }; |
| 1116 | 2000× | out.insert(v[i].clone(), before.max(after) + 1); |
| 1117 | | } |
| 1118 | 1× | out |
| 1119 | 1× | } |
| 1120 | |
|
| 1121 | |
|
| 1122 | |
|
| 1123 | |
|
| 1124 | |
|
| 1125 | |
|
| 1126 | 1× | fn reference_labels(repo: &Repo) -> Result<std::collections::HashMap<ChangeHash, Vec<String>>> { |
| 1127 | | const MAX_HEADS: usize = 3; |
| 1128 | 1× | let view = repo.view()?; |
| 1129 | 1× | let mut out: std::collections::HashMap<ChangeHash, Vec<String>> = Default::default(); |
| 1130 | 1× | let mut add = |heads: Vec<ChangeHash>, label: String| { |
| 1131 | 1× | if heads.len() <= MAX_HEADS { |
| 1132 | 2× | for h in heads { |
| 1133 | 2× | out.entry(h).or_default().push(label.clone()); |
| 1134 | 2× | } |
| 1135 | 0× | } |
| 1136 | 1× | }; |
| 1137 | 1× | for (name, st) in &view.bookmarks { |
| 1138 | 1× | add(repo.store().get_state(st)?.heads, ui::bookmark(name)); |
| 1139 | | } |
| 1140 | 1× | for (remote, bookmarks) in &view.remotes { |
| 1141 | 0× | for (name, st) in bookmarks { |
| 1142 | 0× | if view.bookmarks.get(name) == Some(st) { |
| 1143 | 0× | continue; |
| 1144 | 0× | } |
| 1145 | 0× | add( |
| 1146 | 0× | repo.store().get_state(st)?.heads, |
| 1147 | 0× | ui::dim(format!("{remote}/{name}")), |
| 1148 | | ); |
| 1149 | | } |
| 1150 | | } |
| 1151 | 1× | for t in repo.tags()? { |
| 1152 | 0× | add(t.heads.clone(), ui::warn(&t.name)); |
| 1153 | 0× | } |
| 1154 | 1× | Ok(out) |
| 1155 | 1× | } |
| 1156 | |
|
| 1157 | 1× | pub fn diff(change: Option<&str>, as_json: bool) -> Result<i32> { |
| 1158 | 1× | let repo = open()?; |
| 1159 | 1× | let hash = change.map(|c| repo.resolve_change(c)).transpose()?; |
| 1160 | 1× | if as_json { |
| 1161 | 1× | let files = repo.diff_files(hash.as_ref())?; |
| 1162 | 3× | let items = files.iter().map(|f| { |
| 1163 | 3× | json::obj([ |
| 1164 | 3× | ("path", json::s(&f.path)), |
| 1165 | 3× | ("kind", json::s(&f.kind)), |
| 1166 | 3× | ("from", json::opt(f.from.as_deref(), json::s)), |
| 1167 | | ( |
| 1168 | 3× | "hunks", |
| 1169 | 3× | json::arr(f.hunks.iter().map(|h| { |
| 1170 | 3× | json::obj([ |
| 1171 | 3× | ("base_line", json::n(h.base_line)), |
| 1172 | | ( |
| 1173 | 3× | "added", |
| 1174 | 3× | json::arr( |
| 1175 | 3× | h.added.iter().map(|l| json::s(&String::from_utf8_lossy(l))), |
| 1176 | | ), |
| 1177 | | ), |
| 1178 | | ( |
| 1179 | 3× | "removed", |
| 1180 | 3× | json::arr(h.removed.iter().map(|(n, l)| { |
| 1181 | 0× | json::obj([ |
| 1182 | 0× | ("line", json::n(*n)), |
| 1183 | 0× | ("text", json::s(&String::from_utf8_lossy(l))), |
| 1184 | 0× | ]) |
| 1185 | 0× | })), |
| 1186 | | ), |
| 1187 | | ]) |
| 1188 | 3× | })), |
| 1189 | | ), |
| 1190 | | ]) |
| 1191 | 3× | }); |
| 1192 | 1× | println!( |
| 1193 | | "{}", |
| 1194 | 1× | json::obj([ |
| 1195 | 1× | ("change", json::opt(hash, |h| json::s(&h.to_hex()))), |
| 1196 | 1× | ("files", json::arr(items)) |
| 1197 | | ]) |
| 1198 | | ); |
| 1199 | 1× | return Ok(0); |
| 1200 | 0× | } |
| 1201 | 0× | match hash { |
| 1202 | 0× | Some(h) => { |
| 1203 | 0× | let c = repo.store().get_change(&h)?; |
| 1204 | 0× | let (base, snapshot) = repo.change_base(&c)?; |
| 1205 | 0× | print!("{}", render_change(&c, &base, &snapshot)); |
| 1206 | | } |
| 1207 | | None => { |
| 1208 | | |
| 1209 | 0× | let Some((wc, base, snapshot)) = repo.working_copy_with_base_unless_unchanged()? else { |
| 1210 | 0× | return Ok(0); |
| 1211 | | }; |
| 1212 | 0× | let text = { |
| 1213 | 0× | let _t = oxy_core::timing::phase("diff: render the change"); |
| 1214 | 0× | render_change(&wc, &base, &snapshot) |
| 1215 | | }; |
| 1216 | 0× | print!("{text}"); |
| 1217 | | |
| 1218 | | |
| 1219 | 0× | std::mem::forget((wc, base, snapshot)); |
| 1220 | | } |
| 1221 | | } |
| 1222 | 0× | Ok(0) |
| 1223 | 1× | } |
| 1224 | |
|
| 1225 | 3× | pub fn cat(path: &str, change: Option<&str>) -> Result<i32> { |
| 1226 | 3× | let repo = open()?; |
| 1227 | 3× | let heads = change.map(|c| repo.resolve_heads(c)).transpose()?; |
| 1228 | 3× | let bytes = match repo.file_at(path, heads.as_deref()) { |
| 1229 | 3× | Ok(b) => b, |
| 1230 | | |
| 1231 | | |
| 1232 | | |
| 1233 | 0× | Err(e) => match change { |
| 1234 | 0× | Some(c) |
| 1235 | 0× | if !repo.view()?.bookmarks.contains_key(c) |
| 1236 | 0× | && repo.tag_heads(c)?.is_none() |
| 1237 | 0× | && repo.file_at(path, None).is_ok() => |
| 1238 | | { |
| 1239 | 0× | anyhow::bail!( |
| 1240 | | "{path} is not in {c}'s dependency closure (the change and what it \ |
| 1241 | | depends on); it is on the bookmark: try -r {}, or --at-op OP for the \ |
| 1242 | | state at an operation", |
| 1243 | | repo.config.bookmark |
| 1244 | | ) |
| 1245 | | } |
| 1246 | 0× | _ => return Err(e.into()), |
| 1247 | | }, |
| 1248 | | }; |
| 1249 | | use std::io::Write as _; |
| 1250 | 3× | std::io::stdout().write_all(&bytes)?; |
| 1251 | 3× | Ok(0) |
| 1252 | 3× | } |
| 1253 | |
|
| 1254 | 1× | pub fn root(as_json: bool) -> Result<i32> { |
| 1255 | 1× | let Ok(repo) = Repo::open(&std::env::current_dir()?) else { |
| 1256 | 0× | if as_json { |
| 1257 | 0× | println!("null"); |
| 1258 | 0× | } |
| 1259 | 0× | return Ok(1); |
| 1260 | | }; |
| 1261 | 1× | if as_json { |
| 1262 | 1× | println!( |
| 1263 | 1× | "{}", |
| 1264 | 1× | json::obj([ |
| 1265 | 1× | ("root", json::s(&repo.root().to_string_lossy())), |
| 1266 | 1× | ("workspace", json::s(&repo.workspace().to_hex())), |
| 1267 | 1× | ("bookmark", json::s(&repo.config.bookmark)) |
| 1268 | 1× | ]) |
| 1269 | 1× | ); |
| 1270 | 1× | } else { |
| 1271 | 0× | println!("{}", repo.root().display()); |
| 1272 | 0× | } |
| 1273 | 1× | Ok(0) |
| 1274 | 1× | } |
| 1275 | |
|
| 1276 | 1× | pub fn version(as_json: bool) -> Result<i32> { |
| 1277 | 1× | if as_json { |
| 1278 | 1× | println!( |
| 1279 | 1× | "{}", |
| 1280 | 1× | json::obj([ |
| 1281 | 1× | ("version", json::s(env!("CARGO_PKG_VERSION"))), |
| 1282 | 1× | |
| 1283 | 1× | |
| 1284 | 1× | ("build", json::s(env!("OXY_BUILD_ID"))), |
| 1285 | 1× | ("protocol", json::n(oxy_core::protocol::PROTOCOL_VERSION)), |
| 1286 | 1× | ("format", json::n(2)) |
| 1287 | 1× | ]) |
| 1288 | 1× | ); |
| 1289 | 1× | } else { |
| 1290 | 0× | println!( |
| 1291 | 0× | "oxy {} (repository format 2, protocol {})", |
| 1292 | 0× | env!("OXY_VERSION_LONG"), |
| 1293 | 0× | oxy_core::protocol::PROTOCOL_VERSION |
| 1294 | 0× | ); |
| 1295 | 0× | } |
| 1296 | 1× | Ok(0) |
| 1297 | 1× | } |
| 1298 | |
|
| 1299 | 2× | pub fn discard(paths: &[String], hunks: &[usize]) -> Result<i32> { |
| 1300 | 2× | let repo = open()?; |
| 1301 | 2× | if paths.is_empty() && hunks.is_empty() { |
| 1302 | 0× | anyhow::bail!("name paths to restore, or --hunks to undo chosen hunks"); |
| 1303 | 2× | } |
| 1304 | 2× | let mut n = 0; |
| 1305 | 2× | if !hunks.is_empty() { |
| 1306 | 1× | n += repo.discard_hunks(hunks)?; |
| 1307 | 1× | } |
| 1308 | 2× | if !paths.is_empty() { |
| 1309 | 1× | n += repo.discard(paths)?; |
| 1310 | 1× | } |
| 1311 | 2× | println!( |
| 1312 | | "{} {} restored to the working copy's base", |
| 1313 | 2× | ui::done("Discarded"), |
| 1314 | 2× | plural(n, "path") |
| 1315 | | ); |
| 1316 | 2× | Ok(0) |
| 1317 | 2× | } |
| 1318 | |
|
| 1319 | 2× | pub fn hunks(as_json: bool) -> Result<i32> { |
| 1320 | 2× | let repo = open()?; |
| 1321 | 2× | let (_, hunks) = repo.hunks()?; |
| 1322 | 2× | if as_json { |
| 1323 | 2× | println!( |
| 1324 | | "{}", |
| 1325 | 5× | json::arr(hunks.iter().map(|h| json::obj([ |
| 1326 | 5× | ("index", json::n(h.index)), |
| 1327 | 5× | ("path", json::s(&h.path)), |
| 1328 | 5× | ("text", json::s(&h.text)) |
| 1329 | 5× | ]))) |
| 1330 | | ); |
| 1331 | 2× | return Ok(0); |
| 1332 | 0× | } |
| 1333 | 0× | if hunks.is_empty() { |
| 1334 | 0× | println!( |
| 1335 | 0× | "{}", |
| 1336 | 0× | ui::dim("No hunks: the working copy matches its base.") |
| 1337 | 0× | ); |
| 1338 | 0× | } |
| 1339 | 0× | for h in &hunks { |
| 1340 | 0× | println!( |
| 1341 | | "{} {}", |
| 1342 | 0× | ui::section(format!("hunk {}", h.index)), |
| 1343 | 0× | ui::path(&h.path) |
| 1344 | | ); |
| 1345 | 0× | for line in h.text.lines() { |
| 1346 | 0× | let styled = if line.starts_with('+') { |
| 1347 | 0× | ui::ok(line) |
| 1348 | 0× | } else if line.starts_with('-') { |
| 1349 | 0× | ui::err(line) |
| 1350 | | } else { |
| 1351 | 0× | ui::dim(line) |
| 1352 | | }; |
| 1353 | 0× | println!(" {styled}"); |
| 1354 | | } |
| 1355 | | } |
| 1356 | 0× | Ok(0) |
| 1357 | 2× | } |
| 1358 | |
|
| 1359 | 3× | pub fn show(change: Option<&str>, revset: Option<&str>, stat: bool, as_json: bool) -> Result<i32> { |
| 1360 | 3× | let repo = open()?; |
| 1361 | 3× | repo.snapshot_status("oxy show")?; |
| 1362 | 3× | let Some(r) = revset else { |
| 1363 | 3× | let h = repo.resolve_change(change.unwrap_or("@"))?; |
| 1364 | 3× | return show_one(&repo, h, as_json, false, stat); |
| 1365 | | }; |
| 1366 | | |
| 1367 | 0× | let hashes = repo.revset(r)?.hashes; |
| 1368 | 0× | if as_json { |
| 1369 | 0× | print!("["); |
| 1370 | 0× | for (i, h) in hashes.iter().enumerate() { |
| 1371 | 0× | if i > 0 { |
| 1372 | 0× | print!(","); |
| 1373 | 0× | } |
| 1374 | 0× | show_one(&repo, *h, true, true, stat)?; |
| 1375 | | } |
| 1376 | 0× | println!("]"); |
| 1377 | 0× | return Ok(0); |
| 1378 | 0× | } |
| 1379 | 0× | if hashes.is_empty() { |
| 1380 | 0× | println!("{}", ui::dim(format!("No change matches {r:?}."))); |
| 1381 | 0× | return Ok(0); |
| 1382 | 0× | } |
| 1383 | 0× | for (i, h) in hashes.iter().enumerate() { |
| 1384 | 0× | if i > 0 { |
| 1385 | 0× | println!(); |
| 1386 | 0× | } |
| 1387 | 0× | show_one(&repo, *h, false, false, stat)?; |
| 1388 | | } |
| 1389 | 0× | Ok(0) |
| 1390 | 3× | } |
| 1391 | |
|
| 1392 | |
|
| 1393 | 2× | fn counts(f: &oxy_core::repo::FileDiff) -> (usize, usize) { |
| 1394 | 2× | f.hunks |
| 1395 | 2× | .iter() |
| 1396 | 2× | .fold((0, 0), |(a, r), h| (a + h.added.len(), r + h.removed.len())) |
| 1397 | 2× | } |
| 1398 | |
|
| 1399 | |
|
| 1400 | |
|
| 1401 | 1× | fn print_stat(files: &[oxy_core::repo::FileDiff]) { |
| 1402 | 1× | let width = files |
| 1403 | 1× | .iter() |
| 1404 | 1× | .map(|f| f.path.chars().count()) |
| 1405 | 1× | .max() |
| 1406 | 1× | .unwrap_or(0) |
| 1407 | 1× | .min(60); |
| 1408 | 1× | let (mut added, mut removed) = (0, 0); |
| 1409 | 1× | for f in files { |
| 1410 | 1× | let (a, r) = counts(f); |
| 1411 | 1× | added += a; |
| 1412 | 1× | removed += r; |
| 1413 | 1× | let path = match &f.from { |
| 1414 | 0× | Some(from) => format!("{from} → {}", f.path), |
| 1415 | 1× | None => f.path.clone(), |
| 1416 | | }; |
| 1417 | 1× | println!( |
| 1418 | | " {path:<width$} {} {} {}", |
| 1419 | 1× | ui::ok(format!("{:>6}", format!("+{a}"))), |
| 1420 | 1× | ui::err(format!("{:>6}", format!("-{r}"))), |
| 1421 | 1× | ui::dim(&f.kind) |
| 1422 | | ); |
| 1423 | | } |
| 1424 | 1× | println!( |
| 1425 | | " {}", |
| 1426 | 1× | ui::dim(format!( |
| 1427 | | "{} changed, {added} added, {removed} removed", |
| 1428 | 1× | plural(files.len(), "file") |
| 1429 | | )) |
| 1430 | | ); |
| 1431 | 1× | } |
| 1432 | |
|
| 1433 | |
|
| 1434 | |
|
| 1435 | 3× | fn show_one(repo: &Repo, h: ChangeHash, as_json: bool, inline: bool, stat: bool) -> Result<i32> { |
| 1436 | 3× | let c = repo.store().get_change(&h)?; |
| 1437 | 3× | if as_json { |
| 1438 | 1× | let files = repo.diff_files(Some(&h))?; |
| 1439 | 1× | let heads = repo.bookmark_heads()?; |
| 1440 | 1× | let entry = oxy_core::repo::LogEntry { |
| 1441 | 1× | hash: h, |
| 1442 | 1× | change: oxy_core::state::ChangeMeta::of(&c), |
| 1443 | 1× | is_working_copy: c.kind == oxy_core::objects::ChangeKind::WorkingCopy, |
| 1444 | 1× | is_head: heads.contains(&h), |
| 1445 | 1× | }; |
| 1446 | 1× | let mut body = log_entry_json(repo, &entry)?; |
| 1447 | 1× | body.pop(); |
| 1448 | 1× | body.push_str(&format!( |
| 1449 | | ",{}:{}}}", |
| 1450 | 1× | json::s("files"), |
| 1451 | 1× | json::arr(files.iter().map(|f| { |
| 1452 | 1× | let (a, r) = counts(f); |
| 1453 | 1× | json::obj([ |
| 1454 | 1× | ("path", json::s(&f.path)), |
| 1455 | 1× | ("kind", json::s(&f.kind)), |
| 1456 | 1× | ("from", json::opt(f.from.as_deref(), json::s)), |
| 1457 | 1× | ("added", a.to_string()), |
| 1458 | 1× | ("removed", r.to_string()), |
| 1459 | 1× | ]) |
| 1460 | 1× | })) |
| 1461 | | )); |
| 1462 | 1× | if inline { |
| 1463 | 0× | print!("{body}"); |
| 1464 | 1× | } else { |
| 1465 | 1× | println!("{body}"); |
| 1466 | 1× | } |
| 1467 | 1× | return Ok(0); |
| 1468 | 2× | } |
| 1469 | 2× | println!( |
| 1470 | | "{} {} {}", |
| 1471 | 2× | ui::section("change"), |
| 1472 | 2× | ui::id(short_id(&c)), |
| 1473 | 2× | ui::hash(format!("#{h}")) |
| 1474 | | ); |
| 1475 | 2× | println!("{}", ui::kv("kind", format!("{:?}", c.kind).to_lowercase())); |
| 1476 | | |
| 1477 | 2× | if let Some(at) = repo.remote_only_location(&h)? { |
| 1478 | 0× | println!( |
| 1479 | 0× | "{}", |
| 1480 | 0× | ui::kv( |
| 1481 | 0× | "only in", |
| 1482 | 0× | ui::warn(format!( |
| 1483 | 0× | "{at}: fetched, not yet in a local bookmark (oxy pull)" |
| 1484 | 0× | )) |
| 1485 | 0× | ) |
| 1486 | 0× | ); |
| 1487 | 2× | } |
| 1488 | 2× | let meta = oxy_core::state::ChangeMeta::of(&c); |
| 1489 | 2× | let status = repo.authenticate_change_known(&h, Some(&meta))?; |
| 1490 | 2× | println!("{}", ui::kv("author", ui::author(author_name(repo, &meta)))); |
| 1491 | 2× | if let Some(agent) = via(repo, &status, &meta.author) { |
| 1492 | 0× | println!("{}", ui::kv("via", agent.line())); |
| 1493 | 2× | } |
| 1494 | 2× | println!( |
| 1495 | | "{}", |
| 1496 | 2× | ui::kv( |
| 1497 | 2× | "date", |
| 1498 | 2× | format!( |
| 1499 | | "{} {}", |
| 1500 | 2× | when(c.hlc.wall_ms), |
| 1501 | 2× | ui::dim(format!("({})", ui::ago(c.hlc.wall_ms))) |
| 1502 | | ) |
| 1503 | | ) |
| 1504 | | ); |
| 1505 | 2× | println!("{}", ui::kv("deps", ui::dim(deps_line(&c)))); |
| 1506 | 2× | println!("{}", ui::kv("sig", signature_line(repo, &h)?)); |
| 1507 | 2× | println!(); |
| 1508 | 5× | for line in (if c.message.is_empty() { |
| 1509 | 0× | "(no description)" |
| 1510 | | } else { |
| 1511 | 2× | &c.message |
| 1512 | | }) |
| 1513 | 2× | .lines() |
| 1514 | 5× | { |
| 1515 | 5× | println!(" {line}"); |
| 1516 | 5× | } |
| 1517 | 2× | println!(); |
| 1518 | 2× | if stat { |
| 1519 | 1× | print_stat(&repo.diff_files(Some(&h))?); |
| 1520 | | } else { |
| 1521 | 1× | print!("{}", repo.describe_change(&h)?); |
| 1522 | | } |
| 1523 | 2× | Ok(0) |
| 1524 | 3× | } |
| 1525 | |
|
| 1526 | 0× | pub fn checkout(changes: &[String], discard: bool, carry: bool) -> Result<i32> { |
| 1527 | 0× | let repo = open()?; |
| 1528 | | |
| 1529 | | |
| 1530 | | |
| 1531 | 0× | if let [name] = changes { |
| 1532 | 0× | if *name != repo.config.bookmark && names_bookmark(&repo, name)? { |
| 1533 | 0× | drop(repo); |
| 1534 | 0× | return bookmark_switch(name, discard, carry); |
| 1535 | 0× | } |
| 1536 | 0× | } |
| 1537 | 0× | let heads = if changes.is_empty() { |
| 1538 | 0× | repo.bookmark_heads()? |
| 1539 | | } else { |
| 1540 | 0× | let mut heads = Vec::new(); |
| 1541 | 0× | for c in changes { |
| 1542 | 0× | heads.extend(repo.resolve_heads(c)?); |
| 1543 | | } |
| 1544 | 0× | heads |
| 1545 | | }; |
| 1546 | 0× | let spec = if changes.is_empty() { |
| 1547 | 0× | repo.config.bookmark.clone() |
| 1548 | | } else { |
| 1549 | 0× | changes.join(" ") |
| 1550 | | }; |
| 1551 | 0× | let sp = ui::spinner(&format!("Checking out {spec}…")); |
| 1552 | 0× | let command = format!("oxy checkout {spec}"); |
| 1553 | 0× | let (r, carried) = if carry { |
| 1554 | 0× | repo.checkout_carry(&heads, &spec, &command)? |
| 1555 | | } else { |
| 1556 | 0× | (repo.checkout(&heads, discard, &command)?, vec![]) |
| 1557 | | }; |
| 1558 | 0× | sp.done(&format!( |
| 1559 | | "{} on {}: {} written, {} removed{}", |
| 1560 | 0× | ui::done("Working copy"), |
| 1561 | 0× | ui::bookmark(&spec), |
| 1562 | 0× | plural(r.written, "file"), |
| 1563 | | r.removed, |
| 1564 | 0× | if carried.is_empty() { |
| 1565 | 0× | String::new() |
| 1566 | | } else { |
| 1567 | 0× | format!("; carried {}", plural(carried.len(), "changed path")) |
| 1568 | | } |
| 1569 | | )); |
| 1570 | 0× | print_removed(&r); |
| 1571 | 0× | for n in &r.notes { |
| 1572 | 0× | println!(" {} {n}", ui::note()); |
| 1573 | 0× | } |
| 1574 | 0× | print_kept_both(&r); |
| 1575 | 0× | if r.conflicted { |
| 1576 | 0× | println!( |
| 1577 | 0× | " {} {}", |
| 1578 | 0× | ui::caution(), |
| 1579 | 0× | ui::warn("this state has conflicts; see oxy status") |
| 1580 | 0× | ); |
| 1581 | 0× | } |
| 1582 | 0× | where_now(&repo); |
| 1583 | 0× | Ok(0) |
| 1584 | 0× | } |
| 1585 | |
|
| 1586 | |
|
| 1587 | 3× | fn print_kept_both(r: &oxy_core::repo::CheckoutReport) { |
| 1588 | 3× | for k in &r.kept_both { |
| 1589 | 0× | let how = if k.identical.len() == k.settled.len() { |
| 1590 | 0× | "kept one of its identical sides" |
| 1591 | 0× | } else if k.identical.is_empty() { |
| 1592 | 0× | "kept both sides (merge=union)" |
| 1593 | | } else { |
| 1594 | 0× | "kept one of identical sides and both of others (merge=union)" |
| 1595 | | }; |
| 1596 | 0× | println!( |
| 1597 | | " {} settled {}: {how}; the next commit records it", |
| 1598 | 0× | ui::note(), |
| 1599 | 0× | ui::path(&k.path) |
| 1600 | | ); |
| 1601 | | } |
| 1602 | 3× | } |
| 1603 | |
|
| 1604 | 1× | pub fn resolve(paths: &[String], both: bool, reverse: bool, as_json: bool) -> Result<i32> { |
| 1605 | 1× | let repo = open()?; |
| 1606 | 1× | let mut snap = repo.snapshot("oxy resolve")?; |
| 1607 | 1× | let paths: Vec<String> = paths |
| 1608 | 1× | .iter() |
| 1609 | 1× | .map(|p| p.trim_start_matches("./").trim_end_matches('/').to_string()) |
| 1610 | 1× | .collect(); |
| 1611 | 1× | if both { |
| 1612 | | |
| 1613 | 0× | let kept = repo.keep_both(&snap, &paths, reverse)?; |
| 1614 | 0× | if !as_json { |
| 1615 | 0× | for k in &kept { |
| 1616 | 0× | if !k.settled.is_empty() { |
| 1617 | 0× | let n: Vec<String> = k.settled.iter().map(|n| n.to_string()).collect(); |
| 1618 | 0× | println!("kept both {}: conflict {}", k.path, n.join(", ")); |
| 1619 | 0× | } |
| 1620 | 0× | for (n, why) in &k.left { |
| 1621 | 0× | println!("markers {}: conflict {n}: {why}", k.path); |
| 1622 | 0× | } |
| 1623 | | } |
| 1624 | 0× | } |
| 1625 | 0× | snap = repo.snapshot("oxy resolve")?; |
| 1626 | 1× | } else if !paths.is_empty() { |
| 1627 | | |
| 1628 | | |
| 1629 | 0× | repo.choose_resolutions(&snap.base_snapshot, &paths)?; |
| 1630 | 0× | snap = repo.snapshot("oxy resolve")?; |
| 1631 | 1× | } |
| 1632 | 1× | if as_json { |
| 1633 | 1× | println!("{}", repo.status_json(&snap)?); |
| 1634 | 1× | return Ok(if snap.base_snapshot.is_conflicted() { |
| 1635 | 0× | 2 |
| 1636 | | } else { |
| 1637 | 1× | 0 |
| 1638 | | }); |
| 1639 | 0× | } |
| 1640 | 0× | if !snap.base_snapshot.is_conflicted() { |
| 1641 | 0× | println!("The working copy's base has no conflicts."); |
| 1642 | 0× | return Ok(0); |
| 1643 | 0× | } |
| 1644 | 0× | let mut remaining = 0; |
| 1645 | 0× | for (p, f) in &snap.base_snapshot.files { |
| 1646 | 0× | if !f.is_conflicted() { |
| 1647 | 0× | continue; |
| 1648 | 0× | } |
| 1649 | 0× | match snap |
| 1650 | 0× | .statuses |
| 1651 | 0× | .iter() |
| 1652 | 0× | .find(|s| s.path == *p) |
| 1653 | 0× | .map(|s| s.status) |
| 1654 | | { |
| 1655 | 0× | Some(ChangeStatus::Resolved) => println!("resolved {p}"), |
| 1656 | 0× | Some(ChangeStatus::ConflictEdited) => { |
| 1657 | 0× | remaining += 1; |
| 1658 | 0× | println!("markers {p}"); |
| 1659 | 0× | } |
| 1660 | 0× | _ => { |
| 1661 | 0× | remaining += 1; |
| 1662 | 0× | println!("unresolved {p}"); |
| 1663 | 0× | } |
| 1664 | | } |
| 1665 | | } |
| 1666 | 0× | for c in &snap.base_snapshot.tree.conflicts { |
| 1667 | 0× | remaining += 1; |
| 1668 | 0× | println!("tree {c:?}"); |
| 1669 | 0× | } |
| 1670 | 0× | if remaining == 0 { |
| 1671 | 0× | println!("All conflicts are resolved; `oxy commit` will record the resolution."); |
| 1672 | 0× | Ok(0) |
| 1673 | | } else { |
| 1674 | 0× | Ok(2) |
| 1675 | | } |
| 1676 | 1× | } |
| 1677 | |
|
| 1678 | 0× | pub fn undo() -> Result<i32> { |
| 1679 | 0× | let repo = open()?; |
| 1680 | 0× | let u = repo.undo()?; |
| 1681 | 0× | println!("{} {}", ui::done("Undone:"), u.command); |
| 1682 | 0× | for c in &u.changes { |
| 1683 | 0× | println!(" {} {}", ui::note(), change_words(&repo, c)); |
| 1684 | 0× | } |
| 1685 | 0× | if u.others_kept > 0 { |
| 1686 | 0× | let s = if u.others_kept == 1 { "" } else { "s" }; |
| 1687 | 0× | println!( |
| 1688 | | " {} {}", |
| 1689 | 0× | ui::note(), |
| 1690 | 0× | ui::dim(format!( |
| 1691 | | "{} newer operation{s} from other workspaces left as they are", |
| 1692 | | u.others_kept |
| 1693 | | )) |
| 1694 | | ); |
| 1695 | 0× | } |
| 1696 | 0× | for (bookmark, remote) in &u.pushed { |
| 1697 | 0× | println!( |
| 1698 | 0× | " {} {}", |
| 1699 | 0× | ui::caution(), |
| 1700 | 0× | ui::warn(format!( |
| 1701 | 0× | "{remote}/{bookmark} already has what this undid: undone here only; the next push of {bookmark} needs --force to take it back there" |
| 1702 | 0× | )) |
| 1703 | 0× | ); |
| 1704 | 0× | } |
| 1705 | 0× | where_now(&repo); |
| 1706 | 0× | Ok(0) |
| 1707 | 0× | } |
| 1708 | |
|
| 1709 | 1× | pub fn op_log(limit: Option<usize>, as_json: bool) -> Result<i32> { |
| 1710 | 1× | let repo = open()?; |
| 1711 | 1× | let ops = repo.op_log()?; |
| 1712 | | |
| 1713 | 11× | let changes_of = |i: usize| -> Result<Vec<oxy_core::repo::ViewChange>> { |
| 1714 | 11× | match ops.get(i + 1) { |
| 1715 | 10× | Some((_, before)) => Ok(repo.view_changes(&before.view, &ops[i].1.view)?), |
| 1716 | 1× | None => Ok(vec![]), |
| 1717 | | } |
| 1718 | 11× | }; |
| 1719 | 1× | if as_json { |
| 1720 | 1× | let mut items = Vec::new(); |
| 1721 | 11× | for (i, (r, op)) in ops.iter().enumerate().take(limit.unwrap_or(usize::MAX)) { |
| 1722 | 11× | let summary: Vec<String> = changes_of(i)? |
| 1723 | 11× | .iter() |
| 1724 | 11× | .map(|c| change_words(&repo, c)) |
| 1725 | 11× | .collect(); |
| 1726 | 11× | items.push(json::obj([ |
| 1727 | 11× | ("id", json::s(&r.to_hex())), |
| 1728 | 11× | ("when_ms", json::n(op.hlc.wall_ms)), |
| 1729 | 11× | ("command", json::s(&op.command)), |
| 1730 | 11× | ("snapshot", json::b(op.is_snapshot())), |
| 1731 | 11× | ("current", json::b(i == 0)), |
| 1732 | 11× | ("changes", json::arr(summary.iter().map(|x| json::s(x)))), |
| 1733 | | ])); |
| 1734 | | } |
| 1735 | 1× | println!("{}", json::arr(items)); |
| 1736 | 1× | return Ok(0); |
| 1737 | 0× | } |
| 1738 | 0× | let id_prefix = unique_prefixes(ops.iter().map(|(r, _)| r.to_hex())); |
| 1739 | 0× | let id = |r: &oxy_core::hash::ObjectRef| { |
| 1740 | 0× | let hex = r.to_hex(); |
| 1741 | 0× | let n = id_prefix.get(&hex).copied().unwrap_or(12).min(12); |
| 1742 | 0× | format!("{}{}", ui::id(&hex[..n]), ui::dim(&hex[n..12])) |
| 1743 | 0× | }; |
| 1744 | 0× | let rail = ui::dim(ui::glyph("│", "|")); |
| 1745 | 0× | let (mut i, mut shown) = (0, 0); |
| 1746 | 0× | while i < ops.len() && shown < limit.unwrap_or(usize::MAX) { |
| 1747 | 0× | let (r, op) = &ops[i]; |
| 1748 | 0× | let bullet = if i == 0 { |
| 1749 | 0× | ui::ok(ui::glyph("@", "@")) |
| 1750 | | } else { |
| 1751 | 0× | ui::dim(ui::glyph("○", "o")) |
| 1752 | | }; |
| 1753 | | |
| 1754 | 0× | let mut run = 1; |
| 1755 | 0× | while op.is_snapshot() && i + run < ops.len() && ops[i + run].1.is_snapshot() { |
| 1756 | 0× | run += 1; |
| 1757 | 0× | } |
| 1758 | 0× | let when = ui::dim(ui::ago(op.hlc.wall_ms)); |
| 1759 | 0× | if op.is_snapshot() { |
| 1760 | 0× | let span = if run > 1 { |
| 1761 | 0× | format!("{} {}", ui::dim("…"), id(&ops[i + run - 1].0)) |
| 1762 | | } else { |
| 1763 | 0× | String::new() |
| 1764 | | }; |
| 1765 | 0× | println!( |
| 1766 | | "{bullet} {}{} {when}", |
| 1767 | 0× | id(r), |
| 1768 | 0× | if span.is_empty() { |
| 1769 | 0× | String::new() |
| 1770 | | } else { |
| 1771 | 0× | format!(" {span}") |
| 1772 | | } |
| 1773 | | ); |
| 1774 | 0× | println!( |
| 1775 | | "{rail} {}", |
| 1776 | 0× | ui::dim(if run == 1 { |
| 1777 | 0× | "working-copy snapshot".to_string() |
| 1778 | | } else { |
| 1779 | 0× | format!("{run} working-copy snapshots") |
| 1780 | | }) |
| 1781 | | ); |
| 1782 | | } else { |
| 1783 | 0× | println!("{bullet} {} {when}", id(r)); |
| 1784 | 0× | let words: Vec<String> = changes_of(i)? |
| 1785 | 0× | .iter() |
| 1786 | 0× | .map(|c| change_words(&repo, c)) |
| 1787 | 0× | .collect(); |
| 1788 | 0× | let summary = if words.is_empty() { |
| 1789 | 0× | if i + 1 == ops.len() { |
| 1790 | 0× | "created the repository".to_string() |
| 1791 | | } else { |
| 1792 | 0× | "no bookmark or working-copy change".to_string() |
| 1793 | | } |
| 1794 | | } else { |
| 1795 | 0× | words.join("; ") |
| 1796 | | }; |
| 1797 | 0× | println!( |
| 1798 | | "{rail} {}", |
| 1799 | 0× | if i == 0 { |
| 1800 | 0× | ui::subject(&summary) |
| 1801 | | } else { |
| 1802 | 0× | summary |
| 1803 | | } |
| 1804 | | ); |
| 1805 | 0× | let cmd = op.command.lines().next().unwrap_or(""); |
| 1806 | 0× | let cmd: String = cmd.chars().take(100).collect(); |
| 1807 | 0× | println!("{rail} {}", ui::dim(cmd)); |
| 1808 | | } |
| 1809 | 0× | i += run; |
| 1810 | 0× | shown += 1; |
| 1811 | | } |
| 1812 | 0× | if i < ops.len() { |
| 1813 | 0× | println!( |
| 1814 | 0× | "{}", |
| 1815 | 0× | ui::dim(format!( |
| 1816 | 0× | "{} {} older; pass -n to see more", |
| 1817 | 0× | ui::glyph("…", "..."), |
| 1818 | 0× | plural(ops.len() - i, "operation") |
| 1819 | 0× | )) |
| 1820 | 0× | ); |
| 1821 | 0× | } |
| 1822 | 0× | Ok(0) |
| 1823 | 1× | } |
| 1824 | |
|
| 1825 | |
|
| 1826 | 11× | fn change_words(repo: &Repo, c: &oxy_core::repo::ViewChange) -> String { |
| 1827 | | use oxy_core::repo::ViewChange::*; |
| 1828 | 2× | match c { |
| 1829 | 0× | BookmarkCreated { name, heads } => { |
| 1830 | 0× | format!("created bookmark {name} ({})", plural(*heads, "head")) |
| 1831 | | } |
| 1832 | 0× | BookmarkDeleted { name } => format!("deleted bookmark {name}"), |
| 1833 | 2× | BookmarkMoved { name, from, to } if from == to => format!("moved {name}"), |
| 1834 | 1× | BookmarkMoved { name, from, to } => format!( |
| 1835 | | "{name}: {from} {} {}", |
| 1836 | 1× | ui::glyph("→", "->"), |
| 1837 | 1× | plural(*to, "head") |
| 1838 | | ), |
| 1839 | 1× | RemoteMoved { remote, name } => format!("{remote}/{name} updated"), |
| 1840 | 0× | TagCreated { name } => format!("tagged {name}"), |
| 1841 | 0× | TagDeleted { name } => format!("removed tag {name}"), |
| 1842 | 0× | Policy => "trust policy changed".to_string(), |
| 1843 | 8× | WorkingCopy { change } => match repo.store().get_change_meta(change) { |
| 1844 | 8× | Ok(m) => format!("working copy now {}", m.change_id.short(8)), |
| 1845 | 0× | Err(_) => "working copy moved".to_string(), |
| 1846 | | }, |
| 1847 | | } |
| 1848 | 11× | } |
| 1849 | |
|
| 1850 | 0× | pub fn op_restore(op: &str, discard: bool) -> Result<i32> { |
| 1851 | 0× | let repo = open()?; |
| 1852 | 0× | let sp = ui::spinner(&format!("Restoring to operation {op}…")); |
| 1853 | 0× | let r = repo.op_restore(op, discard)?; |
| 1854 | 0× | sp.done(&format!( |
| 1855 | 0× | "{} to operation {} {}", |
| 1856 | 0× | ui::done("Restored"), |
| 1857 | 0× | ui::hash(r.op.short(12)), |
| 1858 | 0× | ui::dim(r.command.lines().next().unwrap_or("")) |
| 1859 | 0× | )); |
| 1860 | 0× | println!( |
| 1861 | | " {} {}", |
| 1862 | 0× | ui::note(), |
| 1863 | 0× | ui::dim(format!( |
| 1864 | | "{} written, {} removed; oxy undo goes back again", |
| 1865 | 0× | plural(r.checkout.written, "file"), |
| 1866 | | r.checkout.removed |
| 1867 | | )) |
| 1868 | | ); |
| 1869 | 0× | where_now(&repo); |
| 1870 | 0× | Ok(0) |
| 1871 | 0× | } |
| 1872 | |
|
| 1873 | 0× | pub fn op_revert(op: &str) -> Result<i32> { |
| 1874 | 0× | let repo = open()?; |
| 1875 | 0× | let r = repo.op_revert(op)?; |
| 1876 | 0× | let view = repo.view()?; |
| 1877 | 0× | let names: Vec<String> = r |
| 1878 | 0× | .reverted |
| 1879 | 0× | .iter() |
| 1880 | 0× | .map(|n| { |
| 1881 | 0× | if view.bookmarks.contains_key(n) { |
| 1882 | 0× | ui::bookmark(n) |
| 1883 | | } else { |
| 1884 | 0× | format!("{} (removed)", ui::bookmark(n)) |
| 1885 | | } |
| 1886 | 0× | }) |
| 1887 | 0× | .collect(); |
| 1888 | 0× | println!( |
| 1889 | | "{} operation {} {}", |
| 1890 | 0× | ui::done("Reverted"), |
| 1891 | 0× | ui::hash(r.op.short(12)), |
| 1892 | 0× | ui::dim(r.command.lines().next().unwrap_or("")) |
| 1893 | | ); |
| 1894 | 0× | println!( |
| 1895 | | " {} {}", |
| 1896 | 0× | ui::note(), |
| 1897 | 0× | ui::dim(format!("as before it: {}", names.join(", "))) |
| 1898 | | ); |
| 1899 | 0× | if !r.kept.is_empty() { |
| 1900 | 0× | println!( |
| 1901 | 0× | " {} {}", |
| 1902 | 0× | ui::caution(), |
| 1903 | 0× | ui::warn(format!( |
| 1904 | 0× | "left as they are, having moved again since: {}", |
| 1905 | 0× | r.kept.join(", ") |
| 1906 | 0× | )) |
| 1907 | 0× | ); |
| 1908 | 0× | } |
| 1909 | 0× | if !r.checked_out && r.reverted.contains(&repo.config.bookmark) { |
| 1910 | 0× | println!( |
| 1911 | 0× | " {} {}", |
| 1912 | 0× | ui::note(), |
| 1913 | 0× | ui::dim("the working copy has work of its own: oxy checkout to move it") |
| 1914 | 0× | ); |
| 1915 | 0× | } |
| 1916 | 0× | where_now(&repo); |
| 1917 | 0× | Ok(0) |
| 1918 | 0× | } |
| 1919 | |
|
| 1920 | |
|
| 1921 | |
|
| 1922 | 41× | pub fn where_now(repo: &Repo) { |
| 1923 | 41× | let Ok((_, wc)) = repo.working_copy() else { |
| 1924 | 0× | return; |
| 1925 | | }; |
| 1926 | 41× | let heads = repo.bookmark_heads().unwrap_or_default(); |
| 1927 | 41× | let newest = heads |
| 1928 | 41× | .iter() |
| 1929 | 55× | .filter_map(|h| repo.store().get_change_meta(h).ok().map(|m| (m, *h))) |
| 1930 | 41× | .max_by_key(|(m, _)| m.hlc); |
| 1931 | 41× | println!( |
| 1932 | | " {} {}", |
| 1933 | 41× | ui::dim(format!("{:<13}", "Working copy")), |
| 1934 | 41× | ui::id(wc.change_id.short(8)) |
| 1935 | | ); |
| 1936 | 41× | let tail = match newest { |
| 1937 | 41× | Some((m, _)) => format!( |
| 1938 | | ", newest {} {}", |
| 1939 | 41× | ui::id(m.change_id.short(8)), |
| 1940 | 41× | subject_of(&m.message) |
| 1941 | | ), |
| 1942 | 0× | None => String::new(), |
| 1943 | | }; |
| 1944 | 41× | println!( |
| 1945 | | " {} {}: {}{tail}", |
| 1946 | 41× | ui::dim(format!("{:<13}", "Bookmark")), |
| 1947 | 41× | ui::bookmark(&repo.config.bookmark), |
| 1948 | 41× | plural(heads.len(), "head") |
| 1949 | | ); |
| 1950 | 41× | } |
| 1951 | |
|
| 1952 | 1× | pub fn gc(now: bool, expire_snapshots: bool) -> Result<i32> { |
| 1953 | 1× | let mut repo = open()?; |
| 1954 | 1× | if expire_snapshots { |
| 1955 | 0× | repo.config.snapshot_retention_days = 0; |
| 1956 | 1× | } |
| 1957 | 1× | let sp = ui::spinner("Packing and pruning…"); |
| 1958 | | |
| 1959 | 1× | let artifacts = cylinder::roots(&repo)?; |
| 1960 | 5× | let r = repo.gc_keeping(now, &artifacts, &mut |phase| sp.set(&format!("{phase}…")))?; |
| 1961 | 1× | sp.clear(); |
| 1962 | 1× | if r.packed.objects > 0 { |
| 1963 | 1× | println!( |
| 1964 | 1× | "Packed {} into one pack: {} bytes -> {} bytes ({:.0}%).", |
| 1965 | 1× | plural(r.packed.objects, "object"), |
| 1966 | 1× | r.packed.raw_bytes, |
| 1967 | 1× | r.packed.packed_bytes, |
| 1968 | 1× | 100.0 * r.packed.packed_bytes as f64 / r.packed.raw_bytes.max(1) as f64 |
| 1969 | 1× | ); |
| 1970 | 1× | } |
| 1971 | 1× | println!( |
| 1972 | | "{} reachable; pruned {} ({} bytes){}{}; {} present.", |
| 1973 | 1× | plural(r.reachable, "object"), |
| 1974 | 1× | plural(r.pruned.objects, "unreachable object"), |
| 1975 | | r.pruned.bytes, |
| 1976 | 1× | if r.pruned.deferred > 0 { |
| 1977 | 0× | format!( |
| 1978 | | "; {} deferred by the grace period (use --now)", |
| 1979 | 0× | plural(r.pruned.deferred, "object") |
| 1980 | | ) |
| 1981 | | } else { |
| 1982 | 1× | String::new() |
| 1983 | | }, |
| 1984 | 1× | if r.merged_packs > 0 { |
| 1985 | 0× | format!("; merged {} into one", plural(r.merged_packs, "pack")) |
| 1986 | | } else { |
| 1987 | 1× | String::new() |
| 1988 | | }, |
| 1989 | 1× | plural(repo.store().pack_count(), "pack") |
| 1990 | | ); |
| 1991 | | |
| 1992 | | |
| 1993 | 1× | let kept = if r.retired.kept > 0 { |
| 1994 | 0× | format!( |
| 1995 | | "{} retired less than an hour ago kept for readers", |
| 1996 | 0× | plural(r.retired.kept, "file") |
| 1997 | | ) |
| 1998 | | } else { |
| 1999 | 1× | String::new() |
| 2000 | | }; |
| 2001 | 1× | if r.retired.packs + r.retired.loose > 0 { |
| 2002 | 0× | println!( |
| 2003 | | "Deleted {} and {} retired by earlier repacks{}.", |
| 2004 | 0× | plural(r.retired.packs, "pack"), |
| 2005 | 0× | plural(r.retired.loose, "loose object"), |
| 2006 | 0× | if kept.is_empty() { |
| 2007 | 0× | String::new() |
| 2008 | | } else { |
| 2009 | 0× | format!("; {kept}") |
| 2010 | | } |
| 2011 | | ); |
| 2012 | 1× | } else if !kept.is_empty() { |
| 2013 | 0× | println!("{kept}."); |
| 2014 | 1× | } |
| 2015 | 1× | Ok(0) |
| 2016 | 1× | } |
| 2017 | |
|
| 2018 | |
|
| 2019 | 2× | pub fn gc_remote(name: &str, now: bool) -> Result<i32> { |
| 2020 | 2× | let repo = open()?; |
| 2021 | 2× | let sp = ui::spinner(&format!("Folding {name}'s bundles…")); |
| 2022 | 2× | let r = repo.gc_remote(name, now)?; |
| 2023 | 1× | sp.clear(); |
| 2024 | 1× | if r.merged > 0 { |
| 2025 | 1× | println!( |
| 2026 | 1× | "{} {} into one of {} bytes ({})", |
| 2027 | 1× | ui::done("Folded"), |
| 2028 | 1× | plural(r.merged, "bundle"), |
| 2029 | 1× | r.bundle_bytes, |
| 2030 | 1× | plural(r.changes, "change") |
| 2031 | 1× | ); |
| 2032 | 1× | } else { |
| 2033 | 0× | println!( |
| 2034 | 0× | "{} {}", |
| 2035 | 0× | ui::note(), |
| 2036 | 0× | ui::dim("nothing to fold: one bundle holds everything") |
| 2037 | 0× | ); |
| 2038 | 0× | } |
| 2039 | 1× | println!( |
| 2040 | | " {} {}", |
| 2041 | 1× | ui::note(), |
| 2042 | 1× | ui::dim(format!( |
| 2043 | | "removed {}{}", |
| 2044 | 1× | plural(r.deleted, "superseded bundle"), |
| 2045 | 1× | if r.deferred > 0 { |
| 2046 | 0× | format!( |
| 2047 | | "; {} kept for readers that may still fetch them (use --now)", |
| 2048 | 0× | plural(r.deferred, "bundle") |
| 2049 | | ) |
| 2050 | | } else { |
| 2051 | 1× | String::new() |
| 2052 | | } |
| 2053 | | )) |
| 2054 | | ); |
| 2055 | 1× | Ok(0) |
| 2056 | 2× | } |
| 2057 | |
|
| 2058 | |
|
| 2059 | 0× | pub fn doctor(offline: bool, as_json: bool) -> Result<i32> { |
| 2060 | | use oxy_core::repo::Health; |
| 2061 | 0× | let repo = open()?; |
| 2062 | 0× | let sp = ui::spinner(if offline { |
| 2063 | 0× | "Checking…" |
| 2064 | | } else { |
| 2065 | 0× | "Checking, and contacting remotes…" |
| 2066 | | }); |
| 2067 | 0× | let r = repo.doctor(!offline); |
| 2068 | 0× | sp.clear(); |
| 2069 | 0× | let code = if r.worst() == Health::Fail { 1 } else { 0 }; |
| 2070 | 0× | let word = |h: Health| match h { |
| 2071 | 0× | Health::Ok => "ok", |
| 2072 | 0× | Health::Warn => "warning", |
| 2073 | 0× | Health::Fail => "fail", |
| 2074 | 0× | }; |
| 2075 | 0× | if as_json { |
| 2076 | 0× | let checks: Vec<String> = r |
| 2077 | 0× | .checks |
| 2078 | 0× | .iter() |
| 2079 | 0× | .map(|c| { |
| 2080 | 0× | json::obj([ |
| 2081 | 0× | ("area", json::s(&c.area)), |
| 2082 | 0× | ("health", json::s(word(c.health))), |
| 2083 | 0× | ("message", json::s(&c.message)), |
| 2084 | 0× | ("hint", json::opt(c.hint.as_deref(), json::s)), |
| 2085 | 0× | ]) |
| 2086 | 0× | }) |
| 2087 | 0× | .collect(); |
| 2088 | 0× | println!( |
| 2089 | | "{}", |
| 2090 | 0× | json::obj([("ok", json::b(code == 0)), ("checks", json::arr(checks))]) |
| 2091 | | ); |
| 2092 | 0× | return Ok(code); |
| 2093 | 0× | } |
| 2094 | 0× | let mut last = ""; |
| 2095 | 0× | for c in &r.checks { |
| 2096 | 0× | if c.area != last { |
| 2097 | 0× | println!("{}", ui::section(&c.area)); |
| 2098 | 0× | last = &c.area; |
| 2099 | 0× | } |
| 2100 | 0× | let mark = match c.health { |
| 2101 | 0× | Health::Ok => ui::ok(ui::glyph("✔", "v")), |
| 2102 | 0× | Health::Warn => ui::caution(), |
| 2103 | 0× | Health::Fail => ui::err(ui::glyph("✖", "x")), |
| 2104 | | }; |
| 2105 | 0× | println!(" {mark} {}", c.message); |
| 2106 | 0× | if let Some(h) = &c.hint { |
| 2107 | 0× | println!(" {} {}", ui::dim("→"), ui::dim(h)); |
| 2108 | 0× | } |
| 2109 | | } |
| 2110 | 0× | let fails = r.checks.iter().filter(|c| c.health == Health::Fail).count(); |
| 2111 | 0× | let warns = r.checks.iter().filter(|c| c.health == Health::Warn).count(); |
| 2112 | 0× | if fails + warns == 0 { |
| 2113 | 0× | println!("{}", ui::done("Everything looks right.")); |
| 2114 | 0× | } else { |
| 2115 | 0× | println!( |
| 2116 | | "{} {}, {}.", |
| 2117 | 0× | if fails > 0 { |
| 2118 | 0× | ui::err(ui::glyph("✖", "x")) |
| 2119 | | } else { |
| 2120 | 0× | ui::caution() |
| 2121 | | }, |
| 2122 | 0× | plural(fails, "problem"), |
| 2123 | 0× | plural(warns, "warning") |
| 2124 | | ); |
| 2125 | | } |
| 2126 | 0× | Ok(code) |
| 2127 | 0× | } |
| 2128 | |
|
| 2129 | |
|
| 2130 | 0× | pub fn fsck(as_json: bool) -> Result<i32> { |
| 2131 | | use oxy_core::repo::Severity; |
| 2132 | 0× | let repo = open()?; |
| 2133 | 0× | let sp = ui::spinner("Checking every object…"); |
| 2134 | 0× | let r = repo.fsck()?; |
| 2135 | 0× | sp.clear(); |
| 2136 | 0× | let code = if r.errors() > 0 { 1 } else { 0 }; |
| 2137 | 0× | if as_json { |
| 2138 | 0× | let problems: Vec<String> = r |
| 2139 | 0× | .problems |
| 2140 | 0× | .iter() |
| 2141 | 0× | .map(|p| { |
| 2142 | 0× | json::obj([ |
| 2143 | | ( |
| 2144 | | "severity", |
| 2145 | 0× | json::s(match p.severity { |
| 2146 | 0× | Severity::Error => "error", |
| 2147 | 0× | Severity::Warning => "warning", |
| 2148 | | }), |
| 2149 | | ), |
| 2150 | | ( |
| 2151 | 0× | "object", |
| 2152 | 0× | json::opt(p.object.map(|o| o.to_hex()).as_deref(), json::s), |
| 2153 | | ), |
| 2154 | 0× | ("what", json::s(&p.what)), |
| 2155 | | ]) |
| 2156 | 0× | }) |
| 2157 | 0× | .collect(); |
| 2158 | 0× | println!( |
| 2159 | | "{}", |
| 2160 | 0× | json::obj([ |
| 2161 | 0× | ("ok", json::b(code == 0)), |
| 2162 | 0× | ("objects", json::n(r.objects)), |
| 2163 | 0× | ("ops", json::n(r.ops)), |
| 2164 | 0× | ("changes", json::n(r.changes)), |
| 2165 | 0× | ("signed", json::n(r.signed)), |
| 2166 | 0× | ("unsigned", json::n(r.unsigned)), |
| 2167 | 0× | ("checkpoints", json::n(r.checkpoints)), |
| 2168 | 0× | ("attestations", json::n(r.attestations)), |
| 2169 | 0× | ("errors", json::n(r.errors())), |
| 2170 | 0× | ("warnings", json::n(r.warnings())), |
| 2171 | 0× | ("problems", json::arr(problems)), |
| 2172 | 0× | ]) |
| 2173 | | ); |
| 2174 | 0× | return Ok(code); |
| 2175 | 0× | } |
| 2176 | 0× | println!( |
| 2177 | | "Checked {}, {}, {} ({} signed or sealed, {} unsigned), {}, {}.", |
| 2178 | 0× | plural(r.objects, "object"), |
| 2179 | 0× | plural(r.ops, "operation"), |
| 2180 | 0× | plural(r.changes, "change"), |
| 2181 | | r.signed, |
| 2182 | | r.unsigned, |
| 2183 | 0× | plural(r.checkpoints, "checkpoint"), |
| 2184 | 0× | plural(r.attestations, "attestation"), |
| 2185 | | ); |
| 2186 | 0× | for p in &r.problems { |
| 2187 | 0× | let obj = p |
| 2188 | 0× | .object |
| 2189 | 0× | .map(|o| format!("{} ", ui::hash(format!("#{}", o.short(12))))) |
| 2190 | 0× | .unwrap_or_default(); |
| 2191 | 0× | match p.severity { |
| 2192 | 0× | Severity::Error => println!("{} {obj}{}", ui::err(ui::glyph("✖", "x")), p.what), |
| 2193 | 0× | Severity::Warning => println!("{} {obj}{}", ui::caution(), p.what), |
| 2194 | | } |
| 2195 | | } |
| 2196 | 0× | if r.problems.is_empty() { |
| 2197 | 0× | println!("{}", ui::done("No problems found.")); |
| 2198 | 0× | } else if code == 0 { |
| 2199 | 0× | println!( |
| 2200 | 0× | "{}", |
| 2201 | 0× | ui::done(format!("Sound, with {}.", plural(r.warnings(), "warning"))) |
| 2202 | 0× | ); |
| 2203 | 0× | } else { |
| 2204 | 0× | println!( |
| 2205 | 0× | "{} {} and {}. Fetch missing objects from a remote with `oxy pull`, or clone afresh and compare.", |
| 2206 | 0× | ui::err(ui::glyph("✖", "x")), |
| 2207 | 0× | plural(r.errors(), "error"), |
| 2208 | 0× | plural(r.warnings(), "warning") |
| 2209 | 0× | ); |
| 2210 | 0× | } |
| 2211 | 0× | Ok(code) |
| 2212 | 0× | } |
| 2213 | |
|
| 2214 | |
|
| 2215 | |
|
| 2216 | |
|
| 2217 | 0× | fn current_head(repo: &Repo) -> Result<oxy_core::hash::ChangeHash> { |
| 2218 | 0× | let snap = repo.snapshot_status("oxy")?; |
| 2219 | 0× | let base = snap.base.maximal(&snap.wc.deps); |
| 2220 | 0× | match base.as_slice() { |
| 2221 | 0× | [] => anyhow::bail!("the working copy sits on no change yet"), |
| 2222 | 0× | [h] => Ok(*h), |
| 2223 | 0× | many => { |
| 2224 | 0× | let latest = many |
| 2225 | 0× | .iter() |
| 2226 | 0× | .max_by_key(|h| snap.base.get(h).map(|c| (c.hlc, **h))) |
| 2227 | 0× | .copied() |
| 2228 | 0× | .unwrap(); |
| 2229 | 0× | let c = repo.store().get_change(&latest)?; |
| 2230 | 0× | println!( |
| 2231 | | "Defaulting to {} #{} (most recent of the {} heads under the working copy)", |
| 2232 | 0× | short_id(&c), |
| 2233 | 0× | latest.short(12), |
| 2234 | 0× | many.len() |
| 2235 | | ); |
| 2236 | 0× | Ok(latest) |
| 2237 | | } |
| 2238 | | } |
| 2239 | 0× | } |
| 2240 | |
|
| 2241 | |
|
| 2242 | 4× | pub fn describe_name(as_json: bool) -> Result<i32> { |
| 2243 | 4× | let repo = open()?; |
| 2244 | 4× | let d = repo.describe_working_copy()?; |
| 2245 | 4× | if as_json { |
| 2246 | 3× | println!( |
| 2247 | | "{}", |
| 2248 | 3× | json::obj([ |
| 2249 | 3× | ("describe", json::s(&d.line())), |
| 2250 | 3× | ("tag", json::opt(d.tag.as_ref(), |t| json::s(t))), |
| 2251 | 3× | ("distance", json::n(d.distance)), |
| 2252 | 3× | ("bookmark", json::s(&d.bookmark)), |
| 2253 | 3× | ("change", json::s(&d.change.to_hex())), |
| 2254 | 3× | ("hash", json::s(&d.hash.to_hex())), |
| 2255 | 3× | ("dirty", json::b(d.dirty)), |
| 2256 | | ]) |
| 2257 | | ); |
| 2258 | 1× | } else { |
| 2259 | 1× | println!("{}", d.line()); |
| 2260 | 1× | } |
| 2261 | 4× | Ok(0) |
| 2262 | 4× | } |
| 2263 | |
|
| 2264 | 0× | pub fn describe(message: &str, change: Option<&str>) -> Result<i32> { |
| 2265 | 0× | let repo = open()?; |
| 2266 | 0× | let h = match change { |
| 2267 | 0× | Some(c) => repo.resolve_change(c)?, |
| 2268 | 0× | None => current_head(&repo)?, |
| 2269 | | }; |
| 2270 | 0× | let new = repo.describe(&h, message)?; |
| 2271 | 0× | let c = repo.store().get_change_meta(&new)?; |
| 2272 | 0× | println!( |
| 2273 | | "{} {} {} {}", |
| 2274 | 0× | ui::done("Described"), |
| 2275 | 0× | ui::id(c.change_id.short(8)), |
| 2276 | 0× | ui::hash(format!("#{}", new.short(12))), |
| 2277 | 0× | ui::subject(subject_of(message)) |
| 2278 | | ); |
| 2279 | 0× | Ok(0) |
| 2280 | 0× | } |
| 2281 | |
|
| 2282 | 0× | pub fn amend(message: Option<&str>, into: Option<&str>, paths: &[String]) -> Result<i32> { |
| 2283 | 0× | let repo = open()?; |
| 2284 | 0× | let before = repo.view()?; |
| 2285 | 0× | let new = match into { |
| 2286 | 0× | Some(spec) => { |
| 2287 | 0× | let target = repo.resolve_change(spec)?; |
| 2288 | 0× | repo.amend_into(&target, message)? |
| 2289 | | } |
| 2290 | 0× | None if !paths.is_empty() => repo.amend_paths(message, paths)?, |
| 2291 | 0× | None => repo.amend(message)?, |
| 2292 | | }; |
| 2293 | 0× | let c = repo.store().get_change_meta(&new)?; |
| 2294 | 0× | println!( |
| 2295 | | "{} {} {} {}", |
| 2296 | 0× | ui::done("Amended"), |
| 2297 | 0× | ui::id(c.change_id.short(8)), |
| 2298 | 0× | ui::hash(format!("#{}", new.short(12))), |
| 2299 | 0× | ui::subject(subject_of(&c.message)) |
| 2300 | | ); |
| 2301 | | |
| 2302 | | |
| 2303 | 0× | let bookmark = repo.config.bookmark.clone(); |
| 2304 | 0× | let (had, has) = ( |
| 2305 | 0× | repo.heads_of(&before, &bookmark)?, |
| 2306 | 0× | repo.heads_of(&repo.view()?, &bookmark)?, |
| 2307 | | ); |
| 2308 | 0× | for (remote, bookmarks) in &before.remotes { |
| 2309 | 0× | let Some(st) = bookmarks.get(&bookmark) else { |
| 2310 | 0× | continue; |
| 2311 | | }; |
| 2312 | 0× | let theirs = repo.store().get_state(st)?.heads; |
| 2313 | 0× | if theirs.iter().any(|h| had.contains(h) && !has.contains(h)) { |
| 2314 | 0× | println!( |
| 2315 | 0× | " {} {}", |
| 2316 | 0× | ui::caution(), |
| 2317 | 0× | ui::warn(format!( |
| 2318 | 0× | "{remote}/{bookmark} has the change as it was: the next push of {bookmark} needs --force to replace it" |
| 2319 | 0× | )) |
| 2320 | 0× | ); |
| 2321 | 0× | } |
| 2322 | | } |
| 2323 | 0× | where_now(&repo); |
| 2324 | 0× | Ok(0) |
| 2325 | 0× | } |
| 2326 | |
|
| 2327 | 0× | pub fn abandon(change: Option<&str>, revset: Option<&str>, yes: bool) -> Result<i32> { |
| 2328 | 0× | let repo = open()?; |
| 2329 | 0× | let Some(r) = revset else { |
| 2330 | 0× | let h = repo.resolve_change(change.unwrap_or_default())?; |
| 2331 | 0× | abandon_one(&repo, h)?; |
| 2332 | 0× | where_now(&repo); |
| 2333 | 0× | return Ok(0); |
| 2334 | | }; |
| 2335 | | |
| 2336 | 0× | let selected = repo.revset(r)?; |
| 2337 | 0× | let wc = repo.working_copy().ok().map(|(h, _)| h); |
| 2338 | 0× | let hashes: Vec<ChangeHash> = selected |
| 2339 | 0× | .hashes |
| 2340 | 0× | .iter() |
| 2341 | 0× | .copied() |
| 2342 | 0× | .filter(|h| Some(*h) != wc) |
| 2343 | 0× | .collect(); |
| 2344 | 0× | if hashes.is_empty() { |
| 2345 | 0× | anyhow::bail!("{r:?} selects no change to abandon"); |
| 2346 | 0× | } |
| 2347 | 0× | println!( |
| 2348 | | "{} {} selects {}:", |
| 2349 | 0× | ui::section("abandon"), |
| 2350 | 0× | ui::id(r), |
| 2351 | 0× | plural(hashes.len(), "change") |
| 2352 | | ); |
| 2353 | 0× | for h in &hashes { |
| 2354 | 0× | if let Some(m) = selected.metas.get(h) { |
| 2355 | 0× | println!( |
| 2356 | 0× | " {} {} {}", |
| 2357 | 0× | ui::id(m.change_id.short(8)), |
| 2358 | 0× | ui::hash(format!("#{}", h.short(12))), |
| 2359 | 0× | subject_of(&m.message) |
| 2360 | 0× | ); |
| 2361 | 0× | } |
| 2362 | | } |
| 2363 | | const ASK_ABOVE: usize = 5; |
| 2364 | 0× | if hashes.len() > ASK_ABOVE && !yes { |
| 2365 | 0× | anyhow::bail!("that is more than {ASK_ABOVE} changes; pass --yes to abandon them all"); |
| 2366 | 0× | } |
| 2367 | | |
| 2368 | | |
| 2369 | 0× | let mut done = 0; |
| 2370 | 0× | for h in &hashes { |
| 2371 | 0× | let Some(m) = selected.metas.get(h) else { |
| 2372 | 0× | continue; |
| 2373 | | }; |
| 2374 | 0× | let now = match repo.resolve_change(&m.change_id.to_hex()) { |
| 2375 | 0× | Ok(h) => h, |
| 2376 | 0× | Err(_) => continue, |
| 2377 | | }; |
| 2378 | 0× | abandon_one(&repo, now)?; |
| 2379 | 0× | done += 1; |
| 2380 | | } |
| 2381 | 0× | println!("{} {}", ui::done("Abandoned"), plural(done, "change")); |
| 2382 | 0× | where_now(&repo); |
| 2383 | 0× | Ok(0) |
| 2384 | 0× | } |
| 2385 | |
|
| 2386 | 0× | fn abandon_one(repo: &Repo, h: ChangeHash) -> Result<()> { |
| 2387 | 0× | let c = repo.store().get_change_meta(&h)?; |
| 2388 | 0× | let notes = repo.abandon(&h)?; |
| 2389 | 0× | println!( |
| 2390 | | "{} {} {} {}", |
| 2391 | 0× | ui::done("Abandoned"), |
| 2392 | 0× | ui::id(c.change_id.short(8)), |
| 2393 | 0× | ui::hash(format!("#{}", h.short(12))), |
| 2394 | 0× | ui::dim("its content remains on disk as uncommitted work if the working copy sat on it") |
| 2395 | | ); |
| 2396 | 0× | for n in notes { |
| 2397 | 0× | println!(" {} {n}", ui::note()); |
| 2398 | 0× | } |
| 2399 | 0× | Ok(()) |
| 2400 | 0× | } |
| 2401 | |
|
| 2402 | 0× | pub fn checkpoint() -> Result<i32> { |
| 2403 | 0× | let repo = open()?; |
| 2404 | 0× | let sp = ui::spinner("Writing checkpoint…"); |
| 2405 | 0× | let (r, changes, bytes) = repo.checkpoint()?; |
| 2406 | 0× | sp.done(&format!( |
| 2407 | 0× | "{} {} of {} ({} bytes) for {}", |
| 2408 | 0× | ui::done("Checkpoint"), |
| 2409 | 0× | ui::hash(format!("#{}", r.short(12))), |
| 2410 | 0× | plural(changes, "change"), |
| 2411 | 0× | bytes, |
| 2412 | 0× | ui::id(&repo.config.bookmark) |
| 2413 | 0× | )); |
| 2414 | 0× | Ok(0) |
| 2415 | 0× | } |
| 2416 | |
|
| 2417 | |
|
| 2418 | 0× | pub fn prompt(format: &str, glyphs: bool, color: Option<&str>) -> Result<i32> { |
| 2419 | 0× | let Ok(repo) = Repo::open(&std::env::current_dir()?) else { |
| 2420 | 0× | return Ok(1); |
| 2421 | | }; |
| 2422 | 0× | let info = repo.quick_status()?; |
| 2423 | 0× | let fmt = if glyphs && format == oxy_core::repo::DEFAULT_PROMPT_FORMAT { |
| 2424 | 0× | oxy_core::repo::GLYPH_PROMPT_FORMAT |
| 2425 | | } else { |
| 2426 | 0× | format |
| 2427 | | }; |
| 2428 | 0× | let line = match color { |
| 2429 | 0× | Some("bash") => info.format_colored(fmt, ("\\[", "\\]")), |
| 2430 | 0× | Some("zsh") => info.format_colored(fmt, ("%{", "%}")), |
| 2431 | 0× | Some(_) => info.format_colored(fmt, ("", "")), |
| 2432 | 0× | None => info.format(fmt), |
| 2433 | | }; |
| 2434 | 0× | println!("{line}"); |
| 2435 | 0× | Ok(0) |
| 2436 | 0× | } |
| 2437 | |
|
| 2438 | 0× | pub fn sparse(paths: &[String]) -> Result<i32> { |
| 2439 | 0× | let mut repo = open()?; |
| 2440 | 0× | if paths.is_empty() { |
| 2441 | 0× | if repo.config.sparse_paths.is_empty() { |
| 2442 | 0× | println!("No sparse filter: the whole tree is materialized."); |
| 2443 | 0× | } else { |
| 2444 | 0× | println!("{}", ui::heading("Sparse filter")); |
| 2445 | 0× | for p in &repo.config.sparse_paths { |
| 2446 | 0× | println!(" {}", ui::path(p)); |
| 2447 | 0× | } |
| 2448 | | } |
| 2449 | 0× | return Ok(0); |
| 2450 | 0× | } |
| 2451 | 0× | let snap = repo.snapshot("oxy sparse")?; |
| 2452 | 0× | if !snap.wc.tree_ops.is_empty() || !snap.wc.edits.is_empty() { |
| 2453 | 0× | anyhow::bail!( |
| 2454 | | "the working copy has changes; commit them before changing the sparse filter" |
| 2455 | | ); |
| 2456 | 0× | } |
| 2457 | 0× | repo.config.set("filter.paths", &paths.join(","))?; |
| 2458 | 0× | repo.save_config()?; |
| 2459 | 0× | let heads = repo.bookmark_heads()?; |
| 2460 | 0× | let sp = ui::spinner("Re-checking out with the new filter…"); |
| 2461 | 0× | let r = repo.checkout(&heads, true, "oxy sparse (checkout)")?; |
| 2462 | 0× | sp.done(&format!( |
| 2463 | 0× | "{} {}: {} written, {} removed", |
| 2464 | 0× | ui::done("Sparse filter set to"), |
| 2465 | 0× | ui::path(paths.join(",")), |
| 2466 | 0× | plural(r.written, "file"), |
| 2467 | 0× | r.removed |
| 2468 | 0× | )); |
| 2469 | 0× | Ok(0) |
| 2470 | 0× | } |
| 2471 | |
|
| 2472 | 2× | pub fn tag_create(name: &str, changes: &[String], message: &str) -> Result<i32> { |
| 2473 | 2× | let repo = open()?; |
| 2474 | 2× | repo.snapshot_status("oxy tag")?; |
| 2475 | 2× | let heads = if changes.is_empty() { |
| 2476 | 2× | repo.bookmark_heads()? |
| 2477 | | } else { |
| 2478 | 0× | let mut heads = Vec::new(); |
| 2479 | 0× | for c in changes { |
| 2480 | 0× | heads.extend(repo.resolve_heads(c)?); |
| 2481 | | } |
| 2482 | 0× | heads |
| 2483 | | }; |
| 2484 | 2× | let t = repo.tag_create(name, &heads, message)?; |
| 2485 | 2× | println!( |
| 2486 | | "{} {} {} {} ({}){}", |
| 2487 | 2× | ui::done("Tagged"), |
| 2488 | 2× | ui::bookmark(&t.name), |
| 2489 | 2× | ui::dim(ui::glyph("→", "->")), |
| 2490 | 2× | ui::hash(format!("#{}", t.state.short(12))), |
| 2491 | 2× | plural(t.heads.len(), "head"), |
| 2492 | 2× | t.signer |
| 2493 | 2× | .map(|s| format!(", signed by {s}")) |
| 2494 | 2× | .unwrap_or_else(|| ui::dim(", unsigned")) |
| 2495 | | ); |
| 2496 | 2× | Ok(0) |
| 2497 | 2× | } |
| 2498 | |
|
| 2499 | 0× | pub fn tag_delete(name: &str, reason: &str) -> Result<i32> { |
| 2500 | 0× | let repo = open()?; |
| 2501 | 0× | let t = repo.tag_delete(name, reason)?; |
| 2502 | 0× | println!( |
| 2503 | | "{} {} {} {}{}", |
| 2504 | 0× | ui::done("Withdrew"), |
| 2505 | 0× | ui::bookmark(&t.name), |
| 2506 | 0× | ui::dim(ui::glyph("→", "->")), |
| 2507 | 0× | ui::hash(format!("#{}", t.state.short(12))), |
| 2508 | 0× | t.signer |
| 2509 | 0× | .map(|s| format!(", signed by {s}")) |
| 2510 | 0× | .unwrap_or_else(|| ui::dim(", unsigned")) |
| 2511 | | ); |
| 2512 | 0× | println!( |
| 2513 | | " {} {}", |
| 2514 | 0× | ui::note(), |
| 2515 | 0× | ui::dim("`oxy push` sends the withdrawal to a remote that has the tag; the name is not used again") |
| 2516 | | ); |
| 2517 | 0× | Ok(0) |
| 2518 | 0× | } |
| 2519 | |
|
| 2520 | 1× | pub fn tag_list(as_json: bool) -> Result<i32> { |
| 2521 | 1× | let repo = open()?; |
| 2522 | 1× | let tags = repo.tags()?; |
| 2523 | 1× | if as_json { |
| 2524 | 1× | println!( |
| 2525 | | "{}", |
| 2526 | 1× | json::arr(tags.iter().map(|t| { |
| 2527 | 1× | json::obj([ |
| 2528 | 1× | ("name", json::s(&t.name)), |
| 2529 | 1× | ("state", json::s(&t.state.to_hex())), |
| 2530 | 1× | ("heads", json::hashes(&t.heads)), |
| 2531 | 1× | ("message", json::s(&t.message)), |
| 2532 | | ( |
| 2533 | 1× | "signer", |
| 2534 | 1× | json::opt(t.signer.as_ref(), |s| json::s(&s.to_string())), |
| 2535 | | ), |
| 2536 | | ]) |
| 2537 | 1× | })) |
| 2538 | | ); |
| 2539 | 1× | return Ok(0); |
| 2540 | 0× | } |
| 2541 | 0× | if tags.is_empty() { |
| 2542 | 0× | println!("{}", ui::dim("No tags.")); |
| 2543 | 0× | } |
| 2544 | 0× | for t in tags { |
| 2545 | 0× | println!( |
| 2546 | | "{} {} {}{}{}{}", |
| 2547 | 0× | ui::ok(ui::glyph("⚑", "#")), |
| 2548 | 0× | ui::bookmark(&t.name), |
| 2549 | | |
| 2550 | 0× | ui::hash( |
| 2551 | 0× | t.heads |
| 2552 | 0× | .iter() |
| 2553 | 0× | .map(|h| format!("#{}", h.short(12))) |
| 2554 | 0× | .collect::<Vec<_>>() |
| 2555 | 0× | .join(" ") |
| 2556 | | ), |
| 2557 | 0× | ui::sep() + &ui::dim(plural(t.heads.len(), "head")), |
| 2558 | 0× | if t.message.is_empty() { |
| 2559 | 0× | String::new() |
| 2560 | | } else { |
| 2561 | 0× | format!(" {}", ui::subject(subject_of(&t.message))) |
| 2562 | | }, |
| 2563 | 0× | t.signer |
| 2564 | 0× | .map(|s| ui::sep() + &ui::ok(format!("{} {s}", ui::glyph("✔", "v")))) |
| 2565 | 0× | .unwrap_or_default() |
| 2566 | | ); |
| 2567 | | } |
| 2568 | 0× | Ok(0) |
| 2569 | 1× | } |
| 2570 | |
|
| 2571 | |
|
| 2572 | |
|
| 2573 | 0× | fn names_bookmark(repo: &oxy_core::repo::Repo, name: &str) -> Result<bool> { |
| 2574 | 0× | Ok(repo.view()?.bookmarks.contains_key(name) |
| 2575 | 0× | || repo |
| 2576 | 0× | .remote_only_bookmarks()? |
| 2577 | 0× | .iter() |
| 2578 | 0× | .any(|b| b.name == name || format!("{}/{}", b.remote, b.name) == name)) |
| 2579 | 0× | } |
| 2580 | |
|
| 2581 | 0× | pub fn workspace_add(path: &std::path::Path, changes: &[String]) -> Result<i32> { |
| 2582 | 0× | let repo = open()?; |
| 2583 | | |
| 2584 | | |
| 2585 | 0× | if let [name] = changes { |
| 2586 | 0× | if names_bookmark(&repo, name)? { |
| 2587 | 0× | let (local, _) = repo.local_bookmark_for(name)?; |
| 2588 | 0× | let heads = repo.resolve_heads(&local)?; |
| 2589 | 0× | let sp = ui::spinner(&format!("Creating workspace at {}…", path.display())); |
| 2590 | 0× | let ws = repo.workspace_add_on(path, Some(&heads), Some(&local))?; |
| 2591 | 0× | sp.done(&format!( |
| 2592 | 0× | "{} {} (workspace {}) on {}", |
| 2593 | 0× | ui::done("Workspace created at"), |
| 2594 | 0× | ui::path(ws.root().display()), |
| 2595 | 0× | ui::id(ws.workspace().short(8)), |
| 2596 | 0× | ui::bookmark(&local) |
| 2597 | 0× | )); |
| 2598 | 0× | return Ok(0); |
| 2599 | 0× | } |
| 2600 | 0× | } |
| 2601 | 0× | let heads = if changes.is_empty() { |
| 2602 | 0× | None |
| 2603 | | } else { |
| 2604 | 0× | let mut heads = Vec::new(); |
| 2605 | 0× | for c in changes { |
| 2606 | 0× | heads.extend(repo.resolve_heads(c)?); |
| 2607 | | } |
| 2608 | 0× | Some(heads) |
| 2609 | | }; |
| 2610 | 0× | let sp = ui::spinner(&format!("Creating workspace at {}…", path.display())); |
| 2611 | 0× | let ws = repo.workspace_add(path, heads.as_deref())?; |
| 2612 | 0× | sp.done(&format!( |
| 2613 | 0× | "{} {} (workspace {})", |
| 2614 | 0× | ui::done("Workspace created at"), |
| 2615 | 0× | ui::path(ws.root().display()), |
| 2616 | 0× | ui::id(ws.workspace().short(8)) |
| 2617 | 0× | )); |
| 2618 | 0× | Ok(0) |
| 2619 | 0× | } |
| 2620 | |
|
| 2621 | 1× | pub fn workspace_list(as_json: bool) -> Result<i32> { |
| 2622 | 1× | let repo = open()?; |
| 2623 | 1× | if as_json { |
| 2624 | 1× | println!( |
| 2625 | | "{}", |
| 2626 | 1× | json::arr(repo.workspaces()?.iter().map(|w| { |
| 2627 | 1× | json::obj([ |
| 2628 | 1× | ("id", json::s(&w.id.to_hex())), |
| 2629 | 1× | ("path", json::s(&w.path.to_string_lossy())), |
| 2630 | 1× | ("current", json::b(w.current)), |
| 2631 | | ( |
| 2632 | 1× | "working_copy", |
| 2633 | 1× | json::opt(w.working_copy, |h| json::s(&h.to_hex())), |
| 2634 | | ), |
| 2635 | | ]) |
| 2636 | 1× | })) |
| 2637 | | ); |
| 2638 | 1× | return Ok(0); |
| 2639 | 0× | } |
| 2640 | 0× | for w in repo.workspaces()? { |
| 2641 | 0× | println!( |
| 2642 | | "{} {} {}{}", |
| 2643 | 0× | if w.current { |
| 2644 | 0× | ui::ok(ui::glyph("●", "*")) |
| 2645 | | } else { |
| 2646 | 0× | ui::dim(ui::glyph("○", "o")) |
| 2647 | | }, |
| 2648 | 0× | ui::id(w.id.short(8)), |
| 2649 | 0× | ui::path(w.path.display()), |
| 2650 | 0× | w.working_copy |
| 2651 | 0× | .map(|h| ui::sep() + &ui::dim(format!("working copy #{}", h.short(12)))) |
| 2652 | 0× | .unwrap_or_else(|| ui::sep() + &ui::warn("not in view")) |
| 2653 | | ); |
| 2654 | | } |
| 2655 | 0× | Ok(0) |
| 2656 | 1× | } |
| 2657 | |
|
| 2658 | 0× | pub fn workspace_forget(spec: &str) -> Result<i32> { |
| 2659 | 0× | let repo = open()?; |
| 2660 | 0× | let all = repo.workspaces()?; |
| 2661 | 0× | let target = std::fs::canonicalize(spec).ok(); |
| 2662 | 0× | let matches: Vec<_> = all |
| 2663 | 0× | .iter() |
| 2664 | 0× | .filter(|w| w.id.starts_with_hex(spec) || target.as_ref().is_some_and(|t| *t == w.path)) |
| 2665 | 0× | .collect(); |
| 2666 | 0× | let w = match matches.as_slice() { |
| 2667 | 0× | [w] => *w, |
| 2668 | 0× | [] => anyhow::bail!("no workspace matches {spec:?}"), |
| 2669 | 0× | _ => anyhow::bail!("{spec:?} is ambiguous"), |
| 2670 | | }; |
| 2671 | 0× | repo.workspace_forget(&w.id)?; |
| 2672 | 0× | println!( |
| 2673 | | "{} workspace {} at {}; its files were left in place.", |
| 2674 | 0× | ui::done("Forgot"), |
| 2675 | 0× | ui::id(w.id.short(8)), |
| 2676 | 0× | ui::path(w.path.display()) |
| 2677 | | ); |
| 2678 | 0× | Ok(0) |
| 2679 | 0× | } |
| 2680 | |
|
| 2681 | 0× | pub fn lock(path: &str) -> Result<i32> { |
| 2682 | 0× | let repo = open()?; |
| 2683 | 0× | repo.lock_path(path)?; |
| 2684 | 0× | println!( |
| 2685 | | "{} {} {}", |
| 2686 | 0× | ui::done("Locked"), |
| 2687 | 0× | ui::path(path), |
| 2688 | 0× | ui::dim("(advisory, local)") |
| 2689 | | ); |
| 2690 | 0× | Ok(0) |
| 2691 | 0× | } |
| 2692 | |
|
| 2693 | 0× | pub fn unlock(path: &str) -> Result<i32> { |
| 2694 | 0× | let repo = open()?; |
| 2695 | 0× | repo.unlock_path(path)?; |
| 2696 | 0× | println!("{} {}", ui::done("Unlocked"), ui::path(path)); |
| 2697 | 0× | Ok(0) |
| 2698 | 0× | } |
| 2699 | |
|
| 2700 | 0× | pub fn split(change: &str, paths: &[String]) -> Result<i32> { |
| 2701 | 0× | let repo = open()?; |
| 2702 | 0× | let h = repo.resolve_change(change)?; |
| 2703 | 0× | let (h1, h2) = repo.split(&h, paths)?; |
| 2704 | 0× | let c1 = repo.store().get_change_meta(&h1)?; |
| 2705 | 0× | let c2 = repo.store().get_change_meta(&h2)?; |
| 2706 | 0× | println!( |
| 2707 | | "{} into {} {} and {} {}", |
| 2708 | 0× | ui::done("Split"), |
| 2709 | 0× | ui::id(c1.change_id.short(8)), |
| 2710 | 0× | ui::hash(format!("#{}", h1.short(12))), |
| 2711 | 0× | ui::id(c2.change_id.short(8)), |
| 2712 | 0× | ui::hash(format!("#{}", h2.short(12))) |
| 2713 | | ); |
| 2714 | 0× | Ok(0) |
| 2715 | 0× | } |
| 2716 | |
|
| 2717 | 6× | fn heads_from(repo: &Repo, changes: &[String]) -> Result<Vec<oxy_core::hash::ChangeHash>> { |
| 2718 | 6× | let mut heads = Vec::new(); |
| 2719 | 6× | for c in changes { |
| 2720 | 6× | heads.extend(repo.resolve_heads(c)?); |
| 2721 | | } |
| 2722 | 6× | Ok(heads) |
| 2723 | 6× | } |
| 2724 | |
|
| 2725 | 6× | pub fn bookmark_create(name: &str, changes: &[String], upto: bool, only: bool) -> Result<i32> { |
| 2726 | 6× | let repo = open()?; |
| 2727 | 6× | repo.snapshot_status("oxy bookmark create")?; |
| 2728 | 6× | let heads = if changes.is_empty() { |
| 2729 | 3× | None |
| 2730 | | } else { |
| 2731 | 3× | let named = heads_from(&repo, changes)?; |
| 2732 | 3× | let (_, wc) = repo.working_copy()?; |
| 2733 | 3× | let gap = repo.state_gap(&wc.deps, &named)?; |
| 2734 | 3× | if upto { |
| 2735 | 1× | Some(gap.upto) |
| 2736 | 2× | } else if only || gap.earlier.is_empty() { |
| 2737 | 1× | Some(named) |
| 2738 | | } else { |
| 2739 | 1× | anyhow::bail!( |
| 2740 | | "{name} at {} would lack {} of the current state recorded before it:\n{}\n\ |
| 2741 | | A state is its heads and what they depend on, not everything recorded before them.\n\ |
| 2742 | | Pass --upto for everything of the current state up to {0} (as a Git branch would hold),\n\ |
| 2743 | | or --only for just {0} and what it depends on.", |
| 2744 | 1× | changes.join(" "), |
| 2745 | 1× | plural(gap.earlier.len(), "change"), |
| 2746 | 1× | listed_changes(&repo, &gap.earlier) |
| 2747 | | ); |
| 2748 | | } |
| 2749 | | }; |
| 2750 | 5× | let heads = repo.bookmark_create(name, heads.as_deref())?; |
| 2751 | 5× | println!( |
| 2752 | | "{} bookmark {} {} {}", |
| 2753 | 5× | ui::done("Created"), |
| 2754 | 5× | ui::bookmark(name), |
| 2755 | 5× | ui::dim(ui::glyph("→", "->")), |
| 2756 | 5× | ui::dim(plural(heads.len(), "head")) |
| 2757 | | ); |
| 2758 | 5× | println!( |
| 2759 | | " {} {}", |
| 2760 | 5× | ui::note(), |
| 2761 | 5× | ui::dim(format!("switch to it with oxy bookmark switch {name}")) |
| 2762 | | ); |
| 2763 | 5× | Ok(0) |
| 2764 | 6× | } |
| 2765 | |
|
| 2766 | |
|
| 2767 | 2× | fn listed_changes(repo: &Repo, changes: &[oxy_core::hash::ChangeHash]) -> String { |
| 2768 | 2× | let shown = changes.len().min(5); |
| 2769 | 2× | let mut lines: Vec<String> = changes[changes.len() - shown..] |
| 2770 | 2× | .iter() |
| 2771 | 2× | .map(|h| { |
| 2772 | 2× | let subject = repo |
| 2773 | 2× | .store() |
| 2774 | 2× | .get_change(h) |
| 2775 | 2× | .map(|c| subject_of(&c.message).to_string()) |
| 2776 | 2× | .unwrap_or_default(); |
| 2777 | 2× | format!(" #{} {subject}", h.short(12)) |
| 2778 | 2× | }) |
| 2779 | 2× | .collect(); |
| 2780 | 2× | if changes.len() > shown { |
| 2781 | 0× | lines.insert(0, format!(" … and {} earlier", changes.len() - shown)); |
| 2782 | 2× | } |
| 2783 | 2× | lines.join("\n") |
| 2784 | 2× | } |
| 2785 | |
|
| 2786 | 3× | pub fn bookmark_set(name: &str, changes: &[String], upto: bool, only: bool) -> Result<i32> { |
| 2787 | 3× | let repo = open()?; |
| 2788 | 3× | repo.snapshot_status("oxy bookmark set")?; |
| 2789 | 3× | let named = heads_from(&repo, changes)?; |
| 2790 | | |
| 2791 | 3× | let current = repo |
| 2792 | 3× | .bookmarks()? |
| 2793 | 3× | .into_iter() |
| 2794 | 6× | .find(|b| b.name == name) |
| 2795 | 3× | .map(|b| b.heads) |
| 2796 | 3× | .ok_or_else(|| anyhow::anyhow!("no bookmark {name}"))?; |
| 2797 | 3× | let gap = repo.state_gap(¤t, &named)?; |
| 2798 | | |
| 2799 | | |
| 2800 | | |
| 2801 | | |
| 2802 | 3× | let newest = current |
| 2803 | 3× | .iter() |
| 2804 | 8× | .filter_map(|h| repo.store().get_change_meta(h).ok().map(|m| (m.hlc, *h))) |
| 2805 | 3× | .max() |
| 2806 | 3× | .map(|(_, h)| h); |
| 2807 | | |
| 2808 | | |
| 2809 | 3× | let ahead = repo |
| 2810 | 3× | .load_state_history(¤t) |
| 2811 | 3× | .is_ok_and(|st| named.iter().any(|h| !st.contains(h))); |
| 2812 | 3× | let forward = !only |
| 2813 | 3× | && ahead |
| 2814 | 1× | && !gap.earlier.is_empty() |
| 2815 | 1× | && newest.is_some_and(|n| { |
| 2816 | 1× | repo.load_state_history(&named) |
| 2817 | 1× | .is_ok_and(|st| st.contains(&n)) |
| 2818 | 1× | }); |
| 2819 | 3× | let heads = if upto { |
| 2820 | 1× | gap.upto |
| 2821 | 2× | } else if forward { |
| 2822 | 1× | println!( |
| 2823 | | " {} {}", |
| 2824 | 1× | ui::note(), |
| 2825 | 1× | ui::dim(format!( |
| 2826 | | "{} builds on {name}'s newest head; keeping {} it holds that {0} does not depend on", |
| 2827 | 1× | changes.join(" "), |
| 2828 | 1× | plural(gap.earlier.len(), "change") |
| 2829 | | )) |
| 2830 | | ); |
| 2831 | 1× | gap.upto |
| 2832 | 1× | } else if only || gap.earlier.is_empty() { |
| 2833 | 0× | named |
| 2834 | | } else { |
| 2835 | 1× | anyhow::bail!( |
| 2836 | | "{name} at {} would lose {} it holds now, recorded before it:\n{}\n\ |
| 2837 | | A state is its heads and what they depend on, not everything recorded before them.\n\ |
| 2838 | | Pass --upto to keep everything {name} holds up to {0} (as a Git branch would),\n\ |
| 2839 | | or --only for just {0} and what it depends on.", |
| 2840 | 1× | changes.join(" "), |
| 2841 | 1× | plural(gap.earlier.len(), "change"), |
| 2842 | 1× | listed_changes(&repo, &gap.earlier) |
| 2843 | | ); |
| 2844 | | }; |
| 2845 | 2× | let heads = repo.bookmark_set(name, &heads)?; |
| 2846 | 2× | println!( |
| 2847 | | "{} bookmark {} {} {}", |
| 2848 | 2× | ui::done("Moved"), |
| 2849 | 2× | ui::bookmark(name), |
| 2850 | 2× | ui::dim(ui::glyph("→", "->")), |
| 2851 | 2× | ui::dim(plural(heads.len(), "head")) |
| 2852 | | ); |
| 2853 | 2× | Ok(0) |
| 2854 | 3× | } |
| 2855 | |
|
| 2856 | 0× | pub fn bookmark_delete(name: &str) -> Result<i32> { |
| 2857 | 0× | let repo = open()?; |
| 2858 | 0× | repo.bookmark_delete(name)?; |
| 2859 | 0× | println!( |
| 2860 | | "{} bookmark {} {}", |
| 2861 | 0× | ui::done("Deleted"), |
| 2862 | 0× | ui::bookmark(name), |
| 2863 | 0× | ui::dim("(its changes stay until unreachable)") |
| 2864 | | ); |
| 2865 | 0× | Ok(0) |
| 2866 | 0× | } |
| 2867 | |
|
| 2868 | |
|
| 2869 | 0× | fn heads_newest_first( |
| 2870 | 0× | repo: &Repo, |
| 2871 | 0× | heads: &[oxy_core::hash::ChangeHash], |
| 2872 | 0× | ) -> Vec<(oxy_core::hash::ChangeHash, oxy_core::state::ChangeMeta)> { |
| 2873 | 0× | let mut out: Vec<_> = heads |
| 2874 | 0× | .iter() |
| 2875 | 0× | .filter_map(|h| repo.store().get_change_meta(h).ok().map(|m| (*h, m))) |
| 2876 | 0× | .collect(); |
| 2877 | 0× | out.sort_by_key(|(_, m)| std::cmp::Reverse(m.hlc)); |
| 2878 | 0× | out |
| 2879 | 0× | } |
| 2880 | |
|
| 2881 | |
|
| 2882 | 0× | fn print_head( |
| 2883 | 0× | repo: &Repo, |
| 2884 | 0× | indent: &str, |
| 2885 | 0× | h: &oxy_core::hash::ChangeHash, |
| 2886 | 0× | m: &oxy_core::state::ChangeMeta, |
| 2887 | 0× | ) { |
| 2888 | 0× | println!( |
| 2889 | | "{indent}{} {} {}{}{}{}{}", |
| 2890 | 0× | ui::id(m.change_id.short(8)), |
| 2891 | 0× | ui::dim(format!("#{}", &h.to_hex()[..8])), |
| 2892 | 0× | author_name(repo, m), |
| 2893 | 0× | ui::sep(), |
| 2894 | 0× | ui::dim(ui::ago(m.hlc.wall_ms)), |
| 2895 | 0× | ui::sep(), |
| 2896 | 0× | ui::subject(subject_of(&m.message)) |
| 2897 | | ); |
| 2898 | 0× | } |
| 2899 | |
|
| 2900 | 0× | pub fn bookmark_show(name: Option<&str>, as_json: bool) -> Result<i32> { |
| 2901 | 0× | let repo = open()?; |
| 2902 | 0× | let name = name.unwrap_or(&repo.config.bookmark).to_string(); |
| 2903 | 0× | let view = repo.view()?; |
| 2904 | 0× | let st = match view.bookmarks.get(&name) { |
| 2905 | 0× | Some(st) => *st, |
| 2906 | 0× | None => match oxy_core::repo::remote_bookmark(&view, &name) { |
| 2907 | 0× | Some((_, _, st)) => st, |
| 2908 | 0× | None => anyhow::bail!("no bookmark {name}: oxy bookmark list --all lists them"), |
| 2909 | | }, |
| 2910 | | }; |
| 2911 | 0× | let heads = repo.store().get_state(&st)?.heads; |
| 2912 | 0× | let shown = heads_newest_first(&repo, &heads); |
| 2913 | 0× | if as_json { |
| 2914 | 0× | println!( |
| 2915 | | "{}", |
| 2916 | 0× | json::obj([ |
| 2917 | 0× | ("name", json::s(&name)), |
| 2918 | 0× | ("current", json::b(name == repo.config.bookmark)), |
| 2919 | | ( |
| 2920 | 0× | "heads", |
| 2921 | 0× | json::arr(shown.iter().map(|(h, m)| { |
| 2922 | 0× | json::obj([ |
| 2923 | 0× | ("hash", json::s(&h.to_hex())), |
| 2924 | 0× | ("id", json::s(&m.change_id.to_string())), |
| 2925 | 0× | ("author", json::s(&author_name(&repo, m))), |
| 2926 | 0× | ("time_ms", json::n(m.hlc.wall_ms)), |
| 2927 | 0× | ("subject", json::s(subject_of(&m.message))), |
| 2928 | 0× | ]) |
| 2929 | 0× | })) |
| 2930 | | ), |
| 2931 | | ]) |
| 2932 | | ); |
| 2933 | 0× | return Ok(0); |
| 2934 | 0× | } |
| 2935 | 0× | println!( |
| 2936 | | "{} {}{}{}", |
| 2937 | 0× | ui::ok(ui::glyph("●", "*")), |
| 2938 | 0× | ui::bookmark(&name), |
| 2939 | 0× | ui::sep(), |
| 2940 | 0× | ui::dim(plural(heads.len(), "head")) |
| 2941 | | ); |
| 2942 | 0× | for (h, m) in &shown { |
| 2943 | 0× | print_head(&repo, " ", h, m); |
| 2944 | 0× | } |
| 2945 | 0× | if heads.len() > 1 { |
| 2946 | 0× | println!( |
| 2947 | 0× | " {}", |
| 2948 | 0× | ui::dim("None depends on another: the bookmark is all of them and their dependencies.") |
| 2949 | 0× | ); |
| 2950 | 0× | } |
| 2951 | 0× | Ok(0) |
| 2952 | 0× | } |
| 2953 | |
|
| 2954 | 5× | pub fn bookmark_list(all: bool, verbose: bool, as_json: bool) -> Result<i32> { |
| 2955 | 5× | let repo = open()?; |
| 2956 | 5× | let remote_only = repo.remote_only_bookmarks()?; |
| 2957 | 5× | if as_json { |
| 2958 | 5× | let mut items: Vec<String> = repo |
| 2959 | 5× | .bookmarks()? |
| 2960 | 5× | .into_iter() |
| 2961 | 13× | .map(|b| { |
| 2962 | 13× | json::obj([ |
| 2963 | 13× | ("name", json::s(&b.name)), |
| 2964 | 13× | ("heads", json::hashes(&b.heads)), |
| 2965 | 13× | ("current", json::b(b.current)), |
| 2966 | 13× | ("ahead", json::opt(b.ahead, json::n)), |
| 2967 | 13× | ("behind", json::opt(b.behind, json::n)), |
| 2968 | 13× | ("remote", json::opt(b.remote.as_deref(), json::s)), |
| 2969 | 13× | ]) |
| 2970 | 13× | }) |
| 2971 | 5× | .collect(); |
| 2972 | 5× | if all { |
| 2973 | 0× | items.extend(remote_only.iter().map(|b| { |
| 2974 | 0× | json::obj([ |
| 2975 | 0× | ("name", json::s(&format!("{}/{}", b.remote, b.name))), |
| 2976 | 0× | ("heads", json::hashes(&b.heads)), |
| 2977 | 0× | ("current", json::b(false)), |
| 2978 | 0× | ("ahead", "null".into()), |
| 2979 | 0× | ("behind", "null".into()), |
| 2980 | 0× | ("remote", json::s(&b.remote)), |
| 2981 | 0× | ]) |
| 2982 | 0× | })); |
| 2983 | 5× | } |
| 2984 | 5× | println!("{}", json::arr(items)); |
| 2985 | 5× | return Ok(0); |
| 2986 | 0× | } |
| 2987 | 0× | for b in repo.bookmarks()? { |
| 2988 | 0× | let mark = if b.current { |
| 2989 | 0× | ui::ok(ui::glyph("●", "*")) |
| 2990 | | } else { |
| 2991 | 0× | ui::dim(ui::glyph("○", "o")) |
| 2992 | | }; |
| 2993 | 0× | let mut tail = String::new(); |
| 2994 | 0× | if let Some(n) = b.ahead.filter(|n| *n > 0) { |
| 2995 | 0× | tail += &ui::ok(format!(" {}{n}", ui::glyph("↑", "+"))); |
| 2996 | 0× | } |
| 2997 | 0× | if let Some(n) = b.behind.filter(|n| *n > 0) { |
| 2998 | 0× | tail += &ui::warn(format!(" {}{n}", ui::glyph("↓", "-"))); |
| 2999 | 0× | } |
| 3000 | | |
| 3001 | 0× | if !tail.is_empty() && repo.config.remotes.len() > 1 { |
| 3002 | 0× | if let Some(r) = &b.remote { |
| 3003 | 0× | tail += &ui::dim(format!(" {r}")); |
| 3004 | 0× | } |
| 3005 | 0× | } |
| 3006 | 0× | let shown = heads_newest_first(&repo, &b.heads); |
| 3007 | | |
| 3008 | 0× | let head = match shown.first() { |
| 3009 | 0× | Some(_) if verbose => String::new(), |
| 3010 | 0× | Some((_, m)) if b.heads.len() > 1 => format!( |
| 3011 | | "{} {}", |
| 3012 | 0× | ui::dim("newest"), |
| 3013 | 0× | ui::subject(subject_of(&m.message)) |
| 3014 | | ), |
| 3015 | 0× | Some((_, m)) => ui::subject(subject_of(&m.message)), |
| 3016 | 0× | None => ui::dim("(empty)"), |
| 3017 | | }; |
| 3018 | 0× | let head = if head.is_empty() { |
| 3019 | 0× | head |
| 3020 | | } else { |
| 3021 | 0× | format!(" {head}") |
| 3022 | | }; |
| 3023 | 0× | println!( |
| 3024 | | "{mark} {}{}{}{tail}{head}", |
| 3025 | 0× | ui::bookmark(&b.name), |
| 3026 | 0× | ui::sep(), |
| 3027 | 0× | ui::dim(plural(b.heads.len(), "head")) |
| 3028 | | ); |
| 3029 | 0× | if verbose { |
| 3030 | 0× | for (h, m) in &shown { |
| 3031 | 0× | print_head(&repo, " ", h, m); |
| 3032 | 0× | } |
| 3033 | 0× | } |
| 3034 | | } |
| 3035 | 0× | if remote_only.is_empty() { |
| 3036 | 0× | return Ok(0); |
| 3037 | 0× | } |
| 3038 | 0× | if !all { |
| 3039 | 0× | println!( |
| 3040 | | "{}", |
| 3041 | 0× | ui::dim(format!( |
| 3042 | | " and {} only on a remote: oxy bookmark list --all", |
| 3043 | 0× | plural(remote_only.len(), "bookmark") |
| 3044 | | )) |
| 3045 | | ); |
| 3046 | 0× | return Ok(0); |
| 3047 | 0× | } |
| 3048 | 0× | for b in &remote_only { |
| 3049 | 0× | println!( |
| 3050 | 0× | "{} {}{}{}", |
| 3051 | 0× | ui::dim(ui::glyph("◌", ".")), |
| 3052 | 0× | ui::dim(format!("{}/{}", b.remote, b.name)), |
| 3053 | 0× | ui::sep(), |
| 3054 | 0× | ui::dim(plural(b.heads.len(), "head")) |
| 3055 | 0× | ); |
| 3056 | 0× | } |
| 3057 | 0× | println!( |
| 3058 | | " {} {}", |
| 3059 | 0× | ui::note(), |
| 3060 | 0× | ui::dim("oxy bookmark switch NAME makes a local bookmark from a remote's") |
| 3061 | | ); |
| 3062 | 0× | Ok(0) |
| 3063 | 5× | } |
| 3064 | |
|
| 3065 | 3× | pub fn bookmark_switch(name: &str, discard: bool, carry: bool) -> Result<i32> { |
| 3066 | 3× | let mut repo = open()?; |
| 3067 | 3× | let from = repo.config.bookmark.clone(); |
| 3068 | 3× | let (local, created_from) = repo.local_bookmark_for(name)?; |
| 3069 | 3× | if let Some(remote) = created_from { |
| 3070 | 0× | println!( |
| 3071 | 0× | "{} {} from {}", |
| 3072 | 0× | ui::done("Created"), |
| 3073 | 0× | ui::bookmark(&local), |
| 3074 | 0× | ui::dim(format!("{remote}/{local}")) |
| 3075 | 0× | ); |
| 3076 | 3× | } |
| 3077 | 3× | let name = local.as_str(); |
| 3078 | | |
| 3079 | | |
| 3080 | 3× | if let Some(n) = repo.catch_up_bookmark(name)? { |
| 3081 | 0× | println!( |
| 3082 | 0× | "{} {} with {}: {} it lacked", |
| 3083 | 0× | ui::done("Caught up"), |
| 3084 | 0× | ui::bookmark(name), |
| 3085 | 0× | ui::dim(format!("{}/{name}", repo.default_remote())), |
| 3086 | 0× | plural(n, "change") |
| 3087 | 0× | ); |
| 3088 | 3× | } |
| 3089 | 3× | let sp = ui::spinner(&format!("Switching to {name}…")); |
| 3090 | 3× | let (r, carried) = if carry { |
| 3091 | 0× | repo.bookmark_switch_carry(name)? |
| 3092 | | } else { |
| 3093 | 3× | (repo.bookmark_switch(name, discard)?, vec![]) |
| 3094 | | }; |
| 3095 | 3× | sp.done(&format!( |
| 3096 | | "{} to {}: {} written, {} removed{}", |
| 3097 | 3× | ui::done("Switched"), |
| 3098 | 3× | ui::bookmark(name), |
| 3099 | 3× | plural(r.written, "file"), |
| 3100 | | r.removed, |
| 3101 | 3× | if carried.is_empty() { |
| 3102 | 3× | String::new() |
| 3103 | | } else { |
| 3104 | 0× | format!("; carried {}", plural(carried.len(), "changed path")) |
| 3105 | | } |
| 3106 | | )); |
| 3107 | 3× | print_removed(&r); |
| 3108 | 3× | print_carried(&carried); |
| 3109 | 3× | print_kept_both(&r); |
| 3110 | 3× | if r.conflicted { |
| 3111 | 0× | println!( |
| 3112 | 0× | " {} {}", |
| 3113 | 0× | ui::caution(), |
| 3114 | 0× | ui::warn("this state has conflicts; see oxy status") |
| 3115 | 0× | ); |
| 3116 | 3× | } |
| 3117 | | |
| 3118 | | |
| 3119 | | |
| 3120 | 3× | if r.lacks > 0 { |
| 3121 | 1× | println!( |
| 3122 | 1× | " {} {}", |
| 3123 | 1× | ui::note(), |
| 3124 | 1× | ui::dim(format!( |
| 3125 | 1× | "{name} lacks {} that {from} has", |
| 3126 | 1× | plural(r.lacks, "change") |
| 3127 | 1× | )) |
| 3128 | 1× | ); |
| 3129 | 2× | } |
| 3130 | 3× | super::sync::subrepos_behind(&repo); |
| 3131 | 3× | where_now(&repo); |
| 3132 | 3× | Ok(0) |
| 3133 | 3× | } |
| 3134 | |
|
| 3135 | 0× | pub fn merge(bookmark: &str) -> Result<i32> { |
| 3136 | 0× | let repo = open()?; |
| 3137 | 0× | let sp = ui::spinner(&format!("Merging {bookmark}…")); |
| 3138 | 0× | let r = repo.merge(bookmark)?; |
| 3139 | 0× | if !r.changed { |
| 3140 | 0× | sp.done(&format!( |
| 3141 | 0× | "{} {}", |
| 3142 | 0× | ui::ok(ui::glyph("✔", "*")), |
| 3143 | 0× | ui::dim(format!( |
| 3144 | 0× | "{} already contains {bookmark}", |
| 3145 | 0× | repo.config.bookmark |
| 3146 | 0× | )) |
| 3147 | 0× | )); |
| 3148 | 0× | return Ok(0); |
| 3149 | 0× | } |
| 3150 | 0× | sp.done(&format!( |
| 3151 | | "{} {} into {}: now {}{}", |
| 3152 | 0× | ui::done("Merged"), |
| 3153 | 0× | ui::bookmark(bookmark), |
| 3154 | 0× | ui::bookmark(&repo.config.bookmark), |
| 3155 | 0× | plural(r.heads.len(), "head"), |
| 3156 | 0× | ui::dim(if r.checked_out { |
| 3157 | 0× | "; working copy updated" |
| 3158 | | } else { |
| 3159 | 0× | "; run oxy checkout to update the working copy" |
| 3160 | | }) |
| 3161 | | )); |
| 3162 | 0× | let snap = repo.snapshot("oxy merge")?; |
| 3163 | 0× | if snap.base_snapshot.is_conflicted() { |
| 3164 | 0× | println!( |
| 3165 | 0× | " {} {}", |
| 3166 | 0× | ui::caution(), |
| 3167 | 0× | ui::warn("the result has conflicts; see oxy status") |
| 3168 | 0× | ); |
| 3169 | 0× | } |
| 3170 | 0× | where_now(&repo); |
| 3171 | 0× | Ok(0) |
| 3172 | 0× | } |
| 3173 | |
|
| 3174 | 0× | pub fn squash(change: &str, into: &str) -> Result<i32> { |
| 3175 | 0× | let repo = open()?; |
| 3176 | 0× | let c = repo.resolve_change(change)?; |
| 3177 | 0× | let d = repo.resolve_change(into)?; |
| 3178 | 0× | let sp = ui::spinner("Squashing…"); |
| 3179 | 0× | let h = repo.squash(&c, &d)?; |
| 3180 | 0× | let new = repo.store().get_change_meta(&h)?; |
| 3181 | 0× | sp.done(&format!( |
| 3182 | 0× | "{} into {} {} {}", |
| 3183 | 0× | ui::done("Squashed"), |
| 3184 | 0× | ui::id(new.change_id.short(8)), |
| 3185 | 0× | ui::hash(format!("#{}", h.short(12))), |
| 3186 | 0× | ui::subject(subject_of(&new.message)) |
| 3187 | 0× | )); |
| 3188 | 0× | Ok(0) |
| 3189 | 0× | } |
| 3190 | |
|
| 3191 | 0× | pub fn rebase(change: &str, onto: &[String], into_working_copy: bool) -> Result<i32> { |
| 3192 | 0× | let repo = open()?; |
| 3193 | 0× | let c = repo.resolve_change(change)?; |
| 3194 | 0× | let mut heads = Vec::new(); |
| 3195 | 0× | for o in onto { |
| 3196 | 0× | heads.extend(repo.resolve_heads(o)?); |
| 3197 | | } |
| 3198 | 0× | let sp = ui::spinner("Rebasing…"); |
| 3199 | 0× | let h = match repo.rebase(&c, &heads) { |
| 3200 | 0× | Err(oxy_core::repo::RepoError::Conflicted(_)) if into_working_copy => { |
| 3201 | 0× | let r = repo.rebase_into_working_copy(&c, &heads)?; |
| 3202 | 0× | sp.done(&format!( |
| 3203 | 0× | "{} {} into {}: now {}", |
| 3204 | 0× | ui::done("Merged"), |
| 3205 | 0× | onto.join(" "), |
| 3206 | 0× | ui::bookmark(&repo.config.bookmark), |
| 3207 | 0× | plural(r.heads.len(), "head"), |
| 3208 | 0× | )); |
| 3209 | 0× | if r.checked_out { |
| 3210 | 0× | println!( |
| 3211 | 0× | " {} {}", |
| 3212 | 0× | ui::caution(), |
| 3213 | 0× | ui::warn("the conflict is in the working copy: fix the marked files (see oxy status), then oxy commit") |
| 3214 | 0× | ); |
| 3215 | 0× | } else { |
| 3216 | 0× | println!( |
| 3217 | 0× | " {} {}", |
| 3218 | 0× | ui::caution(), |
| 3219 | 0× | ui::warn("the working copy has changes of its own: commit or discard them, then oxy checkout to take the conflict in") |
| 3220 | 0× | ); |
| 3221 | 0× | } |
| 3222 | 0× | where_now(&repo); |
| 3223 | 0× | return Ok(0); |
| 3224 | | } |
| 3225 | 0× | other => other?, |
| 3226 | | }; |
| 3227 | 0× | let new = repo.store().get_change_meta(&h)?; |
| 3228 | 0× | sp.done(&format!( |
| 3229 | 0× | "{} {} {} {}", |
| 3230 | 0× | ui::done("Rebased"), |
| 3231 | 0× | ui::id(new.change_id.short(8)), |
| 3232 | 0× | ui::hash(format!("#{}", h.short(12))), |
| 3233 | 0× | ui::dim(format!( |
| 3234 | 0× | "now depends on {}", |
| 3235 | 0× | plural(new.deps.len(), "change") |
| 3236 | 0× | )) |
| 3237 | 0× | )); |
| 3238 | 0× | where_now(&repo); |
| 3239 | 0× | Ok(0) |
| 3240 | 0× | } |
| 3241 | |
|
| 3242 | | #[cfg(test)] |
| 3243 | | mod tests { |
| 3244 | | use super::*; |
| 3245 | |
|
| 3246 | | #[test] |
| 3247 | 1× | fn id_prefixes_agree_with_the_whole_list() { |
| 3248 | | |
| 3249 | | |
| 3250 | 1× | let mut x: u64 = 0x9e37_79b9_7f4a_7c15; |
| 3251 | 7999× | let mut next = || { |
| 3252 | 7999× | x ^= x << 13; |
| 3253 | 7999× | x ^= x >> 7; |
| 3254 | 7999× | x ^= x << 17; |
| 3255 | 7999× | x |
| 3256 | 7999× | }; |
| 3257 | 1× | let mut ids: Vec<[u8; 16]> = Vec::new(); |
| 3258 | 1× | for _ in 0..2000 { |
| 3259 | 2000× | let mut id = [0u8; 16]; |
| 3260 | 2000× | id[..8].copy_from_slice(&next().to_le_bytes()); |
| 3261 | 2000× | id[8..].copy_from_slice(&next().to_le_bytes()); |
| 3262 | 2000× | let share = (next() % 4) as usize; |
| 3263 | 2000× | if let Some(prev) = ids.last() { |
| 3264 | 1999× | id[..share].copy_from_slice(&prev[..share]); |
| 3265 | 1999× | if next() % 2 == 0 { |
| 3266 | 973× | id[share] = (prev[share] & 0xf0) | (id[share] & 0x0f); |
| 3267 | 1026× | } |
| 3268 | 1× | } |
| 3269 | 2000× | ids.push(id); |
| 3270 | | } |
| 3271 | 4000× | let hex = |id: &[u8; 16]| oxy_core::ids::ChangeId(*id).to_hex(); |
| 3272 | 1× | let whole = unique_prefixes(ids.iter().map(hex)); |
| 3273 | 1× | let some = id_prefixes(ids.clone(), ids.iter().copied()); |
| 3274 | 2000× | for id in &ids { |
| 3275 | 2000× | assert_eq!(some[id], whole[&hex(id)], "{}", hex(id)); |
| 3276 | | } |
| 3277 | 1× | } |
| 3278 | | } |