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