| 1 |
|
| 2 |
|
| 3 |
use super::format::{author_name, deps_line, deps_of, plural, short_id, short_meta_id, signature_line, signature_mark, subject_of, when}; |
| 4 |
use super::json; |
| 5 |
use oxy_core::repo::SignatureStatus; |
| 6 |
use super::open; |
| 7 |
use super::ui; |
| 8 |
use anyhow::Result; |
| 9 |
use oxy_core::objects::ChangeKind; |
| 10 |
use oxy_core::repo::{render_change, ChangeStatus, Repo}; |
| 11 |
use std::path::PathBuf; |
| 12 |
|
| 13 |
pub fn init(path: Option<PathBuf>) -> Result<i32> { |
| 14 |
let path = path.unwrap_or(std::env::current_dir()?); |
| 15 |
let repo = Repo::init(&path)?; |
| 16 |
println!("{} oxy repository in {}", ui::done("Initialized"), ui::path(repo.root().display())); |
| 17 |
println!("{}", ui::kv("bookmark", ui::bookmark(&repo.config.bookmark))); |
| 18 |
println!("{}", ui::kv("author", format!("{} <{}>", ui::author(&repo.config.author_name), repo.config.author_email))); |
| 19 |
Ok(0) |
| 20 |
} |
| 21 |
|
| 22 |
fn status_tag(s: ChangeStatus) -> &'static str { |
| 23 |
match s { |
| 24 |
ChangeStatus::Added => "A", |
| 25 |
ChangeStatus::Modified => "M", |
| 26 |
ChangeStatus::Removed => "D", |
| 27 |
ChangeStatus::Renamed => "R", |
| 28 |
ChangeStatus::ModeChanged => "X", |
| 29 |
ChangeStatus::ConflictEdited => "C!", |
| 30 |
ChangeStatus::Resolved => "V", |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
pub fn status(json: bool) -> Result<i32> { |
| 35 |
let repo = open()?; |
| 36 |
let sp = ui::spinner("Snapshotting the working copy…"); |
| 37 |
let snap = repo.snapshot("oxy status")?; |
| 38 |
let heads = repo.bookmark_heads()?; |
| 39 |
sp.clear(); |
| 40 |
if json { |
| 41 |
println!("{}", repo.status_json(&snap)?); |
| 42 |
return Ok(if snap.base_snapshot.is_conflicted() { 2 } else { 0 }); |
| 43 |
} |
| 44 |
println!( |
| 45 |
"{} {}{}working copy {}{}{}", |
| 46 |
ui::dim("On"), |
| 47 |
ui::bookmark(&repo.config.bookmark), |
| 48 |
ui::sep(), |
| 49 |
ui::id(snap.wc.change_id.short(8)), |
| 50 |
ui::sep(), |
| 51 |
ui::dim(plural(heads.len(), "head")) |
| 52 |
); |
| 53 |
if snap.statuses.is_empty() { |
| 54 |
println!(" {} {}", ui::ok(ui::glyph("✔", "*")), ui::dim("nothing to commit, working copy clean")); |
| 55 |
} else { |
| 56 |
println!(); |
| 57 |
for s in &snap.statuses { |
| 58 |
println!(" {} {}", ui::status_mark(status_tag(s.status)), ui::path(&s.path)); |
| 59 |
} |
| 60 |
} |
| 61 |
if !snap.base_snapshot.is_conflicted() { |
| 62 |
return Ok(0); |
| 63 |
} |
| 64 |
println!(); |
| 65 |
println!("{} {}", ui::section(ui::err("Conflicts")), ui::dim("edit the marked files, or the .oxy-conflict-N candidates, then commit")); |
| 66 |
for c in &snap.base_snapshot.tree.conflicts { |
| 67 |
println!(" {} tree: {c:?}", ui::err(ui::glyph("✱", "!"))); |
| 68 |
} |
| 69 |
for (p, f) in &snap.base_snapshot.files { |
| 70 |
if f.is_conflicted() { |
| 71 |
let state = match snap.statuses.iter().find(|s| s.path == *p).map(|s| s.status) { |
| 72 |
Some(ChangeStatus::Resolved) => ui::ok(format!("{} resolved in working copy", ui::glyph("✔", "*"))), |
| 73 |
Some(ChangeStatus::ConflictEdited) => ui::warn(format!("{} markers remain", ui::glyph("‼", "!"))), |
| 74 |
_ => ui::err(format!("{} unresolved", ui::glyph("✱", "!"))), |
| 75 |
}; |
| 76 |
println!(" {} {state}", ui::path(p)); |
| 77 |
} |
| 78 |
} |
| 79 |
Ok(2) |
| 80 |
} |
| 81 |
|
| 82 |
pub fn commit(message: &str, depends_on: &[String], paths: &[String], interactive: bool, hunks: &[usize]) -> Result<i32> { |
| 83 |
let repo = open()?; |
| 84 |
let mut deps = Vec::new(); |
| 85 |
for d in depends_on { |
| 86 |
deps.push(repo.resolve_change(d)?); |
| 87 |
} |
| 88 |
let selection = if !hunks.is_empty() { |
| 89 |
oxy_core::repo::Selection::Hunks(hunks.to_vec()) |
| 90 |
} else if interactive { |
| 91 |
let (_, hunks) = repo.hunks()?; |
| 92 |
if hunks.is_empty() { |
| 93 |
anyhow::bail!("nothing to commit: the working copy matches its base"); |
| 94 |
} |
| 95 |
let hunks: Vec<_> = hunks.into_iter().filter(|h| paths.is_empty() || paths.iter().any(|p| h.path == *p || h.path.starts_with(&format!("{}/", p.trim_end_matches('/'))))).collect(); |
| 96 |
let mut chosen = Vec::new(); |
| 97 |
let stdin = std::io::stdin(); |
| 98 |
let mut take_rest = false; |
| 99 |
for (n, h) in hunks.iter().enumerate() { |
| 100 |
if take_rest { |
| 101 |
chosen.push(h.index); |
| 102 |
continue; |
| 103 |
} |
| 104 |
println!("{} {} {}", ui::section(format!("hunk {} of {}", n + 1, hunks.len())), ui::path(&h.path), ui::dim(format!("(#{})", h.index))); |
| 105 |
for line in h.text.lines() { |
| 106 |
let styled = if line.starts_with('+') { ui::ok(line) } else if line.starts_with('-') { ui::err(line) } else { ui::dim(line) }; |
| 107 |
println!(" {styled}"); |
| 108 |
} |
| 109 |
loop { |
| 110 |
print!("{} ", ui::warn("Commit this hunk? [y]es [n]o [a]ll remaining [q]uit:")); |
| 111 |
use std::io::Write as _; |
| 112 |
std::io::stdout().flush()?; |
| 113 |
let mut answer = String::new(); |
| 114 |
if stdin.read_line(&mut answer)? == 0 { |
| 115 |
anyhow::bail!("aborted"); |
| 116 |
} |
| 117 |
match answer.trim() { |
| 118 |
"y" | "Y" => { |
| 119 |
chosen.push(h.index); |
| 120 |
break; |
| 121 |
} |
| 122 |
"n" | "N" | "" => break, |
| 123 |
"a" | "A" => { |
| 124 |
chosen.push(h.index); |
| 125 |
take_rest = true; |
| 126 |
break; |
| 127 |
} |
| 128 |
"q" | "Q" => anyhow::bail!("aborted; nothing committed"), |
| 129 |
_ => continue, |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
if chosen.is_empty() { |
| 134 |
anyhow::bail!("no hunks chosen; nothing committed"); |
| 135 |
} |
| 136 |
oxy_core::repo::Selection::Hunks(chosen) |
| 137 |
} else if paths.is_empty() { |
| 138 |
oxy_core::repo::Selection::All |
| 139 |
} else { |
| 140 |
oxy_core::repo::Selection::Paths(paths.to_vec()) |
| 141 |
}; |
| 142 |
let sp = ui::spinner("Committing…"); |
| 143 |
let h = repo.commit_selection(message, &deps, &selection)?; |
| 144 |
let c = repo.store().get_change(&h)?; |
| 145 |
sp.done(&format!("{} {} {} {}", ui::done("Committed"), ui::id(short_id(&c)), ui::hash(format!("#{}", h.short(12))), ui::subject(subject_of(message)))); |
| 146 |
if !matches!(selection, oxy_core::repo::Selection::All) { |
| 147 |
let rest = repo.snapshot("oxy commit")?; |
| 148 |
if !rest.statuses.is_empty() { |
| 149 |
println!(" {} {}", ui::note(), ui::dim(format!("{} left uncommitted", plural(rest.statuses.len(), "path")))); |
| 150 |
} |
| 151 |
} |
| 152 |
Ok(0) |
| 153 |
} |
| 154 |
|
| 155 |
pub fn blame(path: &str, json: bool) -> Result<i32> { |
| 156 |
let repo = open()?; |
| 157 |
let lines = repo.blame(path)?; |
| 158 |
if json { |
| 159 |
let items = lines.iter().map(|l| { |
| 160 |
format!( |
| 161 |
"{{\"line\":{},\"change_id\":{},\"hash\":{},\"index\":{},\"author\":{},\"when_ms\":{}}}", |
| 162 |
l.line, |
| 163 |
oxy_core::repo::json_str(&l.change_id.to_hex()), |
| 164 |
oxy_core::repo::json_str(&l.hash.to_hex()), |
| 165 |
l.index, |
| 166 |
oxy_core::repo::json_str(&l.author), |
| 167 |
l.when_ms |
| 168 |
) |
| 169 |
}); |
| 170 |
println!("{}", oxy_core::repo::json_array(items)); |
| 171 |
return Ok(0); |
| 172 |
} |
| 173 |
let width = lines.iter().map(|l| l.author.chars().count()).max().unwrap_or(0).min(20); |
| 174 |
let digits = lines.len().to_string().len(); |
| 175 |
for l in &lines { |
| 176 |
let author: String = l.author.chars().take(20).collect(); |
| 177 |
let text = String::from_utf8_lossy(&l.text); |
| 178 |
println!( |
| 179 |
"{} {} {} {:>digits$}{} {}", |
| 180 |
ui::id(l.change_id.short(8)), |
| 181 |
ui::author(format!("{author:<width$}")), |
| 182 |
ui::dim(ui::ago(l.when_ms)), |
| 183 |
l.line, |
| 184 |
ui::dim(ui::glyph("│", "|")), |
| 185 |
text.trim_end_matches('\n') |
| 186 |
); |
| 187 |
} |
| 188 |
Ok(0) |
| 189 |
} |
| 190 |
|
| 191 |
fn bisect_report(repo: &Repo, step: &oxy_core::repo::BisectStep) -> Result<i32> { |
| 192 |
use oxy_core::repo::BisectStep::*; |
| 193 |
match step { |
| 194 |
NeedMarks { good, bad } => { |
| 195 |
println!("{} {} {}", ui::note(), ui::dim(format!("{good} good, {bad} bad;")), ui::dim(if *good == 0 { "mark a known-good change or bookmark: oxy bisect good CHANGE" } else { "mark a known-bad change or bookmark: oxy bisect bad CHANGE" })); |
| 196 |
Ok(0) |
| 197 |
} |
| 198 |
Testing { heads, remaining, steps_left } => { |
| 199 |
println!("{} {} {}", ui::done("Testing"), ui::dim(format!("{} in the state under test;", plural(heads.len(), "head"))), ui::dim(format!("{remaining} candidates left, about {steps_left} more steps"))); |
| 200 |
println!(" {} {}", ui::note(), ui::dim("run your test, then oxy bisect good | bad | skip")); |
| 201 |
Ok(0) |
| 202 |
} |
| 203 |
Found { culprit } => { |
| 204 |
let c = repo.store().get_change(culprit)?; |
| 205 |
println!("{} {} {} {}", ui::done("Found"), ui::id(short_id(&c)), ui::hash(format!("#{}", culprit.short(12))), ui::subject(subject_of(&c.message))); |
| 206 |
println!(" {} {}", ui::note(), ui::dim("the first change whose inclusion makes the state bad; oxy bisect reset to return")); |
| 207 |
Ok(0) |
| 208 |
} |
| 209 |
Inconclusive { candidates } => { |
| 210 |
println!("{} {}", ui::caution(), ui::warn(format!("every remaining candidate was skipped ({}); oxy bisect reset to return", plural(candidates.len(), "change")))); |
| 211 |
Ok(1) |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
pub fn bisect_start() -> Result<i32> { |
| 217 |
let repo = open()?; |
| 218 |
repo.bisect_start()?; |
| 219 |
println!("{} bisect; {}", ui::done("Started"), ui::dim("now: oxy bisect good CHANGE and oxy bisect bad CHANGE")); |
| 220 |
Ok(0) |
| 221 |
} |
| 222 |
|
| 223 |
pub fn bisect_mark(good: Option<bool>, changes: &[String]) -> Result<i32> { |
| 224 |
let repo = open()?; |
| 225 |
let mut heads = Vec::new(); |
| 226 |
for c in changes { |
| 227 |
heads.extend(repo.resolve_heads(c)?); |
| 228 |
} |
| 229 |
let step = repo.bisect_mark(good, &heads)?; |
| 230 |
bisect_report(&repo, &step) |
| 231 |
} |
| 232 |
|
| 233 |
pub fn bisect_run(command: &[String]) -> Result<i32> { |
| 234 |
let repo = open()?; |
| 235 |
let step = repo.bisect_run(command)?; |
| 236 |
bisect_report(&repo, &step) |
| 237 |
} |
| 238 |
|
| 239 |
pub fn bisect_status(as_json: bool) -> Result<i32> { |
| 240 |
let repo = open()?; |
| 241 |
if as_json { |
| 242 |
use oxy_core::repo::BisectStep::*; |
| 243 |
let v = match repo.bisect_status()? { |
| 244 |
None => "null".to_string(), |
| 245 |
Some(NeedMarks { good, bad }) => json::obj([("state", json::s("need-marks")), ("good", json::n(good)), ("bad", json::n(bad))]), |
| 246 |
Some(Testing { heads, remaining, steps_left }) => json::obj([("state", json::s("testing")), ("heads", json::hashes(&heads)), ("remaining", json::n(remaining)), ("steps_left", json::n(steps_left))]), |
| 247 |
Some(Found { culprit }) => json::obj([("state", json::s("found")), ("culprit", json::s(&culprit.to_hex()))]), |
| 248 |
Some(Inconclusive { candidates }) => json::obj([("state", json::s("inconclusive")), ("candidates", json::hashes(&candidates))]), |
| 249 |
}; |
| 250 |
println!("{v}"); |
| 251 |
return Ok(0); |
| 252 |
} |
| 253 |
match repo.bisect_status()? { |
| 254 |
None => { |
| 255 |
println!("{}", ui::dim("No bisect in progress.")); |
| 256 |
Ok(0) |
| 257 |
} |
| 258 |
Some(step) => bisect_report(&repo, &step), |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
pub fn bisect_reset() -> Result<i32> { |
| 263 |
let repo = open()?; |
| 264 |
repo.bisect_reset()?; |
| 265 |
println!("{} bisect; working copy restored", ui::done("Reset")); |
| 266 |
Ok(0) |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
fn signature_json(repo: &Repo, h: &oxy_core::hash::ChangeHash) -> Result<String> { |
| 273 |
Ok(match repo.authenticate_change(h)? { |
| 274 |
SignatureStatus::Unsigned => json::obj([("status", json::s("unsigned"))]), |
| 275 |
SignatureStatus::Valid { key, name, email, sealed, revoked } => json::obj([ |
| 276 |
("status", json::s(if sealed { "sealed" } else { "signed" })), |
| 277 |
("key", json::s(&key.to_hex())), |
| 278 |
("name", json::s(&name)), |
| 279 |
("email", json::s(&email)), |
| 280 |
("revoked", json::b(revoked)), |
| 281 |
]), |
| 282 |
SignatureStatus::Invalid(why) => json::obj([("status", json::s("invalid")), ("reason", json::s(&why))]), |
| 283 |
}) |
| 284 |
} |
| 285 |
|
| 286 |
fn log_entry_json(repo: &Repo, e: &oxy_core::repo::LogEntry) -> Result<String> { |
| 287 |
let c = &e.change; |
| 288 |
Ok(json::obj([ |
| 289 |
("hash", json::s(&e.hash.to_hex())), |
| 290 |
("change_id", json::s(&c.change_id.to_hex())), |
| 291 |
("author", json::s(&author_name(repo, c))), |
| 292 |
("author_key", json::s(&c.author.to_hex())), |
| 293 |
("when_ms", json::n(c.hlc.wall_ms)), |
| 294 |
("message", json::s(&c.message)), |
| 295 |
("kind", json::s(&format!("{:?}", c.kind).to_lowercase())), |
| 296 |
("deps", json::hashes(&c.deps)), |
| 297 |
("head", json::b(e.is_head)), |
| 298 |
("working_copy", json::b(e.is_working_copy)), |
| 299 |
("signature", if e.is_working_copy { "null".into() } else { signature_json(repo, &e.hash)? }), |
| 300 |
])) |
| 301 |
} |
| 302 |
|
| 303 |
pub fn log(limit: Option<usize>, verbose: bool, path: Option<&str>, as_json: bool) -> Result<i32> { |
| 304 |
let repo = open()?; |
| 305 |
let snap = repo.snapshot("oxy log")?; |
| 306 |
let conflicted = snap.base_snapshot.is_conflicted(); |
| 307 |
let entries = match path { |
| 308 |
Some(p) => repo.log_for_path(p)?, |
| 309 |
None => repo.log()?, |
| 310 |
}; |
| 311 |
if as_json { |
| 312 |
let shown = limit.unwrap_or(entries.len()).min(entries.len()); |
| 313 |
let mut items = Vec::new(); |
| 314 |
for e in entries.iter().take(shown) { |
| 315 |
items.push(log_entry_json(&repo, e)?); |
| 316 |
} |
| 317 |
println!("{}", json::obj([("bookmark", json::s(&repo.config.bookmark)), ("total", json::n(entries.len())), ("changes", json::arr(items))])); |
| 318 |
return Ok(0); |
| 319 |
} |
| 320 |
let total = entries.len(); |
| 321 |
let shown = limit.unwrap_or(total).min(total); |
| 322 |
let rail = ui::rail(); |
| 323 |
for (i, e) in entries.iter().take(shown).enumerate() { |
| 324 |
let c = &e.change; |
| 325 |
let bullet = ui::log_bullet(e.is_working_copy, e.is_head, e.is_working_copy && conflicted); |
| 326 |
let mut labels = Vec::new(); |
| 327 |
if e.is_working_copy { |
| 328 |
labels.push(ui::warn("working copy")); |
| 329 |
} |
| 330 |
if c.kind == ChangeKind::Resolution { |
| 331 |
labels.push(ui::warn("resolution")); |
| 332 |
} |
| 333 |
let labels = if labels.is_empty() { String::new() } else { format!(" {}{}{}", ui::dim("("), labels.join(ui::dim(", ").as_str()), ui::dim(")")) }; |
| 334 |
let subject = if e.is_working_copy && c.message.is_empty() { |
| 335 |
ui::dim(if snap.statuses.is_empty() { "(clean)" } else { "(uncommitted changes)" }) |
| 336 |
} else { |
| 337 |
ui::subject(subject_of(&c.message)) |
| 338 |
}; |
| 339 |
println!("{bullet} {}{labels} {subject}", ui::id(short_meta_id(c))); |
| 340 |
let meta = format!( |
| 341 |
"{}{}{}{}{}{}{}", |
| 342 |
ui::hash(format!("#{}", e.hash.short(12))), |
| 343 |
ui::sep(), |
| 344 |
ui::author(author_name(&repo, c)), |
| 345 |
ui::sep(), |
| 346 |
ui::dim(ui::ago(c.hlc.wall_ms)), |
| 347 |
ui::sep(), |
| 348 |
if e.is_working_copy { ui::dim("not yet committed") } else { signature_mark(&repo, &e.hash)? } |
| 349 |
); |
| 350 |
println!("{rail} {meta}"); |
| 351 |
if verbose { |
| 352 |
if !e.is_working_copy { |
| 353 |
println!("{rail} {}", ui::dim(format!("{} {}", when(c.hlc.wall_ms), signature_line(&repo, &e.hash)?))); |
| 354 |
} |
| 355 |
if !c.deps.is_empty() { |
| 356 |
println!("{rail} {} {}", ui::dim(ui::glyph("↳", "->")), ui::dim(deps_of(&c.deps))); |
| 357 |
} |
| 358 |
let body: Vec<&str> = c.message.lines().skip(1).skip_while(|l| l.trim().is_empty()).collect(); |
| 359 |
if !body.is_empty() { |
| 360 |
println!("{rail}"); |
| 361 |
for line in body { |
| 362 |
println!("{rail} {line}"); |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
if i + 1 < shown { |
| 367 |
println!("{rail}"); |
| 368 |
} |
| 369 |
} |
| 370 |
if shown < total { |
| 371 |
println!("{}", ui::dim(format!("{} {} more; pass -n to see them", ui::glyph("…", "..."), total - shown))); |
| 372 |
} |
| 373 |
Ok(0) |
| 374 |
} |
| 375 |
|
| 376 |
pub fn diff(change: Option<&str>, as_json: bool) -> Result<i32> { |
| 377 |
let repo = open()?; |
| 378 |
let hash = change.map(|c| repo.resolve_change(c)).transpose()?; |
| 379 |
if as_json { |
| 380 |
let files = repo.diff_files(hash.as_ref())?; |
| 381 |
let items = files.iter().map(|f| { |
| 382 |
json::obj([ |
| 383 |
("path", json::s(&f.path)), |
| 384 |
("kind", json::s(&f.kind)), |
| 385 |
("from", json::opt(f.from.as_deref(), json::s)), |
| 386 |
( |
| 387 |
"hunks", |
| 388 |
json::arr(f.hunks.iter().map(|h| { |
| 389 |
json::obj([ |
| 390 |
("base_line", json::n(h.base_line)), |
| 391 |
("added", json::arr(h.added.iter().map(|l| json::s(&String::from_utf8_lossy(l))))), |
| 392 |
("removed", json::arr(h.removed.iter().map(|(n, l)| json::obj([("line", json::n(*n)), ("text", json::s(&String::from_utf8_lossy(l)))])))), |
| 393 |
]) |
| 394 |
})), |
| 395 |
), |
| 396 |
]) |
| 397 |
}); |
| 398 |
println!("{}", json::obj([("change", json::opt(hash, |h| json::s(&h.to_hex()))), ("files", json::arr(items))])); |
| 399 |
return Ok(0); |
| 400 |
} |
| 401 |
match hash { |
| 402 |
Some(h) => { |
| 403 |
let c = repo.store().get_change(&h)?; |
| 404 |
let base = repo.load_state(&c.deps)?; |
| 405 |
let snapshot = base.materialize(); |
| 406 |
print!("{}", render_change(&c, &base, &snapshot)); |
| 407 |
} |
| 408 |
None => { |
| 409 |
let snap = repo.snapshot("oxy diff")?; |
| 410 |
print!("{}", render_change(&snap.wc, &snap.base, &snap.base_snapshot)); |
| 411 |
} |
| 412 |
} |
| 413 |
Ok(0) |
| 414 |
} |
| 415 |
|
| 416 |
pub fn cat(path: &str, change: Option<&str>) -> Result<i32> { |
| 417 |
let repo = open()?; |
| 418 |
let heads = change.map(|c| repo.resolve_heads(c)).transpose()?; |
| 419 |
let bytes = match repo.file_at(path, heads.as_deref()) { |
| 420 |
Ok(b) => b, |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
Err(e) => match change { |
| 425 |
Some(c) |
| 426 |
if !repo.view()?.bookmarks.contains_key(c) |
| 427 |
&& repo.tag_heads(c)?.is_none() |
| 428 |
&& repo.file_at(path, None).is_ok() => |
| 429 |
{ |
| 430 |
anyhow::bail!( |
| 431 |
"{path} is not in {c}'s dependency closure (the change and what it \ |
| 432 |
depends on); it is on the bookmark: try -r {}, or --at-op OP for the \ |
| 433 |
state at an operation", |
| 434 |
repo.config.bookmark |
| 435 |
) |
| 436 |
} |
| 437 |
_ => return Err(e.into()), |
| 438 |
}, |
| 439 |
}; |
| 440 |
use std::io::Write as _; |
| 441 |
std::io::stdout().write_all(&bytes)?; |
| 442 |
Ok(0) |
| 443 |
} |
| 444 |
|
| 445 |
pub fn root(as_json: bool) -> Result<i32> { |
| 446 |
let Ok(repo) = Repo::open(&std::env::current_dir()?) else { |
| 447 |
if as_json { |
| 448 |
println!("null"); |
| 449 |
} |
| 450 |
return Ok(1); |
| 451 |
}; |
| 452 |
if as_json { |
| 453 |
println!("{}", json::obj([("root", json::s(&repo.root().to_string_lossy())), ("workspace", json::s(&repo.workspace().to_hex())), ("bookmark", json::s(&repo.config.bookmark))])); |
| 454 |
} else { |
| 455 |
println!("{}", repo.root().display()); |
| 456 |
} |
| 457 |
Ok(0) |
| 458 |
} |
| 459 |
|
| 460 |
pub fn version(as_json: bool) -> Result<i32> { |
| 461 |
if as_json { |
| 462 |
println!("{}", json::obj([("version", json::s(env!("CARGO_PKG_VERSION"))), ("protocol", json::n(oxy_core::protocol::PROTOCOL_VERSION)), ("format", json::n(2))])); |
| 463 |
} else { |
| 464 |
println!("oxy {} (repository format 2, protocol {})", env!("CARGO_PKG_VERSION"), oxy_core::protocol::PROTOCOL_VERSION); |
| 465 |
} |
| 466 |
Ok(0) |
| 467 |
} |
| 468 |
|
| 469 |
pub fn discard(paths: &[String], hunks: &[usize]) -> Result<i32> { |
| 470 |
let repo = open()?; |
| 471 |
if paths.is_empty() && hunks.is_empty() { |
| 472 |
anyhow::bail!("name paths to restore, or --hunks to undo chosen hunks"); |
| 473 |
} |
| 474 |
let mut n = 0; |
| 475 |
if !hunks.is_empty() { |
| 476 |
n += repo.discard_hunks(hunks)?; |
| 477 |
} |
| 478 |
if !paths.is_empty() { |
| 479 |
n += repo.discard(paths)?; |
| 480 |
} |
| 481 |
println!("{} {} restored to the working copy's base", ui::done("Discarded"), plural(n, "path")); |
| 482 |
Ok(0) |
| 483 |
} |
| 484 |
|
| 485 |
pub fn hunks(as_json: bool) -> Result<i32> { |
| 486 |
let repo = open()?; |
| 487 |
let (_, hunks) = repo.hunks()?; |
| 488 |
if as_json { |
| 489 |
println!("{}", json::arr(hunks.iter().map(|h| json::obj([("index", json::n(h.index)), ("path", json::s(&h.path)), ("text", json::s(&h.text))])))); |
| 490 |
return Ok(0); |
| 491 |
} |
| 492 |
if hunks.is_empty() { |
| 493 |
println!("{}", ui::dim("No hunks: the working copy matches its base.")); |
| 494 |
} |
| 495 |
for h in &hunks { |
| 496 |
println!("{} {}", ui::section(format!("hunk {}", h.index)), ui::path(&h.path)); |
| 497 |
for line in h.text.lines() { |
| 498 |
let styled = if line.starts_with('+') { ui::ok(line) } else if line.starts_with('-') { ui::err(line) } else { ui::dim(line) }; |
| 499 |
println!(" {styled}"); |
| 500 |
} |
| 501 |
} |
| 502 |
Ok(0) |
| 503 |
} |
| 504 |
|
| 505 |
pub fn show(change: &str, as_json: bool) -> Result<i32> { |
| 506 |
let repo = open()?; |
| 507 |
repo.snapshot("oxy show")?; |
| 508 |
let h = repo.resolve_change(change)?; |
| 509 |
let c = repo.store().get_change(&h)?; |
| 510 |
if as_json { |
| 511 |
let files = repo.diff_files(Some(&h))?; |
| 512 |
let heads = repo.bookmark_heads()?; |
| 513 |
let entry = oxy_core::repo::LogEntry { hash: h, change: oxy_core::state::ChangeMeta::of(&c), is_working_copy: c.kind == oxy_core::objects::ChangeKind::WorkingCopy, is_head: heads.contains(&h) }; |
| 514 |
let mut body = log_entry_json(&repo, &entry)?; |
| 515 |
body.pop(); |
| 516 |
body.push_str(&format!(",{}:{}}}", json::s("files"), json::arr(files.iter().map(|f| json::obj([("path", json::s(&f.path)), ("kind", json::s(&f.kind)), ("from", json::opt(f.from.as_deref(), json::s))]))))); |
| 517 |
println!("{body}"); |
| 518 |
return Ok(0); |
| 519 |
} |
| 520 |
println!("{} {} {}", ui::section("change"), ui::id(short_id(&c)), ui::hash(format!("#{h}"))); |
| 521 |
println!("{}", ui::kv("kind", format!("{:?}", c.kind).to_lowercase())); |
| 522 |
println!("{}", ui::kv("author", ui::author(author_name(&repo, &oxy_core::state::ChangeMeta::of(&c))))); |
| 523 |
println!("{}", ui::kv("date", format!("{} {}", when(c.hlc.wall_ms), ui::dim(format!("({})", ui::ago(c.hlc.wall_ms)))))); |
| 524 |
println!("{}", ui::kv("deps", ui::dim(deps_line(&c)))); |
| 525 |
println!("{}", ui::kv("sig", signature_line(&repo, &h)?)); |
| 526 |
println!(); |
| 527 |
for line in (if c.message.is_empty() { "(no description)" } else { &c.message }).lines() { |
| 528 |
println!(" {line}"); |
| 529 |
} |
| 530 |
println!(); |
| 531 |
print!("{}", repo.describe_change(&h)?); |
| 532 |
Ok(0) |
| 533 |
} |
| 534 |
|
| 535 |
pub fn checkout(changes: &[String], discard: bool) -> Result<i32> { |
| 536 |
let repo = open()?; |
| 537 |
let heads = if changes.is_empty() { |
| 538 |
repo.bookmark_heads()? |
| 539 |
} else { |
| 540 |
let mut heads = Vec::new(); |
| 541 |
for c in changes { |
| 542 |
heads.extend(repo.resolve_heads(c)?); |
| 543 |
} |
| 544 |
heads |
| 545 |
}; |
| 546 |
let spec = if changes.is_empty() { repo.config.bookmark.clone() } else { changes.join(" ") }; |
| 547 |
let sp = ui::spinner(&format!("Checking out {spec}…")); |
| 548 |
let r = repo.checkout(&heads, discard, &format!("oxy checkout {spec}"))?; |
| 549 |
sp.done(&format!("{} on {}: {} written, {} removed", ui::done("Working copy"), ui::bookmark(&spec), plural(r.written, "file"), r.removed)); |
| 550 |
for n in &r.notes { |
| 551 |
println!(" {} {n}", ui::note()); |
| 552 |
} |
| 553 |
if r.conflicted { |
| 554 |
println!(" {} {}", ui::caution(), ui::warn("this state has conflicts; see oxy status")); |
| 555 |
} |
| 556 |
Ok(0) |
| 557 |
} |
| 558 |
|
| 559 |
pub fn resolve(as_json: bool) -> Result<i32> { |
| 560 |
let repo = open()?; |
| 561 |
let snap = repo.snapshot("oxy resolve")?; |
| 562 |
if as_json { |
| 563 |
println!("{}", repo.status_json(&snap)?); |
| 564 |
return Ok(if snap.base_snapshot.is_conflicted() { 2 } else { 0 }); |
| 565 |
} |
| 566 |
if !snap.base_snapshot.is_conflicted() { |
| 567 |
println!("The working copy's base has no conflicts."); |
| 568 |
return Ok(0); |
| 569 |
} |
| 570 |
let mut remaining = 0; |
| 571 |
for (p, f) in &snap.base_snapshot.files { |
| 572 |
if !f.is_conflicted() { |
| 573 |
continue; |
| 574 |
} |
| 575 |
match snap.statuses.iter().find(|s| s.path == *p).map(|s| s.status) { |
| 576 |
Some(ChangeStatus::Resolved) => println!("resolved {p}"), |
| 577 |
Some(ChangeStatus::ConflictEdited) => { |
| 578 |
remaining += 1; |
| 579 |
println!("markers {p}"); |
| 580 |
} |
| 581 |
_ => { |
| 582 |
remaining += 1; |
| 583 |
println!("unresolved {p}"); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
for c in &snap.base_snapshot.tree.conflicts { |
| 588 |
remaining += 1; |
| 589 |
println!("tree {c:?}"); |
| 590 |
} |
| 591 |
if remaining == 0 { |
| 592 |
println!("All conflicts are resolved; `oxy commit` will record the resolution."); |
| 593 |
Ok(0) |
| 594 |
} else { |
| 595 |
Ok(2) |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
pub fn undo() -> Result<i32> { |
| 600 |
let repo = open()?; |
| 601 |
let op = repo.undo()?; |
| 602 |
println!("{} now at: {}", ui::done("Undone;"), ui::dim(&op.command)); |
| 603 |
Ok(0) |
| 604 |
} |
| 605 |
|
| 606 |
pub fn op_log(as_json: bool) -> Result<i32> { |
| 607 |
let repo = open()?; |
| 608 |
if as_json { |
| 609 |
let ops = repo.op_log()?; |
| 610 |
println!( |
| 611 |
"{}", |
| 612 |
json::arr(ops.iter().enumerate().map(|(i, (r, op))| { |
| 613 |
json::obj([ |
| 614 |
("id", json::s(&r.to_hex())), |
| 615 |
("when_ms", json::n(op.hlc.wall_ms)), |
| 616 |
("command", json::s(&op.command)), |
| 617 |
("snapshot", json::b(op.command.ends_with("(snapshot)"))), |
| 618 |
("current", json::b(i == 0)), |
| 619 |
]) |
| 620 |
})) |
| 621 |
); |
| 622 |
return Ok(0); |
| 623 |
} |
| 624 |
for (i, (r, op)) in repo.op_log()?.into_iter().enumerate() { |
| 625 |
let bullet = if i == 0 { ui::ok(ui::glyph("●", "@")) } else { ui::dim(ui::glyph("○", "o")) }; |
| 626 |
let cmd = subject_of(&op.command).split("\\n").next().unwrap_or(""); |
| 627 |
println!("{bullet} {}{}{} {}", ui::hash(r.short(12)), ui::sep(), ui::dim(format!("{:<16}", ui::ago(op.hlc.wall_ms))), if i == 0 { ui::subject(cmd) } else { cmd.to_string() }); |
| 628 |
} |
| 629 |
Ok(0) |
| 630 |
} |
| 631 |
|
| 632 |
pub fn gc(now: bool) -> Result<i32> { |
| 633 |
let repo = open()?; |
| 634 |
let sp = ui::spinner("Packing and pruning…"); |
| 635 |
let r = repo.gc(now)?; |
| 636 |
sp.clear(); |
| 637 |
if r.packed.objects > 0 { |
| 638 |
println!( |
| 639 |
"Packed {}: {} bytes -> {} bytes ({:.0}%).", |
| 640 |
plural(r.packed.objects, "loose object"), |
| 641 |
r.packed.raw_bytes, |
| 642 |
r.packed.packed_bytes, |
| 643 |
100.0 * r.packed.packed_bytes as f64 / r.packed.raw_bytes.max(1) as f64 |
| 644 |
); |
| 645 |
} |
| 646 |
println!("{} reachable; pruned {} ({} bytes){}{}; {} present.", |
| 647 |
plural(r.reachable, "object"), |
| 648 |
plural(r.pruned.objects, "unreachable object"), |
| 649 |
r.pruned.bytes, |
| 650 |
if r.pruned.deferred > 0 { format!("; {} deferred by the grace period (use --now)", plural(r.pruned.deferred, "object")) } else { String::new() }, |
| 651 |
if r.merged_packs > 0 { format!("; merged {} into one", plural(r.merged_packs, "pack")) } else { String::new() }, |
| 652 |
plural(repo.store().pack_count(), "pack") |
| 653 |
); |
| 654 |
Ok(0) |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
fn current_head(repo: &Repo) -> Result<oxy_core::hash::ChangeHash> { |
| 659 |
let snap = repo.snapshot("oxy")?; |
| 660 |
let base = snap.base.maximal(&snap.wc.deps); |
| 661 |
match base.as_slice() { |
| 662 |
[h] => Ok(*h), |
| 663 |
_ => anyhow::bail!("the working copy sits on {} heads; name the change explicitly", base.len()), |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
pub fn describe(message: &str, change: Option<&str>) -> Result<i32> { |
| 668 |
let repo = open()?; |
| 669 |
let h = match change { |
| 670 |
Some(c) => repo.resolve_change(c)?, |
| 671 |
None => current_head(&repo)?, |
| 672 |
}; |
| 673 |
let new = repo.describe(&h, message)?; |
| 674 |
let c = repo.store().get_change(&new)?; |
| 675 |
println!("{} {} {} {}", ui::done("Described"), ui::id(short_id(&c)), ui::hash(format!("#{}", new.short(12))), ui::subject(subject_of(message))); |
| 676 |
Ok(0) |
| 677 |
} |
| 678 |
|
| 679 |
pub fn amend(message: Option<&str>, into: Option<&str>) -> Result<i32> { |
| 680 |
let repo = open()?; |
| 681 |
let new = match into { |
| 682 |
Some(spec) => { |
| 683 |
let target = repo.resolve_change(spec)?; |
| 684 |
repo.amend_into(&target, message)? |
| 685 |
} |
| 686 |
None => repo.amend(message)?, |
| 687 |
}; |
| 688 |
let c = repo.store().get_change(&new)?; |
| 689 |
println!("{} {} {} {}", ui::done("Amended"), ui::id(short_id(&c)), ui::hash(format!("#{}", new.short(12))), ui::subject(subject_of(&c.message))); |
| 690 |
Ok(0) |
| 691 |
} |
| 692 |
|
| 693 |
pub fn abandon(change: &str) -> Result<i32> { |
| 694 |
let repo = open()?; |
| 695 |
let h = repo.resolve_change(change)?; |
| 696 |
let c = repo.store().get_change(&h)?; |
| 697 |
let notes = repo.abandon(&h)?; |
| 698 |
println!("{} {} {} {}", ui::done("Abandoned"), ui::id(short_id(&c)), ui::hash(format!("#{}", h.short(12))), ui::dim("its content remains on disk as uncommitted work if the working copy sat on it")); |
| 699 |
for n in notes { |
| 700 |
println!(" {} {n}", ui::note()); |
| 701 |
} |
| 702 |
Ok(0) |
| 703 |
} |
| 704 |
|
| 705 |
pub fn checkpoint() -> Result<i32> { |
| 706 |
let repo = open()?; |
| 707 |
let sp = ui::spinner("Writing checkpoint…"); |
| 708 |
let (r, changes, bytes) = repo.checkpoint()?; |
| 709 |
sp.done(&format!("{} {} of {} ({} bytes) for {}", ui::done("Checkpoint"), ui::hash(format!("#{}", r.short(12))), plural(changes, "change"), bytes, ui::id(&repo.config.bookmark))); |
| 710 |
Ok(0) |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
pub fn prompt(format: &str, glyphs: bool, color: Option<&str>) -> Result<i32> { |
| 715 |
let Ok(repo) = Repo::open(&std::env::current_dir()?) else { return Ok(1) }; |
| 716 |
let info = repo.quick_status()?; |
| 717 |
let fmt = if glyphs && format == oxy_core::repo::DEFAULT_PROMPT_FORMAT { oxy_core::repo::GLYPH_PROMPT_FORMAT } else { format }; |
| 718 |
let line = match color { |
| 719 |
Some("bash") => info.format_colored(fmt, ("\\[", "\\]")), |
| 720 |
Some("zsh") => info.format_colored(fmt, ("%{", "%}")), |
| 721 |
Some(_) => info.format_colored(fmt, ("", "")), |
| 722 |
None => info.format(fmt), |
| 723 |
}; |
| 724 |
println!("{line}"); |
| 725 |
Ok(0) |
| 726 |
} |
| 727 |
|
| 728 |
pub fn sparse(paths: &[String]) -> Result<i32> { |
| 729 |
let mut repo = open()?; |
| 730 |
if paths.is_empty() { |
| 731 |
if repo.config.sparse_paths.is_empty() { |
| 732 |
println!("No sparse filter: the whole tree is materialized."); |
| 733 |
} else { |
| 734 |
println!("{}", ui::heading("Sparse filter")); |
| 735 |
for p in &repo.config.sparse_paths { |
| 736 |
println!(" {}", ui::path(p)); |
| 737 |
} |
| 738 |
} |
| 739 |
return Ok(0); |
| 740 |
} |
| 741 |
let snap = repo.snapshot("oxy sparse")?; |
| 742 |
if !snap.wc.tree_ops.is_empty() || !snap.wc.edits.is_empty() { |
| 743 |
anyhow::bail!("the working copy has changes; commit them before changing the sparse filter"); |
| 744 |
} |
| 745 |
repo.config.set("filter.paths", &paths.join(","))?; |
| 746 |
repo.save_config()?; |
| 747 |
let heads = repo.bookmark_heads()?; |
| 748 |
let sp = ui::spinner("Re-checking out with the new filter…"); |
| 749 |
let r = repo.checkout(&heads, true, "oxy sparse (checkout)")?; |
| 750 |
sp.done(&format!("{} {}: {} written, {} removed", ui::ok("Sparse filter set to"), ui::path(paths.join(",")), plural(r.written, "file"), r.removed)); |
| 751 |
Ok(0) |
| 752 |
} |
| 753 |
|
| 754 |
pub fn tag_create(name: &str, changes: &[String], message: &str) -> Result<i32> { |
| 755 |
let repo = open()?; |
| 756 |
repo.snapshot("oxy tag")?; |
| 757 |
let heads = if changes.is_empty() { |
| 758 |
repo.bookmark_heads()? |
| 759 |
} else { |
| 760 |
let mut heads = Vec::new(); |
| 761 |
for c in changes { |
| 762 |
heads.extend(repo.resolve_heads(c)?); |
| 763 |
} |
| 764 |
heads |
| 765 |
}; |
| 766 |
let t = repo.tag_create(name, &heads, message)?; |
| 767 |
println!( |
| 768 |
"{} {} {} {} ({}){}", |
| 769 |
ui::done("Tagged"), |
| 770 |
ui::bookmark(&t.name), |
| 771 |
ui::dim(ui::glyph("→", "->")), |
| 772 |
ui::hash(format!("#{}", t.state.short(12))), |
| 773 |
plural(t.heads.len(), "head"), |
| 774 |
t.signer.map(|s| format!(", signed by {s}")).unwrap_or_else(|| ui::dim(", unsigned")) |
| 775 |
); |
| 776 |
Ok(0) |
| 777 |
} |
| 778 |
|
| 779 |
pub fn tag_list() -> Result<i32> { |
| 780 |
let repo = open()?; |
| 781 |
let tags = repo.tags()?; |
| 782 |
if tags.is_empty() { |
| 783 |
println!("{}", ui::dim("No tags.")); |
| 784 |
} |
| 785 |
for t in tags { |
| 786 |
println!( |
| 787 |
"{} {} {}{}{}{}", |
| 788 |
ui::ok(ui::glyph("⚑", "#")), |
| 789 |
ui::bookmark(&t.name), |
| 790 |
ui::hash(format!("#{}", t.state.short(12))), |
| 791 |
ui::sep() + &ui::dim(plural(t.heads.len(), "head")), |
| 792 |
if t.message.is_empty() { String::new() } else { format!(" {}", ui::subject(subject_of(&t.message))) }, |
| 793 |
t.signer.map(|s| ui::sep() + &ui::ok(format!("{} {s}", ui::glyph("✔", "v")))).unwrap_or_default() |
| 794 |
); |
| 795 |
} |
| 796 |
Ok(0) |
| 797 |
} |
| 798 |
|
| 799 |
pub fn workspace_add(path: &std::path::Path, changes: &[String]) -> Result<i32> { |
| 800 |
let repo = open()?; |
| 801 |
let heads = if changes.is_empty() { |
| 802 |
None |
| 803 |
} else { |
| 804 |
let mut heads = Vec::new(); |
| 805 |
for c in changes { |
| 806 |
heads.extend(repo.resolve_heads(c)?); |
| 807 |
} |
| 808 |
Some(heads) |
| 809 |
}; |
| 810 |
let sp = ui::spinner(&format!("Creating workspace at {}…", path.display())); |
| 811 |
let ws = repo.workspace_add(path, heads.as_deref())?; |
| 812 |
sp.done(&format!("{} {} (workspace {})", ui::done("Workspace created at"), ui::path(ws.root().display()), ui::id(ws.workspace().short(8)))); |
| 813 |
Ok(0) |
| 814 |
} |
| 815 |
|
| 816 |
pub fn workspace_list() -> Result<i32> { |
| 817 |
let repo = open()?; |
| 818 |
for w in repo.workspaces()? { |
| 819 |
println!( |
| 820 |
"{} {} {}{}", |
| 821 |
if w.current { ui::ok(ui::glyph("●", "*")) } else { ui::dim(ui::glyph("○", "o")) }, |
| 822 |
ui::id(w.id.short(8)), |
| 823 |
ui::path(w.path.display()), |
| 824 |
w.working_copy.map(|h| ui::sep() + &ui::dim(format!("working copy #{}", h.short(12)))).unwrap_or_else(|| ui::sep() + &ui::warn("not in view")) |
| 825 |
); |
| 826 |
} |
| 827 |
Ok(0) |
| 828 |
} |
| 829 |
|
| 830 |
pub fn workspace_forget(spec: &str) -> Result<i32> { |
| 831 |
let repo = open()?; |
| 832 |
let all = repo.workspaces()?; |
| 833 |
let target = std::fs::canonicalize(spec).ok(); |
| 834 |
let matches: Vec<_> = all.iter().filter(|w| w.id.starts_with_hex(spec) || target.as_ref().is_some_and(|t| *t == w.path)).collect(); |
| 835 |
let w = match matches.as_slice() { |
| 836 |
[w] => *w, |
| 837 |
[] => anyhow::bail!("no workspace matches {spec:?}"), |
| 838 |
_ => anyhow::bail!("{spec:?} is ambiguous"), |
| 839 |
}; |
| 840 |
repo.workspace_forget(&w.id)?; |
| 841 |
println!("{} workspace {} at {}; its files were left in place.", ui::done("Forgot"), ui::id(w.id.short(8)), ui::path(w.path.display())); |
| 842 |
Ok(0) |
| 843 |
} |
| 844 |
|
| 845 |
pub fn lock(path: &str) -> Result<i32> { |
| 846 |
let repo = open()?; |
| 847 |
repo.lock_path(path)?; |
| 848 |
println!("{} {} {}", ui::done("Locked"), ui::path(path), ui::dim("(advisory, local)")); |
| 849 |
Ok(0) |
| 850 |
} |
| 851 |
|
| 852 |
pub fn unlock(path: &str) -> Result<i32> { |
| 853 |
let repo = open()?; |
| 854 |
repo.unlock_path(path)?; |
| 855 |
println!("{} {}", ui::done("Unlocked"), ui::path(path)); |
| 856 |
Ok(0) |
| 857 |
} |
| 858 |
|
| 859 |
pub fn split(change: &str, paths: &[String]) -> Result<i32> { |
| 860 |
let repo = open()?; |
| 861 |
let h = repo.resolve_change(change)?; |
| 862 |
let (h1, h2) = repo.split(&h, paths)?; |
| 863 |
let c1 = repo.store().get_change(&h1)?; |
| 864 |
let c2 = repo.store().get_change(&h2)?; |
| 865 |
println!("{} into {} {} and {} {}", ui::done("Split"), ui::id(short_id(&c1)), ui::hash(format!("#{}", h1.short(12))), ui::id(short_id(&c2)), ui::hash(format!("#{}", h2.short(12)))); |
| 866 |
Ok(0) |
| 867 |
} |
| 868 |
|
| 869 |
fn heads_from(repo: &Repo, changes: &[String]) -> Result<Vec<oxy_core::hash::ChangeHash>> { |
| 870 |
let mut heads = Vec::new(); |
| 871 |
for c in changes { |
| 872 |
heads.extend(repo.resolve_heads(c)?); |
| 873 |
} |
| 874 |
Ok(heads) |
| 875 |
} |
| 876 |
|
| 877 |
pub fn bookmark_create(name: &str, changes: &[String]) -> Result<i32> { |
| 878 |
let repo = open()?; |
| 879 |
repo.snapshot("oxy bookmark create")?; |
| 880 |
let heads = if changes.is_empty() { None } else { Some(heads_from(&repo, changes)?) }; |
| 881 |
let heads = repo.bookmark_create(name, heads.as_deref())?; |
| 882 |
println!("{} bookmark {} {} {}", ui::done("Created"), ui::bookmark(name), ui::dim(ui::glyph("→", "->")), ui::dim(plural(heads.len(), "head"))); |
| 883 |
println!(" {} {}", ui::note(), ui::dim(format!("switch to it with oxy bookmark switch {name}"))); |
| 884 |
Ok(0) |
| 885 |
} |
| 886 |
|
| 887 |
pub fn bookmark_set(name: &str, changes: &[String]) -> Result<i32> { |
| 888 |
let repo = open()?; |
| 889 |
repo.snapshot("oxy bookmark set")?; |
| 890 |
let heads = heads_from(&repo, changes)?; |
| 891 |
let heads = repo.bookmark_set(name, &heads)?; |
| 892 |
println!("{} bookmark {} {} {}", ui::done("Moved"), ui::bookmark(name), ui::dim(ui::glyph("→", "->")), ui::dim(plural(heads.len(), "head"))); |
| 893 |
Ok(0) |
| 894 |
} |
| 895 |
|
| 896 |
pub fn bookmark_delete(name: &str) -> Result<i32> { |
| 897 |
let repo = open()?; |
| 898 |
repo.bookmark_delete(name)?; |
| 899 |
println!("{} bookmark {} {}", ui::done("Deleted"), ui::bookmark(name), ui::dim("(its changes stay until unreachable)")); |
| 900 |
Ok(0) |
| 901 |
} |
| 902 |
|
| 903 |
pub fn bookmark_list(as_json: bool) -> Result<i32> { |
| 904 |
let repo = open()?; |
| 905 |
if as_json { |
| 906 |
let items = repo.bookmarks()?.into_iter().map(|b| { |
| 907 |
json::obj([ |
| 908 |
("name", json::s(&b.name)), |
| 909 |
("heads", json::hashes(&b.heads)), |
| 910 |
("current", json::b(b.current)), |
| 911 |
("ahead", json::opt(b.ahead, json::n)), |
| 912 |
("behind", json::opt(b.behind, json::n)), |
| 913 |
]) |
| 914 |
}); |
| 915 |
println!("{}", json::arr(items)); |
| 916 |
return Ok(0); |
| 917 |
} |
| 918 |
for b in repo.bookmarks()? { |
| 919 |
let mark = if b.current { ui::ok(ui::glyph("●", "*")) } else { ui::dim(ui::glyph("○", "o")) }; |
| 920 |
let mut tail = String::new(); |
| 921 |
if let Some(n) = b.ahead.filter(|n| *n > 0) { |
| 922 |
tail += &ui::ok(format!(" {}{n}", ui::glyph("↑", "+"))); |
| 923 |
} |
| 924 |
if let Some(n) = b.behind.filter(|n| *n > 0) { |
| 925 |
tail += &ui::warn(format!(" {}{n}", ui::glyph("↓", "-"))); |
| 926 |
} |
| 927 |
let head = b.heads.first().map(|h| repo.store().get_change(h).map(|c| ui::subject(subject_of(&c.message))).unwrap_or_default()).unwrap_or_else(|| ui::dim("(empty)")); |
| 928 |
println!("{mark} {}{}{}{tail} {head}", ui::bookmark(&b.name), ui::sep(), ui::dim(plural(b.heads.len(), "head"))); |
| 929 |
} |
| 930 |
Ok(0) |
| 931 |
} |
| 932 |
|
| 933 |
pub fn bookmark_switch(name: &str, discard: bool) -> Result<i32> { |
| 934 |
let mut repo = open()?; |
| 935 |
let sp = ui::spinner(&format!("Switching to {name}…")); |
| 936 |
let r = repo.bookmark_switch(name, discard)?; |
| 937 |
sp.done(&format!("{} to {}: {} written, {} removed", ui::done("Switched"), ui::bookmark(name), plural(r.written, "file"), r.removed)); |
| 938 |
if r.conflicted { |
| 939 |
println!(" {} {}", ui::caution(), ui::warn("this state has conflicts; see oxy status")); |
| 940 |
} |
| 941 |
Ok(0) |
| 942 |
} |
| 943 |
|
| 944 |
pub fn merge(bookmark: &str) -> Result<i32> { |
| 945 |
let repo = open()?; |
| 946 |
let sp = ui::spinner(&format!("Merging {bookmark}…")); |
| 947 |
let r = repo.merge(bookmark)?; |
| 948 |
if !r.changed { |
| 949 |
sp.done(&format!("{} {}", ui::ok(ui::glyph("✔", "*")), ui::dim(format!("{} already contains {bookmark}", repo.config.bookmark)))); |
| 950 |
return Ok(0); |
| 951 |
} |
| 952 |
sp.done(&format!( |
| 953 |
"{} {} into {}: now {}{}", |
| 954 |
ui::done("Merged"), |
| 955 |
ui::bookmark(bookmark), |
| 956 |
ui::bookmark(&repo.config.bookmark), |
| 957 |
plural(r.heads.len(), "head"), |
| 958 |
ui::dim(if r.checked_out { "; working copy updated" } else { "; run oxy checkout to update the working copy" }) |
| 959 |
)); |
| 960 |
let snap = repo.snapshot("oxy merge")?; |
| 961 |
if snap.base_snapshot.is_conflicted() { |
| 962 |
println!(" {} {}", ui::caution(), ui::warn("the result has conflicts; see oxy status")); |
| 963 |
} |
| 964 |
Ok(0) |
| 965 |
} |
| 966 |
|
| 967 |
pub fn squash(change: &str, into: &str) -> Result<i32> { |
| 968 |
let repo = open()?; |
| 969 |
let c = repo.resolve_change(change)?; |
| 970 |
let d = repo.resolve_change(into)?; |
| 971 |
let sp = ui::spinner("Squashing…"); |
| 972 |
let h = repo.squash(&c, &d)?; |
| 973 |
let new = repo.store().get_change(&h)?; |
| 974 |
sp.done(&format!("{} into {} {} {}", ui::done("Squashed"), ui::id(short_id(&new)), ui::hash(format!("#{}", h.short(12))), ui::subject(subject_of(&new.message)))); |
| 975 |
Ok(0) |
| 976 |
} |
| 977 |
|
| 978 |
pub fn rebase(change: &str, onto: &[String]) -> Result<i32> { |
| 979 |
let repo = open()?; |
| 980 |
let c = repo.resolve_change(change)?; |
| 981 |
let mut heads = Vec::new(); |
| 982 |
for o in onto { |
| 983 |
heads.extend(repo.resolve_heads(o)?); |
| 984 |
} |
| 985 |
let sp = ui::spinner("Rebasing…"); |
| 986 |
let h = repo.rebase(&c, &heads)?; |
| 987 |
let new = repo.store().get_change(&h)?; |
| 988 |
sp.done(&format!("{} {} {} {}", ui::done("Rebased"), ui::id(short_id(&new)), ui::hash(format!("#{}", h.short(12))), ui::dim(format!("now depends on {}", plural(new.deps.len(), "change"))))); |
| 989 |
Ok(0) |
| 990 |
} |
| 991 |
|