| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "io/fs" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "slices" |
| 11 | "strings" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/copydesk" |
| 15 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 16 | ) |
| 17 | |
| 18 | type copyWork struct { |
| 19 | root string |
| 20 | contract *copydesk.Contract |
| 21 | before map[string][]byte |
| 22 | compared string |
| 23 | } |
| 24 | |
| 25 | func (e *Engineer) copyRead(ctx context.Context, tree workspace.TaskTree, sha, path string) ([]byte, error) { |
| 26 | if !shaPattern.MatchString(sha) || !copydesk.SafePath(path) { |
| 27 | return nil, errors.New("copy: invalid revision or path") |
| 28 | } |
| 29 | return gitRead(ctx, tree.Git, tree.GitDir, 2<<20, "show", sha+":"+path) |
| 30 | } |
| 31 | func copyFiles(root string) (map[string][]byte, error) { |
| 32 | out := map[string][]byte{} |
| 33 | size := 0 |
| 34 | err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error { |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | rel, err := filepath.Rel(root, p) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | if d.IsDir() { |
| 43 | if d.Name() == ".git" || d.Name() == "vendor" { |
| 44 | return filepath.SkipDir |
| 45 | } |
| 46 | return nil |
| 47 | } |
| 48 | if d.Type()&os.ModeSymlink != 0 { |
| 49 | return errors.New("copy: symlink in command output") |
| 50 | } |
| 51 | raw, err := os.ReadFile(p) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | size += len(raw) |
| 56 | if size > 32<<20 { |
| 57 | return errors.New("copy: input too large") |
| 58 | } |
| 59 | out[filepath.ToSlash(rel)] = raw |
| 60 | return nil |
| 61 | }) |
| 62 | return out, err |
| 63 | } |
| 64 | func putCopyFile(root, path string, raw []byte) error { |
| 65 | if !copydesk.SafePath(path) { |
| 66 | return errors.New("copy: invalid path") |
| 67 | } |
| 68 | full := filepath.Join(root, filepath.FromSlash(path)) |
| 69 | if err := os.MkdirAll(filepath.Dir(full), 0700); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | return os.WriteFile(full, raw, 0600) |
| 73 | } |
| 74 | func (e *Engineer) prepareCopyContract(ctx context.Context, r stepRow, tree workspace.TaskTree) (copyWork, error) { |
| 75 | root, err := os.MkdirTemp(e.O.Runner.WorkspaceRoot, "copy-") |
| 76 | w := copyWork{root: root, compared: r.Copy.Compared, before: map[string][]byte{}} |
| 77 | if err != nil { |
| 78 | return w, err |
| 79 | } |
| 80 | raw, err := e.copyRead(ctx, tree, r.BaseSHA, ".equipo/copy.json") |
| 81 | if err == nil { |
| 82 | c, err := copydesk.ParseContract(raw) |
| 83 | if err != nil { |
| 84 | return w, err |
| 85 | } |
| 86 | w.contract = &c |
| 87 | } |
| 88 | if c := w.contract; c != nil { |
| 89 | commands := [][]string{c.Extract, c.Apply, c.Accept, c.Regenerate, c.Classify} |
| 90 | if len(c.Reference) > 0 { |
| 91 | commands = append(commands, c.Reference) |
| 92 | } |
| 93 | for _, a := range commands { |
| 94 | raw, err := e.copyRead(ctx, tree, r.BaseSHA, a[1]) |
| 95 | if err != nil { |
| 96 | return w, err |
| 97 | } |
| 98 | if err := putCopyFile(filepath.Join(root, "trusted"), a[1], raw); err != nil { |
| 99 | return w, err |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | return w, nil |
| 104 | } |
| 105 | func (e *Engineer) prepareCopy(ctx context.Context, r stepRow, tree workspace.TaskTree) (copyWork, error) { |
| 106 | w, err := e.prepareCopyContract(ctx, r, tree) |
| 107 | if err != nil { |
| 108 | return w, err |
| 109 | } |
| 110 | root := w.root |
| 111 | changed := map[string]bool{} |
| 112 | if w.contract == nil { |
| 113 | diff, err := gitRead(ctx, tree.Git, tree.GitDir, 2<<20, "diff", "--name-only", "-z", w.compared, r.CandidateSHA) |
| 114 | if err != nil { |
| 115 | return w, err |
| 116 | } |
| 117 | for _, path := range strings.Split(string(diff), "\x00") { |
| 118 | changed[path] = true |
| 119 | } |
| 120 | } |
| 121 | // git ls-tree supplies the exact tracked paths, including spaces, without a shell. |
| 122 | listing, err := gitRead(ctx, tree.Git, tree.GitDir, 2<<20, "ls-files", "--stage", "-z") |
| 123 | if err != nil { |
| 124 | return w, err |
| 125 | } |
| 126 | for _, line := range strings.Split(string(listing), "\x00") { |
| 127 | if line == "" { |
| 128 | continue |
| 129 | } |
| 130 | meta, path, ok := strings.Cut(line, "\t") |
| 131 | if !ok || !copydesk.SafePath(path) { |
| 132 | return w, errors.New("copy: invalid tree") |
| 133 | } |
| 134 | selected := w.contract == nil && changed[path] && copydesk.GenericSupported(path) |
| 135 | if c := w.contract; c != nil { |
| 136 | selected = slices.Contains(c.Paths, path) || slices.Contains(c.Bookkeeping, path) || strings.HasSuffix(path, "_test.go") && !copydesk.Excluded(strings.TrimSuffix(path, "_test.go")+".go") |
| 137 | } |
| 138 | for _, ref := range r.Copy.References { |
| 139 | selected = selected || ref.Path == path |
| 140 | } |
| 141 | if !selected { |
| 142 | continue |
| 143 | } |
| 144 | if !strings.HasPrefix(meta, "100644 ") && !strings.HasPrefix(meta, "100755 ") { |
| 145 | return w, errors.New("copy: nonregular source") |
| 146 | } |
| 147 | value, err := os.ReadFile(filepath.Join(tree.Dir, filepath.FromSlash(path))) |
| 148 | if err != nil { |
| 149 | return w, err |
| 150 | } |
| 151 | w.before[path] = value |
| 152 | if err := putCopyFile(filepath.Join(root, "candidate"), path, value); err != nil { |
| 153 | return w, err |
| 154 | } |
| 155 | old, err := e.copyRead(ctx, tree, w.compared, path) |
| 156 | if err == nil { |
| 157 | if err := putCopyFile(filepath.Join(root, "base"), path, old); err != nil { |
| 158 | return w, err |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return w, nil |
| 164 | } |
| 165 | |
| 166 | func (e *Engineer) copyCommand(ctx context.Context, r stepRow, w copyWork, argv []string, input any, out any) error { |
| 167 | raw, err := json.Marshal(input) |
| 168 | if err != nil || len(raw) > 2<<20 { |
| 169 | return errors.New("copy: command input") |
| 170 | } |
| 171 | args := slices.Clone(argv) |
| 172 | args[0] = "/usr/bin/python3" |
| 173 | args[1] = "/workspace/trusted/" + args[1] |
| 174 | result := e.runGate(ctx, r, w.root, gate{jsonOutput: true, name: "copy", argv: args, input: raw, limit: 30 * time.Second}) |
| 175 | if !result.terminated || result.reason != "" || result.code != 0 { |
| 176 | return errors.New("copy: command refused or failed") |
| 177 | } |
| 178 | if out != nil { |
| 179 | return json.Unmarshal([]byte(result.output), out) |
| 180 | } |
| 181 | return nil |
| 182 | } |
| 183 | func (e *Engineer) copyExtract(ctx context.Context, r stepRow, w copyWork) (copydesk.Extraction, error) { |
| 184 | ex := copydesk.Extraction{Version: 1, Strings: []copydesk.Literal{}} |
| 185 | if w.contract != nil { |
| 186 | err := e.copyCommand(ctx, r, w, w.contract.Extract, map[string]string{"base": "/workspace/base", "candidate": "/workspace/candidate"}, &ex) |
| 187 | return ex, err |
| 188 | } |
| 189 | keys := make([]string, 0, len(w.before)) |
| 190 | for k := range w.before { |
| 191 | keys = append(keys, k) |
| 192 | } |
| 193 | slices.Sort(keys) |
| 194 | for _, path := range keys { |
| 195 | old, _ := os.ReadFile(filepath.Join(w.root, "base", path)) |
| 196 | ss, err := copydesk.Generic(path, old, w.before[path]) |
| 197 | if err != nil { |
| 198 | continue |
| 199 | } |
| 200 | ex.Strings = append(ex.Strings, ss...) |
| 201 | } |
| 202 | return ex, nil |
| 203 | } |
| 204 | |