//! 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 GitImportReport { pub commits: usize, pub merges: usize, pub skipped_submodules: usize, pub heads: usize, } /// A running `git cat-file --batch`, for reading many blobs cheaply. struct CatFile { child: std::process::Child, stdin: std::process::ChildStdin, stdout: std::io::BufReader, } impl CatFile { fn start(git_dir: &Path) -> Result { let mut child = Command::new("git") .arg("-C") .arg(git_dir) .args(["cat-file", "--batch"]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::null()) .spawn() .map_err(|e| RepoError::Msg(format!("running git cat-file: {e}")))?; let stdin = child.stdin.take().unwrap(); let stdout = std::io::BufReader::new(child.stdout.take().unwrap()); Ok(CatFile { child, stdin, stdout }) } fn read(&mut self, sha: &str) -> Result> { use std::io::{BufRead, Read}; writeln!(self.stdin, "{sha}")?; self.stdin.flush()?; let mut header = String::new(); self.stdout.read_line(&mut header)?; let parts: Vec<&str> = header.trim_end().split(' ').collect(); if parts.len() != 3 { return fail(format!("git cat-file: object {sha} not found")); } let size: usize = parts[2].parse().map_err(|_| RepoError::Msg("git cat-file: bad size".into()))?; let mut body = vec![0u8; size]; self.stdout.read_exact(&mut body)?; let mut nl = [0u8; 1]; self.stdout.read_exact(&mut nl)?; Ok(body) } } impl Drop for CatFile { fn drop(&mut self) { let _ = self.child.kill(); let _ = self.child.wait(); } } /// One commit as `git log` describes it. struct GitCommit { sha: String, parents: Vec, author_secs: u64, author: String, message: String, } fn list_commits(git_dir: &Path, branch: &str) -> Result> { let out = Command::new("git") .arg("-C") .arg(git_dir) .args(["log", "--reverse", "--topo-order", "--format=%H%x00%P%x00%at%x00%an <%ae>%x00%B%x1e", branch]) .output() .map_err(|e| RepoError::Msg(format!("running git log: {e}")))?; if !out.status.success() { return fail(format!("git log {branch} failed: {}", String::from_utf8_lossy(&out.stderr).trim())); } let text = String::from_utf8_lossy(&out.stdout); let mut commits = Vec::new(); for record in text.split('\x1e') { let record = record.trim_start_matches('\n'); if record.trim().is_empty() { continue; } let f: Vec<&str> = record.splitn(5, '\0').collect(); if f.len() != 5 { return fail("unexpected git log record"); } commits.push(GitCommit { sha: f[0].to_string(), parents: f[1].split_whitespace().map(str::to_string).collect(), author_secs: f[2].parse().unwrap_or(0), author: f[3].to_string(), message: f[4].trim_end().to_string(), }); } Ok(commits) } /// `path → (mode, blob sha)` for a commit's tree. fn list_tree(git_dir: &Path, sha: &str) -> Result> { let text = git(git_dir, &["ls-tree", "-r", "-z", sha])?; let mut out = BTreeMap::new(); for entry in text.split('\0') { if entry.is_empty() { continue; } let Some((meta, path)) = entry.split_once('\t') else { continue }; let m: Vec<&str> = meta.split(' ').collect(); if m.len() == 3 { out.insert(path.to_string(), (m[0].to_string(), m[2].to_string())); } } Ok(out) } /// The ChangeId for a Git commit: its `Change-Id` trailer if it has a /// 32-hex one, else BLAKE3-128 of the commit hash (spec §23). fn change_id_for(sha: &str, message: &str) -> ChangeId { for line in message.lines().rev() { if let Some(v) = line.strip_prefix("Change-Id:") { if let Some(id) = ChangeId::from_hex(v.trim()) { return id; } } } let h = blake3::hash(sha.as_bytes()); ChangeId(h.as_bytes()[..16].try_into().unwrap()) } #[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) } /// Import branch `branch` of the Git repository at `git_dir` into this /// (empty) repository's bookmark (spec §23): one change per commit, its /// edits the difference between the union of its parents' states and its /// tree, so merge commits become resolutions. Submodules are skipped. pub fn import_git(&self, git_dir: &Path, branch: &str) -> Result { if !self.bookmark_heads()?.is_empty() { return fail("git import needs an empty bookmark; import into a fresh repository"); } let commits = list_commits(git_dir, branch)?; let mut cat = CatFile::start(git_dir)?; let mut report = GitImportReport::default(); let mut change_of: HashMap = HashMap::new(); // The state after the most recently imported commit, reused when the // next commit's first parent is that commit. let mut last: Option<(String, State)> = None; let mut blob_cache: HashMap> = HashMap::new(); let mut cache_bytes: usize = 0; for gc in &commits { // Base: union of every parent's state. let mut base = match (&gc.parents.first(), &last) { (Some(p1), Some((sha, st))) if *p1 == sha => st.clone(), (Some(p1), _) => self.git_ancestor_state(git_dir, &[p1.as_str()], &change_of)?, (None, _) => State::new(), }; if gc.parents.len() > 1 { report.merges += 1; let mut args: Vec = gc.parents[1..].to_vec(); args.push(format!("^{}", gc.parents[0])); let extra = git(git_dir, &["rev-list".to_string()].iter().map(String::as_str).chain(args.iter().map(String::as_str)).collect::>())?; for sha in extra.lines() { if let Some(h) = change_of.get(sha) { base.add_unchecked(self.store.get_change(h)?); } } } let base_snapshot = base.materialize(); // The commit's tree as in-memory entries. let mut entries: BTreeMap = BTreeMap::new(); for (path, (mode, sha)) in list_tree(git_dir, &gc.sha)? { let (kind, exec) = match mode.as_str() { "100644" | "100664" => (NodeKind::File, false), "100755" => (NodeKind::File, true), "120000" => (NodeKind::Symlink, false), "160000" => { report.skipped_submodules += 1; continue; } _ => continue, }; let content = match blob_cache.get(&sha) { Some(c) => c.clone(), None => { let c = cat.read(&sha)?; if cache_bytes + c.len() > 256 * 1024 * 1024 { blob_cache.clear(); cache_bytes = 0; } cache_bytes += c.len(); blob_cache.insert(sha.clone(), c.clone()); c } }; entries.insert(path, FsEntry { kind, exec, content }); } let built = self.build_from_entries(&base, &base_snapshot, HashMap::new(), &entries)?; let mut deps: BTreeSet = built.contributors.iter().copied().collect(); let mut message = gc.message.clone(); if !message.ends_with('\n') { message.push('\n'); } message.push_str(&format!("\nGit-Commit: {}\nGit-Author: {}\n", gc.sha, gc.author)); let mut change = Change { change_id: change_id_for(&gc.sha, &gc.message), deps: vec![], author: ObjectRef::ZERO, hlc: Hlc { wall_ms: gc.author_secs * 1000, logical: 0 }, message, tree_ops: built.tree_ops, edits: built.edits, kind: if built.resolves { ChangeKind::Resolution } else { ChangeKind::Normal }, }; deps.extend(referenced_changes(&change, &base)); change.deps = deps.into_iter().collect(); change.normalize(); let hash = self.store.put_change(&change)?; base.add_unchecked(change); change_of.insert(gc.sha.clone(), hash); last = Some((gc.sha.clone(), base)); report.commits += 1; } // The bookmark: heads of the tip's state; the working copy follows. let Some((_, tip_state)) = last else { return Ok(report) }; let heads = tip_state.heads(); report.heads = heads.len(); let (op_ref, op) = self.head_op()?; let mut view = op.view.clone(); view.bookmarks.insert(self.config.bookmark.clone(), self.store.put_state(&StateObject::new(heads.clone()))?); self.record_op(&[op_ref], &format!("oxy git import {} {branch}", git_dir.display()), view)?; self.checkout(&heads, true, "oxy git import (checkout)")?; Ok(report) } /// The state holding the changes of every Git ancestor of `shas`. fn git_ancestor_state(&self, git_dir: &Path, shas: &[&str], change_of: &HashMap) -> Result { let mut args = vec!["rev-list"]; args.extend(shas); let out = git(git_dir, &args)?; let mut st = State::new(); for sha in out.lines() { if let Some(h) = change_of.get(sha) { st.add_unchecked(self.store.get_change(h)?); } } Ok(st) } 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(()) } }