| 1 | // What a snapshot remembers between commands: facts about objects that |
| 2 | // never change, kept under the user's cache directory, outside every |
| 3 | // checkout. |
| 4 | // |
| 5 | // <cache>/trees/<tree ID> the valley-tree-v1 digest of a git tree |
| 6 | // <cache>/landings/<SHA-256> a transfer statement that passed every |
| 7 | // content check (transfer.go) |
| 8 | // <cache>/pairs/<SHA-256> whether two heads conflict when merged |
| 9 | // onto main's tip (Pair) |
| 10 | // |
| 11 | // A tree entry is the line "valley-tree-v1 <tree ID> <digest>", then a |
| 12 | // line holding the SHA-256 of the first. A pair entry is named by the |
| 13 | // SHA-256 of its first line, "valley-pair-v1 <tip> <head> <head>" with the |
| 14 | // two heads sorted; its second line is how many paths the merge left |
| 15 | // conflicted, then come the first of those paths that fit the entry, each |
| 16 | // Go-quoted on a line of its own, and a last line holds the SHA-256 of |
| 17 | // every line before it. A landing entry is the text that |
| 18 | // names what was checked, under the SHA-256 of that text: it is found only |
| 19 | // when the file holds exactly that text. An entry that does not read as |
| 20 | // one, such as one cut short or damaged, or one that is not a regular |
| 21 | // file, is not found, and the work it would have saved is done again. |
| 22 | // Each entry is written to a file of its own and renamed into place, so |
| 23 | // commands running at once never see one half written. Nothing here fails |
| 24 | // a command or prints anything: a cache that cannot be read or written |
| 25 | // leaves the work to be done each time. |
| 26 | // |
| 27 | // A landing entry is never a version's landing evidence. It records that |
| 28 | // a statement's content checks passed for one head, landing commit, |
| 29 | // request name and signing key; the snapshot still checks, on every read, |
| 30 | // that the key is accepted, that the integrator's records bind the head to |
| 31 | // the statement, and that the landing commit is on main. |
| 32 | |
| 33 | package rebase |
| 34 | |
| 35 | import ( |
| 36 | "crypto/sha256" |
| 37 | "encoding/hex" |
| 38 | "io" |
| 39 | "os" |
| 40 | "path/filepath" |
| 41 | "sort" |
| 42 | "strconv" |
| 43 | "strings" |
| 44 | ) |
| 45 | |
| 46 | // CacheDir is the directory a snapshot remembers verified transfer |
| 47 | // statements in: valley/transfers under the user's cache directory |
| 48 | // (os.UserCacheDir, which honours XDG_CACHE_HOME), beside the clones |
| 49 | // valley keeps there, or "" when there is none. |
| 50 | func CacheDir() string { |
| 51 | dir, err := os.UserCacheDir() |
| 52 | if err != nil || dir == "" { |
| 53 | return "" |
| 54 | } |
| 55 | return filepath.Join(dir, "valley", "transfers") |
| 56 | } |
| 57 | |
| 58 | // cache is a snapshot's cache directory, or none when dir is "". |
| 59 | type cache struct{ dir string } |
| 60 | |
| 61 | // entryLimit is the most an entry is read to: every entry is far smaller. |
| 62 | const entryLimit = 4096 |
| 63 | |
| 64 | // tree is the digest remembered for a tree, if one is. |
| 65 | func (c cache) tree(oid string) (string, bool) { |
| 66 | data, ok := c.read("trees", oid) |
| 67 | if !ok { |
| 68 | return "", false |
| 69 | } |
| 70 | line, sum, ok := strings.Cut(string(data), "\n") |
| 71 | fields := strings.Fields(line) |
| 72 | if !ok || sum != hexSum(line)+"\n" || len(fields) != 3 || fields[0] != "valley-tree-v1" || fields[1] != oid || !isHex(fields[2], 64) { |
| 73 | return "", false |
| 74 | } |
| 75 | return fields[2], true |
| 76 | } |
| 77 | |
| 78 | // putTree remembers a tree's digest. |
| 79 | func (c cache) putTree(oid, digest string) { |
| 80 | line := "valley-tree-v1 " + oid + " " + digest |
| 81 | c.write("trees", oid, line+"\n"+hexSum(line)+"\n") |
| 82 | } |
| 83 | |
| 84 | // landing reports whether a landing entry holding text is remembered. |
| 85 | func (c cache) landing(text string) bool { |
| 86 | data, ok := c.read("landings", hexSum(text)) |
| 87 | return ok && string(data) == text |
| 88 | } |
| 89 | |
| 90 | // putLanding remembers a landing entry holding text. |
| 91 | func (c cache) putLanding(text string) { |
| 92 | c.write("landings", hexSum(text), text) |
| 93 | } |
| 94 | |
| 95 | // Pair is what merging two heads onto main's tip, with the tip as the |
| 96 | // merge base, reported: how many paths it left conflicted, none for a |
| 97 | // clean merge, and the first of them in sorted order, as many as an entry |
| 98 | // holds. Paths holds them all when it has Total of them. |
| 99 | type Pair struct { |
| 100 | Total int |
| 101 | Paths []string |
| 102 | } |
| 103 | |
| 104 | // pairKey is a pair entry's first line, the heads sorted. |
| 105 | func pairKey(tip, a, b string) (string, bool) { |
| 106 | for _, oid := range []string{tip, a, b} { |
| 107 | if (len(oid) != 40 && len(oid) != 64) || !isHex(oid, len(oid)) { |
| 108 | return "", false |
| 109 | } |
| 110 | } |
| 111 | if b < a { |
| 112 | a, b = b, a |
| 113 | } |
| 114 | return "valley-pair-v1 " + tip + " " + a + " " + b, true |
| 115 | } |
| 116 | |
| 117 | // RememberedPair is the answer dir remembers for heads a and b merged onto |
| 118 | // tip, in either order, if it remembers one. |
| 119 | func RememberedPair(dir, tip, a, b string) (Pair, bool) { |
| 120 | key, ok := pairKey(tip, a, b) |
| 121 | if !ok { |
| 122 | return Pair{}, false |
| 123 | } |
| 124 | data, ok := cache{dir: dir}.read("pairs", hexSum(key)) |
| 125 | text := string(data) |
| 126 | cut := strings.LastIndex(strings.TrimSuffix(text, "\n"), "\n") + 1 |
| 127 | if !ok || cut == 0 || !strings.HasSuffix(text, "\n") || text[cut:] != hexSum(text[:cut])+"\n" { |
| 128 | return Pair{}, false |
| 129 | } |
| 130 | lines := strings.Split(strings.TrimSuffix(text[:cut], "\n"), "\n") |
| 131 | if len(lines) < 2 || lines[0] != key { |
| 132 | return Pair{}, false |
| 133 | } |
| 134 | p := Pair{} |
| 135 | n, err := strconv.Atoi(lines[1]) |
| 136 | if err != nil || n < len(lines)-2 || strconv.Itoa(n) != lines[1] { |
| 137 | return Pair{}, false |
| 138 | } |
| 139 | p.Total = n |
| 140 | for _, q := range lines[2:] { |
| 141 | path, err := strconv.Unquote(q) |
| 142 | if err != nil || path == "" { |
| 143 | return Pair{}, false |
| 144 | } |
| 145 | p.Paths = append(p.Paths, path) |
| 146 | } |
| 147 | return p, true |
| 148 | } |
| 149 | |
| 150 | // RememberPair remembers in dir what merging heads a and b onto tip |
| 151 | // reported, and returns it as an entry holds it: its paths sorted, and |
| 152 | // cut to those that fit, so an answer reads the same remembered or not. |
| 153 | // With dir "", it remembers nothing and returns the same. |
| 154 | func RememberPair(dir, tip, a, b string, p Pair) Pair { |
| 155 | paths := append([]string{}, p.Paths...) |
| 156 | sort.Strings(paths) |
| 157 | kept := Pair{Total: max(p.Total, len(paths))} |
| 158 | key, ok := pairKey(tip, a, b) |
| 159 | if !ok { |
| 160 | kept.Paths = paths |
| 161 | return kept |
| 162 | } |
| 163 | body := key + "\n" + strconv.Itoa(kept.Total) + "\n" |
| 164 | for _, path := range paths { |
| 165 | line := strconv.Quote(path) + "\n" |
| 166 | if len(body)+len(line)+65 > entryLimit { |
| 167 | break |
| 168 | } |
| 169 | body += line |
| 170 | kept.Paths = append(kept.Paths, path) |
| 171 | } |
| 172 | cache{dir: dir}.write("pairs", hexSum(key), body+hexSum(body)+"\n") |
| 173 | return kept |
| 174 | } |
| 175 | |
| 176 | func hexSum(s string) string { |
| 177 | sum := sha256.Sum256([]byte(s)) |
| 178 | return hex.EncodeToString(sum[:]) |
| 179 | } |
| 180 | |
| 181 | // read is the content of an entry, if it reads. Only a regular file is |
| 182 | // an entry: the name is opened without following a symlink or waiting |
| 183 | // (entryFlags), so a FIFO, a device or a symlink in an entry's place is a |
| 184 | // miss, and never a read that blocks. |
| 185 | func (c cache) read(kind, name string) ([]byte, bool) { |
| 186 | if c.dir == "" || !isHex(name, len(name)) || name == "" { |
| 187 | return nil, false |
| 188 | } |
| 189 | f, err := os.OpenFile(filepath.Join(c.dir, kind, name), os.O_RDONLY|entryFlags, 0) |
| 190 | if err != nil { |
| 191 | return nil, false |
| 192 | } |
| 193 | defer f.Close() |
| 194 | if info, err := f.Stat(); err != nil || !info.Mode().IsRegular() { |
| 195 | return nil, false |
| 196 | } |
| 197 | data, err := io.ReadAll(io.LimitReader(f, entryLimit)) |
| 198 | return data, err == nil |
| 199 | } |
| 200 | |
| 201 | // write puts an entry in place whole: written to a file of its own in the |
| 202 | // same directory, then renamed over its name. |
| 203 | func (c cache) write(kind, name, content string) { |
| 204 | if c.dir == "" || !isHex(name, len(name)) || name == "" { |
| 205 | return |
| 206 | } |
| 207 | dir := filepath.Join(c.dir, kind) |
| 208 | if os.MkdirAll(dir, 0o700) != nil { |
| 209 | return |
| 210 | } |
| 211 | f, err := os.CreateTemp(dir, ".new-*") |
| 212 | if err != nil { |
| 213 | return |
| 214 | } |
| 215 | _, err = f.WriteString(content) |
| 216 | if cerr := f.Close(); err == nil { |
| 217 | err = cerr |
| 218 | } |
| 219 | if err == nil { |
| 220 | err = os.Rename(f.Name(), filepath.Join(dir, name)) |
| 221 | } |
| 222 | if err != nil { |
| 223 | os.Remove(f.Name()) |
| 224 | } |
| 225 | } |