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