oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package wire
2
3import (
4 "encoding/json"
5 "path"
6 "strings"
7 "unicode"
8 "unicode/utf8"
9)
10
11const (
12 ChangeFileAdded = "added"
13 ChangeFileModified = "modified"
14 ChangeFileDeleted = "deleted"
15 ChangeFileRenamed = "renamed"
16)
17
18// ChangeFiles is presentation only. Merge authority consumes ChangeFacts,
19// independently measured by Review, never these Engineering measurements.
20type ChangeFiles struct {
21 Base string `json:"base"`
22 SHA string `json:"sha"`
23 Files int `json:"files"`
24 Additions int `json:"additions"`
25 Deletions int `json:"deletions"`
26 Binary int `json:"binary"`
27 Entries []ChangedFile `json:"entries"`
28 Truncated bool `json:"truncated"`
29}
30type ChangedFile struct {
31 Path string `json:"path"`
32 OldPath string `json:"old_path,omitempty"`
33 Status string `json:"status"`
34 Additions int `json:"additions"`
35 Deletions int `json:"deletions"`
36 Binary bool `json:"binary"`
37}
38
39// MaxChangeFilesBytes leaves room for the rest of a bounded StepReport.
40const MaxChangeFilesBytes = 96 << 10
41
42func ValidChangePath(s string) bool {
43 return s != "" && len(s) <= 4096 && utf8.ValidString(s) && !strings.HasPrefix(s, "/") &&
44 s != "." && s != ".." && !strings.HasPrefix(s, "../") && path.Clean(s) == s &&
45 !strings.ContainsAny(s, "\\") && strings.IndexFunc(s, unsafeChangePathRune) < 0
46}
47
48// Keep in sync with native ThinChangeFiles.path and the shared path fixture.
49// Other format characters (for example ZWJ in emoji names) are valid.
50func unsafeChangePathRune(r rune) bool {
51 return unicode.IsControl(r) || r == 0x061c || r == 0x200e || r == 0x200f ||
52 r >= 0x202a && r <= 0x202e || r >= 0x2066 && r <= 0x2069
53}
54
55func ValidChangeFiles(f *ChangeFiles) bool {
56 if f == nil || f.Entries == nil || f.Base != "" && len(f.Base) != 40 || len(f.SHA) != 40 || strings.Trim(f.Base+f.SHA, "0123456789abcdef") != "" ||
57 f.Files < 0 || f.Additions < 0 || f.Deletions < 0 || f.Binary < 0 || f.Binary > f.Files ||
58 len(f.Entries) > MaxChangeEntries || len(f.Entries) > f.Files || f.Truncated != (len(f.Entries) < f.Files) {
59 return false
60 }
61 a, d, b := 0, 0, 0
62 seen := map[string]bool{}
63 for _, e := range f.Entries {
64 if !ValidChangePath(e.Path) || seen[e.Path] || e.Additions < 0 || e.Deletions < 0 {
65 return false
66 }
67 seen[e.Path] = true
68 switch e.Status {
69 case ChangeFileAdded, ChangeFileModified, ChangeFileDeleted:
70 if e.OldPath != "" {
71 return false
72 }
73 case ChangeFileRenamed:
74 if !ValidChangePath(e.OldPath) || e.OldPath == e.Path {
75 return false
76 }
77 default:
78 return false
79 }
80 // Subtraction checks also prevent integer overflow.
81 if e.Additions > f.Additions-a || e.Deletions > f.Deletions-d {
82 return false
83 }
84 a += e.Additions
85 d += e.Deletions
86 if e.Binary {
87 b++
88 if e.Additions != 0 || e.Deletions != 0 {
89 return false
90 }
91 }
92 }
93 if b > f.Binary || !f.Truncated && (a != f.Additions || d != f.Deletions || b != f.Binary) {
94 return false
95 }
96 raw, err := json.Marshal(f)
97 return err == nil && len(raw) <= MaxChangeFilesBytes
98}
99