gunk-dev/the-valley

conflicts.go

sourcec14b0e68e9c3main's published tip history
8.0 KiB
1package main
2
3// Conflicts between ready changes: which of a project's ready changes
4// conflict textually with each other, so the operator sees which approval
5// costs another change a rebase. valley pending marks them under each
6// row, and valley-pending/v1 and valley-changes/v1 carry them on each
7// ready change that has not landed.
8//
9// The candidates are a project's ready changes that have not landed as
10// any version (pendingChange). Two candidates are a pair unless one's
11// head is an ancestor of the other's: the layers of one stack land
12// together and are never paired. A pair is compared on T, main's tip on
13// origin as this run read it, and only when T is an ancestor of both
14// heads. It conflicts when git merge-tree --write-tree --merge-base=T A B
15// leaves paths conflicted (carry, carried.go), and those are its paths.
16// That is the merge the integrator makes of the second change once the
17// first lands: it writes the first as one commit on T whose tree is the
18// first's head's, then merges the second's head into the new tip from
19// their merge base, which is T, since no head holds the landing commit
20// (mergeDelta, integrator/git.go). The two heads' own merge base is not
21// used: sibling heads can share an unlanded ancestor, and a merge from it
22// reports a conflict the integrator never meets, where one head restores
23// a line the ancestor changed and the other changes it again. One merge
24// serves either landing order, since a three-way merge conflicts on the
25// same paths whichever side is ours.
26//
27// A pair with a head T is not an ancestor of is awaiting restack: the
28// restacker rebases each ready change onto main, and the pair is compared
29// once both are. A merge that does not run or parse is not computed. At
30// most pairBound merges run in one invocation, and the pairs past it are
31// not computed either. A pair not computed is listed, with why, on both
32// its changes, so a missing answer never reads as a clean one.
33//
34// A pair's answer depends only on T and the two heads, so it is
35// remembered in the client cache (rebase.RememberPair): a remembered
36// answer runs no merge and counts nothing against the bound. The merges
37// run in a scratch git directory per project (carryGit), made at its
38// first merge.
39
40import (
41 "fmt"
42 "io"
43 "strings"
44 "unicode/utf8"
45
46 "the-valley/change"
47 "the-valley/rebase"
48)
49
50// pairBound is the most merges one invocation runs to compare pairs. Each
51// is one git merge-tree, and a cold listing of n ready changes asks for up
52// to n(n-1)/2 of them.
53var pairBound = 64
54
55// Why a pair was not compared, as conflicts_unknown gives it.
56const (
57 whyRestack = "awaiting restack"
58 whyBound = "past the bound"
59 whyFailed = "merge failed"
60)
61
62// jsonConflict is a conflict with another ready change: its change ID and
63// handle, the first of the conflicted paths in sorted order that are
64// UTF-8, and how many paths conflict in all.
65type jsonConflict struct {
66 ChangeID string `json:"change_id"`
67 Handle string `json:"handle"`
68 Paths []string `json:"paths"`
69 PathsTotal int `json:"paths_total"`
70}
71
72// jsonConflictUnknown is another ready change a change was not compared
73// with, and why: whyRestack, whyBound or whyFailed.
74type jsonConflictUnknown struct {
75 ChangeID string `json:"change_id"`
76 Handle string `json:"handle"`
77 Why string `json:"why"`
78}
79
80// pairing is what one invocation has left for comparing pairs: the merges
81// it may still run, and the client cache directory.
82type pairing struct {
83 left int
84 cache string
85}
86
87func newPairing() *pairing { return &pairing{left: pairBound, cache: rebase.CacheDir()} }
88
89// addConflicts gives each ready change in view that has not landed its
90// conflicts with the others and the pairs not computed, each an empty
91// list when there are none. A merge that fails says so on stderr.
92func (l listed) addConflicts(view *jsonRepo, p *pairing, stderr io.Writer) {
93 if l.err != nil || p == nil {
94 return
95 }
96 var cands []int
97 for i, c := range view.Changes {
98 if c.State == change.Ready && c.LandedVersion == nil {
99 cands = append(cands, i)
100 view.Changes[i].Conflicts = &[]jsonConflict{}
101 view.Changes[i].ConflictsUnknown = &[]jsonConflictUnknown{}
102 }
103 }
104 if len(cands) < 2 {
105 return
106 }
107 defer phase("conflicts")()
108 s := l.snap
109 heads, onTip := map[int]string{}, map[int]bool{}
110 for _, i := range cands {
111 c := view.Changes[i]
112 h := c.Versions[c.Active-1].Head
113 heads[i], onTip[i] = h, s.tip != "" && (h == s.tip || s.strictAncestor(s.tip, h))
114 }
115 var cg git
116 done := func() {}
117 defer func() { done() }()
118 for x, i := range cands {
119 for _, j := range cands[x+1:] {
120 a, b := heads[i], heads[j]
121 if a == b || s.strictAncestor(a, b) || s.strictAncestor(b, a) {
122 continue
123 }
124 pair, why := rebase.Pair{}, ""
125 remembered := false
126 if onTip[i] && onTip[j] {
127 pair, remembered = rebase.RememberedPair(p.cache, s.tip, a, b)
128 }
129 switch {
130 case remembered:
131 case !onTip[i] || !onTip[j]:
132 why = whyRestack
133 case p.left == 0:
134 why = whyBound
135 default:
136 p.left--
137 var err error
138 if cg.dir == "" {
139 if cg, done, err = carryGit(s.g); err != nil {
140 done()
141 cg, done = git{}, func() {}
142 }
143 }
144 var c carried
145 if err == nil {
146 c, err = carry(cg, s.tip, a, b)
147 }
148 if err != nil {
149 why = whyFailed
150 fmt.Fprintf(stderr, "valley: %s: whether %s and %s conflict did not read: %v\n",
151 l.name, view.Changes[i].Handle, view.Changes[j].Handle, err)
152 break
153 }
154 paths := c.paths()
155 pair = rebase.RememberPair(p.cache, s.tip, a, b, rebase.Pair{Total: len(paths), Paths: paths})
156 }
157 markPair(view, i, j, pair, why)
158 markPair(view, j, i, pair, why)
159 }
160 }
161}
162
163// markPair puts on change i of view what comparing it with change j found.
164func markPair(view *jsonRepo, i, j int, pair rebase.Pair, why string) {
165 other := view.Changes[j]
166 c := &view.Changes[i]
167 switch {
168 case why != "":
169 *c.ConflictsUnknown = append(*c.ConflictsUnknown, jsonConflictUnknown{other.ChangeID, other.Handle, why})
170 case pair.Total > 0:
171 paths := []string{}
172 for _, p := range pair.Paths {
173 if utf8.ValidString(p) {
174 paths = append(paths, p)
175 }
176 }
177 *c.Conflicts = append(*c.Conflicts, jsonConflict{other.ChangeID, other.Handle, paths, pair.Total})
178 }
179}
180
181// conflictLines are the lines under a row for a change's conflicts: one
182// naming the changes it conflicts with, or, with full, one for each such
183// change naming the conflicted paths.
184func conflictLines(c jsonChange, full bool) []string {
185 var lines, names []string
186 if c.Conflicts != nil {
187 for _, x := range *c.Conflicts {
188 names = append(names, x.Handle)
189 paths := []string{}
190 for _, p := range x.Paths {
191 paths = append(paths, readable(p))
192 }
193 say := strings.Join(paths, ", ")
194 if len(paths) > 3 || x.PathsTotal > len(paths) {
195 say = fmt.Sprintf("%s and %d more", strings.Join(paths[:min(3, len(paths))], ", "), x.PathsTotal-min(3, len(paths)))
196 }
197 if len(paths) == 0 {
198 say = counted(x.PathsTotal, "path")
199 }
200 lines = append(lines, "conflicts with "+x.Handle+": "+say)
201 }
202 }
203 if full || len(names) == 0 {
204 return lines
205 }
206 return []string{"conflicts with " + strings.Join(names, ", ")}
207}
208
209// uncompared is the line saying how many pairs among the listed changes
210// were not compared, and why, or "" when every pair was.
211func uncompared(all []*entry) string {
212 type pairKey struct{ repo, a, b string }
213 whys := map[pairKey]string{}
214 for _, e := range all {
215 for _, x := range e.unknown {
216 k := pairKey{e.repo, min(e.id, x.ChangeID), max(e.id, x.ChangeID)}
217 whys[k] = x.Why
218 }
219 }
220 if len(whys) == 0 {
221 return ""
222 }
223 count := map[string]int{}
224 for _, why := range whys {
225 count[why]++
226 }
227 var parts []string
228 for _, why := range []string{whyRestack, whyBound, whyFailed} {
229 if n := count[why]; n > 0 {
230 if why == whyBound {
231 why = fmt.Sprintf("past the bound of %d merges", pairBound)
232 }
233 parts = append(parts, fmt.Sprintf("%d %s", n, why))
234 }
235 }
236 if len(parts) == 1 {
237 parts[0] = strings.TrimLeft(parts[0], "0123456789 ")
238 }
239 noun := "pairs"
240 if len(whys) == 1 {
241 noun = "pair"
242 }
243 return fmt.Sprintf("%d %s not compared: %s", len(whys), noun, strings.Join(parts, ", "))
244}