oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package copydesk
2
3import (
4 "bytes"
5 "errors"
6 "go/scanner"
7 "go/token"
8 "regexp"
9 "slices"
10 "sort"
11 "strconv"
12 "strings"
13)
14
15var markerComment = regexp.MustCompile(`(?m)[ \t]*// COPY (?:ACCEPTED|APPROVED|REVIEW)[^\n]*`)
16
17func ReplaceTestLiterals(raw []byte, pairs map[string]string) ([]byte, error) {
18 var s scanner.Scanner
19 fs := token.NewFileSet()
20 f := fs.AddFile("test.go", fs.Base(), len(raw))
21 s.Init(f, raw, nil, 0)
22 var out bytes.Buffer
23 last := 0
24 for {
25 pos, tok, lit := s.Scan()
26 if tok == token.EOF {
27 break
28 }
29 if tok != token.STRING {
30 continue
31 }
32 v, err := strconv.Unquote(lit)
33 if err != nil {
34 return nil, err
35 }
36 next, ok := pairs[v]
37 if !ok {
38 continue
39 }
40 start := f.Offset(pos)
41 out.Write(raw[last:start])
42 out.WriteString(strconv.Quote(next))
43 last = start + len(lit)
44 }
45 out.Write(raw[last:])
46 return out.Bytes(), nil
47}
48func ValidateFiles(before, after map[string][]byte, ex []Literal, r Review, bookkeeping []string, contract bool) error {
49 words, err := ValidateReview(ex, r)
50 if err != nil {
51 return err
52 }
53 pairs := map[string]string{}
54 byFile := map[string][]Literal{}
55 byKey := map[string]Wording{}
56 for _, w := range words {
57 byKey[w.Key] = w
58 }
59 for _, s := range ex {
60 w := byKey[s.Key]
61 if old, ok := pairs[s.Text]; ok && old != w.Text {
62 return errors.New("copy: conflicting replacement")
63 }
64 pairs[s.Text] = w.Text
65 byFile[s.Path] = append(byFile[s.Path], s)
66 }
67 for name, raw := range before {
68 now, ok := after[name]
69 if !ok {
70 return errors.New("copy: deleted file")
71 }
72 if bytes.Equal(raw, now) {
73 continue
74 }
75 if contract && slices.Contains(bookkeeping, name) {
76 continue
77 }
78 if loc := byFile[name]; len(loc) > 0 {
79 subset := Review{Version: 1}
80 for _, s := range loc {
81 subset.Strings = append(subset.Strings, byKey[s.Key])
82 }
83 if !contract {
84 if err := ValidateScope(raw, now, loc, subset); err != nil {
85 return err
86 }
87 continue
88 }
89 // The contract may encode string contents and update markers, never logic.
90 expected := slices.Clone(raw)
91 sort.Slice(loc, func(i, j int) bool { return loc[i].Start > loc[j].Start })
92 for _, s := range loc {
93 if s.Start < 1 || s.End >= len(raw) || s.End < s.Start {
94 return errors.New("copy: invalid literal range")
95 }
96 old, err := strconv.Unquote(string(raw[s.Start-1 : s.End+1]))
97 if err != nil || old != s.Text {
98 return errors.New("copy: stale literal")
99 }
100 expected = append(append(append([]byte{}, expected[:s.Start-1]...), []byte(strconv.Quote(byKey[s.Key].Text))...), expected[s.End+1:]...)
101 }
102 if !sameCode(markerComment.ReplaceAll(expected, nil), markerComment.ReplaceAll(now, nil), name) {
103 return errors.New("copy: source logic changed")
104 }
105 continue
106 }
107 if contract && strings.HasSuffix(name, "_test.go") {
108 expected, err := ReplaceTestLiterals(raw, pairs)
109 if err != nil {
110 return err
111 }
112 if sameCode(expected, now, name) {
113 continue
114 }
115 }
116 return errors.New("copy: edit outside extraction")
117 }
118 for name := range after {
119 if _, ok := before[name]; !ok {
120 return errors.New("copy: added file")
121 }
122 }
123 return nil
124}
125
126func sameCode(a, b []byte, name string) bool {
127 if !strings.HasSuffix(name, ".go") {
128 return bytes.Equal(a, b)
129 }
130 tokens := func(raw []byte) string {
131 var s scanner.Scanner
132 fs := token.NewFileSet()
133 f := fs.AddFile(name, fs.Base(), len(raw))
134 s.Init(f, raw, nil, scanner.ScanComments)
135 var out strings.Builder
136 for {
137 _, tok, lit := s.Scan()
138 if tok == token.EOF {
139 break
140 }
141 out.WriteString(tok.String())
142 out.WriteByte(0)
143 out.WriteString(lit)
144 out.WriteByte(0)
145 }
146 return out.String()
147 }
148 return tokens(a) == tokens(b)
149}
150