oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package markup
2
3// The three-way merge by block (§6, decision 11): a save against an
4// older revision is merged with what landed since. Edits to different
5// blocks merge silently; when both sides changed the same blocks, the
6// hunk is a conflict, and the person chooses. Nothing is lost silently.
7
8// Hunk is one stretch of the merged body: settled blocks, or a conflict
9// with both sides and what they started from.
10type Hunk struct {
11 Blocks []Block
12 // From and At place a settled hunk's blocks: the side they were
13 // taken from, and the index of the first of them in that side.
14 From Side
15 At int
16 // Conflict marks a hunk both sides changed. Base, Mine and Theirs
17 // are its three sides, starting at BaseAt, MineAt and TheirAt.
18 Conflict bool
19 Base, Mine, Theirs []Block
20 BaseAt, MineAt, TheirAt int
21 // texts are the three bodies the hunk came from, shared by every
22 // hunk of one merge, so Settle can keep their bytes (level-up §6.4).
23 texts *sides
24}
25
26// Side is which body a settled block was taken from.
27type Side int
28
29// The two sides a block can come from. A block both sides kept is
30// theirs: the current revision's bytes are the anchor.
31const (
32 FromTheirs Side = iota + 1
33 FromMine
34)
35
36// sides is one merge's three bodies, normalised, and their blocks.
37type sides struct {
38 base, mine, theirs string
39 baseBlocks, mineBlocks, theirsBlocks []Block
40}
41
42// cursor is a position in each of the three bodies' blocks.
43type cursor struct{ base, mine, theirs int }
44
45// Merge3 merges mine and theirs, both edited from base. clean reports
46// that no hunk is a conflict.
47func Merge3(base, mine, theirs string) (hunks []Hunk, clean bool) {
48 texts := &sides{base: normalise(base), mine: normalise(mine),
49 theirs: normalise(theirs)}
50 texts.baseBlocks, texts.mineBlocks, texts.theirsBlocks =
51 Parse(texts.base), Parse(texts.mine), Parse(texts.theirs)
52 hunks, clean = texts.merge(false)
53 if texts.anyFence() && !texts.layoutsHold(hunks) {
54 // A column layout would break: merge again with every layout a
55 // unit (columns and icons spec §1.8, rules 1 and 2).
56 hunks, clean = texts.merge(true)
57 }
58 return hunks, clean
59}
60
61// merge is one pass of Merge3. With whole, a stretch that touches a
62// column layout on any side is widened to whole layouts on every side,
63// and resolved as one.
64func (texts *sides) merge(whole bool) (hunks []Hunk, clean bool) {
65 toMine := align(texts.baseBlocks, texts.mineBlocks)
66 toTheirs := align(texts.baseBlocks, texts.theirsBlocks)
67 clean = true
68 at := cursor{}
69 for {
70 stable := texts.nextStable(at.base, toMine, toTheirs, whole)
71 end := cursor{len(texts.baseBlocks), len(texts.mineBlocks),
72 len(texts.theirsBlocks)}
73 if stable >= 0 {
74 end = cursor{stable, toMine[stable], toTheirs[stable]}
75 }
76 for _, hunk := range texts.resolveStretch(at, end, whole) {
77 if hunk.Conflict || len(hunk.Blocks) > 0 {
78 hunks = append(hunks, hunk)
79 clean = clean && !hunk.Conflict
80 }
81 }
82 if stable < 0 {
83 return hunks, clean
84 }
85 hunks = append(hunks, texts.settled(FromTheirs, toTheirs[stable], 1))
86 at = cursor{stable + 1, toMine[stable] + 1, toTheirs[stable] + 1}
87 }
88}
89
90// nextStable is the first base block at or after from that both sides
91// kept, or -1. With whole, a block in a column layout on any side is
92// never an anchor, so a stretch reaches out to whole layouts.
93func (texts *sides) nextStable(from int, toMine, toTheirs []int,
94 whole bool) int {
95 for at := from; at < len(toMine); at++ {
96 if toMine[at] < 0 || toTheirs[at] < 0 {
97 continue
98 }
99 if whole && (inLayout(texts.baseBlocks[at]) ||
100 inLayout(texts.mineBlocks[toMine[at]]) ||
101 inLayout(texts.theirsBlocks[toTheirs[at]])) {
102 continue
103 }
104 return at
105 }
106 return -1
107}
108
109// resolveStretch resolves one unstable stretch. When all three sides
110// hold the same number of blocks, each was edited in place, so each
111// position is resolved on its own: Ana's edit to one block and Bo's to
112// the next do not conflict just because they are neighbours.
113func (texts *sides) resolveStretch(at, end cursor, whole bool) []Hunk {
114 if hunks, ok := texts.addedColumns(at, end); ok {
115 return hunks
116 }
117 count := end.base - at.base
118 if count < 2 || end.mine-at.mine != count ||
119 end.theirs-at.theirs != count || whole && texts.fenced(at, end) {
120 return []Hunk{texts.resolve(at, end)}
121 }
122 hunks := make([]Hunk, count)
123 for step := range count {
124 from := cursor{at.base + step, at.mine + step, at.theirs + step}
125 hunks[step] = texts.resolve(from,
126 cursor{from.base + 1, from.mine + 1, from.theirs + 1})
127 }
128 return hunks
129}
130
131// resolve is diff3's rule for one unstable stretch: a side that did not
132// change it yields to the side that did; two identical changes are one;
133// anything else is a conflict.
134func (texts *sides) resolve(at, end cursor) Hunk {
135 base := texts.baseBlocks[at.base:end.base]
136 mine := texts.mineBlocks[at.mine:end.mine]
137 theirs := texts.theirsBlocks[at.theirs:end.theirs]
138 switch {
139 case sameBlocks(mine, base), sameBlocks(mine, theirs):
140 return texts.settled(FromTheirs, at.theirs, len(theirs))
141 case sameBlocks(theirs, base):
142 return texts.settled(FromMine, at.mine, len(mine))
143 }
144 return Hunk{Conflict: true, Base: base, Mine: mine, Theirs: theirs,
145 BaseAt: at.base, MineAt: at.mine, TheirAt: at.theirs, texts: texts}
146}
147
148// settled is a hunk of count blocks taken from one side, from at.
149func (texts *sides) settled(from Side, at, count int) Hunk {
150 blocks := texts.theirsBlocks
151 if from == FromMine {
152 blocks = texts.mineBlocks
153 }
154 return Hunk{Blocks: blocks[at : at+count], From: from, At: at,
155 texts: texts}
156}
157
158func sameBlocks(one, other []Block) bool {
159 if len(one) != len(other) {
160 return false
161 }
162 for at := range one {
163 if one[at].Source != other[at].Source {
164 return false
165 }
166 }
167 return true
168}
169
170// align maps each block of from to its match in to, by a longest common
171// subsequence of block sources, or -1 where it has none.
172func align(fromBlocks, toBlocks []Block) []int {
173 from, to := alignKeys(fromBlocks), alignKeys(toBlocks)
174 rows, columns := len(from), len(to)
175 lengths := make([][]int32, rows+1)
176 for row := range lengths {
177 lengths[row] = make([]int32, columns+1)
178 }
179 for row := rows - 1; row >= 0; row-- {
180 for column := columns - 1; column >= 0; column-- {
181 if from[row] == to[column] {
182 lengths[row][column] = lengths[row+1][column+1] + 1
183 } else {
184 lengths[row][column] = max(lengths[row+1][column],
185 lengths[row][column+1])
186 }
187 }
188 }
189 matched := make([]int, rows)
190 row, column := 0, 0
191 for row < rows {
192 switch {
193 case column < columns && from[row] == to[column]:
194 matched[row] = column
195 row, column = row+1, column+1
196 case column < columns &&
197 lengths[row][column+1] >= lengths[row+1][column]:
198 column++
199 default:
200 matched[row] = -1
201 row++
202 }
203 }
204 return matched
205}
206
207// Choice is how a person settles one conflict.
208type Choice int
209
210// The three answers to a conflict.
211const (
212 KeepMine Choice = iota + 1
213 KeepTheirs
214 KeepBoth
215)
216