Federated via graft
8.9 kB
253 lines
1// Package sync is the actual synchronization brick: it takes a Forgejo
2// client and a Radicle client for one repo pair and mirrors content between
3// them, using the state store to stay idempotent across runs.
4//
5// Scope of this first version, deliberately: fast-forward-only git mirroring
6// (a real divergence is logged and skipped, never force-pushed), and
7// create-only issue/patch mirroring (edits made after the initial mirror are
8// not propagated — that needs per-item diffing, left for a follow-up once
9// the create path is proven in practice).
10package sync
11
12import (
13 "bytes"
14 "fmt"
15 "os"
16 "os/exec"
17 "path/filepath"
18 "strings"
19
20 "graft/internal/state"
21)
22
23// GitSyncer mirrors a repository's default branch between Forgejo and
24// Radicle using a local working clone with two remotes.
25type GitSyncer struct {
26 RepoPair string // key used in the state store
27 WorkDir string // local clone, created on first run if absent
28 ForgejoURL string // authenticated clone URL (token embedded), for git
29 ForgejoWebURL string // https://host/owner/repo, for links in the UI
30 RadicleWebURL string // https://explorer/nodes/host/rid, for links in the UI
31 ForgejoBranch string
32 RID string
33 RadHome string
34 State *state.Store
35 Series string // dashboard row this pair's activity groups under
36 SeriesURL string // where clicking that row's name goes
37}
38
39func (g *GitSyncer) run(args ...string) (string, error) {
40 cmd := exec.Command("git", args...)
41 cmd.Dir = g.WorkDir
42 cmd.Env = append(os.Environ(), "RAD_HOME="+g.RadHome)
43 var out bytes.Buffer
44 cmd.Stdout = &out
45 cmd.Stderr = &out
46 err := cmd.Run()
47 if err != nil {
48 return out.String(), fmt.Errorf("git %v: %w: %s", args, err, out.String())
49 }
50 return out.String(), nil
51}
52
53// ensureClone creates the working clone and its two remotes if this is the
54// first run for this repo pair. The rad remote mirrors exactly what
55// `rad init` itself sets up (verified against a live repo): a fetch URL of
56// rad://<rid>, and a separate push URL of rad://<rid>/<local-node-id> —
57// git-remote-rad needs the pushing node's own id in the push URL.
58func (g *GitSyncer) ensureClone() error {
59 if _, err := os.Stat(filepath.Join(g.WorkDir, ".git")); err == nil {
60 return nil
61 }
62 if err := os.MkdirAll(g.WorkDir, 0o700); err != nil {
63 return fmt.Errorf("create work dir: %w", err)
64 }
65 if _, err := g.run("init", "-q", "-b", g.ForgejoBranch); err != nil {
66 return err
67 }
68 if _, err := g.run("remote", "add", "forgejo", g.ForgejoURL); err != nil {
69 return err
70 }
71
72 ridBare := strings.TrimPrefix(g.RID, "rad:")
73 nid, err := g.localNodeID()
74 if err != nil {
75 return fmt.Errorf("resolve local node id: %w", err)
76 }
77 if _, err := g.run("remote", "add", "rad", "rad://"+ridBare); err != nil {
78 return err
79 }
80 if _, err := g.run("remote", "set-url", "--push", "rad", "rad://"+ridBare+"/"+nid); err != nil {
81 return err
82 }
83 return nil
84}
85
86// localNodeID returns the Radicle node id (DID, without the did:key:
87// prefix) that owns g.RadHome.
88func (g *GitSyncer) localNodeID() (string, error) {
89 // --nid is deprecated in favor of `rad node status --only nid` and
90 // prints a warning to stderr; keep that separate from stdout so it
91 // never ends up concatenated into the id itself.
92 cmd := exec.Command("rad", "self", "--nid")
93 cmd.Env = append(os.Environ(), "RAD_HOME="+g.RadHome)
94 var out, errOut bytes.Buffer
95 cmd.Stdout = &out
96 cmd.Stderr = &errOut
97 if err := cmd.Run(); err != nil {
98 return "", fmt.Errorf("rad self --nid: %w: %s", err, errOut.String())
99 }
100 return strings.TrimSpace(out.String()), nil
101}
102
103// headOf returns the commit SHA a remote branch currently points to, or ""
104// if the branch does not exist yet on that remote.
105func (g *GitSyncer) headOf(remote, branch string) (string, error) {
106 out, err := g.run("ls-remote", remote, "refs/heads/"+branch)
107 if err != nil {
108 return "", err
109 }
110 if out == "" {
111 return "", nil
112 }
113 var sha string
114 fmt.Sscanf(out, "%s", &sha)
115 return sha, nil
116}
117
118// isAncestor reports whether commit a is an ancestor of commit b (i.e.
119// fast-forwarding from a to b is safe).
120func (g *GitSyncer) isAncestor(a, b string) bool {
121 _, err := g.run("merge-base", "--is-ancestor", a, b)
122 return err == nil
123}
124
125// Sync mirrors the default branch in whichever direction has new,
126// fast-forwardable commits. It never force-pushes: a genuine divergence
127// (commits on both sides since the last sync, neither an ancestor of the
128// other) is reported as an error so the operator can resolve it by hand.
129func (g *GitSyncer) Sync() error {
130 if err := g.ensureClone(); err != nil {
131 return err
132 }
133
134 forgejoHead, err := g.headOf("forgejo", g.ForgejoBranch)
135 if err != nil {
136 return fmt.Errorf("read forgejo head: %w", err)
137 }
138 radHead, err := g.headOf("rad", g.ForgejoBranch)
139 if err != nil {
140 return fmt.Errorf("read rad head: %w", err)
141 }
142
143 lastForgejo, lastRad, err := g.State.GitCursor(g.RepoPair)
144 if err != nil {
145 return fmt.Errorf("read cursor: %w", err)
146 }
147
148 switch {
149 case forgejoHead == radHead:
150 // Already in sync; still record the cursor in case this is the
151 // first run and it was empty before.
152
153 case radHead == "" || radHead == lastRad:
154 // Radicle hasn't moved since we last looked: forgejo is ahead.
155 if err := g.pushBranch("forgejo", "rad", radHead, state.ForgejoToRadicle); err != nil {
156 return fmt.Errorf("mirror forgejo -> rad: %w", err)
157 }
158
159 case forgejoHead == "" || forgejoHead == lastForgejo:
160 // Forgejo hasn't moved: rad is ahead.
161 if err := g.pushBranch("rad", "forgejo", forgejoHead, state.RadicleToForgejo); err != nil {
162 return fmt.Errorf("mirror rad -> forgejo: %w", err)
163 }
164
165 case g.isAncestor(lastForgejo, forgejoHead) && g.isAncestor(lastRad, radHead) &&
166 g.isAncestor(radHead, forgejoHead):
167 // Both moved, but rad's tip is contained in forgejo's: forgejo wins.
168 if err := g.pushBranch("forgejo", "rad", radHead, state.ForgejoToRadicle); err != nil {
169 return fmt.Errorf("mirror forgejo -> rad: %w", err)
170 }
171
172 case g.isAncestor(forgejoHead, radHead):
173 // Symmetric case: forgejo's tip is contained in rad's.
174 if err := g.pushBranch("rad", "forgejo", forgejoHead, state.RadicleToForgejo); err != nil {
175 return fmt.Errorf("mirror rad -> forgejo: %w", err)
176 }
177
178 default:
179 return fmt.Errorf(
180 "%s: forgejo and rad diverged since last sync (forgejo=%s rad=%s, last forgejo=%s rad=%s); resolve manually, not auto-merging",
181 g.RepoPair, forgejoHead, radHead, lastForgejo, lastRad)
182 }
183
184 forgejoHead, err = g.headOf("forgejo", g.ForgejoBranch)
185 if err != nil {
186 return err
187 }
188 radHead, err = g.headOf("rad", g.ForgejoBranch)
189 if err != nil {
190 return err
191 }
192 return g.State.SetGitCursor(g.RepoPair, forgejoHead, radHead)
193}
194
195// pushBranch fetches g.ForgejoBranch from "from" and pushes it to "to".
196// oldToHead is that branch's head on "to" before the push (possibly ""),
197// used only to log which commits this pass actually moved, for the status
198// page's activity calendar.
199func (g *GitSyncer) pushBranch(from, to, oldToHead, direction string) error {
200 if _, err := g.run("fetch", "-q", from, g.ForgejoBranch); err != nil {
201 return fmt.Errorf("fetch %s: %w", from, err)
202 }
203 if _, err := g.run("push", to, "FETCH_HEAD:refs/heads/"+g.ForgejoBranch); err != nil {
204 return fmt.Errorf("push %s: %w", to, err)
205 }
206 g.logMirroredCommits(oldToHead, direction, to)
207 return nil
208}
209
210// logMirroredCommits records one activity_log entry per commit newly
211// reachable from FETCH_HEAD that wasn't reachable from oldHead. Best-effort:
212// a failure here never fails the sync itself, it just leaves the status
213// page's calendar short an entry. to is "forgejo" or "rad" — the remote the
214// commits were just pushed to — so the logged link (and the dashboard's
215// derived server label) points at where the content actually landed,
216// instead of always pointing at Forgejo regardless of direction.
217func (g *GitSyncer) logMirroredCommits(oldHead, direction, to string) {
218 rangeSpec := "FETCH_HEAD"
219 if oldHead != "" {
220 rangeSpec = oldHead + "..FETCH_HEAD"
221 }
222 // %x1f (unit separator) between fields: commit subjects can contain
223 // anything else printable, so a space or comma delimiter would be
224 // ambiguous to split back apart.
225 out, err := g.run("log", "--pretty=format:%H%x1f%h %s", rangeSpec)
226 if err != nil || out == "" {
227 return
228 }
229 for _, line := range strings.Split(out, "\n") {
230 if line == "" {
231 continue
232 }
233 full, summary, ok := strings.Cut(line, "\x1f")
234 if !ok {
235 continue
236 }
237 url := ""
238 switch to {
239 case "rad":
240 if g.RadicleWebURL != "" {
241 url = g.RadicleWebURL + "/commits/" + full
242 }
243 default:
244 if g.ForgejoWebURL != "" {
245 url = g.ForgejoWebURL + "/commit/" + full
246 }
247 }
248 g.State.LogActivity(state.Activity{
249 RepoPair: g.RepoPair, Series: g.Series, SeriesURL: g.SeriesURL,
250 Kind: "git", Direction: direction, Summary: summary, URL: url,
251 })
252 }
253}