| fce44ae9 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
use super::format::plural; |
| 75167672 Andy McCall |
4 |
use super::json; |
| fce44ae9 Andy McCall ↶ |
5 |
use super::open; |
| 135212de Andy McCall ↶ |
6 |
use super::ui; |
| fce44ae9 Andy McCall ↶ |
7 |
use anyhow::{Context, Result}; |
| 8 |
use oxy_core::repo::Repo; |
| 9 |
use std::path::PathBuf; |
| 10 |
|
| 75167672 Andy McCall |
11 |
pub fn clone( |
| 12 |
url: &str, |
| 13 |
dest: Option<PathBuf>, |
| 14 |
max_blob: Option<&str>, |
| 15 |
sparse: &[String], |
| 16 |
depth: Option<u32>, |
| 17 |
) -> Result<i32> { |
| fce44ae9 Andy McCall ↶ |
18 |
let dest = dest.unwrap_or_else(|| { |
| 75167672 Andy McCall |
19 |
let base = url |
| 20 |
.trim_end_matches('/') |
| 21 |
.rsplit('/') |
| 22 |
.next() |
| 23 |
.unwrap_or("repo"); |
| fce44ae9 Andy McCall ↶ |
24 |
PathBuf::from(base) |
| 25 |
}); |
| b3c5a59a Andy McCall ↶ |
26 |
let max_blob = max_blob.map(oxy_core::repo::parse_size).transpose()?; |
| 135212de Andy McCall ↶ |
27 |
let sp = ui::spinner(&format!("Cloning {url}…")); |
| 2863456a Andy McCall ↶ |
28 |
let repo = match depth { |
| 29 |
Some(d) => Repo::clone_shallow(url, &dest, d, max_blob, sparse)?, |
| 30 |
None => Repo::clone_with(url, &dest, max_blob, sparse)?, |
| 31 |
}; |
| fce44ae9 Andy McCall ↶ |
32 |
let heads = repo.bookmark_heads()?; |
| 75167672 Andy McCall |
33 |
sp.done(&format!( |
| 34 |
"{} into {} {}", |
| 35 |
ui::done("Cloned"), |
| 36 |
ui::path(repo.root().display()), |
| 37 |
ui::dim(format!( |
| 38 |
"({url}, {} with {})", |
| 39 |
ui::bookmark(&repo.config.bookmark), |
| 40 |
plural(heads.len(), "head") |
| 41 |
)) |
| 42 |
)); |
| fce44ae9 Andy McCall ↶ |
43 |
Ok(0) |
| 44 |
} |
| 45 |
|
| 46 |
pub fn remote_add(name: &str, url: &str) -> Result<i32> { |
| 47 |
let mut repo = open()?; |
| 48 |
repo.config.set(&format!("remote.{name}.url"), url)?; |
| 49 |
repo.save_config()?; |
| 75167672 Andy McCall |
50 |
println!( |
| 51 |
"{} remote {} {} {url}", |
| 52 |
ui::done("Added"), |
| 53 |
ui::bookmark(name), |
| 54 |
ui::dim(ui::glyph("→", "->")) |
| 55 |
); |
| fce44ae9 Andy McCall ↶ |
56 |
Ok(0) |
| 57 |
} |
| 58 |
|
| 59 |
pub fn remote_remove(name: &str) -> Result<i32> { |
| 60 |
let mut repo = open()?; |
| 61 |
repo.config.set(&format!("remote.{name}.url"), "")?; |
| 62 |
repo.save_config()?; |
| 94084087 Andy McCall ↶ |
63 |
println!("{} remote {}", ui::done("Removed"), ui::bookmark(name)); |
| fce44ae9 Andy McCall ↶ |
64 |
Ok(0) |
| 65 |
} |
| 66 |
|
| c6aa5bd9 Andy McCall ↶ |
67 |
pub fn remote_list(as_json: bool) -> Result<i32> { |
| fce44ae9 Andy McCall ↶ |
68 |
let repo = open()?; |
| 69 |
let view = repo.view()?; |
| c6aa5bd9 Andy McCall ↶ |
70 |
if as_json { |
| 71 |
let items = repo.config.remotes.iter().map(|(name, url)| { |
| 72 |
let bms = view.remotes.get(name).map(|b| { |
| 73 |
json::arr(b.iter().map(|(bm, st)| { |
| 75167672 Andy McCall |
74 |
let heads = repo |
| 75 |
.store() |
| 76 |
.get_state(st) |
| 77 |
.map(|s| s.heads) |
| 78 |
.unwrap_or_default(); |
| c6aa5bd9 Andy McCall ↶ |
79 |
json::obj([("name", json::s(bm)), ("heads", json::hashes(&heads))]) |
| 80 |
})) |
| 81 |
}); |
| 75167672 Andy McCall |
82 |
json::obj([ |
| 83 |
("name", json::s(name)), |
| 84 |
("url", json::s(url)), |
| 85 |
("bookmarks", bms.unwrap_or_else(|| "[]".into())), |
| 86 |
]) |
| c6aa5bd9 Andy McCall ↶ |
87 |
}); |
| 88 |
println!("{}", json::arr(items)); |
| 89 |
return Ok(0); |
| 90 |
} |
| 94084087 Andy McCall ↶ |
91 |
if repo.config.remotes.is_empty() { |
| 75167672 Andy McCall |
92 |
println!( |
| 93 |
"{}", |
| 94 |
ui::dim("No remotes; add one with oxy remote add <name> <url>.") |
| 95 |
); |
| 94084087 Andy McCall ↶ |
96 |
} |
| fce44ae9 Andy McCall ↶ |
97 |
for (name, url) in &repo.config.remotes { |
| 75167672 Andy McCall |
98 |
println!( |
| 99 |
"{} {}{}{}", |
| 100 |
ui::ok(ui::glyph("⇄", "<>")), |
| 101 |
ui::bookmark(name), |
| 102 |
ui::sep(), |
| 103 |
ui::dim(url) |
| 104 |
); |
| fce44ae9 Andy McCall ↶ |
105 |
if let Some(bms) = view.remotes.get(name) { |
| 106 |
for (bm, st) in bms { |
| 75167672 Andy McCall |
107 |
let heads = repo |
| 108 |
.store() |
| 109 |
.get_state(st) |
| 110 |
.map(|s| s.heads.len()) |
| 111 |
.unwrap_or(0); |
| 112 |
println!( |
| 113 |
" {} {}{}{}", |
| 114 |
ui::dim(ui::glyph("↳", "->")), |
| 115 |
ui::id(format!("{name}/{bm}")), |
| 116 |
ui::sep(), |
| 117 |
ui::dim(plural(heads, "head")) |
| 118 |
); |
| fce44ae9 Andy McCall ↶ |
119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
Ok(0) |
| 123 |
} |
| 124 |
|
| 125 |
fn remote_name(remote: Option<&str>) -> String { |
| 126 |
remote.unwrap_or("origin").to_string() |
| 127 |
} |
| 128 |
|
| c6aa5bd9 Andy McCall ↶ |
129 |
pub fn fetch(remote: Option<&str>, as_json: bool) -> Result<i32> { |
| fce44ae9 Andy McCall ↶ |
130 |
let repo = open()?; |
| 131 |
let remote = remote_name(remote); |
| 135212de Andy McCall ↶ |
132 |
let sp = ui::spinner(&format!("Fetching from {remote}…")); |
| 83e0677b Andy McCall ↶ |
133 |
let report = repo.fetch(&remote)?; |
| 135212de Andy McCall ↶ |
134 |
sp.clear(); |
| c6aa5bd9 Andy McCall ↶ |
135 |
if as_json { |
| 136 |
println!( |
| 137 |
"{}", |
| 138 |
json::obj([ |
| 139 |
("remote", json::s(&remote)), |
| 75167672 Andy McCall |
140 |
( |
| 141 |
"refs", |
| 142 |
json::arr(report.refs.iter().map(|(name, heads, n)| json::obj([ |
| 143 |
("name", json::s(name)), |
| 144 |
("heads", json::hashes(heads)), |
| 145 |
("new", json::n(*n)) |
| 146 |
]))) |
| 147 |
), |
| c6aa5bd9 Andy McCall ↶ |
148 |
("new_tags", json::n(report.new_tags)), |
| 149 |
("unverified", json::b(report.unverified)), |
| 150 |
]) |
| 151 |
); |
| 152 |
return Ok(0); |
| 153 |
} |
| 94084087 Andy McCall ↶ |
154 |
if report.refs.is_empty() { |
| 75167672 Andy McCall |
155 |
println!( |
| 156 |
"{} {}", |
| 157 |
ui::done("Fetched"), |
| 158 |
ui::dim(format!("nothing new from {remote}")) |
| 159 |
); |
| 94084087 Andy McCall ↶ |
160 |
} |
| 83e0677b Andy McCall ↶ |
161 |
for (name, heads, n) in &report.refs { |
| 75167672 Andy McCall |
162 |
let what = if *n == 0 { |
| 163 |
ui::dim("up to date") |
| 164 |
} else { |
| 165 |
ui::ok(plural(*n, "new change")) |
| 166 |
}; |
| 167 |
println!( |
| 168 |
"{} {}{}{}{}{what}", |
| 169 |
ui::done("Fetched"), |
| 170 |
ui::id(format!("{remote}/{name}")), |
| 171 |
ui::sep(), |
| 172 |
ui::dim(plural(heads.len(), "head")), |
| 173 |
ui::sep() |
| 174 |
); |
| 83e0677b Andy McCall ↶ |
175 |
} |
| 132b884b Andy McCall ↶ |
176 |
if report.new_tags > 0 { |
| 75167672 Andy McCall |
177 |
println!( |
| 178 |
" {} {}", |
| 179 |
ui::note(), |
| 180 |
ui::dim(format!("{} adopted", plural(report.new_tags, "tag"))) |
| 181 |
); |
| 132b884b Andy McCall ↶ |
182 |
} |
| 83e0677b Andy McCall ↶ |
183 |
if report.unverified { |
| 75167672 Andy McCall |
184 |
eprintln!( |
| 185 |
"{} {remote} has no trust policy; nothing fetched from it was verified", |
| 186 |
ui::caution() |
| 187 |
); |
| fce44ae9 Andy McCall ↶ |
188 |
} |
| 189 |
Ok(0) |
| 190 |
} |
| 191 |
|
| c6aa5bd9 Andy McCall ↶ |
192 |
pub fn pull(remote: Option<&str>, as_json: bool) -> Result<i32> { |
| fce44ae9 Andy McCall ↶ |
193 |
let repo = open()?; |
| 194 |
let remote = remote_name(remote); |
| 135212de Andy McCall ↶ |
195 |
let sp = ui::spinner(&format!("Pulling from {remote}…")); |
| fce44ae9 Andy McCall ↶ |
196 |
let r = repo.pull(&remote)?; |
| c6aa5bd9 Andy McCall ↶ |
197 |
if as_json { |
| 198 |
sp.clear(); |
| cc7a0056 Andy McCall ↶ |
199 |
let conflicted = repo |
| 200 |
.snapshot_status("oxy pull")? |
| 201 |
.base_snapshot |
| 202 |
.is_conflicted(); |
| 75167672 Andy McCall |
203 |
println!( |
| 204 |
"{}", |
| 205 |
json::obj([ |
| 206 |
("remote", json::s(&remote)), |
| 207 |
("heads", json::hashes(&r.heads)), |
| 208 |
("changed", json::b(r.changed)), |
| 209 |
("checked_out", json::b(r.checked_out)), |
| 210 |
("conflicted", json::b(conflicted)) |
| 211 |
]) |
| 212 |
); |
| c6aa5bd9 Andy McCall ↶ |
213 |
return Ok(0); |
| 214 |
} |
| fce44ae9 Andy McCall ↶ |
215 |
if !r.changed { |
| 75167672 Andy McCall |
216 |
sp.done(&format!( |
| 217 |
"{} {}", |
| 218 |
ui::ok(ui::glyph("✔", "*")), |
| 219 |
ui::dim("Already up to date.") |
| 220 |
)); |
| fce44ae9 Andy McCall ↶ |
221 |
return Ok(0); |
| 222 |
} |
| 135212de Andy McCall ↶ |
223 |
sp.done(&format!( |
| 224 |
"{} {} now has {}{}", |
| 94084087 Andy McCall ↶ |
225 |
ui::done("Pulled:"), |
| 226 |
ui::bookmark(&repo.config.bookmark), |
| fce44ae9 Andy McCall ↶ |
227 |
plural(r.heads.len(), "head"), |
| 75167672 Andy McCall |
228 |
ui::dim(if r.checked_out { |
| 229 |
"; working copy updated" |
| 230 |
} else { |
| 231 |
"; run oxy checkout to update the working copy" |
| 232 |
}) |
| 135212de Andy McCall ↶ |
233 |
)); |
| fce44ae9 Andy McCall ↶ |
234 |
let snap = repo.snapshot("oxy pull")?; |
| 235 |
if snap.base_snapshot.is_conflicted() { |
| 75167672 Andy McCall |
236 |
println!( |
| 237 |
" {} {}", |
| 238 |
ui::caution(), |
| 239 |
ui::warn("the result has conflicts; see oxy status") |
| 240 |
); |
| fce44ae9 Andy McCall ↶ |
241 |
} |
| 242 |
Ok(0) |
| 243 |
} |
| 244 |
|
| 75167672 Andy McCall |
245 |
pub fn push( |
| 246 |
remote: Option<&str>, |
| 247 |
force: bool, |
| 248 |
bookmark: Option<&str>, |
| 249 |
as_json: bool, |
| 250 |
) -> Result<i32> { |
| fce44ae9 Andy McCall ↶ |
251 |
let repo = open()?; |
| 252 |
let remote = remote_name(remote); |
| 135212de Andy McCall ↶ |
253 |
let sp = ui::spinner(&format!("Pushing to {remote}…")); |
| 7b9809e6 Andy McCall ↶ |
254 |
let results = match bookmark { |
| 255 |
Some(b) => repo.push_bookmark(&remote, b, force)?, |
| 256 |
None => repo.push(&remote, force)?, |
| 257 |
}; |
| 135212de Andy McCall ↶ |
258 |
sp.clear(); |
| c6aa5bd9 Andy McCall ↶ |
259 |
if as_json { |
| 75167672 Andy McCall |
260 |
println!( |
| 261 |
"{}", |
| 262 |
json::arr(results.iter().map(|r| json::obj([ |
| 263 |
("name", json::s(&r.name)), |
| 264 |
("ok", json::b(r.ok)), |
| 265 |
("reason", json::s(&r.reason)) |
| 266 |
]))) |
| 267 |
); |
| c6aa5bd9 Andy McCall ↶ |
268 |
return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 }); |
| 269 |
} |
| fce44ae9 Andy McCall ↶ |
270 |
let mut code = 0; |
| 135212de Andy McCall ↶ |
271 |
for r in results { |
| fce44ae9 Andy McCall ↶ |
272 |
if r.ok { |
| 75167672 Andy McCall |
273 |
println!( |
| 274 |
"{} {}{}{}", |
| 275 |
ui::done("Pushed"), |
| 276 |
ui::id(format!("{remote}/{}", r.name)), |
| 277 |
ui::sep(), |
| 278 |
ui::dim(if r.reason.is_empty() { |
| 279 |
"updated" |
| 280 |
} else { |
| 281 |
r.reason.as_str() |
| 282 |
}) |
| 283 |
); |
| fce44ae9 Andy McCall ↶ |
284 |
} else { |
| 75167672 Andy McCall |
285 |
println!( |
| 286 |
"{} {} {}{}{}", |
| 287 |
ui::err(ui::glyph("✖", "x")), |
| 288 |
ui::err("Rejected"), |
| 289 |
ui::id(format!("{remote}/{}", r.name)), |
| 290 |
ui::sep(), |
| 291 |
r.reason |
| 292 |
); |
| fce44ae9 Andy McCall ↶ |
293 |
code = 1; |
| 294 |
} |
| 295 |
} |
| 296 |
Ok(code) |
| 297 |
} |
| 298 |
|
| 75167672 Andy McCall |
299 |
pub fn serve( |
| 300 |
root: PathBuf, |
| 301 |
bind: &str, |
| 302 |
tokens: Vec<String>, |
| 303 |
tls: Option<(PathBuf, PathBuf)>, |
| 304 |
) -> Result<i32> { |
| fce44ae9 Andy McCall ↶ |
305 |
let listener = std::net::TcpListener::bind(bind).with_context(|| format!("binding {bind}"))?; |
| 306 |
let root = root.canonicalize()?; |
| 2b5ee371 Andy McCall ↶ |
307 |
let tls = tls.map(|(cert, key)| oxy_server::TlsFiles { cert, key }); |
| fce44ae9 Andy McCall ↶ |
308 |
println!( |
| 2b5ee371 Andy McCall ↶ |
309 |
"Serving repositories under {} on {}://{bind}/<repo>/oxy/v1/ ({})", |
| fce44ae9 Andy McCall ↶ |
310 |
root.display(), |
| 2b5ee371 Andy McCall ↶ |
311 |
if tls.is_some() { "https" } else { "http" }, |
| 75167672 Andy McCall |
312 |
if tokens.is_empty() { |
| 313 |
"open: no push tokens configured" |
| 314 |
} else { |
| 315 |
"push requires a token" |
| 316 |
} |
| fce44ae9 Andy McCall ↶ |
317 |
); |
| 75167672 Andy McCall |
318 |
if tls.is_none() |
| 319 |
&& !bind.starts_with("127.") |
| 320 |
&& !bind.starts_with("localhost") |
| 321 |
&& !bind.starts_with("[::1]") |
| 322 |
{ |
| 2b5ee371 Andy McCall ↶ |
323 |
eprintln!("warning: serving plain HTTP on a non-loopback address; put a TLS terminator in front or pass --tls-cert/--tls-key"); |
| 324 |
} |
| 325 |
oxy_server::run_blocking(listener, oxy_server::ServerConfig { root, tokens, tls })?; |
| fce44ae9 Andy McCall ↶ |
326 |
Ok(0) |
| 327 |
} |
| 79ab8528 Andy McCall ↶ |
328 |
|
| 30ea9c1e Andy McCall ↶ |
329 |
pub fn git_export(git_dir: &std::path::Path, branch: Option<&str>) -> Result<i32> { |
| 79ab8528 Andy McCall ↶ |
330 |
let repo = open()?; |
| 135212de Andy McCall ↶ |
331 |
let sp = ui::spinner("Exporting to Git…"); |
| 30ea9c1e Andy McCall ↶ |
332 |
let r = match branch { |
| 333 |
Some(b) => repo.git_export_as(git_dir, b)?, |
| 334 |
None => repo.git_export(git_dir)?, |
| 335 |
}; |
| 135212de Andy McCall ↶ |
336 |
sp.clear(); |
| 79ab8528 Andy McCall ↶ |
337 |
println!( |
| 94084087 Andy McCall ↶ |
338 |
"{} {} to {} as branch {} {}", |
| 339 |
ui::done("Exported"), |
| e8b4e156 Andy McCall ↶ |
340 |
plural(r.exported, "new state"), |
| 94084087 Andy McCall ↶ |
341 |
ui::path(git_dir.display()), |
| 342 |
ui::bookmark(&r.branch), |
| 75167672 Andy McCall |
343 |
ui::dim(format!( |
| 344 |
"(currently {}; {} already exported)", |
| 345 |
plural(r.heads, "head"), |
| 346 |
r.skipped |
| 347 |
)) |
| 79ab8528 Andy McCall ↶ |
348 |
); |
| 349 |
Ok(0) |
| 350 |
} |
| c3069537 Andy McCall ↶ |
351 |
|
| 30ea9c1e Andy McCall ↶ |
352 |
pub fn git_fetch(url: &str, branch: &str) -> Result<i32> { |
| 353 |
let repo = open()?; |
| 354 |
let sp = ui::spinner(&format!("Fetching {url} {branch}…")); |
| 355 |
let r = repo.git_fetch(url, branch)?; |
| 356 |
sp.clear(); |
| 357 |
if r.commits == 0 { |
| 75167672 Andy McCall |
358 |
println!( |
| 359 |
"{} {}", |
| 360 |
ui::done("Fetched"), |
| 361 |
ui::dim(format!("nothing new from {url} {branch}")) |
| 362 |
); |
| 30ea9c1e Andy McCall ↶ |
363 |
} else { |
| 364 |
println!( |
| 365 |
"{} {} ({} merges) from {} {}{}{} now has {}", |
| 366 |
ui::done("Fetched"), |
| 367 |
plural(r.commits, "new commit"), |
| 368 |
r.merges, |
| 369 |
ui::path(url), |
| 370 |
ui::bookmark(branch), |
| 371 |
ui::sep(), |
| 372 |
ui::bookmark(&repo.config.bookmark), |
| 373 |
plural(r.heads, "head") |
| 374 |
); |
| 375 |
if !repo.snapshot("oxy git fetch")?.changed { |
| 376 |
println!(" {} {}", ui::note(), ui::dim("working copy updated")); |
| 377 |
} else { |
| 75167672 Andy McCall |
378 |
println!( |
| 379 |
" {} {}", |
| 380 |
ui::note(), |
| 381 |
ui::dim("run oxy checkout to update the working copy") |
| 382 |
); |
| 30ea9c1e Andy McCall ↶ |
383 |
} |
| 384 |
} |
| 4dee277c Andy McCall ↶ |
385 |
if r.submodules > 0 { |
| 75167672 Andy McCall |
386 |
println!( |
| 387 |
" {} {}", |
| 388 |
ui::note(), |
| 389 |
ui::dim(format!( |
| 390 |
"{} submodule {} imported as subrepos with Git pins", |
| 391 |
r.submodules, |
| 392 |
if r.submodules == 1 { |
| 393 |
"entry" |
| 394 |
} else { |
| 395 |
"entries" |
| 396 |
} |
| 397 |
)) |
| 398 |
); |
| 30ea9c1e Andy McCall ↶ |
399 |
} |
| 400 |
Ok(0) |
| 401 |
} |
| 402 |
|
| 403 |
pub fn git_push(url: &str, branch: &str) -> Result<i32> { |
| 404 |
let repo = open()?; |
| 405 |
let sp = ui::spinner(&format!("Pushing to {url} {branch}…")); |
| 406 |
let r = repo.git_push(url, branch)?; |
| 407 |
sp.clear(); |
| 408 |
println!( |
| 409 |
"{} {} to {} {} {}", |
| 410 |
ui::done("Pushed"), |
| 411 |
plural(r.exported, "new commit"), |
| 412 |
ui::path(url), |
| 413 |
ui::bookmark(&r.branch), |
| 414 |
ui::dim(format!("({} already there)", r.skipped)) |
| 415 |
); |
| 416 |
Ok(0) |
| 417 |
} |
| 418 |
|
| 62794ecc Andy McCall ↶ |
419 |
|
| 420 |
pub fn git_verify(git_dir: &std::path::Path, branch: &str) -> Result<i32> { |
| 421 |
let repo = open()?; |
| 422 |
let started = std::time::Instant::now(); |
| 423 |
let sp = ui::spinner(&format!( |
| 424 |
"Rebuilding every commit of {} {branch}…", |
| 425 |
git_dir.display() |
| 426 |
)); |
| 427 |
let r = repo.verify_git_import(git_dir, branch)?; |
| 428 |
sp.clear(); |
| 429 |
for (_, what) in &r.mismatches { |
| 430 |
println!("{} {what}", ui::err(ui::glyph("✖", "x"))); |
| 431 |
} |
| 432 |
if r.mismatches.is_empty() { |
| 433 |
println!( |
| 434 |
"{}", |
| 435 |
ui::done(format!( |
| 436 |
"Every commit reproduced: {} commits, {} files compared, in {:.1}s", |
| 437 |
r.commits, |
| 438 |
r.files_compared, |
| 439 |
started.elapsed().as_secs_f64() |
| 440 |
)) |
| 441 |
); |
| 442 |
Ok(0) |
| 443 |
} else { |
| 444 |
println!( |
| 445 |
"{} differences in {} commits checked", |
| 446 |
r.mismatches.len(), |
| 447 |
r.commits |
| 448 |
); |
| 449 |
Ok(1) |
| 450 |
} |
| 451 |
} |
| 452 |
|
| c3069537 Andy McCall ↶ |
453 |
pub fn git_import(git_dir: &std::path::Path, dest: &std::path::Path, branch: &str) -> Result<i32> { |
| 454 |
let repo = Repo::init(dest)?; |
| 455 |
let started = std::time::Instant::now(); |
| 135212de Andy McCall ↶ |
456 |
let sp = ui::spinner(&format!("Importing {} {branch}…", git_dir.display())); |
| c3069537 Andy McCall ↶ |
457 |
let r = repo.import_git(git_dir, branch)?; |
| 135212de Andy McCall ↶ |
458 |
sp.clear(); |
| c3069537 Andy McCall ↶ |
459 |
println!( |
| 94084087 Andy McCall ↶ |
460 |
"{} {} ({} merges) from {} {} into {} {}", |
| 461 |
ui::done("Imported"), |
| c3069537 Andy McCall ↶ |
462 |
plural(r.commits, "commit"), |
| 463 |
r.merges, |
| 94084087 Andy McCall ↶ |
464 |
ui::path(git_dir.display()), |
| 465 |
ui::bookmark(branch), |
| 466 |
ui::path(repo.root().display()), |
| 75167672 Andy McCall |
467 |
ui::dim(format!( |
| 468 |
"in {:.1?}; {} now has {}", |
| 469 |
started.elapsed(), |
| 470 |
repo.config.bookmark, |
| 471 |
plural(r.heads, "head") |
| 472 |
)) |
| c3069537 Andy McCall ↶ |
473 |
); |
| 4dee277c Andy McCall ↶ |
474 |
if r.submodules > 0 { |
| 75167672 Andy McCall |
475 |
println!( |
| 476 |
" {} {}", |
| 477 |
ui::note(), |
| 478 |
ui::dim(format!( |
| 479 |
"{} submodule {} imported as subrepos with Git pins", |
| 480 |
r.submodules, |
| 481 |
if r.submodules == 1 { |
| 482 |
"entry" |
| 483 |
} else { |
| 484 |
"entries" |
| 485 |
} |
| 486 |
)) |
| 487 |
); |
| 4dee277c Andy McCall ↶ |
488 |
} |
| 489 |
Ok(0) |
| 490 |
} |
| 491 |
|
| 492 |
pub fn submodule_add(url: &str, path: &str, bookmark: Option<&str>) -> Result<i32> { |
| 493 |
let repo = open()?; |
| 494 |
let sp = ui::spinner(&format!("Cloning {url} into {path}…")); |
| 495 |
let pin = repo.submodule_add(url, path, bookmark)?; |
| 75167672 Andy McCall |
496 |
sp.done(&format!( |
| 497 |
"{} subrepo {} {} {}", |
| 498 |
ui::done("Added"), |
| 499 |
ui::path(path), |
| 500 |
ui::dim(ui::glyph("→", "->")), |
| 501 |
ui::dim(pin.summary()) |
| 502 |
)); |
| 503 |
println!( |
| 504 |
" {} {}", |
| 505 |
ui::note(), |
| 506 |
ui::dim("recorded in .oxymodules; commit to pin it") |
| 507 |
); |
| 4dee277c Andy McCall ↶ |
508 |
Ok(0) |
| 509 |
} |
| 510 |
|
| 511 |
pub fn submodule_update(init: bool, paths: &[String]) -> Result<i32> { |
| 512 |
let repo = open()?; |
| 513 |
let sp = ui::spinner("Updating subrepos…"); |
| 514 |
let results = repo.submodule_update(init, paths)?; |
| 515 |
sp.clear(); |
| 516 |
if results.is_empty() { |
| 517 |
println!("{}", ui::dim("No subrepos.")); |
| 518 |
} |
| 519 |
let mut code = 0; |
| 520 |
for (path, r) in results { |
| 521 |
use oxy_core::repo::SubrepoSync::*; |
| 522 |
let line = match r { |
| 523 |
Updated => ui::done("Updated"), |
| 524 |
UpToDate => ui::ok(format!("{} up to date", ui::glyph("✔", "*"))), |
| 525 |
Missing => { |
| 526 |
code = 1; |
| 75167672 Andy McCall |
527 |
ui::warn(format!( |
| 528 |
"{} missing (pass --init to clone)", |
| 529 |
ui::glyph("⚠", "!") |
| 530 |
)) |
| 4dee277c Andy McCall ↶ |
531 |
} |
| 532 |
Dirty => { |
| 533 |
code = 1; |
| 75167672 Andy McCall |
534 |
ui::warn(format!( |
| 535 |
"{} has uncommitted changes; left as is", |
| 536 |
ui::glyph("⚠", "!") |
| 537 |
)) |
| 4dee277c Andy McCall ↶ |
538 |
} |
| 539 |
Failed(e) => { |
| 540 |
code = 1; |
| 541 |
ui::err(format!("{} {e}", ui::glyph("✖", "x"))) |
| 542 |
} |
| 543 |
}; |
| 544 |
println!("{line} {}", ui::path(path)); |
| 545 |
} |
| 546 |
Ok(code) |
| 547 |
} |
| 548 |
|
| c6aa5bd9 Andy McCall ↶ |
549 |
fn pin_json(p: &oxy_core::subrepo::Pin) -> String { |
| 550 |
match p { |
| 75167672 Andy McCall |
551 |
oxy_core::subrepo::Pin::Oxy { |
| 552 |
repo, |
| 553 |
heads, |
| 554 |
bookmark, |
| 555 |
} => json::obj([ |
| 556 |
("kind", json::s("oxy")), |
| 557 |
( |
| 558 |
"repo", |
| 559 |
json::s(&repo.iter().map(|b| format!("{b:02x}")).collect::<String>()), |
| 560 |
), |
| 561 |
("heads", json::hashes(heads)), |
| 562 |
("bookmark", json::s(bookmark)), |
| 563 |
]), |
| 564 |
oxy_core::subrepo::Pin::Git { sha, branch } => json::obj([ |
| 565 |
("kind", json::s("git")), |
| 566 |
("sha", json::s(sha)), |
| 567 |
("branch", json::s(branch)), |
| 568 |
]), |
| c6aa5bd9 Andy McCall ↶ |
569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
pub fn submodule_status(as_json: bool) -> Result<i32> { |
| 4dee277c Andy McCall ↶ |
573 |
let repo = open()?; |
| 574 |
let subs = repo.subrepos()?; |
| c6aa5bd9 Andy McCall ↶ |
575 |
if as_json { |
| 576 |
println!( |
| 577 |
"{}", |
| 578 |
json::arr(subs.iter().map(|s| { |
| 579 |
json::obj([ |
| 580 |
("path", json::s(&s.path)), |
| 581 |
("pin", pin_json(&s.pin)), |
| 582 |
("actual", json::opt(s.actual.as_ref(), pin_json)), |
| 583 |
("at_pin", json::b(s.actual.as_ref() == Some(&s.pin))), |
| 584 |
("dirty", json::b(s.dirty)), |
| 585 |
("url", json::opt(s.url.as_deref(), json::s)), |
| 586 |
]) |
| 587 |
})) |
| 588 |
); |
| 589 |
return Ok(0); |
| 590 |
} |
| 4dee277c Andy McCall ↶ |
591 |
if subs.is_empty() { |
| 592 |
println!("{}", ui::dim("No subrepos.")); |
| 94084087 Andy McCall ↶ |
593 |
} |
| 4dee277c Andy McCall ↶ |
594 |
for s in subs { |
| 595 |
let state = match (&s.actual, s.dirty) { |
| 596 |
(None, _) => ui::warn(format!("{} missing", ui::glyph("⚠", "!"))), |
| 597 |
(Some(a), dirty) if *a == s.pin => { |
| 75167672 Andy McCall |
598 |
if dirty { |
| 599 |
ui::warn(format!( |
| 600 |
"{} at pin, uncommitted changes", |
| 601 |
ui::glyph("✎", "~") |
| 602 |
)) |
| 603 |
} else { |
| 604 |
ui::ok(format!("{} at pin", ui::glyph("✔", "*"))) |
| 605 |
} |
| 4dee277c Andy McCall ↶ |
606 |
} |
| 607 |
(Some(a), _) => ui::warn(format!("{} at {}", ui::glyph("↕", "<>"), a.summary())), |
| 608 |
}; |
| 75167672 Andy McCall |
609 |
println!( |
| 610 |
"{} {}{}{}{}{state}", |
| 611 |
ui::ok(ui::glyph("⧉", "#")), |
| 612 |
ui::path(&s.path), |
| 613 |
ui::sep(), |
| 614 |
ui::dim(s.pin.summary()), |
| 615 |
ui::sep() |
| 616 |
); |
| 4dee277c Andy McCall ↶ |
617 |
if let Some(u) = s.url { |
| 618 |
println!(" {} {}", ui::dim(ui::glyph("↳", "->")), ui::dim(u)); |
| 619 |
} |
| 620 |
} |
| 621 |
Ok(0) |
| 622 |
} |
| 623 |
|
| 624 |
pub fn submodule_remove(path: &str) -> Result<i32> { |
| 625 |
let repo = open()?; |
| 626 |
repo.submodule_remove(path)?; |
| 75167672 Andy McCall |
627 |
println!( |
| 628 |
"{} subrepo {} {}", |
| 629 |
ui::done("Removed"), |
| 630 |
ui::path(path), |
| 631 |
ui::dim("(directory left in place; commit to record the removal)") |
| 632 |
); |
| c3069537 Andy McCall ↶ |
633 |
Ok(0) |
| 634 |
} |
| 2863456a Andy McCall ↶ |
635 |
|
| 636 |
pub fn deepen(remote: &str, depth: u32) -> Result<i32> { |
| 637 |
let repo = open()?; |
| 638 |
if !repo.is_shallow() { |
| 75167672 Andy McCall |
639 |
println!( |
| 640 |
"{}", |
| 641 |
ui::dim("This repository is not shallow; nothing to deepen.") |
| 642 |
); |
| 2863456a Andy McCall ↶ |
643 |
return Ok(0); |
| 644 |
} |
| 645 |
let sp = ui::spinner(&format!("Fetching {depth} more levels from {remote}…")); |
| 646 |
let n = repo.deepen(remote, depth)?; |
| 75167672 Andy McCall |
647 |
sp.done(&format!( |
| 648 |
"{} {}; {}", |
| 649 |
ui::ok("Deepened by"), |
| 650 |
plural(n, "change"), |
| 651 |
if repo.is_shallow() { |
| 652 |
"still shallow".to_string() |
| 653 |
} else { |
| 654 |
ui::ok("history is now complete") |
| 655 |
} |
| 656 |
)); |
| 2863456a Andy McCall ↶ |
657 |
Ok(0) |
| 658 |
} |