Onset: version control where nobody waits and nothing gets lost. The on command, its core and onset-server.
| 1 | //! The git bridge: git history in, and git history out to a host. |
| 2 | //! |
| 3 | //! Onset reads git repositories with the git on this machine (`git cat-file |
| 4 | //! --batch`). Each commit on the default branch's first-parent line becomes |
| 5 | //! a version; each git file and folder becomes an Onset object once, however |
| 6 | //! many commits share it. A git branch can come in as a change. Publishing |
| 7 | //! an imported project copies its original git history (branches and tags, |
| 8 | //! byte for byte) to the host's git view, which then carries on from it. |
| 9 |
| 10 | use std::collections::HashMap; |
| 11 | use std::io::{BufRead, BufReader, IsTerminal, Read, Write}; |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio}; |
| 14 | use std::time::Instant; |
| 15 |
| 16 | use anyhow::{Result, anyhow, bail}; |
| 17 | use base64::Engine; |
| 18 | use serde_json::json; |
| 19 | use onset_core::object::{Kind, Object}; |
| 20 | use onset_core::tree::{self, EntryKind, TreeEntry}; |
| 21 | use onset_core::version::Version; |
| 22 | use onset_core::Id; |
| 23 |
| 24 | use crate::app::App; |
| 25 | use crate::db::{Change, Project, Save, Workspace}; |
| 26 | use crate::ids; |
| 27 | use crate::objects::Store; |
| 28 | use crate::out::{Fail, Out, Report}; |
| 29 | use crate::ws::{self, Index}; |
| 30 |
| 31 | /// A git repository to read: one on this machine, or a temporary mirror of |
| 32 | /// a remote one (removed when dropped). |
| 33 | pub struct Repo { |
| 34 | pub dir: PathBuf, |
| 35 | temp: Option<PathBuf>, |
| 36 | } |
| 37 |
| 38 | impl Repo { |
| 39 | /// Bytes in its object IDs: 32 for a SHA-256 repository, 20 for SHA-1. |
| 40 | pub fn id_width(&self) -> usize { |
| 41 | match git(&self.dir, &["rev-parse", "--show-object-format"]) { |
| 42 | Ok(f) if f.trim() == "sha256" => 32, |
| 43 | _ => 20, |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 |
| 48 | impl Drop for Repo { |
| 49 | fn drop(&mut self) { |
| 50 | if let Some(t) = &self.temp { |
| 51 | let _ = std::fs::remove_dir_all(t); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 |
| 56 | fn git_missing() -> Fail { |
| 57 | Fail::new("failed", "reading git repositories needs git installed").state("nothing changed") |
| 58 | } |
| 59 |
| 60 | /// Runs git in `dir` and returns what it printed. |
| 61 | fn git(dir: &Path, args: &[&str]) -> Result<String, Fail> { |
| 62 | let out = Command::new("git").arg("--git-dir").arg(dir).args(args).stdin(Stdio::null()).output().map_err(|_| git_missing())?; |
| 63 | if !out.status.success() { |
| 64 | let err = String::from_utf8_lossy(&out.stderr).trim().to_string(); |
| 65 | return Err(Fail::new("failed", format!("git {}: {err}", args.first().unwrap_or(&""))).state("nothing changed")); |
| 66 | } |
| 67 | Ok(String::from_utf8_lossy(&out.stdout).into_owned()) |
| 68 | } |
| 69 |
| 70 | /// The clone URL for a project address or URL (Upstream projects clone from git.coline.app). |
| 71 | fn clone_url(text: &str) -> String { |
| 72 | let t = text.trim().trim_end_matches('/'); |
| 73 | let bare = t.strip_prefix("https://").or_else(|| t.strip_prefix("http://")).unwrap_or(t); |
| 74 | if let Some(rest) = bare.strip_prefix("upstream.codes/") { |
| 75 | let rest = rest.strip_suffix(".git").unwrap_or(rest); |
| 76 | if rest.split('/').count() == 2 { |
| 77 | return format!("https://git.coline.app/{rest}.git"); |
| 78 | } |
| 79 | } |
| 80 | if t.contains("://") || t.contains('@') { t.to_string() } else { format!("https://{t}") } |
| 81 | } |
| 82 |
| 83 | /// Opens a git repository: a folder on this machine, or a URL (mirrored to a temporary folder). |
| 84 | pub fn open(app: &App, source: &str) -> Result<Repo, Fail> { |
| 85 | open_as(app, source, None) |
| 86 | } |
| 87 |
| 88 | /// Like open, cloning with `token` as the password (through git's config |
| 89 | /// variables in the environment, so it isn't on the command line). |
| 90 | pub fn open_as(app: &App, source: &str, token: Option<&str>) -> Result<Repo, Fail> { |
| 91 | let local = app.cwd.join(source); |
| 92 | if local.exists() { |
| 93 | let out = Command::new("git").arg("-C").arg(&local).args(["rev-parse", "--absolute-git-dir"]).output().map_err(|_| git_missing())?; |
| 94 | if !out.status.success() { |
| 95 | return Err(Fail::new("bad_usage", format!("{source} isn't a git repository")).state("nothing changed")); |
| 96 | } |
| 97 | return Ok(Repo { dir: PathBuf::from(String::from_utf8_lossy(&out.stdout).trim()), temp: None }); |
| 98 | } |
| 99 | let url = clone_url(source); |
| 100 | let temp = crate::paths::home().join("tmp").join(format!("import-{}", ids::new_workspace_id())); |
| 101 | std::fs::create_dir_all(temp.parent().unwrap())?; |
| 102 | let mut clone = Command::new("git"); |
| 103 | if let Some(t) = token { |
| 104 | let basic = base64::engine::general_purpose::STANDARD.encode(format!("vcs:{t}")); |
| 105 | clone.env("GIT_CONFIG_COUNT", "1").env("GIT_CONFIG_KEY_0", "http.extraHeader").env("GIT_CONFIG_VALUE_0", format!("Authorization: Basic {basic}")); |
| 106 | } |
| 107 | let status = clone |
| 108 | .args(["clone", "--mirror", "--quiet", &url]) |
| 109 | .arg(&temp) |
| 110 | .stdin(Stdio::null()) |
| 111 | .status() |
| 112 | .map_err(|_| git_missing())?; |
| 113 | if !status.success() { |
| 114 | let _ = std::fs::remove_dir_all(&temp); |
| 115 | return Err(Fail::new("not_found", format!("couldn't clone {url}")).state("nothing changed")); |
| 116 | } |
| 117 | Ok(Repo { dir: temp.clone(), temp: Some(temp) }) |
| 118 | } |
| 119 |
| 120 | impl Repo { |
| 121 | /// The default branch's name and commit. |
| 122 | fn default_branch(&self) -> Result<(String, String), Fail> { |
| 123 | let head = git(&self.dir, &["symbolic-ref", "-q", "HEAD"]).unwrap_or_default(); |
| 124 | let name = head.trim().strip_prefix("refs/heads/").unwrap_or("HEAD").to_string(); |
| 125 | let sha = git(&self.dir, &["rev-parse", "--verify", "-q", "HEAD^{commit}"]) |
| 126 | .map_err(|_| Fail::new("bad_usage", "the git repository has no commits").state("nothing changed"))?; |
| 127 | Ok((name, sha.trim().to_string())) |
| 128 | } |
| 129 | } |
| 130 |
| 131 | /// `git cat-file --batch`: objects by ID, through one long-running git. |
| 132 | struct Cat { |
| 133 | child: Child, |
| 134 | stdin: ChildStdin, |
| 135 | out: BufReader<ChildStdout>, |
| 136 | } |
| 137 |
| 138 | impl Cat { |
| 139 | fn open(dir: &Path) -> Result<Cat> { |
| 140 | let mut child = Command::new("git") |
| 141 | .arg("--git-dir") |
| 142 | .arg(dir) |
| 143 | .args(["cat-file", "--batch"]) |
| 144 | .stdin(Stdio::piped()) |
| 145 | .stdout(Stdio::piped()) |
| 146 | .stderr(Stdio::null()) |
| 147 | .spawn() |
| 148 | .map_err(|_| git_missing().into_anyhow())?; |
| 149 | let stdin = child.stdin.take().unwrap(); |
| 150 | let out = BufReader::with_capacity(1 << 20, child.stdout.take().unwrap()); |
| 151 | Ok(Cat { child, stdin, out }) |
| 152 | } |
| 153 |
| 154 | fn get(&mut self, sha: &str) -> Result<(String, Vec<u8>)> { |
| 155 | writeln!(self.stdin, "{sha}")?; |
| 156 | self.stdin.flush()?; |
| 157 | let mut header = String::new(); |
| 158 | self.out.read_line(&mut header)?; |
| 159 | let parts: Vec<&str> = header.trim_end().split(' ').collect(); |
| 160 | if parts.len() != 3 { |
| 161 | bail!("git object {sha} is missing"); |
| 162 | } |
| 163 | let size: usize = parts[2].parse()?; |
| 164 | let mut data = vec![0; size]; |
| 165 | self.out.read_exact(&mut data)?; |
| 166 | let mut newline = [0u8; 1]; |
| 167 | self.out.read_exact(&mut newline)?; |
| 168 | Ok((parts[1].to_string(), data)) |
| 169 | } |
| 170 | } |
| 171 |
| 172 | impl Drop for Cat { |
| 173 | fn drop(&mut self) { |
| 174 | let _ = self.child.kill(); |
| 175 | let _ = self.child.wait(); |
| 176 | } |
| 177 | } |
| 178 |
| 179 | fn hex(bytes: &[u8]) -> String { |
| 180 | bytes.iter().map(|b| format!("{b:02x}")).collect() |
| 181 | } |
| 182 |
| 183 | /// Git trees and files as Onset objects, each converted once. |
| 184 | struct Converter<'a> { |
| 185 | store: &'a Store, |
| 186 | cat: Cat, |
| 187 | trees: HashMap<Vec<u8>, Option<Id>>, |
| 188 | files: HashMap<Vec<u8>, Id>, |
| 189 | /// Bytes in a git object ID: 20 (SHA-1), or 32 in a SHA-256 repository. |
| 190 | width: usize, |
| 191 | submodules: usize, |
| 192 | odd_names: usize, |
| 193 | } |
| 194 |
| 195 | impl<'a> Converter<'a> { |
| 196 | fn new(store: &'a Store, repo: &Repo) -> Result<Converter<'a>> { |
| 197 | Ok(Converter { store, cat: Cat::open(&repo.dir)?, trees: HashMap::new(), files: HashMap::new(), width: repo.id_width(), submodules: 0, odd_names: 0 }) |
| 198 | } |
| 199 |
| 200 | /// Entries of a git tree: (mode, name, id). |
| 201 | fn entries(&mut self, sha: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>, Vec<u8>)>> { |
| 202 | let (kind, data) = self.cat.get(&hex(sha))?; |
| 203 | if kind != "tree" { |
| 204 | bail!("git object {} isn't a tree", hex(sha)); |
| 205 | } |
| 206 | let mut out = Vec::new(); |
| 207 | let mut at = 0; |
| 208 | while at < data.len() { |
| 209 | let space = data[at..].iter().position(|&b| b == b' ').ok_or_else(|| anyhow!("bad git tree"))? + at; |
| 210 | let nul = data[space + 1..].iter().position(|&b| b == 0).ok_or_else(|| anyhow!("bad git tree"))? + space + 1; |
| 211 | let id = data.get(nul + 1..nul + 1 + self.width).ok_or_else(|| anyhow!("bad git tree"))?.to_vec(); |
| 212 | out.push((data[at..space].to_vec(), data[space + 1..nul].to_vec(), id)); |
| 213 | at = nul + 1 + self.width; |
| 214 | } |
| 215 | Ok(out) |
| 216 | } |
| 217 |
| 218 | /// A git tree as an Onset tree (None: nothing left in it, for a subfolder). |
| 219 | fn tree(&mut self, sha: Vec<u8>, root: bool) -> Result<Option<Id>> { |
| 220 | if let Some(t) = self.trees.get(&sha) { |
| 221 | return Ok(*t); |
| 222 | } |
| 223 | let mut entries = Vec::new(); |
| 224 | for (mode, name, id) in self.entries(&sha)? { |
| 225 | let Ok(name) = String::from_utf8(name) else { |
| 226 | self.odd_names += 1; |
| 227 | continue; |
| 228 | }; |
| 229 | // Onset's or git's own folder anywhere, a backslash: unsafe to write out. |
| 230 | if !tree::safe_name(&name) { |
| 231 | self.odd_names += 1; |
| 232 | continue; |
| 233 | } |
| 234 | let (kind, id) = match mode.as_slice() { |
| 235 | b"40000" | b"040000" => match self.tree(id, false)? { |
| 236 | Some(t) => (EntryKind::Tree, t), |
| 237 | None => continue, |
| 238 | }, |
| 239 | // A submodule: the commit it points at, kept as it is. |
| 240 | b"160000" => { |
| 241 | self.submodules += 1; |
| 242 | (EntryKind::Gitlink, self.store.put_file(hex(&id).as_bytes())?) |
| 243 | } |
| 244 | b"120000" => (EntryKind::Link, self.file(id)?), |
| 245 | m if m.ends_with(b"755") => (EntryKind::Exec, self.file(id)?), |
| 246 | _ => (EntryKind::File, self.file(id)?), |
| 247 | }; |
| 248 | entries.push(TreeEntry { name, kind, id }); |
| 249 | } |
| 250 | let out = if entries.is_empty() && !root { None } else { Some(self.store.put(&Object::new(Kind::Tree, tree::encode(&entries)))?) }; |
| 251 | self.trees.insert(sha, out); |
| 252 | Ok(out) |
| 253 | } |
| 254 |
| 255 | fn file(&mut self, sha: Vec<u8>) -> Result<Id> { |
| 256 | if let Some(f) = self.files.get(&sha) { |
| 257 | return Ok(*f); |
| 258 | } |
| 259 | let (_, data) = self.cat.get(&hex(&sha))?; |
| 260 | let id = self.store.put_file(&data)?; |
| 261 | self.files.insert(sha, id); |
| 262 | Ok(id) |
| 263 | } |
| 264 |
| 265 | /// Native ID → git ID for everything under a converted git tree. |
| 266 | fn ids_under(&mut self, sha: Vec<u8>, out: &mut Vec<(Id, String)>) -> Result<()> { |
| 267 | let Some(Some(native)) = self.trees.get(&sha).copied() else { return Ok(()) }; |
| 268 | out.push((native, hex(&sha))); |
| 269 | for (mode, _, id) in self.entries(&sha)? { |
| 270 | if mode == b"40000" || mode == b"040000" { |
| 271 | self.ids_under(id, out)?; |
| 272 | } else if mode == b"160000" { |
| 273 | // Not a git object of this repository. |
| 274 | } else if let Some(f) = self.files.get(&id) { |
| 275 | out.push((*f, hex(&id))); |
| 276 | } |
| 277 | } |
| 278 | Ok(()) |
| 279 | } |
| 280 | } |
| 281 |
| 282 | /// A git object ID's bytes: 40 hex digits (SHA-1), or 64 (SHA-256). |
| 283 | fn sha_bytes(text: &str) -> Result<Vec<u8>> { |
| 284 | let t = text.trim(); |
| 285 | if t.len() != 40 && t.len() != 64 { |
| 286 | bail!("{t} isn't a git object ID"); |
| 287 | } |
| 288 | (0..t.len() / 2).map(|i| Ok(u8::from_str_radix(&t[i * 2..i * 2 + 2], 16)?)).collect() |
| 289 | } |
| 290 |
| 291 | /// A change ID made from a git commit, so importing twice gives the same IDs. |
| 292 | fn change_id(sha: &str) -> String { |
| 293 | sha_bytes(sha).map(|b| b.iter().take(ids::CHANGE_ID_LEN).map(|x| ids::LETTERS[*x as usize % ids::LETTERS.len()] as char).collect()).unwrap_or_else(|_| ids::new_change_id()) |
| 294 | } |
| 295 |
| 296 | struct Commit { |
| 297 | sha: String, |
| 298 | tree: String, |
| 299 | author: String, |
| 300 | time: i64, |
| 301 | title: String, |
| 302 | body: String, |
| 303 | } |
| 304 |
| 305 | fn split_message(message: &str) -> (String, String) { |
| 306 | let mut parts = message.trim().splitn(2, '\n'); |
| 307 | (parts.next().unwrap_or("").trim().to_string(), parts.next().unwrap_or("").trim().to_string()) |
| 308 | } |
| 309 |
| 310 | /// Commits on `rev`'s first-parent line, oldest first. |
| 311 | fn commits(repo: &Repo, rev: &str, limit: Option<usize>) -> Result<Vec<Commit>, Fail> { |
| 312 | let mut args = vec!["log", "--first-parent", "--format=%H%x00%T%x00%an%x00%ae%x00%at%x00%B%x1e"]; |
| 313 | let n; |
| 314 | if let Some(l) = limit { |
| 315 | n = format!("-{l}"); |
| 316 | args.push(&n); |
| 317 | } else { |
| 318 | args.push("--reverse"); |
| 319 | } |
| 320 | args.push(rev); |
| 321 | let text = git(&repo.dir, &args)?; |
| 322 | let mut out = Vec::new(); |
| 323 | for record in text.split('\x1e') { |
| 324 | let record = record.trim_start_matches('\n'); |
| 325 | if record.is_empty() { |
| 326 | continue; |
| 327 | } |
| 328 | let f: Vec<&str> = record.splitn(6, '\0').collect(); |
| 329 | if f.len() != 6 { |
| 330 | continue; |
| 331 | } |
| 332 | let (title, body) = split_message(f[5]); |
| 333 | out.push(Commit { sha: f[0].into(), tree: f[1].into(), author: format!("{} <{}>", f[2], f[3]), time: f[4].parse().unwrap_or(0), title, body }); |
| 334 | } |
| 335 | Ok(out) |
| 336 | } |
| 337 |
| 338 | fn progress(app: &App, text: &str, last: &mut Instant) { |
| 339 | if !app.json && std::io::stderr().is_terminal() && last.elapsed().as_millis() > 500 { |
| 340 | eprint!("\r{text} "); |
| 341 | *last = Instant::now(); |
| 342 | } |
| 343 | } |
| 344 |
| 345 | fn notes_for(app: &App, conv: &Converter) { |
| 346 | if conv.submodules > 0 { |
| 347 | app.notes.borrow_mut().push(format!("{} submodules kept as pointers (their folders stay empty here)", conv.submodules)); |
| 348 | } |
| 349 | if conv.odd_names > 0 { |
| 350 | app.notes.borrow_mut().push(format!("{} paths were left out (names that aren't UTF-8, a .git or .onset folder, or a backslash)", conv.odd_names)); |
| 351 | } |
| 352 | } |
| 353 |
| 354 | /// `on import <repo> [folder] [--to host/owner/name]`: a new project with |
| 355 | /// the repository's history. `--change <ref>` instead brings one git branch |
| 356 | /// into the current workspace's project as a change. |
| 357 | pub fn import(app: &App, source: &str, to: Option<String>, folder: Option<PathBuf>, change: Option<String>) -> Out { |
| 358 | if let Some(rev) = change { |
| 359 | return import_change(app, source, &rev); |
| 360 | } |
| 361 | import_as(app, source, to, folder, None) |
| 362 | } |
| 363 |
| 364 | /// An import whose clone uses `token` (a project just switched on in Upstream: its first on get). |
| 365 | pub fn import_as(app: &App, source: &str, to: Option<String>, folder: Option<PathBuf>, token: Option<&str>) -> Out { |
| 366 | let repo = open_as(app, source, token)?; |
| 367 | let (branch, _) = repo.default_branch()?; |
| 368 | let name = clone_url(source).trim_end_matches('/').rsplit('/').next().unwrap_or("project").trim_end_matches(".git").to_string(); |
| 369 | let root = app.cwd.join(folder.unwrap_or_else(|| PathBuf::from(if name.is_empty() { "project".to_string() } else { name }))); |
| 370 | crate::app::ensure_dir_empty(&root)?; |
| 371 |
| 372 | let list = commits(&repo, "HEAD", None)?; |
| 373 | let project = Project { |
| 374 | id: format!("local-{}", ids::new_workspace_id()), |
| 375 | address: String::new(), |
| 376 | kind: "local".into(), |
| 377 | api: None, |
| 378 | checked: ids::now(), |
| 379 | }; |
| 380 | let mut conv = Converter::new(&app.store, &repo)?; |
| 381 | // What version 1 starts from. |
| 382 | app.store.put(&tree::empty())?; |
| 383 | let mut versions = Vec::with_capacity(list.len()); |
| 384 | let mut prev: Option<Id> = None; |
| 385 | let mut last = Instant::now(); |
| 386 | for (i, c) in list.iter().enumerate() { |
| 387 | progress(app, &format!("reading git history: {} of {} commits", i + 1, list.len()), &mut last); |
| 388 | let tree = conv.tree(sha_bytes(&c.tree)?, true)?.unwrap_or_else(tree::empty_id); |
| 389 | let v = Version { |
| 390 | n: i as u64 + 1, |
| 391 | tree, |
| 392 | prev, |
| 393 | change: change_id(&c.sha), |
| 394 | author: c.author.clone(), |
| 395 | time: c.time, |
| 396 | title: c.title.clone(), |
| 397 | body: c.body.clone(), |
| 398 | git: Some(c.sha.clone()), |
| 399 | }; |
| 400 | prev = Some(v.id(&project.id)); |
| 401 | versions.push(v); |
| 402 | } |
| 403 | if !app.json && std::io::stderr().is_terminal() { |
| 404 | eprint!("\r{}\r", " ".repeat(60)); |
| 405 | } |
| 406 | let main = versions.last().cloned().ok_or_else(|| Fail::new("bad_usage", "the git repository has no commits").state("nothing changed"))?; |
| 407 | let mut git_ids = Vec::new(); |
| 408 | conv.ids_under(sha_bytes(&list.last().unwrap().tree)?, &mut git_ids)?; |
| 409 | notes_for(app, &conv); |
| 410 |
| 411 | // The project, its history and a workspace on main. |
| 412 | std::fs::create_dir_all(&root)?; |
| 413 | let root = crate::sys::canonical(&root)?; |
| 414 | let project = Project { address: root.to_string_lossy().into_owned(), ..project }; |
| 415 | app.db.put_project(&project)?; |
| 416 | { |
| 417 | let tx = app.db.conn.unchecked_transaction()?; |
| 418 | for v in &versions { |
| 419 | app.db.put_version(&project.id, v)?; |
| 420 | } |
| 421 | tx.commit()?; |
| 422 | } |
| 423 | app.db.put_git_ids(&project.id, &git_ids)?; |
| 424 | let origin = if app.cwd.join(source).exists() { crate::sys::canonical(&app.cwd.join(source))?.to_string_lossy().into_owned() } else { clone_url(source) }; |
| 425 | app.db.set_import(&project.id, &origin, &branch)?; |
| 426 | let author = app.author(&project); |
| 427 | let c = Change::new(ids::new_change_id(), &project.id, main.tree, Some(main.n), &author); |
| 428 | app.db.put_change(&c)?; |
| 429 | let workspace = Workspace { id: ids::new_workspace_id(), project: project.id.clone(), path: root.clone(), change: c.id.clone(), only: vec![] }; |
| 430 | ws::write_id(&root, &workspace.id)?; |
| 431 | let files = app.files(&main.tree)?; |
| 432 | let mut index = ws::materialize(&root, &app.store, &Index::default(), &files, &[], &[], None)?; |
| 433 | index.save(&root)?; |
| 434 | app.db.put_workspace(&workspace)?; |
| 435 | drop(conv); |
| 436 | drop(repo); |
| 437 |
| 438 | let mut r = Report::new(); |
| 439 | r.line("imported", format!("{} commits from {branch} as versions 1 to {}", versions.len(), main.n)) |
| 440 | .line("workspace", root.display().to_string()) |
| 441 | .set("project", project.address.clone()) |
| 442 | .set("workspace", root.display().to_string()) |
| 443 | .set("versions", versions.len()) |
| 444 | .set("main", main.n); |
| 445 | if let Some(target) = to { |
| 446 | let cwd_app = App::open(app.json, root.clone()).map_err(Fail::from)?; |
| 447 | let published = crate::remote::publish(&cwd_app, &target)?; |
| 448 | for note in cwd_app.notes.borrow().iter() { |
| 449 | app.notes.borrow_mut().push(note.clone()); |
| 450 | } |
| 451 | r.line("published", target.clone()).set("published", serde_json::Value::Object(published.json.clone())); |
| 452 | r.next("keep working; on land lands on the host"); |
| 453 | } else { |
| 454 | r.next(format!("cd {} and keep working; on publish <host/owner/name> puts it on a host", root.display())); |
| 455 | } |
| 456 | Ok(r) |
| 457 | } |
| 458 |
| 459 | /// `on import <repo> --change <ref>`: the git branch `rev`, as a new change |
| 460 | /// in this workspace. It starts from where the branch left main. |
| 461 | fn import_change(app: &App, source: &str, rev: &str) -> Out { |
| 462 | let mut s = app.session()?; |
| 463 | let current = app.save(&mut s)?; |
| 464 | let repo = open(app, source)?; |
| 465 | let tip = git(&repo.dir, &["rev-parse", "--verify", "-q", &format!("{rev}^{{commit}}")]) |
| 466 | .map_err(|_| Fail::new("not_found", format!("{rev} isn't in {source}")).state("nothing changed"))? |
| 467 | .trim() |
| 468 | .to_string(); |
| 469 | // Where the branch left Onset: the newest commit on its first-parent line |
| 470 | // that is a version, named in the commit (the git view writes |
| 471 | // Onset-Version, and Vcs-Version before Onset had its name) or read by an import. |
| 472 | app.refresh(&s.project)?; |
| 473 | // Git commits are matched to versions by their git IDs, so all of |
| 474 | // main's history is needed here. |
| 475 | app.history(&s.project, 1)?; |
| 476 | let line = commits(&repo, &tip, Some(100_000))?; |
| 477 | let mut found = None; |
| 478 | for c in &line { |
| 479 | let named = c.body.lines().rev().find_map(|l| l.strip_prefix("Onset-Version: ").or_else(|| l.strip_prefix("Vcs-Version: ")).and_then(|n| n.trim().parse::<u64>().ok())); |
| 480 | let v = match named { |
| 481 | Some(n) => app.db.version(&s.project.id, n)?, |
| 482 | None => app.db.version_by_git(&s.project.id, &c.sha)?, |
| 483 | }; |
| 484 | if let Some(v) = v { |
| 485 | found = Some((c, v)); |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | let mut conv = Converter::new(&app.store, &repo)?; |
| 490 | if found.is_none() { |
| 491 | // No commit is one the project knows by ID (another copy of the |
| 492 | // history, a SHA-256 repository): a commit whose files are a version's. |
| 493 | for c in line.iter().take(1000) { |
| 494 | let tree = conv.tree(sha_bytes(&c.tree)?, true)?.unwrap_or_else(tree::empty_id); |
| 495 | if let Some(v) = app.db.version_by_tree(&s.project.id, &tree)? { |
| 496 | found = Some((c, v)); |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | let (base_commit, version) = found.ok_or_else(|| { |
| 502 | Fail::new("not_found", format!("{rev} doesn't start from any version of this project")) |
| 503 | .state("nothing changed") |
| 504 | .next("on import --change works on branches of this project's git view") |
| 505 | })?; |
| 506 | let tip_commit = line.first().ok_or_else(|| Fail::new("failed", "couldn't read the git history"))?; |
| 507 | // The change's base is the git view's tree (without private paths), so |
| 508 | // landing keeps whatever the view left out. |
| 509 | let base_tree = conv.tree(sha_bytes(&base_commit.tree)?, true)?.unwrap_or_else(tree::empty_id); |
| 510 | let head = conv.tree(sha_bytes(&tip_commit.tree)?, true)?.unwrap_or_else(tree::empty_id); |
| 511 | notes_for(app, &conv); |
| 512 | drop(conv); |
| 513 | if head == base_tree { |
| 514 | return Err(Fail::new("empty_change", format!("{rev} changes nothing since version {}", version.n)).state("nothing changed")); |
| 515 | } |
| 516 | let mut c = Change::new(ids::new_change_id(), &s.project.id, base_tree, Some(version.n), &app.author(&s.project)); |
| 517 | let (title, body) = (tip_commit.title.clone(), tip_commit.body.clone()); |
| 518 | c.title = title; |
| 519 | c.body = body; |
| 520 | c.head_tree = Some(head); |
| 521 | c.saves = 1; |
| 522 | app.db.put_change(&c)?; |
| 523 | app.db.add_save(&Save { change: c.id.clone(), n: 1, tree: head, base_tree, at: ids::now(), uploaded: false }, &s.ws.id)?; |
| 524 | app.show_tree(&s, &head)?; |
| 525 | app.put_workspace_change(&mut s, &c.id)?; |
| 526 | app.db.add_op(&s.ws.id, "new", &json!({ "from": current.id, "to": c.id }))?; |
| 527 | let _ = app.host(&s.project).publish_change(&app.db, &app.store, &s.project, &c, &s.ws.id); |
| 528 | let mut r = Report::new(); |
| 529 | r.line("change", format!("{} \"{}\", from git {} on version {}", app.short(&s.project, &c.id), c.title, &tip[..12], version.n)); |
| 530 | r.line("kept", format!("{} is saved; on switch goes back to it", app.short(&s.project, ¤t.id))); |
| 531 | r.set("change", json!({ "id": c.id, "title": c.title, "base_version": version.n, "git": tip })); |
| 532 | r.next("on land"); |
| 533 | Ok(r) |
| 534 | } |
| 535 |
| 536 | /// Copies an imported project's original git history to the host's git |
| 537 | /// view, so the view carries on from the same commits. Nothing happens if |
| 538 | /// the view's main is already there; a view with other history is left as |
| 539 | /// it is (the host pauses the view and says so). |
| 540 | pub fn push_history(app: &App, project: &Project, git_url: &str, token: &str) -> Result<Option<String>, Fail> { |
| 541 | let Some((source, branch)) = app.db.import_source(&project.id)? else { return Ok(None) }; |
| 542 | // Imported in place (the history came from the git view's own |
| 543 | // repository): it's there already, and git main may have moved on since. |
| 544 | if clone_url(&source).trim_end_matches('/').trim_end_matches(".git") == git_url.trim_end_matches('/').trim_end_matches(".git") { |
| 545 | return Ok(None); |
| 546 | } |
| 547 | // The newest version that came from git (later ones are native). |
| 548 | let Some(last) = app.db.versions(&project.id, i64::MAX)?.into_iter().find_map(|v| v.git) else { return Ok(None) }; |
| 549 | let auth = format!("Authorization: Basic {}", base64::engine::general_purpose::STANDARD.encode(format!("vcs:{token}"))); |
| 550 | // The token goes in git's environment, not its arguments. |
| 551 | let with_auth = |cmd: &mut Command| { |
| 552 | cmd.env("GIT_CONFIG_COUNT", "1").env("GIT_CONFIG_KEY_0", "http.extraHeader").env("GIT_CONFIG_VALUE_0", &auth).env("GIT_TERMINAL_PROMPT", "0"); |
| 553 | }; |
| 554 | let mut ls = Command::new("git"); |
| 555 | ls.args(["ls-remote", git_url, "refs/heads/main"]).stdin(Stdio::null()).stderr(Stdio::null()); |
| 556 | with_auth(&mut ls); |
| 557 | let out = ls.output().map_err(|_| git_missing())?; |
| 558 | if out.status.success() && String::from_utf8_lossy(&out.stdout).split_whitespace().next() == Some(last.as_str()) { |
| 559 | return Ok(None); |
| 560 | } |
| 561 | let repo = open(app, &source)?; |
| 562 | // Every branch and tag as it is, and main at the last imported commit. |
| 563 | let _ = branch; |
| 564 | let mut refspecs = vec!["refs/tags/*:refs/tags/*".to_string(), format!("{last}:refs/heads/main")]; |
| 565 | for name in git(&repo.dir, &["for-each-ref", "--format=%(refname)", "refs/heads"])?.lines() { |
| 566 | if name != "refs/heads/main" && !name.is_empty() { |
| 567 | refspecs.push(format!("{name}:{name}")); |
| 568 | } |
| 569 | } |
| 570 | let mut push = Command::new("git"); |
| 571 | push.arg("--git-dir").arg(&repo.dir).args(["push", "--quiet", git_url]).args(&refspecs).stdin(Stdio::null()); |
| 572 | with_auth(&mut push); |
| 573 | let out = push.output().map_err(|_| git_missing())?; |
| 574 | if !out.status.success() { |
| 575 | return Ok(Some(format!( |
| 576 | "the git view at {git_url} already has other history, so it stays paused: {}", |
| 577 | String::from_utf8_lossy(&out.stderr).lines().last().unwrap_or("").trim() |
| 578 | ))); |
| 579 | } |
| 580 | Ok(Some(format!("copied the git history to {git_url}"))) |
| 581 | } |
| 582 |
| 583 | // A mirror: main to a branch of any git repository (GitHub, GitLab, a |
| 584 | // self-hosted Gitea, a bare repository), and back. |
| 585 |
| 586 | fn mirror_table(app: &App) -> Result<()> { |
| 587 | app.db.conn.execute_batch( |
| 588 | "CREATE TABLE IF NOT EXISTS git_mirror (project TEXT NOT NULL, url TEXT NOT NULL, n INTEGER NOT NULL, sha TEXT NOT NULL, PRIMARY KEY (project, url, n)); |
| 589 | CREATE TABLE IF NOT EXISTS git_mirror_in (project TEXT NOT NULL, url TEXT NOT NULL, n INTEGER NOT NULL, sha TEXT NOT NULL, PRIMARY KEY (project, url, n));", |
| 590 | )?; |
| 591 | Ok(()) |
| 592 | } |
| 593 |
| 594 | /// `on git mirror <url> [--branch main] [--once] [--every <secs>]`: keeps a |
| 595 | /// git repository's branch as main, both ways. Each new version of main |
| 596 | /// becomes a commit there (its message, author and time, with Onset-Version |
| 597 | /// and Onset-Change trailers; the same version always makes the same |
| 598 | /// commit), pushed as a fast-forward with git's own credentials. Commits |
| 599 | /// that reach the branch another way (a pull request merged on GitHub, a |
| 600 | /// git push) come into main as a version, and the next commit joins both |
| 601 | /// lines, so neither side loses anything. Runs until stopped, or once. |
| 602 | pub fn mirror(app: &App, url: &str, branch: &str, once: bool, every: u64) -> Out { |
| 603 | let s = app.session()?; |
| 604 | let project = s.project.clone(); |
| 605 | if project.hosted() && !crate::remote::path_rules(app, &project)?.is_empty() { |
| 606 | return Err(Fail::new("not_allowed", "this project has private folders, and a mirror would copy them out") |
| 607 | .state("nothing changed") |
| 608 | .next("on git shows its git view, which leaves them out: mirror that instead")); |
| 609 | } |
| 610 | mirror_table(app)?; |
| 611 | let dir = crate::gitdir::repository(&project.id).map_err(Fail::from)?; |
| 612 | let repo = Repo { dir, temp: None }; |
| 613 | let mut r = Report::new(); |
| 614 | loop { |
| 615 | match mirror_round(app, &s, &repo, url, branch) { |
| 616 | Ok(lines) => { |
| 617 | for (label, text) in lines { |
| 618 | if once { |
| 619 | r.line(&label, text); |
| 620 | } else { |
| 621 | println!("{label:<10} {text}"); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | Err(f) if once => return Err(f), |
| 626 | Err(f) => println!("{:<10} {}", "waiting", f.message), |
| 627 | } |
| 628 | if once { |
| 629 | return Ok(r); |
| 630 | } |
| 631 | std::thread::sleep(std::time::Duration::from_secs(every)); |
| 632 | } |
| 633 | } |
| 634 |
| 635 | fn mirror_round(app: &App, s: &crate::app::Session, repo: &Repo, url: &str, branch: &str) -> Result<Vec<(String, String)>, Fail> { |
| 636 | let project = &s.project; |
| 637 | let mut said = Vec::new(); |
| 638 | let key = { |
| 639 | use sha2::{Digest, Sha256}; |
| 640 | Sha256::digest(url.as_bytes())[..6].iter().map(|b| format!("{b:02x}")).collect::<String>() |
| 641 | }; |
| 642 | let tracking = format!("refs/onset-mirror/{key}"); |
| 643 | // The branch as it is there (none yet: an empty repository). |
| 644 | let listed = git(&repo.dir, &["ls-remote", url, &format!("refs/heads/{branch}")])?; |
| 645 | let remote_head = match listed.split_whitespace().next() { |
| 646 | Some(_) => { |
| 647 | git(&repo.dir, &["fetch", "--quiet", "--no-tags", url, &format!("+refs/heads/{branch}:{tracking}")])?; |
| 648 | Some(git(&repo.dir, &["rev-parse", &tracking])?.trim().to_string()) |
| 649 | } |
| 650 | None => None, |
| 651 | }; |
| 652 | let Some(main) = app.refresh(project)? else { return Ok(said) }; |
| 653 | app.history(project, 1)?; |
| 654 | let last: Option<(u64, String)> = app |
| 655 | .db |
| 656 | .conn |
| 657 | .query_row("SELECT n, sha FROM git_mirror WHERE project = ?1 AND url = ?2 ORDER BY n DESC LIMIT 1", rusqlite::params![project.id, url], |r| Ok((r.get::<_, i64>(0)? as u64, r.get(1)?))) |
| 658 | .ok(); |
| 659 | let host = app.host(project); |
| 660 | // Where the branch and main meet: the last version mirrored, or (the first |
| 661 | // time) the newest commit there that is a version of this project. |
| 662 | let (mut at_n, mut at_sha) = match (&last, &remote_head) { |
| 663 | (Some(l), _) => (l.0, Some(l.1.clone())), |
| 664 | (None, None) => (0, None), |
| 665 | (None, Some(head)) => { |
| 666 | let line = commits(repo, head, Some(100_000))?; |
| 667 | let mut conv = Converter::new(&app.store, repo)?; |
| 668 | let mut found = None; |
| 669 | for c in &line { |
| 670 | let named = c.body.lines().rev().find_map(|l| l.strip_prefix("Onset-Version: ").or_else(|| l.strip_prefix("Vcs-Version: ")).and_then(|n| n.trim().parse::<u64>().ok())); |
| 671 | let by_id = match named { |
| 672 | Some(n) => app.db.version(&project.id, n)?, |
| 673 | None => app.db.version_by_git(&project.id, &c.sha)?, |
| 674 | }; |
| 675 | let v = match by_id { |
| 676 | Some(v) => Some(v), |
| 677 | None => { |
| 678 | let t = conv.tree(sha_bytes(&c.tree)?, true)?.unwrap_or_else(tree::empty_id); |
| 679 | app.db.version_by_tree(&project.id, &t)? |
| 680 | } |
| 681 | }; |
| 682 | if let Some(v) = v { |
| 683 | found = Some((v.n, c.sha.clone())); |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | let (n, sha) = found.ok_or_else(|| { |
| 688 | Fail::new("bad_usage", format!("{url}'s {branch} shares no history with this project")) |
| 689 | .state("nothing changed") |
| 690 | .next("mirror to an empty repository, or to the one this project was imported from") |
| 691 | })?; |
| 692 | remember_mirror(app, project, url, n, &sha)?; |
| 693 | (n, Some(sha)) |
| 694 | } |
| 695 | }; |
| 696 | // Commits that reached the branch another way: they come into main. |
| 697 | if let (Some(head), Some(sha)) = (&remote_head, &at_sha) |
| 698 | && head != sha |
| 699 | { |
| 700 | if git(&repo.dir, &["merge-base", "--is-ancestor", sha, head]).is_err() { |
| 701 | return Err(Fail::new("conflict", format!("{url}'s {branch} was rewritten (a force push), so the mirror won't write over it")) |
| 702 | .state("nothing changed") |
| 703 | .next(format!("put {branch} back on top of {}, or mirror to a new branch", &sha[..12]))); |
| 704 | } |
| 705 | let new_commits = commits(repo, &format!("{sha}..{head}"), Some(1000))?; |
| 706 | let tip = new_commits.first().ok_or_else(|| Fail::new("failed", "couldn't read the branch's new commits"))?; |
| 707 | let mut conv = Converter::new(&app.store, repo)?; |
| 708 | let head_tree = conv.tree(sha_bytes(&tip.tree)?, true)?.unwrap_or_else(tree::empty_id); |
| 709 | drop(conv); |
| 710 | let base = app.version(project, at_n)?.map(|v| v.tree).unwrap_or_else(tree::empty_id); |
| 711 | if head_tree != base { |
| 712 | let title = if new_commits.len() == 1 { tip.title.clone() } else { format!("{} (and {} more commits from git)", tip.title, new_commits.len() - 1) }; |
| 713 | let body = format!("{}\n\nFrom git {} on {url} {branch}.", tip.body, &head[..12]).trim_start().to_string(); |
| 714 | let request = crate::host::LandRequest { |
| 715 | change: ids::new_change_id(), |
| 716 | base_tree: base, |
| 717 | tree: head_tree, |
| 718 | title, |
| 719 | body, |
| 720 | author: tip.author.clone(), |
| 721 | allow_secrets: true, |
| 722 | record: None, |
| 723 | at: None, |
| 724 | signature: None, |
| 725 | line: None, |
| 726 | undoes: None, |
| 727 | }; |
| 728 | match host.land(&app.db, &app.store, project, &request)? { |
| 729 | crate::host::Landed::Landed { version } => { |
| 730 | app.db.put_version(&project.id, &version)?; |
| 731 | // Nothing else landed meanwhile: the version is that very commit. |
| 732 | if version.n == at_n + 1 && version.tree == head_tree { |
| 733 | remember_mirror(app, project, url, version.n, head)?; |
| 734 | at_n = version.n; |
| 735 | at_sha = Some(head.clone()); |
| 736 | } else { |
| 737 | app.db.conn.execute("INSERT OR REPLACE INTO git_mirror_in (project, url, n, sha) VALUES (?1, ?2, ?3, ?4)", rusqlite::params![project.id, url, version.n as i64, head])?; |
| 738 | } |
| 739 | said.push(("came in".into(), format!("{} from git as main {}", plural(new_commits.len(), "commit"), version.n))); |
| 740 | } |
| 741 | crate::host::Landed::Conflict { conflicts, .. } => { |
| 742 | return Err(Fail::new("conflict", format!("{url}'s {branch} and main changed the same lines: {}", conflicts.iter().map(|c| c.path.as_str()).collect::<Vec<_>>().join(", "))) |
| 743 | .state("nothing changed") |
| 744 | .next(format!("on import {url} --change {branch} in a workspace, fix it, on land; the mirror carries on after"))); |
| 745 | } |
| 746 | other => return Err(Fail::new("waiting", format!("bringing in {url}'s {branch}: {other:?}")).state("nothing changed")), |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | let main = app.refresh(project)?.unwrap_or(main); |
| 751 | if main.n <= at_n { |
| 752 | return Ok(said); |
| 753 | } |
| 754 | // Each version since: a commit, with only what changed written. |
| 755 | let mut fresh_blobs: Vec<(Id, String)> = Vec::new(); |
| 756 | let mut stream: Vec<u8> = Vec::new(); |
| 757 | let mut prev_tree = app.version(project, at_n)?.map(|v| v.tree).unwrap_or_else(tree::empty_id); |
| 758 | let mut marks: Vec<(u64, u64)> = Vec::new(); |
| 759 | let mut next_mark = 0u64; |
| 760 | let out_ref = format!("{tracking}-out"); |
| 761 | for n in at_n + 1..=main.n { |
| 762 | let v = app.version(project, n)?.ok_or_else(|| Fail::new("not_found", format!("version {n} isn't here")))?; |
| 763 | host.fetch_tree(&app.store, project, &v.tree, Some(&prev_tree))?; |
| 764 | let changed = tree::diff_trees(&app.store, &prev_tree, &v.tree)?; |
| 765 | let mut lines: Vec<String> = Vec::new(); |
| 766 | for (path, change) in changed { |
| 767 | let entry = match change { |
| 768 | tree::Change::Removed(_) => { |
| 769 | lines.push(format!("D {}", crate::gitdir::fast_import_path(&path))); |
| 770 | continue; |
| 771 | } |
| 772 | tree::Change::Added(e) | tree::Change::Modified(_, e) => e, |
| 773 | }; |
| 774 | let mode = match entry.kind { |
| 775 | EntryKind::File => "100644", |
| 776 | EntryKind::Exec => "100755", |
| 777 | EntryKind::Link => "120000", |
| 778 | EntryKind::Gitlink => { |
| 779 | let data = app.store.read_file(&entry.id)?; |
| 780 | lines.push(format!("M 160000 {} {}", String::from_utf8_lossy(&data).trim(), crate::gitdir::fast_import_path(&path))); |
| 781 | continue; |
| 782 | } |
| 783 | EntryKind::Tree => continue, |
| 784 | }; |
| 785 | let sha = match crate::gitdir::known_blob(&app.db, &project.id, &entry.id)? { |
| 786 | Some(sha) if git(&repo.dir, &["cat-file", "-e", &sha]).is_ok() => sha, |
| 787 | _ => { |
| 788 | let data = crate::seal::open(app.store.read_file(&entry.id)?); |
| 789 | let sha = onset_core::git::hex(&onset_core::git::object_id(onset_core::git::GitKind::Blob, &data)); |
| 790 | next_mark += 1; |
| 791 | stream.extend_from_slice(format!("blob\nmark :{next_mark}\ndata {}\n", data.len()).as_bytes()); |
| 792 | stream.extend_from_slice(&data); |
| 793 | stream.push(b'\n'); |
| 794 | fresh_blobs.push((entry.id, sha.clone())); |
| 795 | sha |
| 796 | } |
| 797 | }; |
| 798 | lines.push(format!("M {mode} {sha} {}", crate::gitdir::fast_import_path(&path))); |
| 799 | } |
| 800 | let (name, email) = match v.author.rsplit_once(" <") { |
| 801 | Some((n, e)) => (n.to_string(), e.trim_end_matches('>').to_string()), |
| 802 | None => (v.author.clone(), String::new()), |
| 803 | }; |
| 804 | let mut message = v.title.clone(); |
| 805 | if !v.body.trim().is_empty() { |
| 806 | message.push_str("\n\n"); |
| 807 | message.push_str(v.body.trim()); |
| 808 | } |
| 809 | message.push_str(&format!("\n\nOnset-Version: {n}\nOnset-Change: {}\n", v.change)); |
| 810 | next_mark += 1; |
| 811 | marks.push((n, next_mark)); |
| 812 | stream.extend_from_slice(format!("commit {out_ref}\nmark :{next_mark}\nauthor {name} <{email}> {} +0000\ncommitter {name} <{email}> {} +0000\ndata {}\n{message}\n", v.time, v.time, message.len()).as_bytes()); |
| 813 | // The first carries on from where the branch is (an empty branch: no parent). |
| 814 | if n == at_n + 1 |
| 815 | && let Some(p) = &at_sha |
| 816 | { |
| 817 | stream.extend_from_slice(format!("from {p}\n").as_bytes()); |
| 818 | } |
| 819 | // A version that brought in git's commits joins both lines. |
| 820 | let joined: Option<String> = app |
| 821 | .db |
| 822 | .conn |
| 823 | .query_row("SELECT sha FROM git_mirror_in WHERE project = ?1 AND url = ?2 AND n = ?3", rusqlite::params![project.id, url, n as i64], |r| r.get(0)) |
| 824 | .ok(); |
| 825 | if let Some(j) = joined { |
| 826 | stream.extend_from_slice(format!("merge {j}\n").as_bytes()); |
| 827 | } |
| 828 | for l in &lines { |
| 829 | stream.extend_from_slice(l.as_bytes()); |
| 830 | stream.push(b'\n'); |
| 831 | } |
| 832 | stream.push(b'\n'); |
| 833 | prev_tree = v.tree; |
| 834 | } |
| 835 | let marks_file = crate::paths::home().join("tmp").join(format!("mirror-{key}.marks")); |
| 836 | std::fs::create_dir_all(marks_file.parent().unwrap())?; |
| 837 | if at_sha.is_none() { |
| 838 | // A first commit has no parent: the ref starts empty. |
| 839 | let _ = git(&repo.dir, &["update-ref", "-d", &out_ref]); |
| 840 | } |
| 841 | let mut child = Command::new("git") |
| 842 | .arg("--git-dir") |
| 843 | .arg(&repo.dir) |
| 844 | .args(["fast-import", "--quiet", "--date-format=raw", "--force"]) |
| 845 | .arg(format!("--export-marks={}", marks_file.display())) |
| 846 | .stdin(Stdio::piped()) |
| 847 | .stdout(Stdio::null()) |
| 848 | .stderr(Stdio::piped()) |
| 849 | .spawn() |
| 850 | .map_err(|_| git_missing())?; |
| 851 | child.stdin.take().unwrap().write_all(&stream)?; |
| 852 | let out = child.wait_with_output()?; |
| 853 | if !out.status.success() { |
| 854 | return Err(Fail::new("failed", format!("git fast-import: {}", String::from_utf8_lossy(&out.stderr).trim())).state("nothing changed")); |
| 855 | } |
| 856 | let exported = std::fs::read_to_string(&marks_file)?; |
| 857 | let _ = std::fs::remove_file(&marks_file); |
| 858 | let by_mark: HashMap<u64, String> = exported.lines().filter_map(|l| l.strip_prefix(':')?.split_once(' ').and_then(|(m, s)| Some((m.parse().ok()?, s.to_string())))).collect(); |
| 859 | for (file, sha) in &fresh_blobs { |
| 860 | crate::gitdir::remember_blob(&app.db, &project.id, file, sha)?; |
| 861 | } |
| 862 | let mut made = Vec::new(); |
| 863 | for (n, mark) in &marks { |
| 864 | let sha = by_mark.get(mark).ok_or_else(|| Fail::new("failed", format!("git fast-import made no commit for version {n}")))?; |
| 865 | made.push((*n, sha.clone())); |
| 866 | } |
| 867 | let (tip_n, tip_sha) = made.last().cloned().ok_or_else(|| Fail::new("failed", "nothing to mirror"))?; |
| 868 | // --no-verify: the repository's hooks are for people in workspaces, not for the mirror. |
| 869 | match git(&repo.dir, &["push", "--quiet", "--no-verify", url, &format!("{tip_sha}:refs/heads/{branch}")]) { |
| 870 | Ok(_) => { |
| 871 | // Only what the branch has is remembered as mirrored. |
| 872 | for (n, sha) in &made { |
| 873 | remember_mirror(app, project, url, *n, sha)?; |
| 874 | } |
| 875 | said.push(("mirrored".into(), format!("main {} to {url} {branch} (commit {})", tip_n, &tip_sha[..12]))); |
| 876 | } |
| 877 | // The branch moved meanwhile: the next round brings that in first. |
| 878 | Err(f) => said.push(("waiting".into(), format!("{url}'s {branch} moved while mirroring; the next round brings it in ({})", f.message))), |
| 879 | } |
| 880 | Ok(said) |
| 881 | } |
| 882 |
| 883 | fn remember_mirror(app: &App, project: &Project, url: &str, n: u64, sha: &str) -> Result<(), Fail> { |
| 884 | app.db.conn.execute("INSERT OR REPLACE INTO git_mirror (project, url, n, sha) VALUES (?1, ?2, ?3, ?4)", rusqlite::params![project.id, url, n as i64, sha])?; |
| 885 | Ok(()) |
| 886 | } |
| 887 |
| 888 | fn plural(n: usize, one: &str) -> String { |
| 889 | if n == 1 { format!("1 {one}") } else { format!("{n} {one}s") } |
| 890 | } |
| 891 |