| 1 | |
|
| 2 | |
|
| 3 | | use super::format::plural; |
| 4 | | use super::json; |
| 5 | | use super::open; |
| 6 | | use super::ui; |
| 7 | | use anyhow::{Context, Result}; |
| 8 | | use oxy_core::repo::Repo; |
| 9 | | use std::path::PathBuf; |
| 10 | |
|
| 11 | 5× | pub fn clone( |
| 12 | 5× | url: &str, |
| 13 | 5× | dest: Option<PathBuf>, |
| 14 | 5× | max_blob: Option<&str>, |
| 15 | 5× | sparse: &[String], |
| 16 | 5× | depth: Option<u32>, |
| 17 | 5× | recursive: bool, |
| 18 | 5× | ) -> Result<i32> { |
| 19 | 5× | let dest = dest.unwrap_or_else(|| { |
| 20 | 0× | let base = url |
| 21 | 0× | .trim_end_matches('/') |
| 22 | 0× | .rsplit('/') |
| 23 | 0× | .next() |
| 24 | 0× | .unwrap_or("repo"); |
| 25 | 0× | PathBuf::from(base) |
| 26 | 0× | }); |
| 27 | 5× | let max_blob = max_blob.map(oxy_core::repo::parse_size).transpose()?; |
| 28 | 5× | let sp = ui::spinner(&format!("Cloning {url}…")); |
| 29 | 5× | let (repo, skipped) = Repo::clone_reporting(url, &dest, max_blob, sparse, depth)?; |
| 30 | 5× | let heads = repo.bookmark_heads()?; |
| 31 | 5× | sp.done(&format!( |
| 32 | 5× | "{} into {} {}", |
| 33 | 5× | ui::done("Cloned"), |
| 34 | 5× | ui::path(repo.root().display()), |
| 35 | 5× | ui::dim(format!( |
| 36 | 5× | "({url}, {} with {})", |
| 37 | 5× | ui::bookmark(&repo.config.bookmark), |
| 38 | 5× | plural(heads.len(), "head") |
| 39 | 5× | )) |
| 40 | 5× | )); |
| 41 | 5× | warn_skipped(&skipped); |
| 42 | 5× | let others = repo.remote_only_bookmarks()?.len(); |
| 43 | 5× | if others > 0 { |
| 44 | 0× | println!( |
| 45 | 0× | " {} {}", |
| 46 | 0× | ui::note(), |
| 47 | 0× | ui::dim(format!( |
| 48 | 0× | "origin has {} more: oxy bookmark list --all, then oxy bookmark switch NAME", |
| 49 | 0× | plural(others, "bookmark") |
| 50 | 0× | )) |
| 51 | 0× | ); |
| 52 | 5× | } |
| 53 | 5× | let subrepos = repo.subrepos()?.len(); |
| 54 | 5× | if subrepos == 0 { |
| 55 | 5× | return Ok(0); |
| 56 | 0× | } |
| 57 | 0× | if recursive { |
| 58 | 0× | let sp = ui::spinner("Cloning subrepos…"); |
| 59 | 0× | let results = repo.submodule_update(true, &[])?; |
| 60 | 0× | sp.clear(); |
| 61 | 0× | return Ok(print_subrepo_results(results)); |
| 62 | 0× | } |
| 63 | 0× | println!( |
| 64 | | " {} {}", |
| 65 | 0× | ui::note(), |
| 66 | 0× | ui::dim(format!( |
| 67 | | "{} not cloned: oxy submodule update --init (or clone with --recursive)", |
| 68 | 0× | plural(subrepos, "subrepo") |
| 69 | | )) |
| 70 | | ); |
| 71 | 0× | Ok(0) |
| 72 | 5× | } |
| 73 | |
|
| 74 | |
|
| 75 | 5× | pub fn subrepos_behind(repo: &Repo) { |
| 76 | 5× | let Ok(subs) = repo.subrepos() else { |
| 77 | 0× | return; |
| 78 | | }; |
| 79 | 5× | let stale = subs |
| 80 | 5× | .iter() |
| 81 | 5× | .filter(|s| s.committed && s.actual.as_ref() != Some(&s.pin)) |
| 82 | 5× | .count(); |
| 83 | 5× | if stale > 0 { |
| 84 | 0× | println!( |
| 85 | | " {} {}", |
| 86 | 0× | ui::note(), |
| 87 | 0× | ui::dim(format!( |
| 88 | | "{} not at {} pin: oxy submodule update --init", |
| 89 | 0× | plural(stale, "subrepo"), |
| 90 | 0× | if stale == 1 { "its" } else { "their" } |
| 91 | | )) |
| 92 | | ); |
| 93 | 5× | } |
| 94 | 5× | } |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | 2× | fn is_store_url(url: &str) -> bool { |
| 99 | 2× | ["s3://", "gs://", "az://", "dir://"] |
| 100 | 2× | .iter() |
| 101 | 5× | .any(|p| url.starts_with(p)) |
| 102 | 2× | } |
| 103 | |
|
| 104 | |
|
| 105 | 2× | fn encode_param(v: &str) -> String { |
| 106 | 2× | let mut out = String::with_capacity(v.len()); |
| 107 | 13× | for b in v.bytes() { |
| 108 | 13× | match b { |
| 109 | 10× | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' | b':' => { |
| 110 | 13× | out.push(b as char) |
| 111 | | } |
| 112 | 0× | _ => out.push_str(&format!("%{b:02X}")), |
| 113 | | } |
| 114 | | } |
| 115 | 2× | out |
| 116 | 2× | } |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | 4× | fn url_with_settings(url: &str, settings: &[(&str, Option<String>)]) -> Result<String> { |
| 121 | 4× | let given: Vec<(&str, &str)> = settings |
| 122 | 4× | .iter() |
| 123 | 16× | .filter_map(|(k, v)| v.as_deref().map(|v| (*k, v))) |
| 124 | 4× | .collect(); |
| 125 | 4× | if given.is_empty() { |
| 126 | 2× | return Ok(url.to_string()); |
| 127 | 2× | } |
| 128 | 2× | if !is_store_url(url) || url.starts_with("dir://") { |
| 129 | 1× | let names: Vec<String> = given.iter().map(|(k, _)| format!("--{k}")).collect(); |
| 130 | 1× | anyhow::bail!( |
| 131 | | "{} apply to a store remote (s3://, gs://, az://), not to {url}", |
| 132 | 1× | names.join(", ") |
| 133 | | ); |
| 134 | 1× | } |
| 135 | 1× | let mut out = url.to_string(); |
| 136 | 2× | for (k, v) in given { |
| 137 | 2× | out.push(if out.contains('?') { '&' } else { '?' }); |
| 138 | 2× | out.push_str(k); |
| 139 | 2× | out.push('='); |
| 140 | 2× | out.push_str(&encode_param(v)); |
| 141 | | } |
| 142 | 1× | Ok(out) |
| 143 | 4× | } |
| 144 | |
|
| 145 | 4× | pub fn remote_add(name: &str, url: &str, settings: &[(&str, Option<String>)]) -> Result<i32> { |
| 146 | 4× | let mut repo = open()?; |
| 147 | 4× | let url = url_with_settings(url, settings)?; |
| 148 | 3× | let url = url.as_str(); |
| 149 | 3× | repo.config.set( |
| 150 | 3× | &format!("remote.{name}.url"), |
| 151 | 3× | &oxy_core::protocol::stored_url(url), |
| 152 | 0× | )?; |
| 153 | 3× | repo.save_config()?; |
| 154 | 3× | println!( |
| 155 | | "{} remote {} {} {url}", |
| 156 | 3× | ui::done("Added"), |
| 157 | 3× | ui::bookmark(name), |
| 158 | 3× | ui::dim(ui::glyph("→", "->")) |
| 159 | | ); |
| 160 | 3× | Ok(0) |
| 161 | 4× | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | 3× | pub fn remote_check(name: &str) -> Result<i32> { |
| 167 | 3× | let repo = open()?; |
| 168 | 3× | let Some(url) = repo.config.remotes.get(name).cloned() else { |
| 169 | 0× | anyhow::bail!("no remote {name:?}; oxy remote list shows the remotes there are"); |
| 170 | | }; |
| 171 | 3× | let options = repo.options_for_url(&url); |
| 172 | 3× | let mine = repo.id()?; |
| 173 | 3× | let started = std::time::Instant::now(); |
| 174 | 3× | let sp = ui::spinner(&format!("Checking {name}…")); |
| 175 | 3× | let Some(store) = oxy_core::protocol::open_store(&url, &options) else { |
| 176 | | |
| 177 | 0× | let (_, remote) = repo.open_remote_at(&url)?; |
| 178 | 0× | let id = remote.repo_id()?; |
| 179 | 0× | let refs = remote.refs()?; |
| 180 | 0× | sp.done(&format!( |
| 181 | 0× | "{} {} {}", |
| 182 | 0× | ui::done("Reachable"), |
| 183 | 0× | ui::bookmark(name), |
| 184 | 0× | ui::dim(format!("({url}, {} ms)", started.elapsed().as_millis())) |
| 185 | 0× | )); |
| 186 | 0× | describe_holding(id, mine, refs.refs.len(), refs.tags.len(), None); |
| 187 | 0× | return Ok(0); |
| 188 | | }; |
| 189 | 3× | let store = store.with_context(|| format!("opening the store of {name}"))?; |
| 190 | | |
| 191 | 3× | let marker = match store.get("oxygen.toml") { |
| 192 | 2× | Ok(b) => Some(b), |
| 193 | 1× | Err(oxy_core::strata::Error::NotFound(_)) => None, |
| 194 | 0× | Err(e) => { |
| 195 | 0× | sp.clear(); |
| 196 | 0× | return Err(anyhow::anyhow!("{e}")) |
| 197 | 0× | .with_context(|| format!("reaching the store of {name}")); |
| 198 | | } |
| 199 | | }; |
| 200 | 3× | let reached = started.elapsed(); |
| 201 | 3× | let bundles = store.list("bundles/")?.len(); |
| 202 | 3× | let record = store.read_ref("refs")?; |
| 203 | 3× | sp.done(&format!( |
| 204 | 3× | "{} {} {}", |
| 205 | 3× | ui::done("Reachable"), |
| 206 | 3× | ui::bookmark(name), |
| 207 | 3× | ui::dim(format!("({url}, {} ms)", reached.as_millis())) |
| 208 | 3× | )); |
| 209 | 3× | match marker { |
| 210 | 1× | None => println!( |
| 211 | | " {} {}", |
| 212 | 1× | ui::note(), |
| 213 | 1× | ui::dim("holds no repository yet: oxy push NAME binds it to this one") |
| 214 | | ), |
| 215 | | Some(_) => { |
| 216 | 2× | let (id, bookmarks, tags) = match record { |
| 217 | 2× | Some((bytes, _)) => { |
| 218 | 2× | let (id, resp) = oxy_core::protocol::wire::decode_refs(&bytes)?; |
| 219 | 2× | (id, resp.refs.len(), resp.tags.len()) |
| 220 | | } |
| 221 | 0× | None => (mine, 0, 0), |
| 222 | | }; |
| 223 | 2× | describe_holding(id, mine, bookmarks, tags, Some(bundles)); |
| 224 | | } |
| 225 | | } |
| 226 | | |
| 227 | 3× | let sp = ui::spinner("Checking that the store behaves…"); |
| 228 | 3× | let probe = format!("oxy-check-{}", std::process::id()); |
| 229 | 3× | let began = std::time::Instant::now(); |
| 230 | 3× | match oxy_core::strata::conformance(store.as_ref(), &probe) { |
| 231 | 3× | Ok(passed) => { |
| 232 | 3× | sp.clear(); |
| 233 | 24× | for p in &passed { |
| 234 | 24× | println!(" {} {}", ui::ok(ui::glyph("✔", "*")), p); |
| 235 | 24× | } |
| 236 | 3× | println!( |
| 237 | | "{} {}", |
| 238 | 3× | ui::done("Behaves"), |
| 239 | 3× | ui::dim(format!( |
| 240 | | "({} checks in {} ms; nothing left behind)", |
| 241 | 3× | passed.len(), |
| 242 | 3× | began.elapsed().as_millis() |
| 243 | | )) |
| 244 | | ); |
| 245 | 3× | Ok(0) |
| 246 | | } |
| 247 | 0× | Err(e) => { |
| 248 | 0× | sp.clear(); |
| 249 | | |
| 250 | 0× | if let Ok(left) = store.list(&probe) { |
| 251 | 0× | for entry in left { |
| 252 | 0× | let _ = store.delete(&entry.key); |
| 253 | 0× | } |
| 254 | 0× | } |
| 255 | 0× | println!("{} {}", ui::err(ui::glyph("✘", "x")), e); |
| 256 | 0× | println!( |
| 257 | | " {} {}", |
| 258 | 0× | ui::note(), |
| 259 | 0× | ui::dim("a store that fails this cannot keep a repository safely; check the service and the credentials' permissions") |
| 260 | | ); |
| 261 | 0× | Ok(1) |
| 262 | | } |
| 263 | | } |
| 264 | 3× | } |
| 265 | |
|
| 266 | 2× | fn describe_holding( |
| 267 | 2× | id: [u8; 16], |
| 268 | 2× | mine: [u8; 16], |
| 269 | 2× | bookmarks: usize, |
| 270 | 2× | tags: usize, |
| 271 | 2× | bundles: Option<usize>, |
| 272 | 2× | ) { |
| 273 | 12× | let short: String = id[..6].iter().map(|b| format!("{b:02x}")).collect(); |
| 274 | 2× | let mut what = format!( |
| 275 | | "repository {short}: {} and {}", |
| 276 | 2× | plural(bookmarks, "bookmark"), |
| 277 | 2× | plural(tags, "tag") |
| 278 | | ); |
| 279 | 2× | if let Some(b) = bundles { |
| 280 | 2× | what.push_str(&format!(" in {}", plural(b, "bundle"))); |
| 281 | 2× | } |
| 282 | 2× | if id == mine { |
| 283 | 1× | println!(" {} {}", ui::note(), ui::dim(format!("holds this {what}"))); |
| 284 | 1× | } else if id == oxy_core::protocol::UNBOUND_ID { |
| 285 | 0× | println!(" {} {}", ui::note(), ui::dim("holds no repository yet")); |
| 286 | 1× | } else { |
| 287 | 1× | println!( |
| 288 | 1× | " {} {}", |
| 289 | 1× | ui::caution(), |
| 290 | 1× | ui::warn(format!("holds another {what}; a push would be refused")) |
| 291 | 1× | ); |
| 292 | 1× | } |
| 293 | 2× | } |
| 294 | |
|
| 295 | 1× | pub fn remote_remove(name: &str) -> Result<i32> { |
| 296 | 1× | let mut repo = open()?; |
| 297 | 1× | repo.remove_remote(name)?; |
| 298 | 1× | println!("{} remote {}", ui::done("Removed"), ui::bookmark(name)); |
| 299 | 1× | Ok(0) |
| 300 | 1× | } |
| 301 | |
|
| 302 | 2× | pub fn remote_list(as_json: bool) -> Result<i32> { |
| 303 | 2× | let repo = open()?; |
| 304 | 2× | let view = repo.view()?; |
| 305 | 2× | if as_json { |
| 306 | 1× | let items = repo.config.remotes.iter().map(|(name, url)| { |
| 307 | 1× | let bms = view.remotes.get(name).map(|b| { |
| 308 | 1× | json::arr(b.iter().map(|(bm, st)| { |
| 309 | 1× | let heads = repo |
| 310 | 1× | .store() |
| 311 | 1× | .get_state(st) |
| 312 | 1× | .map(|s| s.heads) |
| 313 | 1× | .unwrap_or_default(); |
| 314 | 1× | json::obj([("name", json::s(bm)), ("heads", json::hashes(&heads))]) |
| 315 | 1× | })) |
| 316 | 1× | }); |
| 317 | 1× | json::obj([ |
| 318 | 1× | ("name", json::s(name)), |
| 319 | 1× | ("url", json::s(url)), |
| 320 | | |
| 321 | | |
| 322 | | ( |
| 323 | 1× | "ca", |
| 324 | 1× | json::opt(repo.config.remote_cas.get(name), |c| json::s(c)), |
| 325 | | ), |
| 326 | 1× | ("bookmarks", bms.unwrap_or_else(|| "[]".into())), |
| 327 | | ]) |
| 328 | 1× | }); |
| 329 | 1× | println!("{}", json::arr(items)); |
| 330 | 1× | return Ok(0); |
| 331 | 1× | } |
| 332 | 1× | if repo.config.remotes.is_empty() { |
| 333 | 0× | println!( |
| 334 | 0× | "{}", |
| 335 | 0× | ui::dim("No remotes; add one with oxy remote add <name> <url>.") |
| 336 | 0× | ); |
| 337 | 1× | } |
| 338 | 1× | for (name, url) in &repo.config.remotes { |
| 339 | 1× | println!( |
| 340 | | "{} {}{}{}", |
| 341 | 1× | ui::ok(ui::glyph("⇄", "<>")), |
| 342 | 1× | ui::bookmark(name), |
| 343 | 1× | ui::sep(), |
| 344 | 1× | ui::dim(url) |
| 345 | | ); |
| 346 | 1× | if let Some(bms) = view.remotes.get(name) { |
| 347 | 0× | for (bm, st) in bms { |
| 348 | 0× | let heads = repo |
| 349 | 0× | .store() |
| 350 | 0× | .get_state(st) |
| 351 | 0× | .map(|s| s.heads.len()) |
| 352 | 0× | .unwrap_or(0); |
| 353 | 0× | println!( |
| 354 | | " {} {}{}{}", |
| 355 | 0× | ui::dim(ui::glyph("↳", "->")), |
| 356 | 0× | ui::id(format!("{name}/{bm}")), |
| 357 | 0× | ui::sep(), |
| 358 | 0× | ui::dim(plural(heads, "head")) |
| 359 | | ); |
| 360 | | } |
| 361 | 1× | } |
| 362 | | } |
| 363 | 1× | Ok(0) |
| 364 | 2× | } |
| 365 | |
|
| 366 | |
|
| 367 | 12× | fn remote_name(repo: &oxy_core::repo::Repo, remote: Option<&str>) -> String { |
| 368 | 12× | remote.map_or_else(|| repo.default_remote(), str::to_string) |
| 369 | 12× | } |
| 370 | |
|
| 371 | 1× | pub fn fetch(remote: Option<&str>, as_json: bool) -> Result<i32> { |
| 372 | 1× | let repo = open()?; |
| 373 | 1× | let remote = remote_name(&repo, remote); |
| 374 | 1× | let sp = ui::spinner(&format!("Fetching from {remote}…")); |
| 375 | 1× | let report = noted(&repo, &remote, repo.fetch(&remote))?; |
| 376 | 1× | sp.clear(); |
| 377 | 1× | if as_json { |
| 378 | 1× | println!( |
| 379 | | "{}", |
| 380 | 1× | json::obj([ |
| 381 | 1× | ("remote", json::s(&remote)), |
| 382 | | ( |
| 383 | 1× | "refs", |
| 384 | 1× | json::arr(report.refs.iter().map(|(name, heads, n)| { |
| 385 | 1× | let t = repo.tracking(&remote, name).ok().flatten(); |
| 386 | 1× | json::obj([ |
| 387 | 1× | ("name", json::s(name)), |
| 388 | 1× | ("heads", json::hashes(heads)), |
| 389 | 1× | ("new", json::n(*n)), |
| 390 | | ( |
| 391 | 1× | "ahead", |
| 392 | 1× | t.map_or_else(|| "null".into(), |(a, _)| json::n(a)), |
| 393 | | ), |
| 394 | | ( |
| 395 | 1× | "behind", |
| 396 | 1× | t.map_or_else(|| "null".into(), |(_, b)| json::n(b)), |
| 397 | | ), |
| 398 | | ]) |
| 399 | 1× | })) |
| 400 | | ), |
| 401 | 1× | ("new_tags", json::n(report.new_tags)), |
| 402 | | ( |
| 403 | 1× | "withdrawn_tags", |
| 404 | 1× | json::arr(report.withdrawn_tags.iter().map(|n| json::s(n))), |
| 405 | | ), |
| 406 | 1× | ("unverified", json::b(report.unverified)), |
| 407 | | ( |
| 408 | 1× | "log_notes", |
| 409 | 1× | json::arr(report.log_notes.iter().map(|n| json::s(n))), |
| 410 | | ), |
| 411 | 1× | ("skipped", skipped_json(&report.skipped)), |
| 412 | | ]) |
| 413 | | ); |
| 414 | 1× | return Ok(0); |
| 415 | 0× | } |
| 416 | 0× | if report.refs.is_empty() { |
| 417 | 0× | println!( |
| 418 | 0× | "{} {}", |
| 419 | 0× | ui::done("Fetched"), |
| 420 | 0× | ui::dim(format!("nothing new from {remote}")) |
| 421 | 0× | ); |
| 422 | 0× | } |
| 423 | 0× | for (name, heads, n) in &report.refs { |
| 424 | | |
| 425 | | |
| 426 | 0× | let fetched = |
| 427 | 0× | (*n > 0).then(|| format!("{}{}", ui::ok(plural(*n, "new change")), ui::sep())); |
| 428 | 0× | let them = |k: usize| if k == 1 { "it" } else { "them" }; |
| 429 | 0× | let standing = match repo.tracking(&remote, name).ok().flatten() { |
| 430 | 0× | None => ui::dim(format!("no local bookmark {name}")), |
| 431 | 0× | Some((0, 0)) => ui::dim(format!("{name} up to date")), |
| 432 | 0× | Some((0, b)) if *name == repo.config.bookmark => ui::warn(format!( |
| 433 | | "{name} is {} behind: oxy pull to take {}", |
| 434 | 0× | plural(b, "change"), |
| 435 | 0× | them(b) |
| 436 | | )), |
| 437 | 0× | Some((0, b)) => ui::warn(format!( |
| 438 | | "{name} is {} behind: oxy bookmark switch {name} then oxy pull to take {}", |
| 439 | 0× | plural(b, "change"), |
| 440 | 0× | them(b) |
| 441 | | )), |
| 442 | 0× | Some((a, 0)) => ui::dim(format!( |
| 443 | | "{name} is {} ahead: oxy push to send {}", |
| 444 | 0× | plural(a, "change"), |
| 445 | 0× | them(a) |
| 446 | | )), |
| 447 | 0× | Some((a, b)) => ui::warn(format!( |
| 448 | | "{name} and {remote}/{name} have diverged ({a} here, {b} there): oxy pull to merge them" |
| 449 | | )), |
| 450 | | }; |
| 451 | 0× | let what = format!("{}{standing}", fetched.unwrap_or_default()); |
| 452 | 0× | println!( |
| 453 | | "{} {}{}{}{}{what}", |
| 454 | 0× | ui::done("Fetched"), |
| 455 | 0× | ui::id(format!("{remote}/{name}")), |
| 456 | 0× | ui::sep(), |
| 457 | 0× | ui::dim(plural(heads.len(), "head")), |
| 458 | 0× | ui::sep() |
| 459 | | ); |
| 460 | | } |
| 461 | 0× | if report.new_tags > 0 { |
| 462 | 0× | println!( |
| 463 | 0× | " {} {}", |
| 464 | 0× | ui::note(), |
| 465 | 0× | ui::dim(format!("{} adopted", plural(report.new_tags, "tag"))) |
| 466 | 0× | ); |
| 467 | 0× | } |
| 468 | 0× | for t in &report.withdrawn_tags { |
| 469 | 0× | println!( |
| 470 | 0× | " {} {}", |
| 471 | 0× | ui::note(), |
| 472 | 0× | ui::dim(format!("tag {t} was withdrawn on {remote}, and here")) |
| 473 | 0× | ); |
| 474 | 0× | } |
| 475 | 0× | if report.unverified { |
| 476 | 0× | eprintln!( |
| 477 | 0× | "{} {remote} has no trust policy; nothing fetched from it was verified", |
| 478 | 0× | ui::caution() |
| 479 | 0× | ); |
| 480 | 0× | } |
| 481 | 0× | for n in &report.log_notes { |
| 482 | 0× | eprintln!("{} {n}", ui::caution()); |
| 483 | 0× | } |
| 484 | 0× | warn_skipped(&report.skipped); |
| 485 | 0× | Ok(0) |
| 486 | 1× | } |
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | 8× | fn warn_skipped(skipped: &[(String, String)]) { |
| 491 | 8× | for (name, why) in skipped { |
| 492 | 0× | eprintln!( |
| 493 | 0× | "{} {} {}", |
| 494 | 0× | ui::caution(), |
| 495 | 0× | ui::warn(format!("{name} not updated:")), |
| 496 | 0× | ui::dim(why) |
| 497 | 0× | ); |
| 498 | 0× | } |
| 499 | 8× | } |
| 500 | |
|
| 501 | 2× | fn skipped_json(skipped: &[(String, String)]) -> String { |
| 502 | 2× | json::arr( |
| 503 | 2× | skipped |
| 504 | 2× | .iter() |
| 505 | 2× | .map(|(name, why)| json::obj([("name", json::s(name)), ("reason", json::s(why))])), |
| 506 | | ) |
| 507 | 2× | } |
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | 3× | fn others_behind(repo: &oxy_core::repo::Repo, remote: &str) { |
| 513 | 3× | let Ok(bookmarks) = repo.bookmarks() else { |
| 514 | 0× | return; |
| 515 | | }; |
| 516 | 4× | for b in bookmarks { |
| 517 | 4× | let Some(n) = b.behind.filter(|n| *n > 0) else { |
| 518 | 4× | continue; |
| 519 | | }; |
| 520 | 0× | if b.current { |
| 521 | 0× | continue; |
| 522 | 0× | } |
| 523 | 0× | println!( |
| 524 | | " {} {}", |
| 525 | 0× | ui::note(), |
| 526 | 0× | ui::dim(format!( |
| 527 | | "{} is {} behind {remote}/{}: oxy bookmark switch {} then oxy pull to take {}", |
| 528 | | b.name, |
| 529 | 0× | plural(n, "change"), |
| 530 | | b.name, |
| 531 | | b.name, |
| 532 | 0× | if n == 1 { "it" } else { "them" } |
| 533 | | )) |
| 534 | | ); |
| 535 | | } |
| 536 | 3× | } |
| 537 | |
|
| 538 | 4× | pub fn pull(remote: Option<&str>, as_json: bool) -> Result<i32> { |
| 539 | 4× | let repo = open()?; |
| 540 | 4× | let remote = remote_name(&repo, remote); |
| 541 | 4× | let sp = ui::spinner(&format!("Pulling from {remote}…")); |
| 542 | 4× | let r = noted(&repo, &remote, repo.pull(&remote))?; |
| 543 | 4× | if as_json { |
| 544 | 1× | sp.clear(); |
| 545 | 1× | let conflicted = repo |
| 546 | 1× | .snapshot_status("oxy pull")? |
| 547 | | .base_snapshot |
| 548 | 1× | .is_conflicted(); |
| 549 | 1× | println!( |
| 550 | | "{}", |
| 551 | 1× | json::obj([ |
| 552 | 1× | ("remote", json::s(&remote)), |
| 553 | 1× | ("bookmark", json::s(&repo.config.bookmark)), |
| 554 | 1× | ("heads", json::hashes(&r.heads)), |
| 555 | 1× | ("changed", json::b(r.changed)), |
| 556 | 1× | ("checked_out", json::b(r.checked_out)), |
| 557 | 1× | ("conflicted", json::b(conflicted)), |
| 558 | 1× | ("others", json::arr(r.others.iter().map(|o| json::s(o)))), |
| 559 | 1× | ("skipped", skipped_json(&r.skipped)) |
| 560 | | ]) |
| 561 | | ); |
| 562 | 1× | return Ok(0); |
| 563 | 3× | } |
| 564 | 3× | if !r.changed { |
| 565 | 1× | sp.done(&format!( |
| 566 | | "{} {}{}", |
| 567 | 1× | ui::ok(ui::glyph("✔", "*")), |
| 568 | 1× | ui::dim(format!( |
| 569 | | "{} is up to date with {remote}.", |
| 570 | | repo.config.bookmark |
| 571 | | )), |
| 572 | 1× | if r.checked_out { |
| 573 | 0× | ui::dim(" The working copy had fallen behind it and is updated.") |
| 574 | | } else { |
| 575 | 1× | String::new() |
| 576 | | } |
| 577 | | )); |
| 578 | 1× | others_pulled(&r.others); |
| 579 | 1× | others_behind(&repo, &remote); |
| 580 | 1× | warn_skipped(&r.skipped); |
| 581 | 1× | if r.checked_out { |
| 582 | 0× | super::workspace::where_now(&repo); |
| 583 | 1× | } |
| 584 | 1× | return Ok(0); |
| 585 | 2× | } |
| 586 | 2× | sp.done(&format!( |
| 587 | | "{} {} now has {}{}", |
| 588 | 2× | ui::done("Pulled:"), |
| 589 | 2× | ui::bookmark(&repo.config.bookmark), |
| 590 | 2× | plural(r.heads.len(), "head"), |
| 591 | 2× | ui::dim(if r.checked_out { |
| 592 | 2× | "; working copy updated" |
| 593 | | } else { |
| 594 | 0× | "; run oxy checkout to update the working copy" |
| 595 | | }) |
| 596 | | )); |
| 597 | 2× | let snap = repo.snapshot("oxy pull")?; |
| 598 | 2× | if snap.base_snapshot.is_conflicted() { |
| 599 | 0× | println!( |
| 600 | 0× | " {} {}", |
| 601 | 0× | ui::caution(), |
| 602 | 0× | ui::warn("the result has conflicts; see oxy status") |
| 603 | 0× | ); |
| 604 | 2× | } |
| 605 | 2× | others_pulled(&r.others); |
| 606 | 2× | others_behind(&repo, &remote); |
| 607 | 2× | warn_skipped(&r.skipped); |
| 608 | 2× | subrepos_behind(&repo); |
| 609 | 2× | super::workspace::where_now(&repo); |
| 610 | 2× | Ok(0) |
| 611 | 4× | } |
| 612 | |
|
| 613 | |
|
| 614 | 3× | fn others_pulled(others: &[String]) { |
| 615 | 3× | if others.is_empty() { |
| 616 | 2× | return; |
| 617 | 1× | } |
| 618 | 1× | let shown: Vec<String> = others.iter().take(8).map(ui::bookmark).collect(); |
| 619 | 1× | let more = others.len().saturating_sub(8); |
| 620 | 1× | println!( |
| 621 | | " {} {}{}", |
| 622 | 1× | ui::done("Also updated"), |
| 623 | 1× | shown.join(", "), |
| 624 | 1× | if more > 0 { |
| 625 | 0× | ui::dim(format!(" and {more} more")) |
| 626 | | } else { |
| 627 | 1× | String::new() |
| 628 | | } |
| 629 | | ); |
| 630 | 3× | } |
| 631 | |
|
| 632 | |
|
| 633 | 0× | pub fn push_delete(remote: Option<&str>, name: &str, as_json: bool) -> Result<i32> { |
| 634 | 0× | let repo = open()?; |
| 635 | 0× | let remote = remote_name(&repo, remote); |
| 636 | 0× | let sp = ui::spinner(&format!("Deleting {remote}/{name}…")); |
| 637 | 0× | let results = repo.push_delete(&remote, name)?; |
| 638 | 0× | sp.clear(); |
| 639 | 0× | if as_json { |
| 640 | 0× | println!( |
| 641 | | "{}", |
| 642 | 0× | json::arr(results.iter().map(|r| { |
| 643 | 0× | json::obj([ |
| 644 | 0× | ("name", json::s(&r.name)), |
| 645 | 0× | ("ok", json::b(r.ok)), |
| 646 | 0× | ("reason", json::s(&r.reason)), |
| 647 | 0× | ("state", json::s("")), |
| 648 | 0× | ]) |
| 649 | 0× | })) |
| 650 | | ); |
| 651 | 0× | return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 }); |
| 652 | 0× | } |
| 653 | 0× | let mut code = 0; |
| 654 | 0× | for r in results { |
| 655 | 0× | if r.ok { |
| 656 | 0× | let done = if r.name.starts_with("tags/") { |
| 657 | 0× | "Withdrew" |
| 658 | | } else { |
| 659 | 0× | "Deleted" |
| 660 | | }; |
| 661 | 0× | println!( |
| 662 | | "{} {}", |
| 663 | 0× | ui::done(done), |
| 664 | 0× | ui::id(format!("{remote}/{}", r.name)) |
| 665 | | ); |
| 666 | 0× | } else { |
| 667 | 0× | println!( |
| 668 | 0× | "{} {} {}{}{}", |
| 669 | 0× | ui::err(ui::glyph("✖", "x")), |
| 670 | 0× | ui::err("Rejected"), |
| 671 | 0× | ui::id(format!("{remote}/{}", r.name)), |
| 672 | 0× | ui::sep(), |
| 673 | 0× | r.reason |
| 674 | 0× | ); |
| 675 | 0× | code = 1; |
| 676 | 0× | } |
| 677 | | } |
| 678 | 0× | Ok(code) |
| 679 | 0× | } |
| 680 | |
|
| 681 | 7× | pub fn push( |
| 682 | 7× | remote: Option<&str>, |
| 683 | 7× | force: bool, |
| 684 | 7× | bookmark: Option<&str>, |
| 685 | 7× | as_json: bool, |
| 686 | 7× | ) -> Result<i32> { |
| 687 | 7× | let repo = open()?; |
| 688 | 7× | let remote = remote_name(&repo, remote); |
| 689 | 7× | let sp = ui::spinner(&format!("Pushing to {remote}…")); |
| 690 | 7× | let bookmark_name = bookmark.map_or_else(|| repo.config.bookmark.clone(), String::from); |
| 691 | | |
| 692 | 7× | let results = noted( |
| 693 | 7× | &repo, |
| 694 | 7× | &remote, |
| 695 | 7× | repo.push_bookmark_with(&remote, &bookmark_name, force, &mut |m| { |
| 696 | 0× | sp.set(&format!("Pushing to {remote}… {m}")) |
| 697 | 0× | }), |
| 698 | 1× | )?; |
| 699 | 6× | sp.clear(); |
| 700 | 6× | if as_json { |
| 701 | | |
| 702 | | |
| 703 | | |
| 704 | 1× | let view = repo.view()?; |
| 705 | 1× | println!( |
| 706 | | "{}", |
| 707 | 1× | json::arr(results.iter().map(|r| { |
| 708 | 1× | let state = view |
| 709 | 1× | .bookmarks |
| 710 | 1× | .get(&r.name) |
| 711 | 1× | .filter(|_| r.ok) |
| 712 | 1× | .map(|st| st.to_hex()) |
| 713 | 1× | .unwrap_or_default(); |
| 714 | 1× | json::obj([ |
| 715 | 1× | ("name", json::s(&r.name)), |
| 716 | 1× | ("ok", json::b(r.ok)), |
| 717 | 1× | ("reason", json::s(&r.reason)), |
| 718 | 1× | ("state", json::s(&state)), |
| 719 | 1× | ]) |
| 720 | 1× | })) |
| 721 | | ); |
| 722 | 1× | return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 }); |
| 723 | 5× | } |
| 724 | 5× | let mut code = 0; |
| 725 | 5× | for r in results { |
| 726 | 5× | if r.ok { |
| 727 | 5× | println!( |
| 728 | | "{} {}{}{}", |
| 729 | 5× | ui::done("Pushed"), |
| 730 | 5× | ui::id(format!("{remote}/{}", r.name)), |
| 731 | 5× | ui::sep(), |
| 732 | 5× | ui::dim(if r.reason.is_empty() { |
| 733 | 5× | "updated" |
| 734 | | } else { |
| 735 | 0× | r.reason.as_str() |
| 736 | | }) |
| 737 | | ); |
| 738 | 0× | } else { |
| 739 | 0× | println!( |
| 740 | 0× | "{} {} {}{}{}", |
| 741 | 0× | ui::err(ui::glyph("✖", "x")), |
| 742 | 0× | ui::err("Rejected"), |
| 743 | 0× | ui::id(format!("{remote}/{}", r.name)), |
| 744 | 0× | ui::sep(), |
| 745 | 0× | r.reason |
| 746 | 0× | ); |
| 747 | 0× | code = 1; |
| 748 | 0× | } |
| 749 | | } |
| 750 | 5× | Ok(code) |
| 751 | 7× | } |
| 752 | |
|
| 753 | 0× | pub fn serve( |
| 754 | 0× | root: PathBuf, |
| 755 | 0× | bind: &str, |
| 756 | 0× | tokens: Vec<String>, |
| 757 | 0× | tls: Option<(PathBuf, PathBuf)>, |
| 758 | 0× | ) -> Result<i32> { |
| 759 | 0× | let listener = std::net::TcpListener::bind(bind).with_context(|| format!("binding {bind}"))?; |
| 760 | 0× | let root = oxy_core::portable::canonical(&root)?; |
| 761 | 0× | let tls = tls.map(|(cert, key)| oxy_server::TlsFiles { cert, key }); |
| 762 | 0× | println!( |
| 763 | | "Serving repositories under {} on {}://{bind}/<repo>/oxy/v1/ ({})", |
| 764 | 0× | root.display(), |
| 765 | 0× | if tls.is_some() { "https" } else { "http" }, |
| 766 | 0× | if tokens.is_empty() { |
| 767 | 0× | "open: no push tokens configured" |
| 768 | | } else { |
| 769 | 0× | "push requires a token" |
| 770 | | } |
| 771 | | ); |
| 772 | 0× | if tls.is_none() |
| 773 | 0× | && !bind.starts_with("127.") |
| 774 | 0× | && !bind.starts_with("localhost") |
| 775 | 0× | && !bind.starts_with("[::1]") |
| 776 | 0× | { |
| 777 | 0× | eprintln!("warning: serving plain HTTP on a non-loopback address; put a TLS terminator in front or pass --tls-cert/--tls-key"); |
| 778 | 0× | } |
| 779 | 0× | oxy_server::run_blocking(listener, oxy_server::ServerConfig { root, tokens, tls })?; |
| 780 | 0× | Ok(0) |
| 781 | 0× | } |
| 782 | |
|
| 783 | |
|
| 784 | 2× | pub fn git_colocate(off: bool) -> Result<i32> { |
| 785 | 2× | let mut repo = open()?; |
| 786 | 2× | if repo.is_bare() { |
| 787 | 0× | anyhow::bail!("a bare repository has no working copy for a .git beside it"); |
| 788 | 2× | } |
| 789 | 2× | repo.config |
| 790 | 2× | .set("git.colocate", if off { "false" } else { "true" })?; |
| 791 | 2× | repo.save_config()?; |
| 792 | 2× | if off { |
| 793 | 1× | println!( |
| 794 | | "{} the .git beside this checkout is no longer updated {}", |
| 795 | 1× | ui::done("Stopped:"), |
| 796 | 1× | ui::dim("(it is left as it is: delete .git yourself if you want it gone)") |
| 797 | | ); |
| 798 | 1× | return Ok(0); |
| 799 | 1× | } |
| 800 | 1× | let sp = ui::spinner("Exporting to .git…"); |
| 801 | 1× | repo.git_colocate_sync()?; |
| 802 | 1× | sp.clear(); |
| 803 | 1× | println!( |
| 804 | | "{} .git beside .oxy holds {} {}", |
| 805 | 1× | ui::done("Colocated:"), |
| 806 | 1× | ui::bookmark(&repo.config.bookmark), |
| 807 | 1× | ui::dim("(updated after every oxy command; Git's own commits are not read back: commit with oxy)") |
| 808 | | ); |
| 809 | 1× | Ok(0) |
| 810 | 2× | } |
| 811 | |
|
| 812 | 0× | pub fn git_export(git_dir: &std::path::Path, branch: Option<&str>) -> Result<i32> { |
| 813 | 0× | let repo = open()?; |
| 814 | 0× | let sp = ui::spinner("Exporting to Git…"); |
| 815 | 0× | let r = match branch { |
| 816 | 0× | Some(b) => repo.git_export_as(git_dir, b)?, |
| 817 | 0× | None => repo.git_export(git_dir)?, |
| 818 | | }; |
| 819 | 0× | sp.clear(); |
| 820 | 0× | println!( |
| 821 | | "{} {} to {} as branch {} {}", |
| 822 | 0× | ui::done("Exported"), |
| 823 | 0× | plural(r.exported, "new state"), |
| 824 | 0× | ui::path(git_dir.display()), |
| 825 | 0× | ui::bookmark(&r.branch), |
| 826 | 0× | ui::dim(format!( |
| 827 | | "(currently {}; {} already exported)", |
| 828 | 0× | plural(r.heads, "head"), |
| 829 | | r.skipped |
| 830 | | )) |
| 831 | | ); |
| 832 | 0× | Ok(0) |
| 833 | 0× | } |
| 834 | |
|
| 835 | 0× | pub fn git_recover() -> Result<i32> { |
| 836 | 0× | let repo = open()?; |
| 837 | 0× | let sp = ui::spinner("Reading the store…"); |
| 838 | 0× | let (changes, heads) = repo.recover_git_import(&|m| sp.set(m))?; |
| 839 | 0× | sp.done(&format!( |
| 840 | 0× | "{} {} from imported Git commits: {} now has {}", |
| 841 | 0× | ui::done("Recovered"), |
| 842 | 0× | plural(changes, "change"), |
| 843 | 0× | ui::bookmark(&repo.config.bookmark), |
| 844 | 0× | plural(heads, "head") |
| 845 | 0× | )); |
| 846 | 0× | println!( |
| 847 | | " {} {}", |
| 848 | 0× | ui::note(), |
| 849 | 0× | ui::dim("carry on with oxy git fetch GIT_DIR --branch BRANCH") |
| 850 | | ); |
| 851 | 0× | Ok(0) |
| 852 | 0× | } |
| 853 | |
|
| 854 | |
|
| 855 | |
|
| 856 | 0× | fn claim_commits(repo: &mut Repo) -> Result<()> { |
| 857 | 0× | repo.config.set("git.claim", "true")?; |
| 858 | 0× | repo.save_config()?; |
| 859 | 0× | Ok(()) |
| 860 | 0× | } |
| 861 | |
|
| 862 | 0× | pub fn git_fetch(url: &str, branch: &str, claim: bool) -> Result<i32> { |
| 863 | 0× | let mut repo = open()?; |
| 864 | 0× | if claim { |
| 865 | 0× | claim_commits(&mut repo)?; |
| 866 | 0× | } |
| 867 | 0× | let sp = ui::spinner(&format!("Fetching {url} {branch}…")); |
| 868 | 0× | let r = repo.git_fetch(url, branch)?; |
| 869 | 0× | sp.clear(); |
| 870 | 0× | if r.commits == 0 { |
| 871 | 0× | println!( |
| 872 | 0× | "{} {}", |
| 873 | 0× | ui::done("Fetched"), |
| 874 | 0× | ui::dim(format!("nothing new from {url} {branch}")) |
| 875 | 0× | ); |
| 876 | 0× | } else { |
| 877 | 0× | println!( |
| 878 | | "{} {} ({} merges) from {} {}{}{} now has {}", |
| 879 | 0× | ui::done("Fetched"), |
| 880 | 0× | plural(r.commits, "new commit"), |
| 881 | | r.merges, |
| 882 | 0× | ui::path(url), |
| 883 | 0× | ui::bookmark(branch), |
| 884 | 0× | ui::sep(), |
| 885 | 0× | ui::bookmark(&repo.config.bookmark), |
| 886 | 0× | plural(r.heads, "head") |
| 887 | | ); |
| 888 | 0× | if !repo.snapshot("oxy git fetch")?.changed { |
| 889 | 0× | println!(" {} {}", ui::note(), ui::dim("working copy updated")); |
| 890 | 0× | } else { |
| 891 | 0× | println!( |
| 892 | 0× | " {} {}", |
| 893 | 0× | ui::note(), |
| 894 | 0× | ui::dim("run oxy checkout to update the working copy") |
| 895 | 0× | ); |
| 896 | 0× | } |
| 897 | | } |
| 898 | 0× | if r.submodules > 0 { |
| 899 | 0× | println!( |
| 900 | | " {} {}", |
| 901 | 0× | ui::note(), |
| 902 | 0× | ui::dim(format!( |
| 903 | | "{} submodule {} imported as subrepos with Git pins", |
| 904 | | r.submodules, |
| 905 | 0× | if r.submodules == 1 { |
| 906 | 0× | "entry" |
| 907 | | } else { |
| 908 | 0× | "entries" |
| 909 | | } |
| 910 | | )) |
| 911 | | ); |
| 912 | 0× | } |
| 913 | 0× | Ok(0) |
| 914 | 0× | } |
| 915 | |
|
| 916 | 0× | pub fn git_push(url: &str, branch: &str) -> Result<i32> { |
| 917 | 0× | let repo = open()?; |
| 918 | 0× | let sp = ui::spinner(&format!("Pushing to {url} {branch}…")); |
| 919 | 0× | let r = repo.git_push(url, branch)?; |
| 920 | 0× | sp.clear(); |
| 921 | 0× | println!( |
| 922 | | "{} {} to {} {} {}", |
| 923 | 0× | ui::done("Pushed"), |
| 924 | 0× | plural(r.exported, "new commit"), |
| 925 | 0× | ui::path(url), |
| 926 | 0× | ui::bookmark(&r.branch), |
| 927 | 0× | ui::dim(format!("({} already there)", r.skipped)) |
| 928 | | ); |
| 929 | 0× | Ok(0) |
| 930 | 0× | } |
| 931 | |
|
| 932 | |
|
| 933 | 0× | pub fn git_verify( |
| 934 | 0× | git_dir: &std::path::Path, |
| 935 | 0× | branch: &str, |
| 936 | 0× | last: Option<usize>, |
| 937 | 0× | max: usize, |
| 938 | 0× | ) -> Result<i32> { |
| 939 | 0× | let repo = open()?; |
| 940 | 0× | let started = std::time::Instant::now(); |
| 941 | 0× | let sp = ui::spinner(&format!( |
| 942 | 0× | "Rebuilding every commit of {} {branch}…", |
| 943 | 0× | git_dir.display() |
| 944 | 0× | )); |
| 945 | 0× | let r = repo.verify_git_import_last(git_dir, branch, last, max)?; |
| 946 | 0× | sp.clear(); |
| 947 | 0× | for (_, what) in &r.mismatches { |
| 948 | 0× | println!("{} {what}", ui::err(ui::glyph("✖", "x"))); |
| 949 | 0× | } |
| 950 | 0× | if r.not_yet > 0 { |
| 951 | 0× | println!( |
| 952 | 0× | " {} {}", |
| 953 | 0× | ui::note(), |
| 954 | 0× | ui::dim(format!( |
| 955 | 0× | "{} not imported yet, after the last imported commit: not checked", |
| 956 | 0× | plural(r.not_yet, "commit") |
| 957 | 0× | )) |
| 958 | 0× | ); |
| 959 | 0× | } |
| 960 | 0× | if r.mismatches.is_empty() { |
| 961 | 0× | println!( |
| 962 | | "{}", |
| 963 | 0× | ui::done(if r.skipped > 0 { |
| 964 | 0× | format!( |
| 965 | | "The last {} commits reproduced ({} followed), {} files compared, in {:.1}s", |
| 966 | 0× | r.commits - r.skipped, |
| 967 | | r.skipped, |
| 968 | | r.files_compared, |
| 969 | 0× | started.elapsed().as_secs_f64() |
| 970 | | ) |
| 971 | | } else { |
| 972 | 0× | format!( |
| 973 | | "Every commit reproduced: {} commits, {} files compared, in {:.1}s", |
| 974 | | r.commits, |
| 975 | | r.files_compared, |
| 976 | 0× | started.elapsed().as_secs_f64() |
| 977 | | ) |
| 978 | | }) |
| 979 | | ); |
| 980 | 0× | Ok(0) |
| 981 | | } else { |
| 982 | | |
| 983 | | |
| 984 | 0× | let mut kinds: std::collections::BTreeMap<String, usize> = Default::default(); |
| 985 | 0× | for (_, what) in &r.mismatches { |
| 986 | 0× | let kind = what.split_once(": ").map_or(what.as_str(), |(_, k)| k); |
| 987 | 0× | *kinds.entry(kind.to_string()).or_default() += 1; |
| 988 | | } |
| 989 | 0× | let mut kinds: Vec<_> = kinds.into_iter().collect(); |
| 990 | 0× | kinds.sort_by_key(|k| std::cmp::Reverse(k.1)); |
| 991 | 0× | for (kind, n) in kinds.iter().take(10) { |
| 992 | 0× | println!(" {} {}", ui::dim(format!("{n:>6} ×")), kind); |
| 993 | 0× | } |
| 994 | 0× | println!( |
| 995 | | "{} differences in {} commits checked{}", |
| 996 | 0× | r.mismatches.len(), |
| 997 | | r.commits, |
| 998 | 0× | if max > 0 && r.mismatches.len() >= max { |
| 999 | 0× | format!(" (stopped at {max}; --max-differences 0 checks every commit)") |
| 1000 | | } else { |
| 1001 | 0× | String::new() |
| 1002 | | } |
| 1003 | | ); |
| 1004 | 0× | Ok(1) |
| 1005 | | } |
| 1006 | 0× | } |
| 1007 | |
|
| 1008 | 0× | pub fn git_import( |
| 1009 | 0× | git_dir: &std::path::Path, |
| 1010 | 0× | dest: &std::path::Path, |
| 1011 | 0× | branch: &str, |
| 1012 | 0× | claim: bool, |
| 1013 | 0× | ) -> Result<i32> { |
| 1014 | 0× | let mut repo = Repo::init(dest)?; |
| 1015 | 0× | if claim { |
| 1016 | 0× | claim_commits(&mut repo)?; |
| 1017 | 0× | } |
| 1018 | 0× | let started = std::time::Instant::now(); |
| 1019 | 0× | let sp = ui::spinner(&format!("Importing {} {branch}…", git_dir.display())); |
| 1020 | 0× | let r = repo.import_git_with(git_dir, branch, &mut |where_| sp.set(where_))?; |
| 1021 | 0× | sp.clear(); |
| 1022 | 0× | println!( |
| 1023 | | "{} {} ({} merges) from {} {} into {} {}", |
| 1024 | 0× | ui::done("Imported"), |
| 1025 | 0× | plural(r.commits, "commit"), |
| 1026 | | r.merges, |
| 1027 | 0× | ui::path(git_dir.display()), |
| 1028 | 0× | ui::bookmark(branch), |
| 1029 | 0× | ui::path(repo.root().display()), |
| 1030 | 0× | ui::dim(format!( |
| 1031 | | "in {:.1?}; {} now has {}", |
| 1032 | 0× | started.elapsed(), |
| 1033 | | repo.config.bookmark, |
| 1034 | 0× | plural(r.heads, "head") |
| 1035 | | )) |
| 1036 | | ); |
| 1037 | 0× | if r.submodules > 0 { |
| 1038 | 0× | println!( |
| 1039 | | " {} {}", |
| 1040 | 0× | ui::note(), |
| 1041 | 0× | ui::dim(format!( |
| 1042 | | "{} submodule {} imported as subrepos with Git pins", |
| 1043 | | r.submodules, |
| 1044 | 0× | if r.submodules == 1 { |
| 1045 | 0× | "entry" |
| 1046 | | } else { |
| 1047 | 0× | "entries" |
| 1048 | | } |
| 1049 | | )) |
| 1050 | | ); |
| 1051 | 0× | } |
| 1052 | 0× | Ok(0) |
| 1053 | 0× | } |
| 1054 | |
|
| 1055 | 1× | pub fn submodule_add(url: &str, path: &str, bookmark: Option<&str>) -> Result<i32> { |
| 1056 | 1× | let repo = open()?; |
| 1057 | 1× | let sp = ui::spinner(&format!("Cloning {url} into {path}…")); |
| 1058 | 1× | let pin = repo.submodule_add(url, path, bookmark)?; |
| 1059 | 1× | sp.done(&format!( |
| 1060 | 1× | "{} subrepo {} {} {}", |
| 1061 | 1× | ui::done("Added"), |
| 1062 | 1× | ui::path(path), |
| 1063 | 1× | ui::dim(ui::glyph("→", "->")), |
| 1064 | 1× | ui::dim(pin.summary()) |
| 1065 | 1× | )); |
| 1066 | 1× | println!( |
| 1067 | | " {} {}", |
| 1068 | 1× | ui::note(), |
| 1069 | 1× | ui::dim("recorded in .oxymodules; commit to pin it") |
| 1070 | | ); |
| 1071 | 1× | Ok(0) |
| 1072 | 1× | } |
| 1073 | |
|
| 1074 | 0× | pub fn submodule_update(init: bool, paths: &[String]) -> Result<i32> { |
| 1075 | 0× | let repo = open()?; |
| 1076 | 0× | let sp = ui::spinner("Updating subrepos…"); |
| 1077 | 0× | let results = repo.submodule_update(init, paths)?; |
| 1078 | 0× | let unpinned: Vec<_> = repo |
| 1079 | 0× | .unpinned_modules()? |
| 1080 | 0× | .into_iter() |
| 1081 | 0× | .filter(|(p, _)| paths.is_empty() || paths.iter().any(|q| q.trim_end_matches('/') == p)) |
| 1082 | 0× | .collect(); |
| 1083 | 0× | sp.clear(); |
| 1084 | 0× | if results.is_empty() && unpinned.is_empty() { |
| 1085 | 0× | println!("{}", ui::dim("No subrepos.")); |
| 1086 | 0× | } |
| 1087 | 0× | let code = print_subrepo_results(results); |
| 1088 | 0× | print_unpinned(&unpinned); |
| 1089 | 0× | Ok(if unpinned.is_empty() { code } else { 1 }) |
| 1090 | 0× | } |
| 1091 | |
|
| 1092 | |
|
| 1093 | |
|
| 1094 | 0× | fn print_unpinned(unpinned: &[(String, oxy_core::subrepo::Module)]) { |
| 1095 | 0× | for (path, m) in unpinned { |
| 1096 | 0× | let url = if m.url.is_empty() { "<url>" } else { &m.url }; |
| 1097 | 0× | println!( |
| 1098 | | "{} {}", |
| 1099 | 0× | ui::warn(format!( |
| 1100 | | "{} in .oxymodules but never added, so no pin", |
| 1101 | 0× | ui::glyph("⚠", "!") |
| 1102 | | )), |
| 1103 | 0× | ui::path(path) |
| 1104 | | ); |
| 1105 | 0× | println!( |
| 1106 | | " {}", |
| 1107 | 0× | ui::dim(format!("add it with: oxy submodule add {url} {path}")) |
| 1108 | | ); |
| 1109 | | } |
| 1110 | 0× | } |
| 1111 | |
|
| 1112 | 0× | fn print_subrepo_results(results: Vec<(String, oxy_core::repo::SubrepoSync)>) -> i32 { |
| 1113 | 0× | let mut code = 0; |
| 1114 | 0× | for (path, r) in results { |
| 1115 | | use oxy_core::repo::SubrepoSync::*; |
| 1116 | 0× | let line = match r { |
| 1117 | 0× | Updated => ui::done("Updated"), |
| 1118 | 0× | UpToDate => ui::ok(format!("{} up to date", ui::glyph("✔", "*"))), |
| 1119 | | Missing => { |
| 1120 | 0× | code = 1; |
| 1121 | 0× | ui::warn(format!( |
| 1122 | | "{} missing (pass --init to clone)", |
| 1123 | 0× | ui::glyph("⚠", "!") |
| 1124 | | )) |
| 1125 | | } |
| 1126 | | Dirty => { |
| 1127 | 0× | code = 1; |
| 1128 | 0× | ui::warn(format!( |
| 1129 | | "{} has uncommitted changes; left as is", |
| 1130 | 0× | ui::glyph("⚠", "!") |
| 1131 | | )) |
| 1132 | | } |
| 1133 | 0× | KeptBookmark { bookmark, newer } => ui::warn(format!( |
| 1134 | | "{} checked out at the pin; its bookmark {bookmark} keeps {} the pin lacks", |
| 1135 | 0× | ui::glyph("⚠", "!"), |
| 1136 | 0× | plural(newer, "newer change") |
| 1137 | | )), |
| 1138 | 0× | Failed(e) => { |
| 1139 | 0× | code = 1; |
| 1140 | 0× | ui::err(format!("{} {e}", ui::glyph("✖", "x"))) |
| 1141 | | } |
| 1142 | | }; |
| 1143 | 0× | println!("{line} {}", ui::path(path)); |
| 1144 | | } |
| 1145 | 0× | code |
| 1146 | 0× | } |
| 1147 | |
|
| 1148 | 0× | fn pin_json(p: &oxy_core::subrepo::Pin) -> String { |
| 1149 | 0× | match p { |
| 1150 | | oxy_core::subrepo::Pin::Oxy { |
| 1151 | 0× | repo, |
| 1152 | 0× | heads, |
| 1153 | 0× | bookmark, |
| 1154 | 0× | } => json::obj([ |
| 1155 | 0× | ("kind", json::s("oxy")), |
| 1156 | | ( |
| 1157 | 0× | "repo", |
| 1158 | 0× | json::s(&repo.iter().map(|b| format!("{b:02x}")).collect::<String>()), |
| 1159 | | ), |
| 1160 | 0× | ("heads", json::hashes(heads)), |
| 1161 | 0× | ("bookmark", json::s(bookmark)), |
| 1162 | | ]), |
| 1163 | 0× | oxy_core::subrepo::Pin::Git { sha, branch } => json::obj([ |
| 1164 | 0× | ("kind", json::s("git")), |
| 1165 | 0× | ("sha", json::s(sha)), |
| 1166 | 0× | ("branch", json::s(branch)), |
| 1167 | 0× | ]), |
| 1168 | | } |
| 1169 | 0× | } |
| 1170 | |
|
| 1171 | 1× | pub fn submodule_status(as_json: bool) -> Result<i32> { |
| 1172 | 1× | let repo = open()?; |
| 1173 | 1× | let subs = repo.subrepos()?; |
| 1174 | 1× | let unpinned = repo.unpinned_modules()?; |
| 1175 | 1× | if as_json { |
| 1176 | 1× | let pinned = subs.iter().map(|s| { |
| 1177 | 0× | json::obj([ |
| 1178 | 0× | ("path", json::s(&s.path)), |
| 1179 | 0× | ("pin", pin_json(&s.pin)), |
| 1180 | 0× | ("actual", json::opt(s.actual.as_ref(), pin_json)), |
| 1181 | 0× | ("at_pin", json::b(s.actual.as_ref() == Some(&s.pin))), |
| 1182 | 0× | ("dirty", json::b(s.dirty)), |
| 1183 | 0× | ("committed", json::b(s.committed)), |
| 1184 | 0× | ("url", json::opt(s.url.as_deref(), json::s)), |
| 1185 | 0× | ]) |
| 1186 | 0× | }); |
| 1187 | | |
| 1188 | 1× | let unpinned_json = unpinned.iter().map(|(path, m)| { |
| 1189 | 0× | json::obj([ |
| 1190 | 0× | ("path", json::s(path)), |
| 1191 | 0× | ("pin", "null".to_string()), |
| 1192 | 0× | ("actual", "null".to_string()), |
| 1193 | 0× | ("at_pin", json::b(false)), |
| 1194 | 0× | ("dirty", json::b(false)), |
| 1195 | 0× | ("committed", json::b(false)), |
| 1196 | 0× | ("url", json::s(&m.url)), |
| 1197 | 0× | ]) |
| 1198 | 0× | }); |
| 1199 | 1× | println!("{}", json::arr(pinned.chain(unpinned_json))); |
| 1200 | 1× | return Ok(0); |
| 1201 | 0× | } |
| 1202 | 0× | if subs.is_empty() && unpinned.is_empty() { |
| 1203 | 0× | println!("{}", ui::dim("No subrepos.")); |
| 1204 | 0× | } |
| 1205 | 0× | for s in subs { |
| 1206 | 0× | let state = match (&s.actual, s.dirty) { |
| 1207 | 0× | _ if !s.committed => { |
| 1208 | 0× | ui::warn(format!("{} added, not committed yet", ui::glyph("+", "+"))) |
| 1209 | | } |
| 1210 | 0× | (None, _) => ui::warn(format!("{} missing", ui::glyph("⚠", "!"))), |
| 1211 | 0× | (Some(a), dirty) if *a == s.pin => { |
| 1212 | 0× | if dirty { |
| 1213 | 0× | ui::warn(format!( |
| 1214 | | "{} at pin, uncommitted changes", |
| 1215 | 0× | ui::glyph("✎", "~") |
| 1216 | | )) |
| 1217 | | } else { |
| 1218 | 0× | ui::ok(format!("{} at pin", ui::glyph("✔", "*"))) |
| 1219 | | } |
| 1220 | | } |
| 1221 | 0× | (Some(a), _) => ui::warn(format!("{} at {}", ui::glyph("↕", "<>"), a.summary())), |
| 1222 | | }; |
| 1223 | 0× | println!( |
| 1224 | | "{} {}{}{}{}{state}", |
| 1225 | 0× | ui::ok(ui::glyph("⧉", "#")), |
| 1226 | 0× | ui::path(&s.path), |
| 1227 | 0× | ui::sep(), |
| 1228 | 0× | ui::dim(s.pin.summary()), |
| 1229 | 0× | ui::sep() |
| 1230 | | ); |
| 1231 | 0× | if let Some(u) = s.url { |
| 1232 | 0× | println!(" {} {}", ui::dim(ui::glyph("↳", "->")), ui::dim(u)); |
| 1233 | 0× | } |
| 1234 | | } |
| 1235 | 0× | print_unpinned(&unpinned); |
| 1236 | 0× | Ok(0) |
| 1237 | 1× | } |
| 1238 | |
|
| 1239 | 0× | pub fn submodule_remove(path: &str, delete: bool, force: bool) -> Result<i32> { |
| 1240 | 0× | let repo = open()?; |
| 1241 | 0× | let dir = repo.root().join(path.trim_end_matches('/')); |
| 1242 | | |
| 1243 | 0× | if delete && dir.exists() && !force { |
| 1244 | 0× | if let Ok(sub) = oxy_core::repo::Repo::open(&dir) { |
| 1245 | 0× | let snap = sub.snapshot("oxy submodule remove --delete")?; |
| 1246 | 0× | if !snap.statuses.is_empty() { |
| 1247 | 0× | anyhow::bail!( |
| 1248 | | "{path} has {} with uncommitted edits; commit them in the subrepo (and push them if you want them kept), or pass --force to delete them", |
| 1249 | 0× | plural(snap.statuses.len(), "file") |
| 1250 | | ); |
| 1251 | 0× | } |
| 1252 | 0× | } |
| 1253 | 0× | } |
| 1254 | 0× | repo.submodule_remove(path)?; |
| 1255 | 0× | if !delete { |
| 1256 | 0× | println!( |
| 1257 | | "{} subrepo {} {}", |
| 1258 | 0× | ui::done("Removed"), |
| 1259 | 0× | ui::path(path), |
| 1260 | 0× | ui::dim("(directory left in place; commit to record the removal)") |
| 1261 | | ); |
| 1262 | 0× | println!( |
| 1263 | | " {} {}", |
| 1264 | 0× | ui::note(), |
| 1265 | 0× | ui::dim(format!( |
| 1266 | | "to delete the directory too: oxy submodule remove {path} --delete" |
| 1267 | | )) |
| 1268 | | ); |
| 1269 | 0× | return Ok(0); |
| 1270 | 0× | } |
| 1271 | 0× | if dir.exists() { |
| 1272 | 0× | delete_tree(&dir)?; |
| 1273 | 0× | } |
| 1274 | 0× | println!( |
| 1275 | | "{} subrepo {} {}", |
| 1276 | 0× | ui::done("Removed"), |
| 1277 | 0× | ui::path(path), |
| 1278 | 0× | ui::dim("and its directory (commit to record the removal)") |
| 1279 | | ); |
| 1280 | 0× | Ok(0) |
| 1281 | 0× | } |
| 1282 | |
|
| 1283 | |
|
| 1284 | |
|
| 1285 | 0× | fn delete_tree(dir: &std::path::Path) -> Result<()> { |
| 1286 | 0× | fn writable(p: &std::path::Path) -> std::io::Result<()> { |
| 1287 | 0× | let meta = std::fs::symlink_metadata(p)?; |
| 1288 | 0× | if meta.file_type().is_symlink() { |
| 1289 | 0× | return Ok(()); |
| 1290 | 0× | } |
| 1291 | 0× | let mut perms = meta.permissions(); |
| 1292 | 0× | if perms.readonly() { |
| 1293 | | #[allow(clippy::permissions_set_readonly_false)] |
| 1294 | 0× | perms.set_readonly(false); |
| 1295 | 0× | std::fs::set_permissions(p, perms)?; |
| 1296 | 0× | } |
| 1297 | 0× | if meta.is_dir() { |
| 1298 | 0× | for e in std::fs::read_dir(p)? { |
| 1299 | 0× | writable(&e?.path())?; |
| 1300 | | } |
| 1301 | 0× | } |
| 1302 | 0× | Ok(()) |
| 1303 | 0× | } |
| 1304 | 0× | writable(dir).map_err(|e| anyhow::anyhow!("{}: {e}", dir.display()))?; |
| 1305 | 0× | std::fs::remove_dir_all(dir).map_err(|e| anyhow::anyhow!("{}: {e}", dir.display()))?; |
| 1306 | 0× | Ok(()) |
| 1307 | 0× | } |
| 1308 | |
|
| 1309 | 0× | pub fn deepen(remote: &str, depth: u32) -> Result<i32> { |
| 1310 | 0× | let repo = open()?; |
| 1311 | 0× | if !repo.is_shallow() { |
| 1312 | 0× | println!( |
| 1313 | | "{}", |
| 1314 | 0× | ui::dim("This repository is not shallow; nothing to deepen.") |
| 1315 | | ); |
| 1316 | 0× | return Ok(0); |
| 1317 | 0× | } |
| 1318 | 0× | let sp = ui::spinner(&format!("Fetching {depth} more levels from {remote}…")); |
| 1319 | 0× | let n = repo.deepen(remote, depth)?; |
| 1320 | 0× | sp.done(&format!( |
| 1321 | | "{} {}; {}", |
| 1322 | 0× | ui::done("Deepened by"), |
| 1323 | 0× | plural(n, "change"), |
| 1324 | 0× | if repo.is_shallow() { |
| 1325 | 0× | "still shallow".to_string() |
| 1326 | | } else { |
| 1327 | 0× | ui::ok("history is now complete") |
| 1328 | | } |
| 1329 | | )); |
| 1330 | 0× | Ok(0) |
| 1331 | 0× | } |
| 1332 | |
|
| 1333 | |
|
| 1334 | |
|
| 1335 | 12× | fn noted<T>(repo: &oxy_core::repo::Repo, remote: &str, r: oxy_core::repo::Result<T>) -> Result<T> { |
| 1336 | 12× | r.map_err(|e| { |
| 1337 | 1× | let m = e.to_string(); |
| 1338 | 1× | match repo.stale_token_note(remote, &m) { |
| 1339 | 0× | Some(note) => anyhow::anyhow!("{m}\n note: {note}"), |
| 1340 | 1× | None => e.into(), |
| 1341 | | } |
| 1342 | 1× | }) |
| 1343 | 12× | } |