//! Git bridge (spec ยง23), export side: each state the current bookmark //! passed through becomes a Git commit through `git fast-import`, with the //! state's full tree and the previous state's commit as parent. A state //! that added exactly one change carries that change's message, author, //! and `Change-Id`/`Oxy-Hash` trailers; every commit carries `Oxy-State`. //! //! Marks make re-exports incremental: `.oxy/git//git-marks` is //! git's own marks file and `oxy-marks` maps state refs to marks. use super::*; use std::process::{Command, Stdio}; #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct GitExportReport { pub exported: usize, pub skipped: usize, pub branch: String, pub heads: usize, } fn git(dir: &Path, args: &[&str]) -> Result { let out = Command::new("git").arg("-C").arg(dir).args(args).output().map_err(|e| RepoError::Msg(format!("running git: {e}")))?; if !out.status.success() { return fail(format!("git {} failed: {}", args.join(" "), String::from_utf8_lossy(&out.stderr).trim())); } Ok(String::from_utf8_lossy(&out.stdout).trim().to_string()) } impl Repo { fn git_marks_dir(&self, git_dir: &Path) -> Result { let canonical = git_dir.canonicalize().unwrap_or_else(|_| git_dir.to_path_buf()); let key = hash_object(ObjectType::Chunk, canonical.to_string_lossy().as_bytes()).short(16); let dir = self.oxy.join("git").join(key); fs::create_dir_all(&dir)?; Ok(dir) } fn read_oxy_marks(path: &Path) -> Result> { let mut out = BTreeMap::new(); if let Ok(text) = fs::read_to_string(path) { for line in text.lines() { if let Some((h, m)) = line.split_once(' ') { if let (Some(h), Ok(m)) = (ObjectRef::from_hex(h), m.parse()) { out.insert(h, m); } } } } Ok(out) } /// Export the current bookmark to the Git repository at `git_dir`, /// creating a bare one if nothing is there. Each state the bookmark /// passed through (per the operation log) becomes one commit whose tree /// is that state, so the Git branch is a linear history of full trees. pub fn git_export(&self, git_dir: &Path) -> Result { if !git_dir.join("HEAD").exists() && !git_dir.join(".git").exists() { fs::create_dir_all(git_dir)?; git(git_dir, &["init", "--bare", "-q"])?; } let marks_dir = self.git_marks_dir(git_dir)?; let git_marks = marks_dir.join("git-marks"); let oxy_marks_path = marks_dir.join("oxy-marks"); let mut marks = Self::read_oxy_marks(&oxy_marks_path)?; let mut next_mark = marks.values().copied().max().unwrap_or(0) + 1; let bookmark = self.config.bookmark.clone(); let mut stream: Vec = Vec::new(); let mut report = GitExportReport { branch: bookmark.clone(), heads: self.bookmark_heads()?.len(), ..Default::default() }; // States along the bookmark, oldest first, skipping unchanged and pruned ones. let mut ops = self.op_log()?; ops.reverse(); let mut prev: Option<(ObjectRef, Vec)> = None; let mut last_mark: Option = None; for (_, op) in &ops { let Some(state_ref) = op.view.bookmarks.get(&bookmark).copied() else { continue }; if prev.as_ref().is_some_and(|(p, _)| *p == state_ref) { continue; } let Ok(state) = self.store.get_state(&state_ref) else { continue }; if state.heads.is_empty() { prev = Some((state_ref, vec![])); continue; } if let Some(m) = marks.get(&state_ref) { report.skipped += 1; last_mark = Some(*m); prev = Some((state_ref, state.heads.clone())); continue; } let prev_heads = prev.as_ref().map(|(_, h)| h.clone()).unwrap_or_default(); let added = self.negotiate(&prev_heads, &state.heads).unwrap_or_default(); let loaded = self.load_state(&state.heads)?; let snapshot = loaded.materialize(); let (author, secs, mut message) = match added.as_slice() { [h] => { let c = self.store.get_change(h)?; let mut m = c.message.clone(); if !m.ends_with('\n') { m.push('\n'); } m.push_str(&format!("\nChange-Id: {}\nOxy-Hash: {}\n", c.change_id.to_hex(), h.to_hex())); (self.git_author(&c), c.hlc.wall_ms / 1000, m) } [] => ("oxy ".to_string(), op.hlc.wall_ms / 1000, format!("oxy: {}\n\n", op.command)), many => { let mut m = format!("oxy: {} ({} changes)\n\n", op.command, many.len()); for h in many { let c = self.store.get_change(h)?; m.push_str(&format!("- {} {}\n", c.change_id.short(8), c.message.lines().next().unwrap_or(""))); } m.push_str(&format!("\nOxy-Changes: {}\n", many.iter().map(|h| h.to_hex()).collect::>().join(" "))); ("oxy ".to_string(), op.hlc.wall_ms / 1000, m) } }; message.push_str(&format!("Oxy-State: {}\n", state_ref.to_hex())); if snapshot.is_conflicted() { message.push_str("Oxy-Conflicted: true\n"); } let mark = next_mark; next_mark += 1; let parents: Vec = last_mark.into_iter().collect(); self.write_commit(&mut stream, "refs/oxy/export", mark, &author, secs, &message, &parents, &snapshot)?; marks.insert(state_ref, mark); last_mark = Some(mark); prev = Some((state_ref, state.heads.clone())); report.exported += 1; } match last_mark { Some(m) => stream.extend_from_slice(format!("reset refs/heads/{bookmark}\nfrom :{m}\n\ndone\n").as_bytes()), None => stream.extend_from_slice(b"done\n"), } let mut cmd = Command::new("git"); // `--force`: the scratch ref may jump between unrelated states. cmd.arg("-C").arg(git_dir).args(["fast-import", "--quiet", "--done", "--force"]).arg(format!("--export-marks={}", git_marks.display())); if git_marks.exists() { cmd.arg(format!("--import-marks={}", git_marks.display())); } let mut child = cmd.stdin(Stdio::piped()).stdout(Stdio::null()).stderr(Stdio::piped()).spawn().map_err(|e| RepoError::Msg(format!("running git fast-import: {e}")))?; child.stdin.take().unwrap().write_all(&stream)?; let out = child.wait_with_output()?; if !out.status.success() { return fail(format!("git fast-import failed: {}", String::from_utf8_lossy(&out.stderr).trim())); } let text: String = marks.iter().map(|(h, m)| format!("{} {m}\n", h.to_hex())).collect(); write_atomic(&oxy_marks_path, text.as_bytes())?; let _ = git(git_dir, &["update-ref", "-d", "refs/oxy/export"]); Ok(report) } fn git_author(&self, c: &Change) -> String { match self.store.get_identity(&c.author) { Ok(id) if !id.name.is_empty() => format!("{} <{}>", id.name.replace(['<', '>', '\n'], " "), id.email.replace(['<', '>', '\n', ' '], "")), _ => "oxy ".to_string(), } } #[allow(clippy::too_many_arguments)] fn write_commit(&self, out: &mut Vec, reference: &str, mark: u64, author: &str, secs: u64, message: &str, parents: &[u64], snapshot: &Snapshot) -> Result<()> { out.extend_from_slice(format!("commit {reference}\nmark :{mark}\nauthor {author} {secs} +0000\ncommitter {author} {secs} +0000\ndata {}\n{message}\n", message.len()).as_bytes()); for (i, p) in parents.iter().enumerate() { out.extend_from_slice(format!("{} :{p}\n", if i == 0 { "from" } else { "merge" }).as_bytes()); } out.extend_from_slice(b"deleteall\n"); for (path, f) in &snapshot.files { let (mode, bytes): (&str, Vec) = match (&f.kind, &f.content) { (NodeKind::Dir, _) => continue, (NodeKind::Symlink, FileContent::Opaque { blobs }) => ("120000", blobs.first().map(|b| self.read_blob(b)).transpose()?.unwrap_or_default()), (_, FileContent::Text { bytes, .. }) => (if f.mode.exec { "100755" } else { "100644" }, bytes.clone()), (_, FileContent::Opaque { blobs }) => (if f.mode.exec { "100755" } else { "100644" }, blobs.first().map(|b| self.read_blob(b)).transpose()?.unwrap_or_default()), (_, FileContent::Dir) => continue, }; out.extend_from_slice(format!("M {mode} inline {path}\ndata {}\n", bytes.len()).as_bytes()); out.extend_from_slice(&bytes); out.push(b'\n'); } out.push(b'\n'); Ok(()) } }