summaryrefslogtreecommitdiff
path: root/sidecar/store.go
blob: 76e626f8ce9c86da54e0e0eb1ea14317a515b130 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Package sidecar stores vr's append-only JSONL log in a private jj change.
//
// The change is an anonymous direct child of root(), so it is visible and
// durable in the jj repository without becoming part of project history or the
// working copy. A repo-local revset alias names it and git.private-commits keeps
// accidental --change pushes from publishing it.
package sidecar

import (
	"errors"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"
)

const (
	Alias       = "vr_log"
	LogFile     = "vr-log.jsonl"
	Description = "private: vr log"
)

var ErrNoLog = errors.New("vr log has not been initialized")

// Store is a repository's vr log. Open is read-only with respect to jj; the
// sidecar change and its config are created lazily by Append.
type Store struct {
	root     string
	lockPath string
}

// Open finds the jj workspace containing dir.
func Open(dir string) (*Store, error) {
	root, err := jjOutput(dir, "workspace", "root")
	if err != nil {
		return nil, err
	}
	root = strings.TrimSpace(root)
	configPath, err := jjOutput(root, "config", "path", "--repo")
	if err != nil {
		return nil, err
	}
	return &Store{
		root:     root,
		lockPath: filepath.Join(filepath.Dir(strings.TrimSpace(configPath)), "vr-agent-logger.lock"),
	}, nil
}

func (s *Store) Root() string { return s.root }

func (s *Store) String() string {
	return fmt.Sprintf("jj sidecar %s:%s", Alias, LogFile)
}

// Read returns the current log. Before the first sidecar write it falls back
// to a repo-root legacy log, which makes migration transparent.
func (s *Store) Read() ([]byte, error) {
	var data []byte
	err := s.withLock(syscall.LOCK_SH, func() error {
		rev, found, _, err := s.resolveExisting()
		if err != nil {
			return err
		}
		if found {
			data, err = s.readRevision(rev)
			return err
		}
		data, err = os.ReadFile(filepath.Join(s.root, LogFile))
		if errors.Is(err, os.ErrNotExist) {
			return ErrNoLog
		}
		return err
	})
	return data, err
}

// Generation is a cheap identity for the current contents, used by vrsite to
// notice appends. A sidecar rewrite gets a new commit id.
func (s *Store) Generation() (string, error) {
	var generation string
	err := s.withLock(syscall.LOCK_SH, func() error {
		rev, found, _, err := s.resolveExisting()
		if err != nil {
			return err
		}
		if found {
			generation, err = s.logValue(rev, "commit_id")
			return err
		}
		info, err := os.Stat(filepath.Join(s.root, LogFile))
		if errors.Is(err, os.ErrNotExist) {
			generation = "empty"
			return nil
		}
		if err != nil {
			return err
		}
		generation = fmt.Sprintf("legacy:%d:%d", info.Size(), info.ModTime().UnixNano())
		return nil
	})
	return generation, err
}

// Append serializes sidecar rewrites across vr processes. makePayload runs
// while holding that lock and receives the complete current log, allowing note
// numbering and reply validation to be atomic with the append.
func (s *Store) Append(makePayload func(current []byte) ([]byte, error)) error {
	return s.withLock(syscall.LOCK_EX, func() error {
		rev, err := s.ensure()
		if err != nil {
			return err
		}
		sidecarData, err := s.readRevision(rev)
		if err != nil {
			return err
		}
		current := sidecarData
		if len(current) == 0 {
			legacy, legacyErr := os.ReadFile(filepath.Join(s.root, LogFile))
			if legacyErr == nil {
				current = legacy
			} else if !errors.Is(legacyErr, os.ErrNotExist) {
				return legacyErr
			}
		}
		payload, err := makePayload(current)
		if err != nil {
			return err
		}
		if len(payload) == 0 {
			return errors.New("refusing to append an empty vr log payload")
		}

		var appendData []byte
		if len(sidecarData) == 0 && len(current) > 0 {
			appendData = append(appendData, current...)
		}
		if len(current) > 0 && current[len(current)-1] != '\n' {
			appendData = append(appendData, '\n')
		}
		appendData = append(appendData, payload...)
		if appendData[len(appendData)-1] != '\n' {
			appendData = append(appendData, '\n')
		}
		return s.runAppend(appendData)
	})
}

func (s *Store) withLock(kind int, fn func() error) error {
	if err := os.MkdirAll(filepath.Dir(s.lockPath), 0o755); err != nil {
		return err
	}
	f, err := os.OpenFile(s.lockPath, os.O_CREATE|os.O_RDWR, 0o600)
	if err != nil {
		return err
	}
	defer f.Close()
	if err := syscall.Flock(int(f.Fd()), kind); err != nil {
		return err
	}
	defer syscall.Flock(int(f.Fd()), syscall.LOCK_UN) //nolint:errcheck
	return fn()
}

