| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * 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 | import type {RecordOf} from 'immutable'; |
| b69ab31 | | | 9 | import type {LineInfo} from '../linelog'; |
| b69ab31 | | | 10 | import type {FileStackIndex} from './commitStackState'; |
| b69ab31 | | | 11 | import type {FileRev, FileStackState} from './fileStackState'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import {Map as ImMap, List, Record} from 'immutable'; |
| b69ab31 | | | 14 | import {diffLines, splitLines} from 'shared/diff'; |
| b69ab31 | | | 15 | import {dedup, nullthrows} from 'shared/utils'; |
| b69ab31 | | | 16 | import {t} from '../i18n'; |
| b69ab31 | | | 17 | import {assert} from '../utils'; |
| b69ab31 | | | 18 | import {max, prev} from './revMath'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | /** A diff chunk analyzed by `analyseFileStack`. */ |
| b69ab31 | | | 21 | export type AbsorbEditProps = { |
| b69ab31 | | | 22 | /** The start line of the old content (start from 0, inclusive). */ |
| b69ab31 | | | 23 | oldStart: number; |
| b69ab31 | | | 24 | /** The end line of the old content (start from 0, exclusive). */ |
| b69ab31 | | | 25 | oldEnd: number; |
| b69ab31 | | | 26 | /** |
| b69ab31 | | | 27 | * The old content to be replaced by newLines. |
| b69ab31 | | | 28 | * If you know the full content of the old file as `allLines`, you |
| b69ab31 | | | 29 | * can also use `allLines.slice(oldStart, oldEnd)` to get this. |
| b69ab31 | | | 30 | */ |
| b69ab31 | | | 31 | oldLines: List<string>; |
| b69ab31 | | | 32 | /** The start line of the new content (start from 0, inclusive). */ |
| b69ab31 | | | 33 | newStart: number; |
| b69ab31 | | | 34 | /** The end line of the new content (start from 0, exclusive). */ |
| b69ab31 | | | 35 | newEnd: number; |
| b69ab31 | | | 36 | /** The new content to replace oldLines. */ |
| b69ab31 | | | 37 | newLines: List<string>; |
| b69ab31 | | | 38 | /** |
| b69ab31 | | | 39 | * Which rev introduces the "old" chunk. |
| b69ab31 | | | 40 | * The following revs are expected to contain this chunk too. |
| b69ab31 | | | 41 | * This is usually the "blame" rev in the stack. |
| b69ab31 | | | 42 | */ |
| b69ab31 | | | 43 | introductionRev: FileRev; |
| b69ab31 | | | 44 | /** |
| b69ab31 | | | 45 | * File revision (starts from 0) that the diff chunk is currently |
| b69ab31 | | | 46 | * selected to apply to. `null`: no selectioin. |
| b69ab31 | | | 47 | * Initially, this is the "suggested" rev to absorb to. Later, |
| b69ab31 | | | 48 | * the user can change this to a different rev. |
| b69ab31 | | | 49 | * Must be >= introductionRev. |
| b69ab31 | | | 50 | */ |
| b69ab31 | | | 51 | selectedRev: FileRev | null; |
| b69ab31 | | | 52 | /** The "AbsorbEditId" associated with this diff chunk. */ |
| b69ab31 | | | 53 | absorbEditId: AbsorbEditId; |
| b69ab31 | | | 54 | /** The file stack index (in commitState) associated with this diff chunk. */ |
| b69ab31 | | | 55 | fileStackIndex?: FileStackIndex; |
| b69ab31 | | | 56 | }; |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | /** |
| b69ab31 | | | 59 | * Represents an absorb edit from the wdir to the stack. |
| b69ab31 | | | 60 | * |
| b69ab31 | | | 61 | * This looks like a diff chunk, with extra info like "blame" (introductionRev) |
| b69ab31 | | | 62 | * and "amend -to" (selectedRev). Note this is not 1:1 mapping to diff chunks, |
| b69ab31 | | | 63 | * since one diff chunk might be split into multiple `AbsorbEdit`s if they need |
| b69ab31 | | | 64 | * to be absorbed to different commits. |
| b69ab31 | | | 65 | */ |
| b69ab31 | | | 66 | export const AbsorbEdit = Record<AbsorbEditProps>({ |
| b69ab31 | | | 67 | oldStart: 0, |
| b69ab31 | | | 68 | oldEnd: 0, |
| b69ab31 | | | 69 | oldLines: List(), |
| b69ab31 | | | 70 | newStart: 0, |
| b69ab31 | | | 71 | newEnd: 0, |
| b69ab31 | | | 72 | newLines: List(), |
| b69ab31 | | | 73 | introductionRev: 0 as FileRev, |
| b69ab31 | | | 74 | selectedRev: null, |
| b69ab31 | | | 75 | absorbEditId: 0, |
| b69ab31 | | | 76 | fileStackIndex: undefined, |
| b69ab31 | | | 77 | }); |
| b69ab31 | | | 78 | export type AbsorbEdit = RecordOf<AbsorbEditProps>; |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | /** |
| b69ab31 | | | 81 | * Identifier of an `AbsorbEdit` in a file stack. |
| b69ab31 | | | 82 | */ |
| b69ab31 | | | 83 | export type AbsorbEditId = number; |
| b69ab31 | | | 84 | |
| b69ab31 | | | 85 | /** |
| b69ab31 | | | 86 | * Maximum `AbsorbEditId` (exclusive). Must be an exponent of 2. |
| b69ab31 | | | 87 | * |
| b69ab31 | | | 88 | * Practically this shares the 52 bits (defined by IEEE 754) with the integer |
| b69ab31 | | | 89 | * part of the `Rev`. |
| b69ab31 | | | 90 | */ |
| b69ab31 | | | 91 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 92 | const MAX_ABSORB_EDIT_ID = 1 << 20; |
| b69ab31 | | | 93 | const ABSORB_EDIT_ID_FRACTIONAL_UNIT = 1 / MAX_ABSORB_EDIT_ID; |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | /** Extract the "AbsorbEditId" from a linelog Rev */ |
| b69ab31 | | | 96 | export function extractRevAbsorbId(rev: FileRev): [FileRev, AbsorbEditId] { |
| b69ab31 | | | 97 | const fractional = rev % 1; |
| b69ab31 | | | 98 | const integerRev = rev - fractional; |
| b69ab31 | | | 99 | const absorbEditId = fractional / ABSORB_EDIT_ID_FRACTIONAL_UNIT - 1; |
| b69ab31 | | | 100 | assert( |
| b69ab31 | | | 101 | Number.isInteger(absorbEditId) && absorbEditId >= 0, |
| b69ab31 | | | 102 | `${rev} does not contain valid AbsorbEditId`, |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | return [integerRev as FileRev, absorbEditId]; |
| b69ab31 | | | 105 | } |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | /** Embed an absorbEditId into a Rev */ |
| b69ab31 | | | 108 | export function embedAbsorbId(rev: FileRev, absorbEditId: AbsorbEditId): FileRev { |
| b69ab31 | | | 109 | assert(Number.isInteger(rev), `${rev} already has an absorbEditId embedded`); |
| b69ab31 | | | 110 | assert( |
| b69ab31 | | | 111 | absorbEditId < MAX_ABSORB_EDIT_ID - 1, |
| b69ab31 | | | 112 | t( |
| b69ab31 | | | 113 | `Number of absorb diff chunks exceeds maximum limit ($count). Please retry with only a subset of the changes.`, |
| b69ab31 | | | 114 | {replace: {$count: (absorbEditId + 1).toString()}}, |
| b69ab31 | | | 115 | ), |
| b69ab31 | | | 116 | ); |
| b69ab31 | | | 117 | return (rev + ABSORB_EDIT_ID_FRACTIONAL_UNIT * (absorbEditId + 1)) as FileRev; |
| b69ab31 | | | 118 | } |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | /** |
| b69ab31 | | | 121 | * Returns a rev with all absorb edits for this rev included. |
| b69ab31 | | | 122 | * For example, `revWithAbsorb(2)` might return something like `2.999`. |
| b69ab31 | | | 123 | * */ |
| b69ab31 | | | 124 | export function revWithAbsorb(rev: FileRev): FileRev { |
| b69ab31 | | | 125 | return (Math.floor(rev) + 1 - ABSORB_EDIT_ID_FRACTIONAL_UNIT) as FileRev; |
| b69ab31 | | | 126 | } |
| b69ab31 | | | 127 | |
| b69ab31 | | | 128 | /** |
| b69ab31 | | | 129 | * Calculate absorb edits for a stack. |
| b69ab31 | | | 130 | * |
| b69ab31 | | | 131 | * The stack top is treated as `wdir()` to be absorbed to the rest of the |
| b69ab31 | | | 132 | * stack. The stack bottom is treated as imutable `public()`. |
| b69ab31 | | | 133 | * |
| b69ab31 | | | 134 | * All edits in `wdir()` will be broken down and labeled with `AbsorbEditId`s. |
| b69ab31 | | | 135 | * If an edit with `id: AbsorbEditId` has a default absorb destination |
| b69ab31 | | | 136 | * `x: Rev`, then this edit will be inserted in linelog as rev |
| b69ab31 | | | 137 | * `embedAbsorbId(x, id)`, and can be checked out via |
| b69ab31 | | | 138 | * `linelog.checkOut(revWithAbsorb(x))`. |
| b69ab31 | | | 139 | * |
| b69ab31 | | | 140 | * If an edit has no default destination, for example, the surrounding lines |
| b69ab31 | | | 141 | * belong to public commit (rev 0), the edit will be left in the `wdir()`, |
| b69ab31 | | | 142 | * and can be checked out using `revWithAbsorb(wdirRev)`, where `wdirRev` is |
| b69ab31 | | | 143 | * the max integer rev in the linelog. |
| b69ab31 | | | 144 | * |
| b69ab31 | | | 145 | * Returns `FileStackState` with absorb edits embedded in the linelog, along |
| b69ab31 | | | 146 | * with a mapping from the `AbsorbEditId` to the diff chunk. |
| b69ab31 | | | 147 | */ |
| b69ab31 | | | 148 | export function calculateAbsorbEditsForFileStack( |
| b69ab31 | | | 149 | stack: FileStackState, |
| b69ab31 | | | 150 | options?: {fileStackIndex?: FileStackIndex}, |
| b69ab31 | | | 151 | ): [FileStackState, ImMap<AbsorbEditId, AbsorbEdit>] { |
| b69ab31 | | | 152 | // rev 0 (public), 1, 2, ..., wdirRev-1 (stack top to absorb), wdirRev (wdir virtual rev) |
| b69ab31 | | | 153 | const wdirRev = prev(stack.revLength); |
| b69ab31 | | | 154 | assert( |
| b69ab31 | | | 155 | wdirRev >= 1, |
| b69ab31 | | | 156 | 'calculateAbsorbEditsForFileStack requires at least one wdir(), one public()', |
| b69ab31 | | | 157 | ); |
| b69ab31 | | | 158 | const fileStackIndex = options?.fileStackIndex; |
| b69ab31 | | | 159 | const diffChunks = analyseFileStackWithWdirAtTop(stack, {wdirRev, fileStackIndex}); |
| b69ab31 | | | 160 | // Drop wdirRev, then re-insert the chunks. |
| b69ab31 | | | 161 | let newStack = stack.truncate(wdirRev); |
| b69ab31 | | | 162 | let absorbIdToDiffChunk = ImMap<AbsorbEditId, AbsorbEdit>(); |
| b69ab31 | | | 163 | const diffChunksWithAbsorbId = diffChunks.map(chunk => { |
| b69ab31 | | | 164 | absorbIdToDiffChunk = absorbIdToDiffChunk.set(chunk.absorbEditId, chunk); |
| b69ab31 | | | 165 | return chunk; |
| b69ab31 | | | 166 | }); |
| b69ab31 | | | 167 | // Re-insert the chunks with the absorbId. |
| b69ab31 | | | 168 | newStack = applyFileStackEditsWithAbsorbId(newStack, diffChunksWithAbsorbId); |
| b69ab31 | | | 169 | return [newStack, absorbIdToDiffChunk]; |
| b69ab31 | | | 170 | } |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | /** |
| b69ab31 | | | 173 | * Similar to `analyseFileStack`, but the stack contains the "wdir()" at the top: |
| b69ab31 | | | 174 | * The stack revisions look like: `[0:public] [1] [2] ... [stackTop] [wdir]`. |
| b69ab31 | | | 175 | */ |
| b69ab31 | | | 176 | export function analyseFileStackWithWdirAtTop( |
| b69ab31 | | | 177 | stack: FileStackState, |
| b69ab31 | | | 178 | options?: {wdirRev?: FileRev; fileStackIndex?: FileStackIndex}, |
| b69ab31 | | | 179 | ): List<AbsorbEdit> { |
| b69ab31 | | | 180 | const wdirRev = options?.wdirRev ?? prev(stack.revLength); |
| b69ab31 | | | 181 | const stackTopRev = prev(wdirRev); |
| b69ab31 | | | 182 | assert(stackTopRev >= 0, 'stackTopRev must be positive'); |
| b69ab31 | | | 183 | const newText = stack.getRev(wdirRev); |
| b69ab31 | | | 184 | let edits = analyseFileStack(stack, newText, stackTopRev); |
| b69ab31 | | | 185 | const fileStackIndex = options?.fileStackIndex; |
| b69ab31 | | | 186 | if (fileStackIndex != null) { |
| b69ab31 | | | 187 | edits = edits.map(edit => edit.set('fileStackIndex', fileStackIndex)); |
| b69ab31 | | | 188 | } |
| b69ab31 | | | 189 | return edits; |
| b69ab31 | | | 190 | } |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | /** |
| b69ab31 | | | 193 | * Given a stack and the latest changes (usually at the stack top), |
| b69ab31 | | | 194 | * calculate the diff chunks and the revs that they might be absorbed to. |
| b69ab31 | | | 195 | * The rev 0 of the file stack should come from a "public" (immutable) commit. |
| b69ab31 | | | 196 | */ |
| b69ab31 | | | 197 | export function analyseFileStack( |
| b69ab31 | | | 198 | stack: FileStackState, |
| b69ab31 | | | 199 | newText: string, |
| b69ab31 | | | 200 | stackTopRev?: FileRev, |
| b69ab31 | | | 201 | ): List<AbsorbEdit> { |
| b69ab31 | | | 202 | assert(stack.revLength > 0, 'stack should not be empty'); |
| b69ab31 | | | 203 | const linelog = stack.convertToLineLog(); |
| b69ab31 | | | 204 | const oldRev = stackTopRev ?? prev(stack.revLength); |
| b69ab31 | | | 205 | const oldText = stack.getRev(oldRev); |
| b69ab31 | | | 206 | const oldLines = splitLines(oldText); |
| b69ab31 | | | 207 | // The `LineInfo` contains "blame" information. |
| b69ab31 | | | 208 | const oldLineInfos = linelog.checkOutLines(oldRev); |
| b69ab31 | | | 209 | const newLines = splitLines(newText); |
| b69ab31 | | | 210 | const result: Array<AbsorbEdit> = []; |
| b69ab31 | | | 211 | let nextAbsorbId = 0; |
| b69ab31 | | | 212 | const allocateAbsorbId = () => { |
| b69ab31 | | | 213 | const id = nextAbsorbId; |
| b69ab31 | | | 214 | nextAbsorbId += 1; |
| b69ab31 | | | 215 | return id; |
| b69ab31 | | | 216 | }; |
| b69ab31 | | | 217 | diffLines(oldLines, newLines).forEach(([a1, a2, b1, b2]) => { |
| b69ab31 | | | 218 | // a1, a2: line numbers in the `oldRev`. |
| b69ab31 | | | 219 | // b1, b2: line numbers in `newText`. |
| b69ab31 | | | 220 | // See also [`_analysediffchunk`](https://github.com/facebook/sapling/blob/6f29531e83daa62d9bd3bc58b712755d34f41493/eden/scm/sapling/ext/absorb/__init__.py#L346) |
| b69ab31 | | | 221 | let involvedLineInfos = oldLineInfos.slice(a1, a2); |
| b69ab31 | | | 222 | if (involvedLineInfos.length === 0 && oldLineInfos.length > 0) { |
| b69ab31 | | | 223 | // This is an insertion. Check the surrounding lines, excluding lines from the public commit. |
| b69ab31 | | | 224 | const nearbyLineNumbers = dedup([a2, Math.max(0, a1 - 1)]); |
| b69ab31 | | | 225 | involvedLineInfos = nearbyLineNumbers.map(i => oldLineInfos[i]); |
| b69ab31 | | | 226 | } |
| b69ab31 | | | 227 | // Check the revs. Skip public commits. The Python implementation only skips public |
| b69ab31 | | | 228 | // for insertions. Here we aggressively skip public lines for modification and deletion too. |
| b69ab31 | | | 229 | const involvedRevs = dedup( |
| b69ab31 | | | 230 | involvedLineInfos.map(info => info.rev as FileRev).filter(rev => rev > 0), |
| b69ab31 | | | 231 | ); |
| b69ab31 | | | 232 | // Normalize `selectedRev` so it cannot be a public commit (fileRev === 0). |
| b69ab31 | | | 233 | // Setting to `null` to make the edit deselected (left in the working copy). |
| b69ab31 | | | 234 | const normalizeSelectedRev = (rev: FileRev): FileRev | null => (rev === 0 ? null : rev); |
| b69ab31 | | | 235 | if (involvedRevs.length === 1) { |
| b69ab31 | | | 236 | // Only one rev. Set selectedRev to this. |
| b69ab31 | | | 237 | // For simplicity, we're not checking the "continuous" lines here yet (different from Python). |
| b69ab31 | | | 238 | const introductionRev = involvedRevs[0]; |
| b69ab31 | | | 239 | result.push( |
| b69ab31 | | | 240 | AbsorbEdit({ |
| b69ab31 | | | 241 | oldStart: a1, |
| b69ab31 | | | 242 | oldEnd: a2, |
| b69ab31 | | | 243 | oldLines: List(oldLines.slice(a1, a2)), |
| b69ab31 | | | 244 | newStart: b1, |
| b69ab31 | | | 245 | newEnd: b2, |
| b69ab31 | | | 246 | newLines: List(newLines.slice(b1, b2)), |
| b69ab31 | | | 247 | introductionRev, |
| b69ab31 | | | 248 | selectedRev: normalizeSelectedRev(introductionRev), |
| b69ab31 | | | 249 | absorbEditId: allocateAbsorbId(), |
| b69ab31 | | | 250 | }), |
| b69ab31 | | | 251 | ); |
| b69ab31 | | | 252 | } else if (b1 === b2) { |
| b69ab31 | | | 253 | // Deletion. Break the chunk into sub-chunks with different selectedRevs. |
| b69ab31 | | | 254 | // For simplicity, we're not checking the "continuous" lines here yet (different from Python). |
| b69ab31 | | | 255 | splitChunk(a1, a2, oldLineInfos, (oldStart, oldEnd, introductionRev) => { |
| b69ab31 | | | 256 | result.push( |
| b69ab31 | | | 257 | AbsorbEdit({ |
| b69ab31 | | | 258 | oldStart, |
| b69ab31 | | | 259 | oldEnd, |
| b69ab31 | | | 260 | oldLines: List(oldLines.slice(oldStart, oldEnd)), |
| b69ab31 | | | 261 | newStart: b1, |
| b69ab31 | | | 262 | newEnd: b2, |
| b69ab31 | | | 263 | newLines: List([]), |
| b69ab31 | | | 264 | introductionRev, |
| b69ab31 | | | 265 | selectedRev: normalizeSelectedRev(introductionRev), |
| b69ab31 | | | 266 | absorbEditId: allocateAbsorbId(), |
| b69ab31 | | | 267 | }), |
| b69ab31 | | | 268 | ); |
| b69ab31 | | | 269 | }); |
| b69ab31 | | | 270 | } else if (a2 - a1 === b2 - b1 && involvedLineInfos.some(info => info.rev > 0)) { |
| b69ab31 | | | 271 | // Line count matches on both side. No public lines. |
| b69ab31 | | | 272 | // We assume the "a" and "b" sides are 1:1 mapped. |
| b69ab31 | | | 273 | // So, even if the "a"-side lines blame to different revs, we can |
| b69ab31 | | | 274 | // still break the chunks to individual lines. |
| b69ab31 | | | 275 | const delta = b1 - a1; |
| b69ab31 | | | 276 | splitChunk(a1, a2, oldLineInfos, (oldStart, oldEnd, introductionRev) => { |
| b69ab31 | | | 277 | const newStart = oldStart + delta; |
| b69ab31 | | | 278 | const newEnd = oldEnd + delta; |
| b69ab31 | | | 279 | result.push( |
| b69ab31 | | | 280 | AbsorbEdit({ |
| b69ab31 | | | 281 | oldStart, |
| b69ab31 | | | 282 | oldEnd, |
| b69ab31 | | | 283 | oldLines: List(oldLines.slice(oldStart, oldEnd)), |
| b69ab31 | | | 284 | newStart, |
| b69ab31 | | | 285 | newEnd, |
| b69ab31 | | | 286 | newLines: List(newLines.slice(newStart, newEnd)), |
| b69ab31 | | | 287 | introductionRev, |
| b69ab31 | | | 288 | selectedRev: normalizeSelectedRev(introductionRev), |
| b69ab31 | | | 289 | absorbEditId: allocateAbsorbId(), |
| b69ab31 | | | 290 | }), |
| b69ab31 | | | 291 | ); |
| b69ab31 | | | 292 | }); |
| b69ab31 | | | 293 | } else { |
| b69ab31 | | | 294 | // Other cases, like replacing 10 lines from 3 revs to 20 lines. |
| b69ab31 | | | 295 | // It might be possible to build extra fancy UIs to support it |
| b69ab31 | | | 296 | // asking the user which sub-chunk on the "a" side matches which |
| b69ab31 | | | 297 | // sub-chunk on the "b" side. |
| b69ab31 | | | 298 | // For now, we just report this chunk as a whole chunk that can |
| b69ab31 | | | 299 | // only be absorbed to the "max" rev where the left side is |
| b69ab31 | | | 300 | // "settled" down. |
| b69ab31 | | | 301 | result.push( |
| b69ab31 | | | 302 | AbsorbEdit({ |
| b69ab31 | | | 303 | oldStart: a1, |
| b69ab31 | | | 304 | oldEnd: a2, |
| b69ab31 | | | 305 | oldLines: List(oldLines.slice(a1, a2)), |
| b69ab31 | | | 306 | newStart: b1, |
| b69ab31 | | | 307 | newEnd: b2, |
| b69ab31 | | | 308 | newLines: List(newLines.slice(b1, b2)), |
| b69ab31 | | | 309 | introductionRev: max(...involvedRevs, 0), |
| b69ab31 | | | 310 | selectedRev: null, |
| b69ab31 | | | 311 | absorbEditId: allocateAbsorbId(), |
| b69ab31 | | | 312 | }), |
| b69ab31 | | | 313 | ); |
| b69ab31 | | | 314 | } |
| b69ab31 | | | 315 | }); |
| b69ab31 | | | 316 | return List(result); |
| b69ab31 | | | 317 | } |
| b69ab31 | | | 318 | |
| b69ab31 | | | 319 | /** |
| b69ab31 | | | 320 | * Apply edits specified by `chunks`. |
| b69ab31 | | | 321 | * The `chunk.selectedRev` is expected to include the `AbsorbEditId`. |
| b69ab31 | | | 322 | */ |
| b69ab31 | | | 323 | export function applyFileStackEditsWithAbsorbId( |
| b69ab31 | | | 324 | stack: FileStackState, |
| b69ab31 | | | 325 | chunks: Iterable<AbsorbEdit>, |
| b69ab31 | | | 326 | ): FileStackState { |
| b69ab31 | | | 327 | assert(stack.revLength > 0, 'stack should not be empty'); |
| b69ab31 | | | 328 | let linelog = stack.convertToLineLog(); |
| b69ab31 | | | 329 | const wdirRev = stack.revLength; |
| b69ab31 | | | 330 | const stackTopRev = wdirRev - 1; |
| b69ab31 | | | 331 | // Apply the changes. Assuming there are no overlapping chunks, we apply |
| b69ab31 | | | 332 | // from end to start so the line numbers won't need change. |
| b69ab31 | | | 333 | const sortedChunks = [...chunks].toSorted((a, b) => b.oldEnd - a.oldEnd); |
| b69ab31 | | | 334 | sortedChunks.forEach(chunk => { |
| b69ab31 | | | 335 | // If not "selected" to amend to a commit, leave the chunk at the wdir. |
| b69ab31 | | | 336 | const baseRev = chunk.selectedRev ?? (wdirRev as FileRev); |
| b69ab31 | | | 337 | const absorbEditId = nullthrows(chunk.absorbEditId); |
| b69ab31 | | | 338 | const targetRev = embedAbsorbId(baseRev, absorbEditId); |
| b69ab31 | | | 339 | assert( |
| b69ab31 | | | 340 | targetRev >= chunk.introductionRev, |
| b69ab31 | | | 341 | `selectedRev ${targetRev} must be >= introductionRev ${chunk.introductionRev}`, |
| b69ab31 | | | 342 | ); |
| b69ab31 | | | 343 | assert( |
| b69ab31 | | | 344 | targetRev > 0, |
| b69ab31 | | | 345 | 'selectedRev must be > 0 since rev 0 is from the immutable public commit', |
| b69ab31 | | | 346 | ); |
| b69ab31 | | | 347 | // Edit the content of a past revision (targetRev, and follow-ups) from a |
| b69ab31 | | | 348 | // future revision (oldRev, matches the line numbers). |
| b69ab31 | | | 349 | linelog = linelog.editChunk( |
| b69ab31 | | | 350 | stackTopRev, |
| b69ab31 | | | 351 | chunk.oldStart, |
| b69ab31 | | | 352 | chunk.oldEnd, |
| b69ab31 | | | 353 | targetRev, |
| b69ab31 | | | 354 | chunk.newLines.toArray(), |
| b69ab31 | | | 355 | ); |
| b69ab31 | | | 356 | }); |
| b69ab31 | | | 357 | return stack.fromLineLog(linelog); |
| b69ab31 | | | 358 | } |
| b69ab31 | | | 359 | |
| b69ab31 | | | 360 | /** Split the start..end chunk into sub-chunks so each chunk has the same "blame" rev. */ |
| b69ab31 | | | 361 | function splitChunk( |
| b69ab31 | | | 362 | start: number, |
| b69ab31 | | | 363 | end: number, |
| b69ab31 | | | 364 | lineInfos: readonly LineInfo[], |
| b69ab31 | | | 365 | callback: (_start: number, _end: number, _introductionRev: FileRev) => void, |
| b69ab31 | | | 366 | ) { |
| b69ab31 | | | 367 | let lastStart = start; |
| b69ab31 | | | 368 | for (let i = start; i < end; i++) { |
| b69ab31 | | | 369 | const introductionRev = lineInfos[i].rev as FileRev; |
| b69ab31 | | | 370 | if (i + 1 === end || introductionRev != lineInfos[i + 1].rev) { |
| b69ab31 | | | 371 | callback(lastStart, i + 1, introductionRev); |
| b69ab31 | | | 372 | lastStart = i + 1; |
| b69ab31 | | | 373 | } |
| b69ab31 | | | 374 | } |
| b69ab31 | | | 375 | } |