OXYGENHUB
# e8b4e156f2fa oxygen / crates / oxy-core / src / repo / git.rs
9.1 KiB · Rust · last changed Git bridge, export side: one commit per bookmark state via git fast-import, Change-Id/Oxy-Hash/Oxy-State trailers, incremental marks 1 month ago source blame history raw
79ab8528 Andy McCall ↶ 1
//! Git bridge (spec §23), export side: each state the current bookmark
2
//! passed through becomes a Git commit through `git fast-import`, with the
3
//! state's full tree and the previous state's commit as parent. A state
4
//! that added exactly one change carries that change's message, author,
5
//! and `Change-Id`/`Oxy-Hash` trailers; every commit carries `Oxy-State`.
6
//!
7
//! Marks make re-exports incremental: `.oxy/git/<target>/git-marks` is
8
//! git's own marks file and `oxy-marks` maps state refs to marks.
9
10
use super::*;
11
use std::process::{Command, Stdio};
12
13
#[derive(Debug, Clone, Default, PartialEq, Eq)]
14
pub struct GitExportReport {
15
    pub exported: usize,
16
    pub skipped: usize,
17
    pub branch: String,
18
    pub heads: usize,
19
}
20
21
fn git(dir: &Path, args: &[&str]) -> Result<String> {
22
    let out = Command::new("git").arg("-C").arg(dir).args(args).output().map_err(|e| RepoError::Msg(format!("running git: {e}")))?;
23
    if !out.status.success() {
24
        return fail(format!("git {} failed: {}", args.join(" "), String::from_utf8_lossy(&out.stderr).trim()));
25
    }
26
    Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
27
}
28
29
impl Repo {
30
    fn git_marks_dir(&self, git_dir: &Path) -> Result<PathBuf> {
31
        let canonical = git_dir.canonicalize().unwrap_or_else(|_| git_dir.to_path_buf());
32
        let key = hash_object(ObjectType::Chunk, canonical.to_string_lossy().as_bytes()).short(16);
33
        let dir = self.oxy.join("git").join(key);
34
        fs::create_dir_all(&dir)?;
35
        Ok(dir)
36
    }
37
38
    fn read_oxy_marks(path: &Path) -> Result<BTreeMap<ObjectRef, u64>> {
39
        let mut out = BTreeMap::new();
40
        if let Ok(text) = fs::read_to_string(path) {
41
            for line in text.lines() {
42
                if let Some((h, m)) = line.split_once(' ') {
43
                    if let (Some(h), Ok(m)) = (ObjectRef::from_hex(h), m.parse()) {
44
                        out.insert(h, m);
45
                    }
46
                }
47
            }
48
        }
49
        Ok(out)
50
    }
51
52
    /// Export the current bookmark to the Git repository at `git_dir`,
53
    /// creating a bare one if nothing is there. Each state the bookmark
54
    /// passed through (per the operation log) becomes one commit whose tree
55
    /// is that state, so the Git branch is a linear history of full trees.
56
    pub fn git_export(&self, git_dir: &Path) -> Result<GitExportReport> {
57
        if !git_dir.join("HEAD").exists() && !git_dir.join(".git").exists() {
58
            fs::create_dir_all(git_dir)?;
59
            git(git_dir, &["init", "--bare", "-q"])?;
60
        }
61
        let marks_dir = self.git_marks_dir(git_dir)?;
62
        let git_marks = marks_dir.join("git-marks");
63
        let oxy_marks_path = marks_dir.join("oxy-marks");
64
        let mut marks = Self::read_oxy_marks(&oxy_marks_path)?;
65
        let mut next_mark = marks.values().copied().max().unwrap_or(0) + 1;
66
67
        let bookmark = self.config.bookmark.clone();
68
        let mut stream: Vec<u8> = Vec::new();
69
        let mut report = GitExportReport { branch: bookmark.clone(), heads: self.bookmark_heads()?.len(), ..Default::default() };
70
71
        // States along the bookmark, oldest first, skipping unchanged and pruned ones.
72
        let mut ops = self.op_log()?;
73
        ops.reverse();
74
        let mut prev: Option<(ObjectRef, Vec<ChangeHash>)> = None;
75
        let mut last_mark: Option<u64> = None;
76
        for (_, op) in &ops {
77
            let Some(state_ref) = op.view.bookmarks.get(&bookmark).copied() else { continue };
78
            if prev.as_ref().is_some_and(|(p, _)| *p == state_ref) {
79
                continue;
80
            }
81
            let Ok(state) = self.store.get_state(&state_ref) else { continue };
82
            if state.heads.is_empty() {
83
                prev = Some((state_ref, vec![]));
84
                continue;
85
            }
86
            if let Some(m) = marks.get(&state_ref) {
87
                report.skipped += 1;
88
                last_mark = Some(*m);
89
                prev = Some((state_ref, state.heads.clone()));
90
                continue;
91
            }
92
            let prev_heads = prev.as_ref().map(|(_, h)| h.clone()).unwrap_or_default();
93
            let added = self.negotiate(&prev_heads, &state.heads).unwrap_or_default();
94
            let loaded = self.load_state(&state.heads)?;
95
            let snapshot = loaded.materialize();
96
            let (author, secs, mut message) = match added.as_slice() {
97
                [h] => {
98
                    let c = self.store.get_change(h)?;
99
                    let mut m = c.message.clone();
100
                    if !m.ends_with('\n') {
101
                        m.push('\n');
102
                    }
103
                    m.push_str(&format!("\nChange-Id: {}\nOxy-Hash: {}\n", c.change_id.to_hex(), h.to_hex()));
104
                    (self.git_author(&c), c.hlc.wall_ms / 1000, m)
105
                }
106
                [] => ("oxy <oxy@localhost>".to_string(), op.hlc.wall_ms / 1000, format!("oxy: {}\n\n", op.command)),
107
                many => {
108
                    let mut m = format!("oxy: {} ({} changes)\n\n", op.command, many.len());
109
                    for h in many {
110
                        let c = self.store.get_change(h)?;
111
                        m.push_str(&format!("- {} {}\n", c.change_id.short(8), c.message.lines().next().unwrap_or("")));
112
                    }
113
                    m.push_str(&format!("\nOxy-Changes: {}\n", many.iter().map(|h| h.to_hex()).collect::<Vec<_>>().join(" ")));
114
                    ("oxy <oxy@localhost>".to_string(), op.hlc.wall_ms / 1000, m)
115
                }
116
            };
117
            message.push_str(&format!("Oxy-State: {}\n", state_ref.to_hex()));
118
            if snapshot.is_conflicted() {
119
                message.push_str("Oxy-Conflicted: true\n");
120
            }
121
            let mark = next_mark;
122
            next_mark += 1;
123
            let parents: Vec<u64> = last_mark.into_iter().collect();
124
            self.write_commit(&mut stream, "refs/oxy/export", mark, &author, secs, &message, &parents, &snapshot)?;
125
            marks.insert(state_ref, mark);
126
            last_mark = Some(mark);
127
            prev = Some((state_ref, state.heads.clone()));
128
            report.exported += 1;
129
        }
130
        match last_mark {
131
            Some(m) => stream.extend_from_slice(format!("reset refs/heads/{bookmark}\nfrom :{m}\n\ndone\n").as_bytes()),
132
            None => stream.extend_from_slice(b"done\n"),
133
        }
134
135
        let mut cmd = Command::new("git");
136
        // `--force`: the scratch ref may jump between unrelated states.
137
        cmd.arg("-C").arg(git_dir).args(["fast-import", "--quiet", "--done", "--force"]).arg(format!("--export-marks={}", git_marks.display()));
138
        if git_marks.exists() {
139
            cmd.arg(format!("--import-marks={}", git_marks.display()));
140
        }
141
        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}")))?;
142
        child.stdin.take().unwrap().write_all(&stream)?;
143
        let out = child.wait_with_output()?;
144
        if !out.status.success() {
145
            return fail(format!("git fast-import failed: {}", String::from_utf8_lossy(&out.stderr).trim()));
146
        }
147
        let text: String = marks.iter().map(|(h, m)| format!("{} {m}\n", h.to_hex())).collect();
148
        write_atomic(&oxy_marks_path, text.as_bytes())?;
149
        let _ = git(git_dir, &["update-ref", "-d", "refs/oxy/export"]);
150
        Ok(report)
151
    }
