| 1 |
|
| 2 |
|
| 3 |
use super::format::plural; |
| 4 |
use super::open; |
| 5 |
use super::ui; |
| 6 |
use anyhow::{Context, Result}; |
| 7 |
use oxy_core::repo::Repo; |
| 8 |
use std::path::PathBuf; |
| 9 |
|
| 10 |
pub fn clone(url: &str, dest: Option<PathBuf>, max_blob: Option<&str>, sparse: &[String], depth: Option<u32>) -> Result<i32> { |
| 11 |
let dest = dest.unwrap_or_else(|| { |
| 12 |
let base = url.trim_end_matches('/').rsplit('/').next().unwrap_or("repo"); |
| 13 |
PathBuf::from(base) |
| 14 |
}); |
| 15 |
let max_blob = max_blob.map(oxy_core::repo::parse_size).transpose()?; |
| 16 |
let sp = ui::spinner(&format!("Cloning {url}…")); |
| 17 |
let repo = match depth { |
| 18 |
Some(d) => Repo::clone_shallow(url, &dest, d, max_blob, sparse)?, |
| 19 |
None => Repo::clone_with(url, &dest, max_blob, sparse)?, |
| 20 |
}; |
| 21 |
let heads = repo.bookmark_heads()?; |
| 22 |
sp.done(&format!("{} into {} {}", ui::done("Cloned"), ui::path(repo.root().display()), ui::dim(format!("({url}, {} with {})", ui::bookmark(&repo.config.bookmark), plural(heads.len(), "head"))))); |
| 23 |
Ok(0) |
| 24 |
} |
| 25 |
|
| 26 |
pub fn remote_add(name: &str, url: &str) -> Result<i32> { |
| 27 |
let mut repo = open()?; |
| 28 |
repo.config.set(&format!("remote.{name}.url"), url)?; |
| 29 |
repo.save_config()?; |
| 30 |
println!("{} remote {} {} {url}", ui::done("Added"), ui::bookmark(name), ui::dim(ui::glyph("→", "->"))); |
| 31 |
Ok(0) |
| 32 |
} |
| 33 |
|
| 34 |
pub fn remote_remove(name: &str) -> Result<i32> { |
| 35 |
let mut repo = open()?; |
| 36 |
repo.config.set(&format!("remote.{name}.url"), "")?; |
| 37 |
repo.save_config()?; |
| 38 |
println!("{} remote {}", ui::done("Removed"), ui::bookmark(name)); |
| 39 |
Ok(0) |
| 40 |
} |
| 41 |
|
| 42 |
pub fn remote_list() -> Result<i32> { |
| 43 |
let repo = open()?; |
| 44 |
let view = repo.view()?; |
| 45 |
if repo.config.remotes.is_empty() { |
| 46 |
println!("{}", ui::dim("No remotes; add one with oxy remote add <name> <url>.")); |
| 47 |
} |
| 48 |
for (name, url) in &repo.config.remotes { |
| 49 |
println!("{} {}{}{}", ui::ok(ui::glyph("⇄", "<>")), ui::bookmark(name), ui::sep(), ui::dim(url)); |
| 50 |
if let Some(bms) = view.remotes.get(name) { |
| 51 |
for (bm, st) in bms { |
| 52 |
let heads = repo.store().get_state(st).map(|s| s.heads.len()).unwrap_or(0); |
| 53 |
println!(" {} {}{}{}", ui::dim(ui::glyph("↳", "->")), ui::id(format!("{name}/{bm}")), ui::sep(), ui::dim(plural(heads, "head"))); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
Ok(0) |
| 58 |
} |
| 59 |
|
| 60 |
fn remote_name(remote: Option<&str>) -> String { |
| 61 |
remote.unwrap_or("origin").to_string() |
| 62 |
} |
| 63 |
|
| 64 |
pub fn fetch(remote: Option<&str>) -> Result<i32> { |
| 65 |
let repo = open()?; |
| 66 |
let remote = remote_name(remote); |
| 67 |
let sp = ui::spinner(&format!("Fetching from {remote}…")); |
| 68 |
let report = repo.fetch(&remote)?; |
| 69 |
sp.clear(); |
| 70 |
if report.refs.is_empty() { |
| 71 |
println!("{} {}", ui::done("Fetched"), ui::dim(format!("nothing new from {remote}"))); |
| 72 |
} |
| 73 |
for (name, heads, n) in &report.refs { |
| 74 |
let what = if *n == 0 { ui::dim("up to date") } else { ui::ok(plural(*n, "new change")) }; |
| 75 |
println!("{} {}{}{}{}{what}", ui::done("Fetched"), ui::id(format!("{remote}/{name}")), ui::sep(), ui::dim(plural(heads.len(), "head")), ui::sep()); |
| 76 |
} |
| 77 |
if report.new_tags > 0 { |
| 78 |
println!(" {} {}", ui::note(), ui::dim(format!("{} adopted", plural(report.new_tags, "tag")))); |
| 79 |
} |
| 80 |
if report.unverified { |
| 81 |
eprintln!("{} {remote} has no trust policy; nothing fetched from it was verified", ui::caution()); |
| 82 |
} |
| 83 |
Ok(0) |
| 84 |
} |
| 85 |
|
| 86 |
pub fn pull(remote: Option<&str>) -> Result<i32> { |
| 87 |
let repo = open()?; |
| 88 |
let remote = remote_name(remote); |
| 89 |
let sp = ui::spinner(&format!("Pulling from {remote}…")); |
| 90 |
let r = repo.pull(&remote)?; |
| 91 |
if !r.changed { |
| 92 |
sp.done(&format!("{} {}", ui::ok(ui::glyph("✔", "*")), ui::dim("Already up to date."))); |
| 93 |
return Ok(0); |
| 94 |
} |
| 95 |
sp.done(&format!( |
| 96 |
"{} {} now has {}{}", |
| 97 |
ui::done("Pulled:"), |
| 98 |
ui::bookmark(&repo.config.bookmark), |
| 99 |
plural(r.heads.len(), "head"), |
| 100 |
ui::dim(if r.checked_out { "; working copy updated" } else { "; run oxy checkout to update the working copy" }) |
| 101 |
)); |
| 102 |
let snap = repo.snapshot("oxy pull")?; |
| 103 |
if snap.base_snapshot.is_conflicted() { |
| 104 |
println!(" {} {}", ui::caution(), ui::warn("the result has conflicts; see oxy status")); |
| 105 |
} |
| 106 |
Ok(0) |
| 107 |
} |
| 108 |
|
| 109 |
pub fn push(remote: Option<&str>, force: bool) -> Result<i32> { |
| 110 |
let repo = open()?; |
| 111 |
let remote = remote_name(remote); |
| 112 |
let sp = ui::spinner(&format!("Pushing to {remote}…")); |
| 113 |
let results = repo.push(&remote, force)?; |
| 114 |
sp.clear(); |
| 115 |
let mut code = 0; |
| 116 |
for r in results { |
| 117 |
if r.ok { |
| 118 |
println!("{} {}{}{}", ui::done("Pushed"), ui::id(format!("{remote}/{}", r.name)), ui::sep(), ui::dim(if r.reason.is_empty() { "updated" } else { r.reason.as_str() })); |
| 119 |
} else { |
| 120 |
println!("{} {} {}{}{}", ui::err(ui::glyph("✖", "x")), ui::err("Rejected"), ui::id(format!("{remote}/{}", r.name)), ui::sep(), r.reason); |
| 121 |
code = 1; |
| 122 |
} |
| 123 |
} |
| 124 |
Ok(code) |
| 125 |
} |
| 126 |
|
| 127 |
pub fn serve(root: PathBuf, bind: &str, tokens: Vec<String>, tls: Option<(PathBuf, PathBuf)>) -> Result<i32> { |
| 128 |
let listener = std::net::TcpListener::bind(bind).with_context(|| format!("binding {bind}"))?; |
| 129 |
let root = root.canonicalize()?; |
| 130 |
let tls = tls.map(|(cert, key)| oxy_server::TlsFiles { cert, key }); |
| 131 |
println!( |
| 132 |
"Serving repositories under {} on {}://{bind}/<repo>/oxy/v1/ ({})", |
| 133 |
root.display(), |
| 134 |
if tls.is_some() { "https" } else { "http" }, |
| 135 |
if tokens.is_empty() { "open: no push tokens configured" } else { "push requires a token" } |
| 136 |
); |
| 137 |
if tls.is_none() && !bind.starts_with("127.") && !bind.starts_with("localhost") && !bind.starts_with("[::1]") { |
| 138 |
eprintln!("warning: serving plain HTTP on a non-loopback address; put a TLS terminator in front or pass --tls-cert/--tls-key"); |
| 139 |
} |
| 140 |
oxy_server::run_blocking(listener, oxy_server::ServerConfig { root, tokens, tls })?; |
| 141 |
Ok(0) |
| 142 |
} |
| 143 |
|
| 144 |
pub fn git_export(git_dir: &std::path::Path) -> Result<i32> { |
| 145 |
let repo = open()?; |
| 146 |
let sp = ui::spinner("Exporting to Git…"); |
| 147 |
let r = repo.git_export(git_dir)?; |
| 148 |
sp.clear(); |
| 149 |
println!( |
| 150 |
"{} {} to {} as branch {} {}", |
| 151 |
ui::done("Exported"), |
| 152 |
plural(r.exported, "new state"), |
| 153 |
ui::path(git_dir.display()), |
| 154 |
ui::bookmark(&r.branch), |
| 155 |
ui::dim(format!("(currently {}; {} already exported)", plural(r.heads, "head"), r.skipped)) |
| 156 |
); |
| 157 |
Ok(0) |
| 158 |
} |
| 159 |
|
| 160 |
pub fn git_import(git_dir: &std::path::Path, dest: &std::path::Path, branch: &str) -> Result<i32> { |
| 161 |
let repo = Repo::init(dest)?; |
| 162 |
let started = std::time::Instant::now(); |
| 163 |
let sp = ui::spinner(&format!("Importing {} {branch}…", git_dir.display())); |
| 164 |
let r = repo.import_git(git_dir, branch)?; |
| 165 |
sp.clear(); |
| 166 |
println!( |
| 167 |
"{} {} ({} merges) from {} {} into {} {}", |
| 168 |
ui::done("Imported"), |
| 169 |
plural(r.commits, "commit"), |
| 170 |
r.merges, |
| 171 |
ui::path(git_dir.display()), |
| 172 |
ui::bookmark(branch), |
| 173 |
ui::path(repo.root().display()), |
| 174 |
ui::dim(format!("in {:.1?}; {} now has {}", started.elapsed(), repo.config.bookmark, plural(r.heads, "head"))) |
| 175 |
); |
| 176 |
if r.skipped_submodules > 0 { |
| 177 |
println!(" {} {}", ui::note(), ui::dim(format!("skipped {} submodule entries", r.skipped_submodules))); |
| 178 |
} |
| 179 |
Ok(0) |
| 180 |
} |
| 181 |
|
| 182 |
pub fn deepen(remote: &str, depth: u32) -> Result<i32> { |
| 183 |
let repo = open()?; |
| 184 |
if !repo.is_shallow() { |
| 185 |
println!("{}", ui::dim("This repository is not shallow; nothing to deepen.")); |
| 186 |
return Ok(0); |
| 187 |
} |
| 188 |
let sp = ui::spinner(&format!("Fetching {depth} more levels from {remote}…")); |
| 189 |
let n = repo.deepen(remote, depth)?; |
| 190 |
sp.done(&format!("{} {}; {}", ui::ok("Deepened by"), plural(n, "change"), if repo.is_shallow() { "still shallow".to_string() } else { ui::ok("history is now complete") })); |
| 191 |
Ok(0) |
| 192 |
} |