| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Portions Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | /* |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | Copyright (c) 2020 Jun Wu |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| b69ab31 | | | 13 | of this software and associated documentation files (the "Software"), to deal |
| b69ab31 | | | 14 | in the Software without restriction, including without limitation the rights |
| b69ab31 | | | 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| b69ab31 | | | 16 | copies of the Software, and to permit persons to whom the Software is |
| b69ab31 | | | 17 | furnished to do so, subject to the following conditions: |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | The above copyright notice and this permission notice shall be included in all |
| b69ab31 | | | 20 | copies or substantial portions of the Software. |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| b69ab31 | | | 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| b69ab31 | | | 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| b69ab31 | | | 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| b69ab31 | | | 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| b69ab31 | | | 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| b69ab31 | | | 28 | SOFTWARE. |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | */ |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | import type {RecordOf, ValueObject} from 'immutable'; |
| b69ab31 | | | 33 | import type {LRUWithStats} from 'shared/LRU'; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | import {hash, Set as ImSet, List, Record} from 'immutable'; |
| b69ab31 | | | 36 | import {cached, cachedMethod, LRU} from 'shared/LRU'; |
| b69ab31 | | | 37 | import {diffLines, splitLines} from 'shared/diff'; |
| b69ab31 | | | 38 | import {SelfUpdate} from 'shared/immutableExt'; |
| b69ab31 | | | 39 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 40 | import {assert} from './utils'; |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | /** Operation code. */ |
| b69ab31 | | | 43 | enum Op { |
| b69ab31 | | | 44 | /** Unconditional jump. */ |
| b69ab31 | | | 45 | J = 0, |
| b69ab31 | | | 46 | /** Jump if the current rev >= operand. */ |
| b69ab31 | | | 47 | JGE = 1, |
| b69ab31 | | | 48 | /** Jump if the current rev < operand. */ |
| b69ab31 | | | 49 | JL = 2, |
| b69ab31 | | | 50 | /** Append a line. */ |
| b69ab31 | | | 51 | LINE = 3, |
| b69ab31 | | | 52 | /** End execution. */ |
| b69ab31 | | | 53 | END = 4, |
| b69ab31 | | | 54 | } |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | /** J instruction. */ |
| b69ab31 | | | 57 | const J = Record( |
| b69ab31 | | | 58 | { |
| b69ab31 | | | 59 | /** Opcode: J */ |
| b69ab31 | | | 60 | op: Op.J, |
| b69ab31 | | | 61 | /** Program counter (offset to jump). */ |
| b69ab31 | | | 62 | pc: 0, |
| b69ab31 | | | 63 | }, |
| b69ab31 | | | 64 | 'J', |
| b69ab31 | | | 65 | ); |
| b69ab31 | | | 66 | type J = RecordOf<{ |
| b69ab31 | | | 67 | op: Op.J; |
| b69ab31 | | | 68 | pc: number; |
| b69ab31 | | | 69 | }>; |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | /** JGE instruction. */ |
| b69ab31 | | | 72 | const JGE = Record( |
| b69ab31 | | | 73 | { |
| b69ab31 | | | 74 | /** Opcode: JGE */ |
| b69ab31 | | | 75 | op: Op.JGE, |
| b69ab31 | | | 76 | /** `rev` to test. */ |
| b69ab31 | | | 77 | rev: 0, |
| b69ab31 | | | 78 | /** Program counter (offset to jump). */ |
| b69ab31 | | | 79 | pc: 0, |
| b69ab31 | | | 80 | }, |
| b69ab31 | | | 81 | 'JGE', |
| b69ab31 | | | 82 | ); |
| b69ab31 | | | 83 | type JGE = RecordOf<{ |
| b69ab31 | | | 84 | op: Op.JGE; |
| b69ab31 | | | 85 | rev: Rev; |
| b69ab31 | | | 86 | pc: number; |
| b69ab31 | | | 87 | }>; |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | /** JL instruction. */ |
| b69ab31 | | | 90 | const JL = Record( |
| b69ab31 | | | 91 | { |
| b69ab31 | | | 92 | /** Opcode: JL */ |
| b69ab31 | | | 93 | op: Op.JL, |
| b69ab31 | | | 94 | /** `rev` to test. */ |
| b69ab31 | | | 95 | rev: 0, |
| b69ab31 | | | 96 | /** Program counter (offset to jump). */ |
| b69ab31 | | | 97 | pc: 0, |
| b69ab31 | | | 98 | }, |
| b69ab31 | | | 99 | 'JL', |
| b69ab31 | | | 100 | ); |
| b69ab31 | | | 101 | type JL = RecordOf<{ |
| b69ab31 | | | 102 | op: Op.JL; |
| b69ab31 | | | 103 | rev: Rev; |
| b69ab31 | | | 104 | pc: number; |
| b69ab31 | | | 105 | }>; |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | /** LINE instruction. */ |
| b69ab31 | | | 108 | const LINE = Record( |
| b69ab31 | | | 109 | { |
| b69ab31 | | | 110 | /** Opcode: LINE */ |
| b69ab31 | | | 111 | op: Op.LINE, |
| b69ab31 | | | 112 | /** `rev` to test. */ |
| b69ab31 | | | 113 | rev: 0, |
| b69ab31 | | | 114 | /** Line content. Includes EOL. */ |
| b69ab31 | | | 115 | data: '', |
| b69ab31 | | | 116 | }, |
| b69ab31 | | | 117 | 'LINE', |
| b69ab31 | | | 118 | ); |
| b69ab31 | | | 119 | type LINE = RecordOf<{ |
| b69ab31 | | | 120 | op: Op.LINE; |
| b69ab31 | | | 121 | rev: Rev; |
| b69ab31 | | | 122 | data: string; |
| b69ab31 | | | 123 | }>; |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | /** END instruction. */ |
| b69ab31 | | | 126 | const END = Record( |
| b69ab31 | | | 127 | { |
| b69ab31 | | | 128 | /** Opcode: END */ |
| b69ab31 | | | 129 | op: Op.END, |
| b69ab31 | | | 130 | }, |
| b69ab31 | | | 131 | 'END', |
| b69ab31 | | | 132 | ); |
| b69ab31 | | | 133 | type END = RecordOf<{ |
| b69ab31 | | | 134 | op: Op.END; |
| b69ab31 | | | 135 | }>; |
| b69ab31 | | | 136 | |
| b69ab31 | | | 137 | /** Program counter (offset to instructions). */ |
| b69ab31 | | | 138 | type Pc = number; |
| b69ab31 | | | 139 | |
| b69ab31 | | | 140 | /** Revision number. Usually starts from 1. Larger number means newer versions. |
| b69ab31 | | | 141 | * |
| b69ab31 | | | 142 | * For advanced use-cases, this can be a floating number. For example, absorb |
| b69ab31 | | | 143 | * might use rev 2.5 between rev2 and rev 3, indicating the chunk is currently |
| b69ab31 | | | 144 | * absorbed into rev 2 (by checking out 2.999), while still preserving the rev |
| b69ab31 | | | 145 | * 2 losslessly (by checking out 2, instead of 2.999). `absorb` might also use |
| b69ab31 | | | 146 | * different fractional part to "tag" the chunk so they can be treated |
| b69ab31 | | | 147 | * differently. For example, there are 2 chunk edits in the working copy a.txt, |
| b69ab31 | | | 148 | * absorb might use `1 / 1024` to represent the first edit, and `2 / 1024` for |
| b69ab31 | | | 149 | * the second chunk edit so those edits can be moved (remapped) separately. |
| b69ab31 | | | 150 | * absorb might also split a chunk edit into multiple edits so the individual |
| b69ab31 | | | 151 | * lines can be moved separately. |
| b69ab31 | | | 152 | */ |
| b69ab31 | | | 153 | type Rev = number; |
| b69ab31 | | | 154 | |
| b69ab31 | | | 155 | /** Index of a line. Starts from 0. */ |
| b69ab31 | | | 156 | type LineIdx = number; |
| b69ab31 | | | 157 | |
| b69ab31 | | | 158 | /** Instruction. */ |
| b69ab31 | | | 159 | type Inst = J | END | JGE | JL | LINE; |
| b69ab31 | | | 160 | |
| b69ab31 | | | 161 | /** Information about a line. Internal (`lines`) result of `LineLog.checkOut`. */ |
| b69ab31 | | | 162 | interface LineInfo { |
| b69ab31 | | | 163 | /** Line content. Includes EOL. */ |
| b69ab31 | | | 164 | data: string; |
| b69ab31 | | | 165 | /** Added by the given rev. */ |
| b69ab31 | | | 166 | rev: Rev; |
| b69ab31 | | | 167 | /** Produced by the instruction at the given offset. */ |
| b69ab31 | | | 168 | pc: Pc; |
| b69ab31 | | | 169 | /** |
| b69ab31 | | | 170 | * Whether the line is deleted. |
| b69ab31 | | | 171 | * This is always `false` if `checkOut(rev, None)`. |
| b69ab31 | | | 172 | * It might be `true` when checking out a range of revisions |
| b69ab31 | | | 173 | * (aka. `start` passed to `checkOut` is not `null`). |
| b69ab31 | | | 174 | */ |
| b69ab31 | | | 175 | deleted: boolean; |
| b69ab31 | | | 176 | } |
| b69ab31 | | | 177 | |
| b69ab31 | | | 178 | /** A "flatten" line. Result of `LineLog.flatten()`. */ |
| b69ab31 | | | 179 | type FlattenLineProps = { |
| b69ab31 | | | 180 | /** The line is present in the given revisions. */ |
| b69ab31 | | | 181 | revs: ImSet<Rev>; |
| b69ab31 | | | 182 | /** Content of the line, including `\n`. */ |
| b69ab31 | | | 183 | data: string; |
| b69ab31 | | | 184 | }; |
| b69ab31 | | | 185 | const FlattenLine = Record<FlattenLineProps>({revs: ImSet(), data: ''}); |
| b69ab31 | | | 186 | type FlattenLine = RecordOf<FlattenLineProps>; |
| b69ab31 | | | 187 | |
| b69ab31 | | | 188 | /** Used by visitWithInsDelStacks */ |
| b69ab31 | | | 189 | type Frame = {rev: Rev; endPc: Pc}; |
| b69ab31 | | | 190 | |
| b69ab31 | | | 191 | /** |
| b69ab31 | | | 192 | * List of instructions. |
| b69ab31 | | | 193 | * |
| b69ab31 | | | 194 | * This is a wrapper of `List<Inst>` for more efficient `hashCode` and `equals` |
| b69ab31 | | | 195 | * calculations. The default `hashCode` from `immutable.js` scans the whole |
| b69ab31 | | | 196 | * `List`. In this implementation we keep 2 internal values: hash and str. The |
| b69ab31 | | | 197 | * `hash` is used for hashCode, and the `str` is an append-only string that |
| b69ab31 | | | 198 | * tracks the `editChunk` and other operations to `List<Inst>` for testing |
| b69ab31 | | | 199 | * equality. |
| b69ab31 | | | 200 | * |
| b69ab31 | | | 201 | * You might have noticed that the `str` equality might not match the |
| b69ab31 | | | 202 | * `List<Inst>` equality. For example, if we remap 1 to 2, then remap 2 to 1, |
| b69ab31 | | | 203 | * the `List<Inst>` is not changed, but the `str` is changed. It is okay to |
| b69ab31 | | | 204 | * treat the linelogs as different in this case as we almost always immediately |
| b69ab31 | | | 205 | * rebuild linelogs after a `remap`. It's important to make sure `recordText` |
| b69ab31 | | | 206 | * with the same text list gets cache hit. |
| b69ab31 | | | 207 | */ |
| b69ab31 | | | 208 | class Code implements ValueObject { |
| b69ab31 | | | 209 | constructor( |
| b69ab31 | | | 210 | private instList: List<Inst> = List([END() as Inst]), |
| b69ab31 | | | 211 | private __hash: Readonly<number> = 0, |
| b69ab31 | | | 212 | private __valueOf: Readonly<string> = '', |
| b69ab31 | | | 213 | ) {} |
| b69ab31 | | | 214 | |
| b69ab31 | | | 215 | getSize(): number { |
| b69ab31 | | | 216 | return this.instList.size; |
| b69ab31 | | | 217 | } |
| b69ab31 | | | 218 | |
| b69ab31 | | | 219 | get(pc: Pc): Readonly<Inst> | undefined { |
| b69ab31 | | | 220 | return this.instList.get(pc); |
| b69ab31 | | | 221 | } |
| b69ab31 | | | 222 | |
| b69ab31 | | | 223 | valueOf(): string { |
| b69ab31 | | | 224 | return this.__valueOf; |
| b69ab31 | | | 225 | } |
| b69ab31 | | | 226 | |
| b69ab31 | | | 227 | equals(other: Code): boolean { |
| b69ab31 | | | 228 | return this.__valueOf === other.__valueOf; |
| b69ab31 | | | 229 | } |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | hashCode(): number { |
| b69ab31 | | | 232 | return this.__hash; |
| b69ab31 | | | 233 | } |
| b69ab31 | | | 234 | |
| b69ab31 | | | 235 | /** |
| b69ab31 | | | 236 | * Dump instructions in a human readable format. Useful for debugging. |
| b69ab31 | | | 237 | * Note: This exposes internal details which might change in the future. |
| b69ab31 | | | 238 | */ |
| b69ab31 | | | 239 | describeHumanReadableInstructions(): string[] { |
| b69ab31 | | | 240 | return this.instList.map((inst, i) => `${i}: ${describeInst(inst)}`).toArray(); |
| b69ab31 | | | 241 | } |
| b69ab31 | | | 242 | |
| b69ab31 | | | 243 | /** |
| b69ab31 | | | 244 | * Dump lines with ASCII annotated insertions and deletions stacks. |
| b69ab31 | | | 245 | */ |
| b69ab31 | | | 246 | describeHumanReadableInsDelStacks(): string[] { |
| b69ab31 | | | 247 | // 1st Pass: Figure out the max stack depth, line length for padding. |
| b69ab31 | | | 248 | let maxInsStackDepth = 0; |
| b69ab31 | | | 249 | let maxDelStackDepth = 0; |
| b69ab31 | | | 250 | let maxLineLength = 'Insert (rev: 1000)'.length; |
| b69ab31 | | | 251 | this.visitWithInsDelStacks((insStack, delStack) => { |
| b69ab31 | | | 252 | return { |
| b69ab31 | | | 253 | onStackPush: () => { |
| b69ab31 | | | 254 | maxInsStackDepth = Math.max(maxInsStackDepth, insStack.length + 1); |
| b69ab31 | | | 255 | maxDelStackDepth = Math.max(maxDelStackDepth, delStack.length + 2); |
| b69ab31 | | | 256 | }, |
| b69ab31 | | | 257 | onLine: line => { |
| b69ab31 | | | 258 | maxLineLength = Math.max(maxLineLength, line.data.length + 'Line: '.length); |
| b69ab31 | | | 259 | }, |
| b69ab31 | | | 260 | }; |
| b69ab31 | | | 261 | }); |
| b69ab31 | | | 262 | // 2nd Pass: Render the instructions. |
| b69ab31 | | | 263 | const result: string[] = []; |
| b69ab31 | | | 264 | this.visitWithInsDelStacks((insStack, delStack) => { |
| b69ab31 | | | 265 | const pushLine = (data: string, leftAdjust?: number, rightAdjust?: number) => { |
| b69ab31 | | | 266 | const insDepth = insStack.length - 1 + (leftAdjust ?? 0); |
| b69ab31 | | | 267 | const delDepth = delStack.length + (rightAdjust ?? 0); |
| b69ab31 | | | 268 | const insPad = maxInsStackDepth - insDepth; |
| b69ab31 | | | 269 | const delPad = maxDelStackDepth - delDepth; |
| b69ab31 | | | 270 | const left = |
| b69ab31 | | | 271 | '|'.repeat(insDepth) + |
| b69ab31 | | | 272 | (leftAdjust == null ? ' '.repeat(insPad + 1) : `+${'-'.repeat(insPad)}`); |
| b69ab31 | | | 273 | const right = |
| b69ab31 | | | 274 | (rightAdjust == null ? ' '.repeat(delPad + 1) : `${'-'.repeat(delPad)}+`) + |
| b69ab31 | | | 275 | '|'.repeat(delDepth); |
| b69ab31 | | | 276 | const middle = data + ' '.repeat(maxLineLength - data.length); |
| b69ab31 | | | 277 | result.push(left + middle + right); |
| b69ab31 | | | 278 | }; |
| b69ab31 | | | 279 | return { |
| b69ab31 | | | 280 | onStackPush: stack => { |
| b69ab31 | | | 281 | const rev = stack.at(-1)?.rev ?? 0; |
| b69ab31 | | | 282 | if (stack === insStack) { |
| b69ab31 | | | 283 | // | | +------ Insert (rev x) <- this line |
| b69ab31 | | | 284 | // | | | Line: .... <- following lines |
| b69ab31 | | | 285 | pushLine(`Insert (rev ${rev})`, -1); |
| b69ab31 | | | 286 | } else { |
| b69ab31 | | | 287 | pushLine(`Delete (rev ${rev})`, undefined, -1); |
| b69ab31 | | | 288 | } |
| b69ab31 | | | 289 | }, |
| b69ab31 | | | 290 | onStackPop: stack => { |
| b69ab31 | | | 291 | if (stack === insStack) { |
| b69ab31 | | | 292 | pushLine('', 0); |
| b69ab31 | | | 293 | } else { |
| b69ab31 | | | 294 | pushLine('', undefined, 0); |
| b69ab31 | | | 295 | } |
| b69ab31 | | | 296 | }, |
| b69ab31 | | | 297 | onLine: line => { |
| b69ab31 | | | 298 | pushLine(`Line: ${line.data.trimEnd()}`); |
| b69ab31 | | | 299 | }, |
| b69ab31 | | | 300 | }; |
| b69ab31 | | | 301 | }); |
| b69ab31 | | | 302 | return result; |
| b69ab31 | | | 303 | } |
| b69ab31 | | | 304 | |
| b69ab31 | | | 305 | editChunk( |
| b69ab31 | | | 306 | aRev: Rev, |
| b69ab31 | | | 307 | a1: LineIdx, |
| b69ab31 | | | 308 | a2: LineIdx, |
| b69ab31 | | | 309 | bRev: Rev, |
| b69ab31 | | | 310 | bLines: string[], |
| b69ab31 | | | 311 | [aLines, aLinesMutable]: [LineInfo[], true] | [Readonly<LineInfo[]>, false], |
| b69ab31 | | | 312 | ): Code { |
| b69ab31 | | | 313 | const start = this.instList.size; |
| b69ab31 | | | 314 | |
| b69ab31 | | | 315 | assert(a1 <= a2, 'illegal chunk (a1 < a2)'); |
| b69ab31 | | | 316 | assert(a2 <= aLines.length, 'out of bound a2 (wrong aRev?)'); |
| b69ab31 | | | 317 | |
| b69ab31 | | | 318 | // See also https://sapling-scm.com/docs/internals/linelog/#editing-linelog |
| b69ab31 | | | 319 | // # Before # After |
| b69ab31 | | | 320 | // # (pc): Instruction # (pc): Instruction |
| b69ab31 | | | 321 | // : ... : ... |
| b69ab31 | | | 322 | // a1Pc: <a1Inst> a1Pc: J start |
| b69ab31 | | | 323 | // a1Pc+1: ... a1Pc+1: ... |
| b69ab31 | | | 324 | // : ... : ... |
| b69ab31 | | | 325 | // a2Pc: ... a2Pc: ... |
| b69ab31 | | | 326 | // : ... : ... |
| b69ab31 | | | 327 | // len: N/A start: JL brev b2Pc [1] |
| b69ab31 | | | 328 | // : LINE brev b1 [1] |
| b69ab31 | | | 329 | // : LINE brev b1+1 [1] |
| b69ab31 | | | 330 | // : ... [1] |
| b69ab31 | | | 331 | // : LINE brev b2-1 [1] |
| b69ab31 | | | 332 | // b2Pc: JGE brev a2Pc [2] |
| b69ab31 | | | 333 | // : <a1Inst> (moved) [3] |
| b69ab31 | | | 334 | // : J a1Pc+1 [4] |
| b69ab31 | | | 335 | // [1]: Only present if `bLines` is not empty. |
| b69ab31 | | | 336 | // [2]: Only present if `a1 < a2`. |
| b69ab31 | | | 337 | // There are 2 choices for "a2Pc": |
| b69ab31 | | | 338 | // - The a2 line exactly: aLines[a2].pc |
| b69ab31 | | | 339 | // - The next instruction of the "a2 -1" line: aLines[a2 - 1].pc + 1 |
| b69ab31 | | | 340 | // We pick the latter to avoid overly aggressive deletion. |
| b69ab31 | | | 341 | // The original C implementation might pick the former when editing |
| b69ab31 | | | 342 | // the last rev for performance optimization. |
| b69ab31 | | | 343 | // [3]: <a1 Inst> could be LINE or END. |
| b69ab31 | | | 344 | // [4]: As an optimization, this is only present if <a1 Inst> is not END. |
| b69ab31 | | | 345 | // |
| b69ab31 | | | 346 | // Optimization [OPT1] to make reorder less restrictive, treat insertion |
| b69ab31 | | | 347 | // (a1 == a2) at the beginning of another insertion (<a1 Inst> is after a |
| b69ab31 | | | 348 | // <JL>) specially. Our goal is to avoid nested JLs. Instead of patching |
| b69ab31 | | | 349 | // the a1Inst after the JL, we patch the JL (jlInst) so we can insert our |
| b69ab31 | | | 350 | // new JL (for this edit) before the old JL (jlInst, being patched). |
| b69ab31 | | | 351 | // Note this "JL followed by a1Inst" optimization needs to be applicable |
| b69ab31 | | | 352 | // multiple times. To do that, we also move the a1Inst to right after the |
| b69ab31 | | | 353 | // jlInst so the pattern "JL followed by a1Inst" can be recognized by the |
| b69ab31 | | | 354 | // next editChunk to apply the same optimization. |
| b69ab31 | | | 355 | // |
| b69ab31 | | | 356 | // # Before # After |
| b69ab31 | | | 357 | // # (pc): Instruction # (pc): Instruction |
| b69ab31 | | | 358 | // : ... : ... |
| b69ab31 | | | 359 | // : <jlInst> a1Pc-1: J start [*] |
| b69ab31 | | | 360 | // a1Pc: <a1Inst> a1Pc: NOP (J a1Pc+1) [*] |
| b69ab31 | | | 361 | // : ... : ... |
| b69ab31 | | | 362 | // len: N/A start: JL brev b2Pc |
| b69ab31 | | | 363 | // : (bLines) |
| b69ab31 | | | 364 | // b2Pc: <jlInst> (moved) [*] |
| b69ab31 | | | 365 | // : <a1Inst> (moved) |
| b69ab31 | | | 366 | // : J a1Pc [*] |
| b69ab31 | | | 367 | const newInstList = this.instList.withMutations(origCode => { |
| b69ab31 | | | 368 | let code = origCode; |
| b69ab31 | | | 369 | const a1Pc = aLines[a1].pc; |
| b69ab31 | | | 370 | // If `jlInst` is set, optimization [OPT1] is in effect. |
| b69ab31 | | | 371 | let jlInst = a1Pc > 0 && a1 === a2 ? code.get(a1Pc - 1) : undefined; |
| b69ab31 | | | 372 | if (jlInst?.op !== Op.JL) { |
| b69ab31 | | | 373 | jlInst = undefined; |
| b69ab31 | | | 374 | } |
| b69ab31 | | | 375 | if (bLines.length > 0) { |
| b69ab31 | | | 376 | // [1] |
| b69ab31 | | | 377 | const b2Pc = start + bLines.length + 1; |
| b69ab31 | | | 378 | code = code.push(JL({rev: bRev, pc: b2Pc}) as Inst); |
| b69ab31 | | | 379 | bLines.forEach(line => { |
| b69ab31 | | | 380 | code = code.push(LINE({rev: bRev, data: line}) as Inst); |
| b69ab31 | | | 381 | }); |
| b69ab31 | | | 382 | assert(b2Pc === code.size, 'bug: wrong pc'); |
| b69ab31 | | | 383 | } |
| b69ab31 | | | 384 | if (a1 < a2) { |
| b69ab31 | | | 385 | assert(jlInst === undefined, 'OPT1 requires no deletion'); |
| b69ab31 | | | 386 | // [2] |
| b69ab31 | | | 387 | const a2Pc = aLines[a2 - 1].pc + 1; |
| b69ab31 | | | 388 | code = code.push(JGE({rev: bRev, pc: a2Pc}) as Inst); |
| b69ab31 | | | 389 | } |
| b69ab31 | | | 390 | if (aLinesMutable) { |
| b69ab31 | | | 391 | aLines[a1] = {...aLines[a1], pc: jlInst == null ? code.size : code.size + 1}; |
| b69ab31 | | | 392 | } |
| b69ab31 | | | 393 | const a1Inst = nullthrows(code.get(a1Pc)); |
| b69ab31 | | | 394 | if (jlInst === undefined) { |
| b69ab31 | | | 395 | // [3] |
| b69ab31 | | | 396 | code = code.push(a1Inst); |
| b69ab31 | | | 397 | if (a1Inst.op /* LINE or END */ !== Op.END) { |
| b69ab31 | | | 398 | // [4] |
| b69ab31 | | | 399 | code = code.push(J({pc: a1Pc + 1}) as Inst); |
| b69ab31 | | | 400 | } |
| b69ab31 | | | 401 | code = code.set(a1Pc, J({pc: start}) as Inst); |
| b69ab31 | | | 402 | } else { |
| b69ab31 | | | 403 | code = code |
| b69ab31 | | | 404 | .push(jlInst) |
| b69ab31 | | | 405 | .push(a1Inst) |
| b69ab31 | | | 406 | .push(J({pc: a1Pc}) as Inst) |
| b69ab31 | | | 407 | .set(a1Pc - 1, J({pc: start}) as Inst) |
| b69ab31 | | | 408 | .set(a1Pc, J({pc: a1Pc + 1}) as J); |
| b69ab31 | | | 409 | } |
| b69ab31 | | | 410 | return code; |
| b69ab31 | | | 411 | }); |
| b69ab31 | | | 412 | |
| b69ab31 | | | 413 | if (aLinesMutable) { |
| b69ab31 | | | 414 | const newLines = bLines.map((s, i) => { |
| b69ab31 | | | 415 | return {data: s, rev: bRev, pc: start + 1 + i, deleted: false}; |
| b69ab31 | | | 416 | }); |
| b69ab31 | | | 417 | aLines.splice(a1, a2 - a1, ...newLines); |
| b69ab31 | | | 418 | } |
| b69ab31 | | | 419 | |
| b69ab31 | | | 420 | const newValueOf = `E${aRev},${a1},${a2},${bRev},${bLines.join('')}`; |
| b69ab31 | | | 421 | return this.newCode(newInstList, newValueOf); |
| b69ab31 | | | 422 | } |
| b69ab31 | | | 423 | |
| b69ab31 | | | 424 | /** |
| b69ab31 | | | 425 | * Visit (execute) instructions with the insertion and deletion stacks |
| b69ab31 | | | 426 | * converted from JGE and JL instructions maintained by this function. |
| b69ab31 | | | 427 | * |
| b69ab31 | | | 428 | * See the comment in this function about how to turn JGE and JL to |
| b69ab31 | | | 429 | * the stacks. |
| b69ab31 | | | 430 | * |
| b69ab31 | | | 431 | * For stacks like this: |
| b69ab31 | | | 432 | * |
| b69ab31 | | | 433 | * +---- Insertion (rev 1) |
| b69ab31 | | | 434 | * | Line 1 |
| b69ab31 | | | 435 | * | ----+ Deletion (rev 4) |
| b69ab31 | | | 436 | * | Line 2 | |
| b69ab31 | | | 437 | * | +-- Insertion (rev 2) | |
| b69ab31 | | | 438 | * | | Line 3 | |
| b69ab31 | | | 439 | * | | --+ | Deletion (rev 3) |
| b69ab31 | | | 440 | * | | Line 4 | | |
| b69ab31 | | | 441 | * | +-- | | |
| b69ab31 | | | 442 | * | Line 5 | | |
| b69ab31 | | | 443 | * | --+ | |
| b69ab31 | | | 444 | * | Line 6 | |
| b69ab31 | | | 445 | * | ----+ |
| b69ab31 | | | 446 | * | Line 7 |
| b69ab31 | | | 447 | * +---- |
| b69ab31 | | | 448 | * |
| b69ab31 | | | 449 | * When visiting "Line 3", the callsite will get insertion stack = |
| b69ab31 | | | 450 | * [rev 1, rev 2] and deletion stack = [rev 4]. |
| b69ab31 | | | 451 | * |
| b69ab31 | | | 452 | * Internally, this is done by turning conditional jumps (JGE or JL) |
| b69ab31 | | | 453 | * to stack pushes, pops at the JGE or JL destinations, and follow |
| b69ab31 | | | 454 | * unconditional jumps (J) as usual. For more details, see the comment |
| b69ab31 | | | 455 | * inside this function. |
| b69ab31 | | | 456 | * |
| b69ab31 | | | 457 | * This function will call `withContext` to provide the `insStack` and |
| b69ab31 | | | 458 | * `delStack` context, and expect the callsite to provide handlers it |
| b69ab31 | | | 459 | * is interested in. |
| b69ab31 | | | 460 | * |
| b69ab31 | | | 461 | * Typical use-cases include features that need to scan all (ever existed) |
| b69ab31 | | | 462 | * lines like flatten() and calculateDepMap(). |
| b69ab31 | | | 463 | */ |
| b69ab31 | | | 464 | visitWithInsDelStacks( |
| b69ab31 | | | 465 | withContext: ( |
| b69ab31 | | | 466 | insStack: Readonly<Frame[]>, |
| b69ab31 | | | 467 | delStack: Readonly<Frame[]>, |
| b69ab31 | | | 468 | ) => { |
| b69ab31 | | | 469 | /** Before stack pop or push */ |
| b69ab31 | | | 470 | onPc?: (pc: number) => void; |
| b69ab31 | | | 471 | /** After stack pop, before stack push */ |
| b69ab31 | | | 472 | onLine?: (inst: LINE) => void; |
| b69ab31 | | | 473 | /** After stack pop, before stack push */ |
| b69ab31 | | | 474 | onConditionalJump?: (inst: JGE | JL) => void; |
| b69ab31 | | | 475 | /** After stack push */ |
| b69ab31 | | | 476 | onStackPush?: (stack: Readonly<Frame[]>) => void; |
| b69ab31 | | | 477 | /** After stack pop */ |
| b69ab31 | | | 478 | onStackPop?: (stack: Readonly<Frame[]>) => void; |
| b69ab31 | | | 479 | }, |
| b69ab31 | | | 480 | ) { |
| b69ab31 | | | 481 | // How does it work? First, insertions and deletions in linelog form |
| b69ab31 | | | 482 | // tree structures. For example: |
| b69ab31 | | | 483 | // |
| b69ab31 | | | 484 | // +---- Insertion (rev 1) |
| b69ab31 | | | 485 | // | Line 1 |
| b69ab31 | | | 486 | // | ----+ Deletion (rev 4) |
| b69ab31 | | | 487 | // | Line 2 | |
| b69ab31 | | | 488 | // | +-- Insertion (rev 2) | |
| b69ab31 | | | 489 | // | | Line 3 | |
| b69ab31 | | | 490 | // | | --+ | Deletion (rev 3) |
| b69ab31 | | | 491 | // | | Line 4 | | |
| b69ab31 | | | 492 | // | +-- | | |
| b69ab31 | | | 493 | // | Line 5 | | |
| b69ab31 | | | 494 | // | --+ | |
| b69ab31 | | | 495 | // | Line 6 | |
| b69ab31 | | | 496 | // | ----+ |
| b69ab31 | | | 497 | // | Line 7 |
| b69ab31 | | | 498 | // +---- |
| b69ab31 | | | 499 | // |
| b69ab31 | | | 500 | // Note interleaved insertions do not happen. For example, this does not |
| b69ab31 | | | 501 | // happen: |
| b69ab31 | | | 502 | // |
| b69ab31 | | | 503 | // +---- Insertion (rev 1) |
| b69ab31 | | | 504 | // | Line 1 |
| b69ab31 | | | 505 | // | +-- Insertion (rev 2) |
| b69ab31 | | | 506 | // | | Line 2 |
| b69ab31 | | | 507 | // +-|-- |
| b69ab31 | | | 508 | // | Line 3 |
| b69ab31 | | | 509 | // +-- |
| b69ab31 | | | 510 | // |
| b69ab31 | | | 511 | // Similarly, interleaved deletions do not happen. However, insertions |
| b69ab31 | | | 512 | // might interleave with deletions, as shown above. |
| b69ab31 | | | 513 | // |
| b69ab31 | | | 514 | // Let's look at how this is done at the instruction level. First, look at |
| b69ab31 | | | 515 | // the instructions generated by editChunk: |
| b69ab31 | | | 516 | // |
| b69ab31 | | | 517 | // a2Pc: ... |
| b69ab31 | | | 518 | // ... |
| b69ab31 | | | 519 | // start: JL brev b2Pc |
| b69ab31 | | | 520 | // ... |
| b69ab31 | | | 521 | // b2Pc: JGE brev a2Pc |
| b69ab31 | | | 522 | // : <a1 Inst> |
| b69ab31 | | | 523 | // end: J a1Pc+1 |
| b69ab31 | | | 524 | // |
| b69ab31 | | | 525 | // JL is used for insertion, JGE is used for deletion. We then use them to |
| b69ab31 | | | 526 | // manipulate the insStack and delStack: |
| b69ab31 | | | 527 | // |
| b69ab31 | | | 528 | // insStack: |
| b69ab31 | | | 529 | // |
| b69ab31 | | | 530 | // - On "start: JL brev b2Pc": |
| b69ab31 | | | 531 | // Do not follow the JL jump. |
| b69ab31 | | | 532 | // Push {rev, b2Pc} to insStack. |
| b69ab31 | | | 533 | // - When pc is b2Pc, pop insStack. |
| b69ab31 | | | 534 | // |
| b69ab31 | | | 535 | // delStack: |
| b69ab31 | | | 536 | // |
| b69ab31 | | | 537 | // - On "b2Pc: JGE brev a2Pc": |
| b69ab31 | | | 538 | // Do not follow the JGE jump. |
| b69ab31 | | | 539 | // Push {rev, a2Pc} to delStack. |
| b69ab31 | | | 540 | // - When pc is a2Pc, pop delStack. |
| b69ab31 | | | 541 | // |
| b69ab31 | | | 542 | // You might have noticed that we don't use the revs in LINE instructions |
| b69ab31 | | | 543 | // at all. This is because that LINE rev always matches its JL rev in this |
| b69ab31 | | | 544 | // implementation. In other words, the "rev" in LINE instruction is |
| b69ab31 | | | 545 | // redundant as it can be inferred from JL, with an insStack. Note in the |
| b69ab31 | | | 546 | // original C implementation of LineLog the LINE rev can be different from |
| b69ab31 | | | 547 | // the JL rev, to deal with merges while maintaining a linear history. |
| b69ab31 | | | 548 | const insStack: Frame[] = [{rev: 0, endPc: -1}]; |
| b69ab31 | | | 549 | const delStack: Frame[] = []; |
| b69ab31 | | | 550 | const {onPc, onLine, onConditionalJump, onStackPush, onStackPop} = withContext( |
| b69ab31 | | | 551 | insStack, |
| b69ab31 | | | 552 | delStack, |
| b69ab31 | | | 553 | ); |
| b69ab31 | | | 554 | let pc = 0; |
| b69ab31 | | | 555 | let patience = this.getSize() * 2; |
| b69ab31 | | | 556 | while (patience > 0) { |
| b69ab31 | | | 557 | onPc?.(pc); |
| b69ab31 | | | 558 | if (insStack.at(-1)?.endPc === pc) { |
| b69ab31 | | | 559 | insStack.pop(); |
| b69ab31 | | | 560 | onStackPop?.(insStack); |
| b69ab31 | | | 561 | } |
| b69ab31 | | | 562 | if (delStack.at(-1)?.endPc === pc) { |
| b69ab31 | | | 563 | delStack.pop(); |
| b69ab31 | | | 564 | onStackPop?.(delStack); |
| b69ab31 | | | 565 | } |
| b69ab31 | | | 566 | const code = nullthrows(this.get(pc)); |
| b69ab31 | | | 567 | switch (code.op) { |
| b69ab31 | | | 568 | case Op.LINE: |
| b69ab31 | | | 569 | onLine?.(code); |
| b69ab31 | | | 570 | pc += 1; |
| b69ab31 | | | 571 | break; |
| b69ab31 | | | 572 | case Op.END: |
| b69ab31 | | | 573 | patience = -1; |
| b69ab31 | | | 574 | break; |
| b69ab31 | | | 575 | case Op.J: |
| b69ab31 | | | 576 | pc = code.pc; |
| b69ab31 | | | 577 | break; |
| b69ab31 | | | 578 | case Op.JGE: |
| b69ab31 | | | 579 | onConditionalJump?.(code); |
| b69ab31 | | | 580 | delStack.push({rev: code.rev, endPc: code.pc}); |
| b69ab31 | | | 581 | onStackPush?.(delStack); |
| b69ab31 | | | 582 | pc += 1; |
| b69ab31 | | | 583 | break; |
| b69ab31 | | | 584 | case Op.JL: |
| b69ab31 | | | 585 | onConditionalJump?.(code); |
| b69ab31 | | | 586 | insStack.push({rev: code.rev, endPc: code.pc}); |
| b69ab31 | | | 587 | onStackPush?.(insStack); |
| b69ab31 | | | 588 | pc += 1; |
| b69ab31 | | | 589 | break; |
| b69ab31 | | | 590 | default: |
| b69ab31 | | | 591 | assert(false, 'bug: unknown code'); |
| b69ab31 | | | 592 | } |
| b69ab31 | | | 593 | patience -= 1; |
| b69ab31 | | | 594 | } |
| b69ab31 | | | 595 | if (patience === 0) { |
| b69ab31 | | | 596 | assert(false, 'bug: code does not end in time'); |
| b69ab31 | | | 597 | } |
| b69ab31 | | | 598 | } |
| b69ab31 | | | 599 | |
| b69ab31 | | | 600 | remapRevs(revMapOrFunc: Map<Rev, Rev> | ((rev: Rev) => Rev)): [Code, Rev] { |
| b69ab31 | | | 601 | let revMap = new Map<Rev, Rev>(); |
| b69ab31 | | | 602 | if (typeof revMapOrFunc === 'function') { |
| b69ab31 | | | 603 | this.instList.forEach(inst => { |
| b69ab31 | | | 604 | const rev = (inst as RecordOf<{rev?: number}>).rev; |
| b69ab31 | | | 605 | if (rev != null && !revMap.has(rev)) { |
| b69ab31 | | | 606 | revMap.set(rev, revMapOrFunc(rev)); |
| b69ab31 | | | 607 | } |
| b69ab31 | | | 608 | }); |
| b69ab31 | | | 609 | } else { |
| b69ab31 | | | 610 | revMap = revMapOrFunc; |
| b69ab31 | | | 611 | } |
| b69ab31 | | | 612 | |
| b69ab31 | | | 613 | const valueOfString = [...revMap.entries()].toString(); |
| b69ab31 | | | 614 | return this.rewriteInsts((c, _pc) => { |
| b69ab31 | | | 615 | if (c.op === Op.JGE || c.op === Op.JL || c.op === Op.LINE) { |
| b69ab31 | | | 616 | const newRev = revMap.get(c.rev) ?? c.rev; |
| b69ab31 | | | 617 | // TypeScript cannot prove `c` has `rev`. Ideally it can figure out it automatically. |
| b69ab31 | | | 618 | return (c as RecordOf<{rev: number}>).set('rev', newRev) as Inst; |
| b69ab31 | | | 619 | } |
| b69ab31 | | | 620 | return c; |
| b69ab31 | | | 621 | }, valueOfString); |
| b69ab31 | | | 622 | } |
| b69ab31 | | | 623 | |
| b69ab31 | | | 624 | /** |
| b69ab31 | | | 625 | * Rewrite all instructions. Returns [newCode, newMaxRev]. |
| b69ab31 | | | 626 | * `valueOfString` sets a unique string that represents the change for equal tests. |
| b69ab31 | | | 627 | */ |
| b69ab31 | | | 628 | rewriteInsts(func: (inst: Inst, pc: Pc) => Inst, valueOfString?: string): [Code, Rev] { |
| b69ab31 | | | 629 | let newMaxRev = 0; |
| b69ab31 | | | 630 | const newInstList = this.instList |
| b69ab31 | | | 631 | .map((inst, pc) => { |
| b69ab31 | | | 632 | const newInst = func(inst, pc); |
| b69ab31 | | | 633 | const newRev = (newInst as RecordOf<{rev: number}>).rev; |
| b69ab31 | | | 634 | if (newRev != null && newRev > newMaxRev) { |
| b69ab31 | | | 635 | newMaxRev = newRev; |
| b69ab31 | | | 636 | } |
| b69ab31 | | | 637 | return newInst; |
| b69ab31 | | | 638 | }) |
| b69ab31 | | | 639 | .toList(); |
| b69ab31 | | | 640 | const newValueOf = `R${valueOfString ?? func}`; |
| b69ab31 | | | 641 | const newCode = this.newCode(newInstList, newValueOf); |
| b69ab31 | | | 642 | return [newCode, newMaxRev]; |
| b69ab31 | | | 643 | } |
| b69ab31 | | | 644 | |
| b69ab31 | | | 645 | /** |
| b69ab31 | | | 646 | * Drop edits for revs >= the given rev. Returns [newCode, newMaxRev]. |
| b69ab31 | | | 647 | */ |
| b69ab31 | | | 648 | truncate(rev: Rev): [Code, Rev] { |
| b69ab31 | | | 649 | const valueOfString = `TRUNC${rev}`; |
| b69ab31 | | | 650 | return this.rewriteInsts((inst, pc) => { |
| b69ab31 | | | 651 | if (inst.op === Op.JGE || inst.op === Op.LINE) { |
| b69ab31 | | | 652 | if (inst.rev >= rev) { |
| b69ab31 | | | 653 | // NOP. |
| b69ab31 | | | 654 | return J({pc: pc + 1}) as Inst; |
| b69ab31 | | | 655 | } |
| b69ab31 | | | 656 | } else if (inst.op === Op.JL) { |
| b69ab31 | | | 657 | if (inst.rev >= rev) { |
| b69ab31 | | | 658 | // Unconditional jump. |
| b69ab31 | | | 659 | return J({pc: inst.pc}) as Inst; |
| b69ab31 | | | 660 | } |
| b69ab31 | | | 661 | } |
| b69ab31 | | | 662 | return inst; |
| b69ab31 | | | 663 | }, valueOfString); |
| b69ab31 | | | 664 | } |
| b69ab31 | | | 665 | |
| b69ab31 | | | 666 | private newCode(instList: List<Inst>, newValueOf: string): Code { |
| b69ab31 | | | 667 | const newStr = this.__valueOf + '\0' + newValueOf; |
| b69ab31 | | | 668 | // We want bitwise operations. |
| b69ab31 | | | 669 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 670 | const newHash = (this.__hash * 23 + hash(newValueOf)) & 0x7fffffff; |
| b69ab31 | | | 671 | return new Code(instList, newHash, newStr); |
| b69ab31 | | | 672 | } |
| b69ab31 | | | 673 | } |
| b69ab31 | | | 674 | |
| b69ab31 | | | 675 | // Export for testing purpose. |
| b69ab31 | | | 676 | export const executeCache: LRUWithStats = new LRU(1000); |
| b69ab31 | | | 677 | const calculateDepCache: LRUWithStats = new LRU(1000); |
| b69ab31 | | | 678 | const flattenCache: LRUWithStats = new LRU(1000); |
| b69ab31 | | | 679 | const recordTextCache: LRUWithStats = new LRU(1000); |
| b69ab31 | | | 680 | |
| b69ab31 | | | 681 | type LineLogProps = { |
| b69ab31 | | | 682 | /** Core state: instructions. The array index type is `Pc`. */ |
| b69ab31 | | | 683 | code: Code; |
| b69ab31 | | | 684 | /** Maximum rev tracked (inclusive). */ |
| b69ab31 | | | 685 | maxRev: Rev; |
| b69ab31 | | | 686 | }; |
| b69ab31 | | | 687 | |
| b69ab31 | | | 688 | const LineLogRecord = Record<LineLogProps>({ |
| b69ab31 | | | 689 | code: new Code(), |
| b69ab31 | | | 690 | maxRev: 0 as Rev, |
| b69ab31 | | | 691 | }); |
| b69ab31 | | | 692 | type LineLogRecord = RecordOf<LineLogProps>; |
| b69ab31 | | | 693 | |
| b69ab31 | | | 694 | /** |
| b69ab31 | | | 695 | * `LineLog` is a data structure that tracks linear changes to a single text |
| b69ab31 | | | 696 | * file. Conceptually similar to a list of texts like `string[]`, with extra |
| b69ab31 | | | 697 | * features suitable for stack editing: |
| b69ab31 | | | 698 | * - Calculate the "blame" of the text of a given version efficiently. |
| b69ab31 | | | 699 | * - Edit lines or trunks in a past version, and affect future versions. |
| b69ab31 | | | 700 | * - List all lines that ever existed with each line annotated, like |
| b69ab31 | | | 701 | * a unified diff, but for all versions, not just 2 versions. |
| b69ab31 | | | 702 | * |
| b69ab31 | | | 703 | * Internally, `LineLog` is a byte-code interpreter that runs a program to |
| b69ab31 | | | 704 | * emit lines. Changes are done by patching in new byte-codes. There are |
| b69ab31 | | | 705 | * no traditional text patch involved. No operations would cause merge |
| b69ab31 | | | 706 | * conflicts. See https://sapling-scm.com/docs/internals/linelog for more |
| b69ab31 | | | 707 | * details. |
| b69ab31 | | | 708 | * |
| b69ab31 | | | 709 | * This implementation of `LineLog` uses immutable patterns. |
| b69ab31 | | | 710 | * Write operations return new `LineLog`s. |
| b69ab31 | | | 711 | */ |
| b69ab31 | | | 712 | class LineLog extends SelfUpdate<LineLogRecord> { |
| b69ab31 | | | 713 | constructor(props?: {code?: Code; maxRev?: Rev}) { |
| b69ab31 | | | 714 | const record = LineLogRecord(props); |
| b69ab31 | | | 715 | super(record); |
| b69ab31 | | | 716 | } |
| b69ab31 | | | 717 | |
| b69ab31 | | | 718 | get maxRev(): Rev { |
| b69ab31 | | | 719 | return this.inner.maxRev; |
| b69ab31 | | | 720 | } |
| b69ab31 | | | 721 | |
| b69ab31 | | | 722 | get code(): Code { |
| b69ab31 | | | 723 | return this.inner.code; |
| b69ab31 | | | 724 | } |
| b69ab31 | | | 725 | |
| b69ab31 | | | 726 | /** |
| b69ab31 | | | 727 | * Edit chunk. Replace line `a1` (inclusive) to `a2` (exclusive) in rev |
| b69ab31 | | | 728 | * `aRev` with `bLines`. `bLines` are considered introduced by `bRev`. |
| b69ab31 | | | 729 | * If `bLines` is empty, the edit is a deletion. If `a1` equals to `a2`, |
| b69ab31 | | | 730 | * the edit is an insertion. Otherwise, the edit is a modification. |
| b69ab31 | | | 731 | * |
| b69ab31 | | | 732 | * While this function does not cause conflicts or error out, not all |
| b69ab31 | | | 733 | * editings make practical sense. The callsite might want to do some |
| b69ab31 | | | 734 | * extra checks to ensure the edit is meaningful. |
| b69ab31 | | | 735 | * |
| b69ab31 | | | 736 | * `aLinesCache` is optional. If provided, then `editChunk` will skip a |
| b69ab31 | | | 737 | * `checkOutLines` call and modify `aLinesCache` *in place* to reflect |
| b69ab31 | | | 738 | * the edit. It is used by `recordText`. |
| b69ab31 | | | 739 | * |
| b69ab31 | | | 740 | * If `blockShift` is `true`, consider shifting the insertion lines |
| b69ab31 | | | 741 | * to relax dependency for easier reordering. Check the comments |
| b69ab31 | | | 742 | * in this function for details. |
| b69ab31 | | | 743 | */ |
| b69ab31 | | | 744 | editChunk( |
| b69ab31 | | | 745 | aRev: Rev, |
| b69ab31 | | | 746 | a1: LineIdx, |
| b69ab31 | | | 747 | a2: LineIdx, |
| b69ab31 | | | 748 | bRev: Rev, |
| b69ab31 | | | 749 | bLines: string[], |
| b69ab31 | | | 750 | aLinesCache?: LineInfo[], |
| b69ab31 | | | 751 | blockShift = true, |
| b69ab31 | | | 752 | ): LineLog { |
| b69ab31 | | | 753 | const aLinesMutable = aLinesCache != null; |
| b69ab31 | | | 754 | const aLinesInfo: [LineInfo[], true] | [Readonly<LineInfo[]>, false] = aLinesMutable |
| b69ab31 | | | 755 | ? [aLinesCache, true] |
| b69ab31 | | | 756 | : [this.checkOutLines(aRev), false]; |
| b69ab31 | | | 757 | |
| b69ab31 | | | 758 | const bLen = bLines.length; |
| b69ab31 | | | 759 | if (a1 === a2 && bLen > 0 && blockShift) { |
| b69ab31 | | | 760 | // Attempt to shift the insertion chunk so the start of insertion aligns |
| b69ab31 | | | 761 | // with another "start insertion". This might trigger the [OPT1] |
| b69ab31 | | | 762 | // optimization in `code.editChunk`, avoid nested insertions and enable |
| b69ab31 | | | 763 | // more flexible reordering. |
| b69ab31 | | | 764 | // |
| b69ab31 | | | 765 | // For example, we might get "Insert (rev 3)" below that forces a nested |
| b69ab31 | | | 766 | // insertion block. However, if we shift the block and use the |
| b69ab31 | | | 767 | // "Alternative Insert (rev 3)", we can use the [OPT1] optimization. |
| b69ab31 | | | 768 | // |
| b69ab31 | | | 769 | // +----Insert (rev 1) |
| b69ab31 | | | 770 | // | Line: function a () { |
| b69ab31 | | | 771 | // | Line: return 'a'; |
| b69ab31 | | | 772 | // | Line: } |
| b69ab31 | | | 773 | // +---- |
| b69ab31 | | | 774 | // +----Insert (rev 2) |
| b69ab31 | | | 775 | // | ----+ Alternative Insert (rev 3) |
| b69ab31 | | | 776 | // | Line: | |
| b69ab31 | | | 777 | // |+---Insert (rev 3) | |
| b69ab31 | | | 778 | // || Line: function b () { | |
| b69ab31 | | | 779 | // || Line: return 'b'; | |
| b69ab31 | | | 780 | // || Line: } | |
| b69ab31 | | | 781 | // || ----+ |
| b69ab31 | | | 782 | // || Line: |
| b69ab31 | | | 783 | // |+--- |
| b69ab31 | | | 784 | // | Line: function c () { |
| b69ab31 | | | 785 | // | Line: return 'c'; |
| b69ab31 | | | 786 | // | Line: } |
| b69ab31 | | | 787 | // +---- |
| b69ab31 | | | 788 | // |
| b69ab31 | | | 789 | // Block shifting works if the surrounding lines match, see: |
| b69ab31 | | | 790 | // |
| b69ab31 | | | 791 | // A A |
| b69ab31 | | | 792 | // B +-------+ |
| b69ab31 | | | 793 | // +-------+ is equivalent to | B | |
| b69ab31 | | | 794 | // | block | === shift up ==> | block | |
| b69ab31 | | | 795 | // | B | <== shift down === +-------+ |
| b69ab31 | | | 796 | // +-------+ B |
| b69ab31 | | | 797 | // C C |
| b69ab31 | | | 798 | |
| b69ab31 | | | 799 | const aLines: Readonly<LineInfo[]> = aLinesInfo[0]; |
| b69ab31 | | | 800 | const canUseOpt1 = (a: LineIdx): boolean => { |
| b69ab31 | | | 801 | const pc = aLines.at(a)?.pc; |
| b69ab31 | | | 802 | // Check [OPT1] for how this works. |
| b69ab31 | | | 803 | return pc != null && pc > 0 && this.code.get(pc - 1)?.op === Op.JL; |
| b69ab31 | | | 804 | }; |
| b69ab31 | | | 805 | if (!canUseOpt1(a1)) { |
| b69ab31 | | | 806 | const considerShift = (step: 'down' | 'up'): LineLog | undefined => { |
| b69ab31 | | | 807 | let ai = a1; |
| b69ab31 | | | 808 | let lines = [...bLines]; |
| b69ab31 | | | 809 | // Limit overhead. |
| b69ab31 | | | 810 | const threshold = 10; |
| b69ab31 | | | 811 | for (let i = 0; i < threshold; ++i) { |
| b69ab31 | | | 812 | // Out of range? |
| b69ab31 | | | 813 | if (step === 'up' ? ai === 0 : ai === aLines.length - 1) { |
| b69ab31 | | | 814 | return undefined; |
| b69ab31 | | | 815 | } |
| b69ab31 | | | 816 | // Surrounding lines match? |
| b69ab31 | | | 817 | const [aIdx, bIdx] = step === 'up' ? [ai - 1, -1] : [ai, 0]; |
| b69ab31 | | | 818 | const aData = aLines.at(aIdx)?.data; |
| b69ab31 | | | 819 | const bData = lines.at(bIdx); |
| b69ab31 | | | 820 | if (bData !== aData || bData == null) { |
| b69ab31 | | | 821 | return undefined; |
| b69ab31 | | | 822 | } |
| b69ab31 | | | 823 | // Shift. |
| b69ab31 | | | 824 | lines = |
| b69ab31 | | | 825 | step === 'up' ? [bData].concat(lines.slice(0, -1)) : lines.slice(1).concat([bData]); |
| b69ab31 | | | 826 | ai += step === 'up' ? -1 : 1; |
| b69ab31 | | | 827 | // Good enough? |
| b69ab31 | | | 828 | if (canUseOpt1(ai)) { |
| b69ab31 | | | 829 | return this.editChunk(aRev, ai, ai, bRev, lines, aLinesCache, false); |
| b69ab31 | | | 830 | } |
| b69ab31 | | | 831 | } |
| b69ab31 | | | 832 | }; |
| b69ab31 | | | 833 | const maybeShifted = considerShift('up') ?? considerShift('down'); |
| b69ab31 | | | 834 | if (maybeShifted != null) { |
| b69ab31 | | | 835 | return maybeShifted; |
| b69ab31 | | | 836 | } |
| b69ab31 | | | 837 | } |
| b69ab31 | | | 838 | } |
| b69ab31 | | | 839 | const newCode = this.code.editChunk(aRev, a1, a2, bRev, bLines, aLinesInfo); |
| b69ab31 | | | 840 | const newMaxRev = Math.max(bRev, this.maxRev); |
| b69ab31 | | | 841 | return new LineLog({code: newCode, maxRev: newMaxRev}); |
| b69ab31 | | | 842 | } |
| b69ab31 | | | 843 | |
| b69ab31 | | | 844 | /** |
| b69ab31 | | | 845 | * Rewrite `rev` to `mapping[rev] ?? rev`. |
| b69ab31 | | | 846 | * This can be useful for reordering, folding, or insertion. |
| b69ab31 | | | 847 | * |
| b69ab31 | | | 848 | * Note: There are no checks about whether the reordering is |
| b69ab31 | | | 849 | * meaningful or not. The callsite is responsible to perform |
| b69ab31 | | | 850 | * a dependency check and avoid troublesome reorders like |
| b69ab31 | | | 851 | * moving a change to before its dependency. |
| b69ab31 | | | 852 | */ |
| b69ab31 | | | 853 | remapRevs(revMap: Map<Rev, Rev> | ((rev: Rev) => Rev)): LineLog { |
| b69ab31 | | | 854 | const [newCode, newMaxRev] = this.code.remapRevs(revMap); |
| b69ab31 | | | 855 | return new LineLog({code: newCode, maxRev: newMaxRev}); |
| b69ab31 | | | 856 | } |
| b69ab31 | | | 857 | |
| b69ab31 | | | 858 | /** |
| b69ab31 | | | 859 | * Truncate linelog. Drop rev (inclusive) and higher revs. |
| b69ab31 | | | 860 | */ |
| b69ab31 | | | 861 | truncate(rev: Rev): LineLog { |
| b69ab31 | | | 862 | const [newCode, newMaxRev] = this.code.truncate(rev); |
| b69ab31 | | | 863 | return new LineLog({code: newCode, maxRev: newMaxRev}); |
| b69ab31 | | | 864 | } |
| b69ab31 | | | 865 | |
| b69ab31 | | | 866 | /** |
| b69ab31 | | | 867 | * Calculate the dependencies of revisions. |
| b69ab31 | | | 868 | * For example, `{5: [3, 1]}` means rev 5 depends on rev 3 and rev 1. |
| b69ab31 | | | 869 | * |
| b69ab31 | | | 870 | * Based on LineLog, which could be different from traditional textual |
| b69ab31 | | | 871 | * context-line dependencies. LineLog dependency is to prevent |
| b69ab31 | | | 872 | * "malformed cases" [1] when following the dependency to `remapRevs`. |
| b69ab31 | | | 873 | * Practically, LineLog might allow reorder cases that would be |
| b69ab31 | | | 874 | * disallowed by traditional context-line dependencies. See tests |
| b69ab31 | | | 875 | * for examples. |
| b69ab31 | | | 876 | * |
| b69ab31 | | | 877 | * [1]: Malformed cases are when nested blocks (insertions or deletions) |
| b69ab31 | | | 878 | * might be skipped incorrectly. The outer block says "skip" and the |
| b69ab31 | | | 879 | * inner block does not want to "skip" but is still skipped since it |
| b69ab31 | | | 880 | * is skipped altogether with the outer block. See also section 0.4 |
| b69ab31 | | | 881 | * and 0.5 in D3628440. |
| b69ab31 | | | 882 | */ |
| b69ab31 | | | 883 | public calculateDepMap = cachedMethod(this.calculateDepMapImpl, {cache: calculateDepCache}); |
| b69ab31 | | | 884 | |
| b69ab31 | | | 885 | private calculateDepMapImpl(): Readonly<Map<Rev, Set<Rev>>> { |
| b69ab31 | | | 886 | // With the insertion and deletion stacks (see explanation in |
| b69ab31 | | | 887 | // visitWithInsDelStacks), when we see a new insertion block, or deletion |
| b69ab31 | | | 888 | // block, we add two dependencies: |
| b69ab31 | | | 889 | // - The inner rev depends on the outer insertion rev. |
| b69ab31 | | | 890 | // - The outer deletion rev (if present) depends on the inner rev. |
| b69ab31 | | | 891 | // |
| b69ab31 | | | 892 | // Let's look at how this is done at the instruction level. |
| b69ab31 | | | 893 | // the instructions generated by editChunk: |
| b69ab31 | | | 894 | // |
| b69ab31 | | | 895 | // a2Pc: ... |
| b69ab31 | | | 896 | // ... |
| b69ab31 | | | 897 | // start: JL brev b2Pc |
| b69ab31 | | | 898 | // ... |
| b69ab31 | | | 899 | // b2Pc: JGE brev a2Pc |
| b69ab31 | | | 900 | // : <a1 Inst> |
| b69ab31 | | | 901 | // end: J a1Pc+1 |
| b69ab31 | | | 902 | // |
| b69ab31 | | | 903 | // JL is used for insertion, JGE is used for deletion. We then use them to |
| b69ab31 | | | 904 | // manipulate the insStack and delStack: |
| b69ab31 | | | 905 | // |
| b69ab31 | | | 906 | // insStack: |
| b69ab31 | | | 907 | // |
| b69ab31 | | | 908 | // - On "start: JL brev b2Pc": |
| b69ab31 | | | 909 | // Do not follow the JL jump. (by visitWithInsDelStacks) |
| b69ab31 | | | 910 | // Mark brev as dependent on the outer insertion. |
| b69ab31 | | | 911 | // Mark the outer deletion as dependent on this brev. |
| b69ab31 | | | 912 | // Push {rev, b2Pc} to insStack. (by visitWithInsDelStacks) |
| b69ab31 | | | 913 | // - When pc is b2Pc, pop insStack. (by visitWithInsDelStacks) |
| b69ab31 | | | 914 | // |
| b69ab31 | | | 915 | // delStack: |
| b69ab31 | | | 916 | // |
| b69ab31 | | | 917 | // - On "b2Pc: JGE brev a2Pc": |
| b69ab31 | | | 918 | // Do not follow the JGE jump. (by visitWithInsDelStacks) |
| b69ab31 | | | 919 | // Mark brev as dependent on the outer insertion. |
| b69ab31 | | | 920 | // Mark the outer deletion as dependent on this brev. |
| b69ab31 | | | 921 | // Push {rev, a2Pc} to delStack. (by visitWithInsDelStacks) |
| b69ab31 | | | 922 | // - When pc is a2Pc, pop delStack. (by visitWithInsDelStacks) |
| b69ab31 | | | 923 | const depMap = new Map<Rev, Set<Rev>>(); |
| b69ab31 | | | 924 | const addDep = (child: Rev, parent: Rev) => { |
| b69ab31 | | | 925 | if (child > parent) { |
| b69ab31 | | | 926 | if (!depMap.has(child)) { |
| b69ab31 | | | 927 | depMap.set(child, new Set()); |
| b69ab31 | | | 928 | } |
| b69ab31 | | | 929 | depMap.get(child)?.add(parent); |
| b69ab31 | | | 930 | } |
| b69ab31 | | | 931 | }; |
| b69ab31 | | | 932 | this.code.visitWithInsDelStacks((insStack, delStack) => { |
| b69ab31 | | | 933 | const markDep = (rev: Rev) => { |
| b69ab31 | | | 934 | const ins = insStack.at(-1); |
| b69ab31 | | | 935 | if (ins !== undefined) { |
| b69ab31 | | | 936 | addDep(rev, ins.rev); |
| b69ab31 | | | 937 | } |
| b69ab31 | | | 938 | const del = delStack.at(-1); |
| b69ab31 | | | 939 | if (del !== undefined) { |
| b69ab31 | | | 940 | addDep(del.rev, rev); |
| b69ab31 | | | 941 | } |
| b69ab31 | | | 942 | }; |
| b69ab31 | | | 943 | return { |
| b69ab31 | | | 944 | onConditionalJump: inst => markDep(inst.rev), |
| b69ab31 | | | 945 | }; |
| b69ab31 | | | 946 | }); |
| b69ab31 | | | 947 | return depMap; |
| b69ab31 | | | 948 | } |
| b69ab31 | | | 949 | |
| b69ab31 | | | 950 | /** |
| b69ab31 | | | 951 | * Interpret the bytecodes with the given revision range. |
| b69ab31 | | | 952 | * Used by `checkOut`. |
| b69ab31 | | | 953 | */ |
| b69ab31 | | | 954 | public execute = cachedMethod(this.executeImpl, {cache: executeCache}); |
| b69ab31 | | | 955 | |
| b69ab31 | | | 956 | private executeImpl( |
| b69ab31 | | | 957 | startRev: Rev, |
| b69ab31 | | | 958 | endRev: Rev = startRev, |
| b69ab31 | | | 959 | present?: {[pc: number]: boolean}, |
| b69ab31 | | | 960 | ): Readonly<LineInfo[]> { |
| b69ab31 | | | 961 | const rev = endRev; |
| b69ab31 | | | 962 | const lines: LineInfo[] = []; |
| b69ab31 | | | 963 | let pc = 0; |
| b69ab31 | | | 964 | let patience = this.code.getSize() * 2; |
| b69ab31 | | | 965 | const deleted = present == null ? () => false : (pc: Pc) => !present[pc]; |
| b69ab31 | | | 966 | while (patience > 0) { |
| b69ab31 | | | 967 | const code = nullthrows(this.code.get(pc)); |
| b69ab31 | | | 968 | switch (code.op) { |
| b69ab31 | | | 969 | case Op.END: |
| b69ab31 | | | 970 | lines.push({data: '', rev: 0, pc, deleted: deleted(pc)}); |
| b69ab31 | | | 971 | patience = -1; |
| b69ab31 | | | 972 | break; |
| b69ab31 | | | 973 | case Op.LINE: |
| b69ab31 | | | 974 | lines.push({data: code.data, rev: code.rev, pc, deleted: deleted(pc)}); |
| b69ab31 | | | 975 | pc += 1; |
| b69ab31 | | | 976 | break; |
| b69ab31 | | | 977 | case Op.J: |
| b69ab31 | | | 978 | pc = code.pc; |
| b69ab31 | | | 979 | break; |
| b69ab31 | | | 980 | case Op.JGE: |
| b69ab31 | | | 981 | if (startRev >= code.rev) { |
| b69ab31 | | | 982 | pc = code.pc; |
| b69ab31 | | | 983 | } else { |
| b69ab31 | | | 984 | pc += 1; |
| b69ab31 | | | 985 | } |
| b69ab31 | | | 986 | break; |
| b69ab31 | | | 987 | case Op.JL: |
| b69ab31 | | | 988 | if (rev < code.rev) { |
| b69ab31 | | | 989 | pc = code.pc; |
| b69ab31 | | | 990 | } else { |
| b69ab31 | | | 991 | pc += 1; |
| b69ab31 | | | 992 | } |
| b69ab31 | | | 993 | break; |
| b69ab31 | | | 994 | default: |
| b69ab31 | | | 995 | assert(false, 'bug: unknown code'); |
| b69ab31 | | | 996 | } |
| b69ab31 | | | 997 | patience -= 1; |
| b69ab31 | | | 998 | } |
| b69ab31 | | | 999 | if (patience === 0) { |
| b69ab31 | | | 1000 | assert(false, 'bug: code does not end in time'); |
| b69ab31 | | | 1001 | } |
| b69ab31 | | | 1002 | return lines; |
| b69ab31 | | | 1003 | } |
| b69ab31 | | | 1004 | |
| b69ab31 | | | 1005 | /** |
| b69ab31 | | | 1006 | * Flatten lines. Each returned line is associated with a set |
| b69ab31 | | | 1007 | * of `Rev`s, meaning that line is present in those `Rev`s. |
| b69ab31 | | | 1008 | * |
| b69ab31 | | | 1009 | * The returned lines can be useful to figure out file contents |
| b69ab31 | | | 1010 | * after reordering, folding commits. It can also provide a view |
| b69ab31 | | | 1011 | * similar to `absorb -e FILE` to edit all versions of a file in |
| b69ab31 | | | 1012 | * a single view. |
| b69ab31 | | | 1013 | */ |
| b69ab31 | | | 1014 | public flatten = cachedMethod(this.flattenImpl, {cache: flattenCache}); |
| b69ab31 | | | 1015 | |
| b69ab31 | | | 1016 | private flattenImpl(): List<FlattenLine> { |
| b69ab31 | | | 1017 | const result: FlattenLine[] = []; |
| b69ab31 | | | 1018 | |
| b69ab31 | | | 1019 | // See the comments in calculateDepMap for what the stacks mean. |
| b69ab31 | | | 1020 | // |
| b69ab31 | | | 1021 | // The flatten algorithm works as follows: |
| b69ab31 | | | 1022 | // - For each line, we got an insRev (insStack.at(-1).rev), and a |
| b69ab31 | | | 1023 | // delRev (delStack.at(-1)?.rev ?? maxRev + 1), meaning the rev |
| b69ab31 | | | 1024 | // attached to the innermost insertion or deletion blocks, |
| b69ab31 | | | 1025 | // respectively. |
| b69ab31 | | | 1026 | // - That line is then present in insRev .. delRev (exclusive) revs. |
| b69ab31 | | | 1027 | // |
| b69ab31 | | | 1028 | // This works because: |
| b69ab31 | | | 1029 | // - The blocks are nested in order: |
| b69ab31 | | | 1030 | // - For nested insertions, the nested one must have a larger rev, and |
| b69ab31 | | | 1031 | // lines inside the nested block are only present starting from the |
| b69ab31 | | | 1032 | // larger rev. |
| b69ab31 | | | 1033 | // - For nested deletions, the nested one must have a smaller rev, and |
| b69ab31 | | | 1034 | // lines inside the nested block are considered as deleted by the |
| b69ab31 | | | 1035 | // smaller rev. |
| b69ab31 | | | 1036 | // - For interleaved insertion and deletions, insertion rev and deletion |
| b69ab31 | | | 1037 | // rev are tracked separately so their calculations are independent |
| b69ab31 | | | 1038 | // from each other. |
| b69ab31 | | | 1039 | // - Linelog tracks linear history, so (insRev, delRev) can be converted to |
| b69ab31 | | | 1040 | // a Set<Rev>. |
| b69ab31 | | | 1041 | this.code.visitWithInsDelStacks((insStack, delStack) => { |
| b69ab31 | | | 1042 | const maxDelRev = this.maxRev + 1; |
| b69ab31 | | | 1043 | const getCurrentRevs = (): ImSet<Rev> => { |
| b69ab31 | | | 1044 | const insRev = insStack.at(-1)?.rev ?? 0; |
| b69ab31 | | | 1045 | const delRev = delStack.at(-1)?.rev ?? maxDelRev; |
| b69ab31 | | | 1046 | return revRangeToSet(insRev, delRev); |
| b69ab31 | | | 1047 | }; |
| b69ab31 | | | 1048 | let currentRevs = getCurrentRevs(); |
| b69ab31 | | | 1049 | return { |
| b69ab31 | | | 1050 | onStackPush: () => { |
| b69ab31 | | | 1051 | currentRevs = getCurrentRevs(); |
| b69ab31 | | | 1052 | }, |
| b69ab31 | | | 1053 | onStackPop: () => { |
| b69ab31 | | | 1054 | currentRevs = getCurrentRevs(); |
| b69ab31 | | | 1055 | }, |
| b69ab31 | | | 1056 | onLine: ({data}) => { |
| b69ab31 | | | 1057 | result.push(FlattenLine({data, revs: currentRevs})); |
| b69ab31 | | | 1058 | }, |
| b69ab31 | | | 1059 | }; |
| b69ab31 | | | 1060 | }); |
| b69ab31 | | | 1061 | return List(result); |
| b69ab31 | | | 1062 | } |
| b69ab31 | | | 1063 | |
| b69ab31 | | | 1064 | /** |
| b69ab31 | | | 1065 | * Checkout the lines of the given revision `rev`. |
| b69ab31 | | | 1066 | * |
| b69ab31 | | | 1067 | * If `start` is not `null`, checkout a revision range. For example, |
| b69ab31 | | | 1068 | * if `start` is 0, and `rev` is `this.maxRev`, `this.lines` will |
| b69ab31 | | | 1069 | * include all lines ever existed in all revisions. |
| b69ab31 | | | 1070 | * |
| b69ab31 | | | 1071 | * @returns Content of the specified revision. |
| b69ab31 | | | 1072 | */ |
| b69ab31 | | | 1073 | public checkOutLines(rev: Rev, start: Rev | null = null): Readonly<LineInfo[]> { |
| b69ab31 | | | 1074 | // eslint-disable-next-line no-param-reassign |
| b69ab31 | | | 1075 | rev = Math.min(rev, this.maxRev); |
| b69ab31 | | | 1076 | let lines = this.execute(rev); |
| b69ab31 | | | 1077 | if (start !== null) { |
| b69ab31 | | | 1078 | // Checkout a range, including deleted revs. |
| b69ab31 | | | 1079 | const present: {[key: number]: boolean} = {}; |
| b69ab31 | | | 1080 | lines.forEach(l => { |
| b69ab31 | | | 1081 | present[l.pc] = true; |
| b69ab31 | | | 1082 | }); |
| b69ab31 | | | 1083 | |
| b69ab31 | | | 1084 | // Go through all lines again. But do not skip chunks. |
| b69ab31 | | | 1085 | lines = this.execute(start, rev, present); |
| b69ab31 | | | 1086 | } |
| b69ab31 | | | 1087 | return lines; |
| b69ab31 | | | 1088 | } |
| b69ab31 | | | 1089 | |
| b69ab31 | | | 1090 | /** Checkout the content of the given rev. */ |
| b69ab31 | | | 1091 | public checkOut(rev: Rev): string { |
| b69ab31 | | | 1092 | const lines = this.checkOutLines(rev); |
| b69ab31 | | | 1093 | const content = lines.map(l => l.data).join(''); |
| b69ab31 | | | 1094 | return content; |
| b69ab31 | | | 1095 | } |
| b69ab31 | | | 1096 | |
| b69ab31 | | | 1097 | /** |
| b69ab31 | | | 1098 | * Edit LineLog to match the content of `text`. |
| b69ab31 | | | 1099 | * This might affect `rev`s that are >= `rev` in the stack. |
| b69ab31 | | | 1100 | * Previous revisions won't be affected. |
| b69ab31 | | | 1101 | * |
| b69ab31 | | | 1102 | * @param text Content to match. |
| b69ab31 | | | 1103 | * @param rev Revision to to edit (in-place). If not set, append a new revision. |
| b69ab31 | | | 1104 | * @returns A new `LineLog` with the change. |
| b69ab31 | | | 1105 | */ |
| b69ab31 | | | 1106 | public recordText = cachedMethod(this.recordTextImpl, {cache: recordTextCache}); |
| b69ab31 | | | 1107 | |
| b69ab31 | | | 1108 | private recordTextImpl(text: string, rev: Rev | null = null): LineLog { |
| b69ab31 | | | 1109 | // rev to edit from, and rev to match 'text'. |
| b69ab31 | | | 1110 | const [aRev, bRev] = rev != null ? [rev, rev] : [this.maxRev, this.maxRev + 1]; |
| b69ab31 | | | 1111 | const b = text; |
| b69ab31 | | | 1112 | |
| b69ab31 | | | 1113 | const aLineInfos = [...this.checkOutLines(aRev)]; |
| b69ab31 | | | 1114 | const bLines = splitLines(b); |
| b69ab31 | | | 1115 | const aLines = aLineInfos.map(l => l.data); |
| b69ab31 | | | 1116 | aLines.pop(); // Drop the last END empty line. |
| b69ab31 | | | 1117 | const blocks = diffLines(aLines, bLines); |
| b69ab31 | | | 1118 | // eslint-disable-next-line @typescript-eslint/no-this-alias |
| b69ab31 | | | 1119 | let log: LineLog = this; |
| b69ab31 | | | 1120 | |
| b69ab31 | | | 1121 | blocks.reverse().forEach(([a1, a2, b1, b2]) => { |
| b69ab31 | | | 1122 | log = log.editChunk(aRev, a1, a2, bRev, bLines.slice(b1, b2), aLineInfos); |
| b69ab31 | | | 1123 | }); |
| b69ab31 | | | 1124 | |
| b69ab31 | | | 1125 | // This is needed in case editChunk is not called (no difference). |
| b69ab31 | | | 1126 | const newMaxRev = Math.max(bRev, log.maxRev); |
| b69ab31 | | | 1127 | |
| b69ab31 | | | 1128 | // Populate cache for checking out bRev. |
| b69ab31 | | | 1129 | const newLog = new LineLog({code: log.code, maxRev: newMaxRev}); |
| b69ab31 | | | 1130 | executeCache.set(List([newLog, bRev]), aLineInfos); |
| b69ab31 | | | 1131 | |
| b69ab31 | | | 1132 | return newLog; |
| b69ab31 | | | 1133 | } |
| b69ab31 | | | 1134 | } |
| b69ab31 | | | 1135 | |
| b69ab31 | | | 1136 | /** Turn (3, 6) to Set([3, 4, 5]). */ |
| b69ab31 | | | 1137 | const revRangeToSet = cached( |
| b69ab31 | | | 1138 | (startRev, endRev: Rev): ImSet<Rev> => { |
| b69ab31 | | | 1139 | const result: Rev[] = []; |
| b69ab31 | | | 1140 | for (let rev = startRev; rev < endRev; rev++) { |
| b69ab31 | | | 1141 | result.push(rev); |
| b69ab31 | | | 1142 | } |
| b69ab31 | | | 1143 | return ImSet(result); |
| b69ab31 | | | 1144 | }, |
| b69ab31 | | | 1145 | {cacheSize: 1000}, |
| b69ab31 | | | 1146 | ); |
| b69ab31 | | | 1147 | |
| b69ab31 | | | 1148 | function describeInst(inst: Inst): string { |
| b69ab31 | | | 1149 | switch (inst.op) { |
| b69ab31 | | | 1150 | case Op.J: |
| b69ab31 | | | 1151 | return `J ${inst.pc}`; |
| b69ab31 | | | 1152 | case Op.JGE: |
| b69ab31 | | | 1153 | return `JGE ${inst.rev} ${inst.pc}`; |
| b69ab31 | | | 1154 | case Op.JL: |
| b69ab31 | | | 1155 | return `JL ${inst.rev} ${inst.pc}`; |
| b69ab31 | | | 1156 | case Op.LINE: |
| b69ab31 | | | 1157 | return `LINE ${inst.rev} ${JSON.stringify(inst.data.trimEnd())}`; |
| b69ab31 | | | 1158 | case Op.END: |
| b69ab31 | | | 1159 | return 'END'; |
| b69ab31 | | | 1160 | } |
| b69ab31 | | | 1161 | } |
| b69ab31 | | | 1162 | |
| b69ab31 | | | 1163 | export {FlattenLine, LineLog}; |
| b69ab31 | | | 1164 | export type {LineIdx, LineInfo, Rev}; |