152
153
    fn git_author(&self, c: &Change) -> String {
154
        match self.store.get_identity(&c.author) {
155
            Ok(id) if !id.name.is_empty() => format!("{} <{}>", id.name.replace(['<', '>', '\n'], " "), id.email.replace(['<', '>', '\n', ' '], "")),
156
            _ => "oxy <oxy@localhost>".to_string(),
157
        }
158
    }
159
160
    #[allow(clippy::too_many_arguments)]
161
    fn write_commit(&self, out: &mut Vec<u8>, reference: &str, mark: u64, author: &str, secs: u64, message: &str, parents: &[u64], snapshot: &Snapshot) -> Result<()> {
162
        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());
163
        for (i, p) in parents.iter().enumerate() {
164
            out.extend_from_slice(format!("{} :{p}\n", if i == 0 { "from" } else { "merge" }).as_bytes());
165
        }
166
        out.extend_from_slice(b"deleteall\n");
167
        for (path, f) in &snapshot.files {
168
            let (mode, bytes): (&str, Vec<u8>) = match (&f.kind, &f.content) {
169
                (NodeKind::Dir, _) => continue,
170
                (NodeKind::Symlink, FileContent::Opaque { blobs }) => ("120000", blobs.first().map(|b| self.read_blob(b)).transpose()?.unwrap_or_default()),
171
                (_, FileContent::Text { bytes, .. }) => (if f.mode.exec { "100755" } else { "100644" }, bytes.clone()),
172
                (_, FileContent::Opaque { blobs }) => (if f.mode.exec { "100755" } else { "100644" }, blobs.first().map(|b| self.read_blob(b)).transpose()?.unwrap_or_default()),
173
                (_, FileContent::Dir) => continue,
174
            };
175
            out.extend_from_slice(format!("M {mode} inline {path}\ndata {}\n", bytes.len()).as_bytes());
176
            out.extend_from_slice(&bytes);
177
            out.push(b'\n');
178
        }
179
        out.push(b'\n');
180
        Ok(())
181
    }
182
}