oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "encoding/json"
6 "math"
7 "os/exec"
8 "strings"
9
10 "amadan.net/oficina/equipo/internal/runner/workspace"
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// changeFiles reuses Review's NUL-safe parsers, without changing its
15// measurement or its conservative 200-file merge-authority bound.
16func changeFiles(ctx context.Context, git, gitDir, base, sha string) *wire.ChangeFiles {
17 if base != "" && !shaPattern.MatchString(base) || !shaPattern.MatchString(sha) {
18 return nil
19 }
20 run := func(args ...string) (string, bool) {
21 cmd := exec.CommandContext(ctx, git, append([]string{"--git-dir=" + gitDir, "-c", "core.quotePath=false"}, args...)...)
22 env := workspace.GitEnvironment()
23 env["HOME"] = "/nonexistent"
24 for k, v := range env {
25 cmd.Env = append(cmd.Env, k+"="+v)
26 }
27 out, clipped, err := boundedOutput(cmd, factsOutput)
28 return string(out), err == nil && !clipped
29 }
30 pair := []string{base, sha}
31 if base == "" {
32 parents, ok := run("rev-list", "--parents", "-n", "1", sha)
33 if !ok || strings.TrimSpace(parents) != sha {
34 return nil
35 }
36 pair = []string{"--root", "--no-commit-id", sha}
37 } else if _, ok := run("merge-base", "--is-ancestor", base, sha); !ok {
38 return nil
39 }
40 raw, ok1 := run(append([]string{"diff-tree", "-r", "-z", "--raw", "-M", "--no-abbrev", "--no-ext-diff", "--no-textconv"}, pair...)...)
41 num, ok2 := run(append([]string{"diff-tree", "-r", "-z", "--numstat", "-M", "--no-ext-diff", "--no-textconv"}, pair...)...)
42 if !ok1 || !ok2 {
43 return nil
44 }
45 return changeFileListings(base, sha, raw, num)
46}
47
48func changeFileListings(base, sha, raw, num string) *wire.ChangeFiles {
49 entries, ok1 := parseRaw(raw)
50 stats, ok2 := parseNumstat(num)
51 if !ok1 || !ok2 || len(entries) != len(stats) {
52 return nil
53 }
54 f := &wire.ChangeFiles{Base: base, SHA: sha, Files: len(entries), Entries: []wire.ChangedFile{}}
55 for i, e := range entries {
56 s := stats[i]
57 if s.path != e.New || s.old != e.Old || s.added > math.MaxInt-f.Additions || s.deleted > math.MaxInt-f.Deletions {
58 return nil
59 }
60 f.Additions += s.added
61 f.Deletions += s.deleted
62 if s.binary {
63 f.Binary++
64 }
65 status := ""
66 switch e.Status[0] {
67 case 'A':
68 status = wire.ChangeFileAdded
69 case 'D':
70 status = wire.ChangeFileDeleted
71 case 'M', 'T':
72 status = wire.ChangeFileModified
73 case 'R':
74 status = wire.ChangeFileRenamed
75 default:
76 return nil
77 }
78 if len(f.Entries) < wire.MaxChangeEntries {
79 if !wire.ValidChangePath(e.New) || e.Old != "" && !wire.ValidChangePath(e.Old) {
80 return nil
81 }
82 f.Entries = append(f.Entries, wire.ChangedFile{Path: e.New, OldPath: e.Old, Status: status, Additions: s.added, Deletions: s.deleted, Binary: s.binary})
83 }
84 }
85 for {
86 f.Truncated = len(f.Entries) < f.Files
87 encoded, _ := json.Marshal(f)
88 if len(encoded) <= wire.MaxChangeFilesBytes {
89 break
90 }
91 f.Entries = f.Entries[:len(f.Entries)-1]
92 }
93 if !wire.ValidChangeFiles(f) {
94 return nil
95 }
96 return f
97}
98