| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "sort" |
| 8 | "strings" |
| 9 | |
| 10 | "the-valley/change" |
| 11 | ) |
| 12 | |
| 13 | // The handle cache is what completions/_valley reads to offer a change's |
| 14 | // handle. It lives in the current repository, at valley/handles under the |
| 15 | // common git directory, and holds one line for each change that is neither |
| 16 | // landed nor withdrawn, tab-separated: handle, state (ready or draft), |
| 17 | // branch, title. Ready changes come first, then drafts, each in handle |
| 18 | // order. The branch is empty when the active version has none or the name |
| 19 | // holds a tab or a control character. |
| 20 | // |
| 21 | // Completion never fetches, so the cache is as old as the last |
| 22 | // valley pending or valley changes that read origin. A stale line is |
| 23 | // harmless: the verb resolves the name against origin anyway. |
| 24 | |
| 25 | // handleLines are the cache's lines for one repository. |
| 26 | func (l listed) handleLines(h handleMap) []string { |
| 27 | type line struct{ state, handle, text string } |
| 28 | var lines []line |
| 29 | for _, n := range l.snap.all { |
| 30 | st := n.record.State |
| 31 | if (st != change.Ready && st != change.Draft) || l.snap.isLanded(n) { |
| 32 | continue |
| 33 | } |
| 34 | v := n.record.ActiveVersion() |
| 35 | handle := h[handleKey{l.name, n.record.Target, n.id}] |
| 36 | branch := v.Branch |
| 37 | if !printable(branch) || strings.Contains(branch, "\t") { |
| 38 | branch = "" |
| 39 | } |
| 40 | text := fmt.Sprintf("%s\t%s\t%s\t%s", handle, st, branch, readable(v.Title)) |
| 41 | lines = append(lines, line{st, handle, text}) |
| 42 | } |
| 43 | sort.Slice(lines, func(i, j int) bool { |
| 44 | a, b := lines[i], lines[j] |
| 45 | if a.state != b.state { |
| 46 | return a.state == change.Ready |
| 47 | } |
| 48 | return a.handle < b.handle |
| 49 | }) |
| 50 | out := make([]string, len(lines)) |
| 51 | for i, ln := range lines { |
| 52 | out[i] = ln.text |
| 53 | } |
| 54 | return out |
| 55 | } |
| 56 | |
| 57 | // writeHandleCache writes the cache for the repository g is in, when |
| 58 | // repos lists it and read it. It never fails the verb: a cache that |
| 59 | // cannot be written is left as it was. |
| 60 | func writeHandleCache(g git, repos []listed, h handleMap) { |
| 61 | here := project(g) |
| 62 | for _, r := range repos { |
| 63 | if r.name != here || r.err != nil || r.snap == nil { |
| 64 | continue |
| 65 | } |
| 66 | dir, err := g.output(true, "rev-parse", "--git-common-dir") |
| 67 | if err != nil || dir == "" { |
| 68 | return |
| 69 | } |
| 70 | if !filepath.IsAbs(dir) { |
| 71 | dir = filepath.Join(g.at(), dir) |
| 72 | } |
| 73 | dir = filepath.Join(dir, "valley") |
| 74 | if os.MkdirAll(dir, 0o755) != nil { |
| 75 | return |
| 76 | } |
| 77 | body := strings.Join(r.handleLines(h), "\n") |
| 78 | if body != "" { |
| 79 | body += "\n" |
| 80 | } |
| 81 | tmp, err := os.CreateTemp(dir, "handles.*") |
| 82 | if err != nil { |
| 83 | return |
| 84 | } |
| 85 | _, werr := tmp.WriteString(body) |
| 86 | cerr := tmp.Close() |
| 87 | if werr != nil || cerr != nil || os.Rename(tmp.Name(), filepath.Join(dir, "handles")) != nil { |
| 88 | os.Remove(tmp.Name()) |
| 89 | } |
| 90 | return |
| 91 | } |
| 92 | } |