func (s *Store) ensure() (string, error) {
	rev, found, configured, err := s.resolveExisting()
	if err != nil {
		return "", err
	}
	if !found {
		if _, err := s.jj("new", "--no-edit", "root()", "-m", Description); err != nil {
			return "", err
		}
		ids, err := s.descriptionMatches()
		if err != nil {
			return "", err
		}
		if len(ids) != 1 {
			return "", fmt.Errorf("created %q sidecar but found %d matching root children", Description, len(ids))
		}
		rev = ids[0]
	}
	if !configured {
		if _, err := s.jj("config", "set", "--repo", "revset-aliases."+Alias, strconv.Quote(rev)); err != nil {
			return "", err
		}
	}
	private, err := s.jj("config", "get", "git.private-commits")
	if err != nil {
		return "", err
	}
	if !containsRevsetName(private, Alias) {
		expr := fmt.Sprintf("(%s) | %s", strings.TrimSpace(private), Alias)
		if _, err := s.jj("config", "set", "--repo", "git.private-commits", strconv.Quote(expr)); err != nil {
			return "", err
		}
	}
	return rev, nil
}

func (s *Store) resolveExisting() (rev string, found, configured bool, err error) {
	configuredValue, configErr := s.jj("config", "get", "revset-aliases."+Alias)
	if configErr == nil {
		rev, err = s.logValue("exactly("+Alias+", 1)", "change_id")
		if err != nil {
			return "", false, true, fmt.Errorf("repo-local revset alias %s is invalid: %w", Alias, err)
		}
		valid := fmt.Sprintf("exactly(%s & root()+ & subject(exact:%s), 1)", Alias, strconv.Quote(Description))
		if _, err := s.logValue(valid, "change_id"); err != nil {
			return "", false, true, fmt.Errorf("repo-local revset alias %s=%q does not name vr's sidecar: %w",
				Alias, strings.TrimSpace(configuredValue), err)
		}
		return rev, true, true, nil
	}

	ids, err := s.descriptionMatches()
	if err != nil {
		return "", false, false, err
	}
	switch len(ids) {
	case 0:
		return "", false, false, nil
	case 1:
		return ids[0], true, false, nil
	default:
		return "", false, false, fmt.Errorf("found %d root children described %q; cannot choose a vr sidecar", len(ids), Description)
	}
}

func (s *Store) descriptionMatches() ([]string, error) {
	revset := fmt.Sprintf("root()+ & subject(exact:%s)", strconv.Quote(Description))
	out, err := s.jj("log", "-G", "-r", revset, "-T", "change_id ++ \"\\n\"")
	if err != nil {
		return nil, err
	}
	var ids []string
	for _, id := range strings.Fields(out) {
		ids = append(ids, id)
	}
	return ids, nil
}

func (s *Store) logValue(revset, template string) (string, error) {
	out, err := s.jj("log", "-G", "-r", revset, "-T", template)
	if err != nil {
		return "", err
	}
	value := strings.TrimSpace(out)
	if value == "" {
		return "", fmt.Errorf("revision %q produced no %s", revset, template)
	}
	return value, nil
}

func (s *Store) readRevision(rev string) ([]byte, error) {
	listed, err := s.jj("file", "list", "-r", rev, "--", LogFile)
	if err != nil {
		return nil, err
	}
	if strings.TrimSpace(listed) == "" {
		return nil, nil
	}
	out, err := s.jj("file", "show", "-r", rev, "--", LogFile)
	return []byte(out), err
}

func (s *Store) runAppend(data []byte) error {
	f, err := os.CreateTemp(filepath.Dir(s.lockPath), "vr-log-append-*")
	if err != nil {
		return err
	}
	tmp := f.Name()
	defer os.Remove(tmp) //nolint:errcheck
	if _, err := f.Write(data); err != nil {
		f.Close()
		return err
	}
	if err := f.Close(); err != nil {
		return err
	}
	_, err = s.jj("--config", "snapshot.auto-track=\"all()\"",
		"--config", "snapshot.max-new-file-size=\"1GiB\"",
		"run", "--clean", "--root", "-r", "exactly("+Alias+", 1)", "--",
		"sh", "-c", "cat \"$1\" >> "+LogFile, "sh", tmp)
	return err
}

func (s *Store) jj(args ...string) (string, error) {
	return jjOutput(s.root, args...)
}

func jjOutput(dir string, args ...string) (string, error) {
	full := append([]string{"--ignore-working-copy", "--no-pager"}, args...)
	cmd := exec.Command("jj", full...)
	cmd.Dir = dir
	out, err := cmd.Output()
	if err != nil {
		message := strings.TrimSpace(string(out))
		if ee, ok := err.(*exec.ExitError); ok {
			message = strings.TrimSpace(string(ee.Stderr))
		}
		if message != "" {
			return "", fmt.Errorf("jj %s: %w: %s", strings.Join(args, " "), err, message)
		}
		return "", fmt.Errorf("jj %s: %w", strings.Join(args, " "), err)
	}
	return string(out), nil
}

func containsRevsetName(expr, name string) bool {
	for i := 0; i+len(name) <= len(expr); i++ {
		if expr[i:i+len(name)] != name {
			continue
		}
		before := i == 0 || !isNameByte(expr[i-1])
		after := i+len(name) == len(expr) || !isNameByte(expr[i+len(name)])
		if before && after {
			return true
		}
	}
	return false
}

func isNameByte(b byte) bool {
	return b == '_' || b == '-' || b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9'
}