| 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 {RepoPath} from 'shared/types/common'; |
| b69ab31 | | | 9 | import type {CommitStackState} from './commitStackState'; |
| b69ab31 | | | 10 | import type {CommitRev, FileFlag, FileRev} from './common'; |
| b69ab31 | | | 11 | import type {DiffCommit, DiffFile, DiffLine, PartiallySelectedDiffCommit} from './diffSplitTypes'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import {Set as ImSet, List, Range} from 'immutable'; |
| b69ab31 | | | 14 | import type {Repository} from 'isl-server/src/Repository'; |
| b69ab31 | | | 15 | import type {RepositoryContext} from 'isl-server/src/serverTypes'; |
| b69ab31 | | | 16 | import {readableDiffBlocks as diffBlocks, splitLines} from 'shared/diff'; |
| b69ab31 | | | 17 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 18 | import {FlattenLine} from '../linelog'; |
| b69ab31 | | | 19 | import {ABSENT_FLAG, FileState} from './common'; |
| b69ab31 | | | 20 | import {FileStackState} from './fileStackState'; |
| b69ab31 | | | 21 | import {next} from './revMath'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | /** Parameters used by `diffFile()`. */ |
| b69ab31 | | | 24 | type DiffFileProps = { |
| b69ab31 | | | 25 | aContent: string; |
| b69ab31 | | | 26 | bContent: string; |
| b69ab31 | | | 27 | aPath: RepoPath; |
| b69ab31 | | | 28 | bPath: RepoPath; |
| b69ab31 | | | 29 | aFlag: FileFlag; |
| b69ab31 | | | 30 | bFlag: FileFlag; |
| b69ab31 | | | 31 | }; |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | /** |
| b69ab31 | | | 34 | * Calculate the diff for a commit. Returns a JSON-friendly format. |
| b69ab31 | | | 35 | * NOTE: |
| b69ab31 | | | 36 | * - This is not a lossless representation. Certain files (non-utf8, large) are |
| b69ab31 | | | 37 | * silently ignored. |
| b69ab31 | | | 38 | * - Renaming x to y has 2 changes: delete x, edit y (diff against x). |
| b69ab31 | | | 39 | */ |
| b69ab31 | | | 40 | export function diffCommit(stack: CommitStackState, rev: CommitRev): DiffCommit { |
| b69ab31 | | | 41 | const commit = nullthrows(stack.get(rev)); |
| b69ab31 | | | 42 | const aRev = commit.parents.first() ?? (-1 as CommitRev); |
| b69ab31 | | | 43 | const files = stack.getPaths(rev, {text: true}).flatMap(bPath => { |
| b69ab31 | | | 44 | const bFile = stack.getFile(rev, bPath); |
| b69ab31 | | | 45 | const aPath = bFile.copyFrom ?? bPath; |
| b69ab31 | | | 46 | const aFile = stack.getFile(aRev, aPath); |
| b69ab31 | | | 47 | const aContent = stack.getUtf8DataOptional(aFile); |
| b69ab31 | | | 48 | const bContent = stack.getUtf8DataOptional(bFile); |
| b69ab31 | | | 49 | if (aContent === null || bContent === null) { |
| b69ab31 | | | 50 | // Not utf-8. |
| b69ab31 | | | 51 | return []; |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | const aFlag = aFile.flags ?? ''; |
| b69ab31 | | | 54 | const bFlag = bFile.flags ?? ''; |
| b69ab31 | | | 55 | if (aContent === bContent && aFlag === bFlag) { |
| b69ab31 | | | 56 | // Not changed. |
| b69ab31 | | | 57 | return []; |
| b69ab31 | | | 58 | } |
| b69ab31 | | | 59 | return [diffFile({aContent, bContent, aPath, bPath, aFlag, bFlag})]; |
| b69ab31 | | | 60 | }); |
| b69ab31 | | | 61 | return { |
| b69ab31 | | | 62 | message: commit.text, |
| b69ab31 | | | 63 | files, |
| b69ab31 | | | 64 | }; |
| b69ab31 | | | 65 | } |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | /** |
| b69ab31 | | | 68 | * Split the `rev` into `len(selections)`. Each `newDiff` specifies a subset of |
| b69ab31 | | | 69 | * line changes originally from `diffCommit(stack, rev)`. |
| b69ab31 | | | 70 | * |
| b69ab31 | | | 71 | * Designed to be robust about "bad" input of `selections`: |
| b69ab31 | | | 72 | * - If `selections` contains line references not present in |
| b69ab31 | | | 73 | * `diffCommit(stack, rev)`, they will be ignored. |
| b69ab31 | | | 74 | * - The last diff's line selection is ignored so we can force match |
| b69ab31 | | | 75 | * the content of the original commit. |
| b69ab31 | | | 76 | * |
| b69ab31 | | | 77 | * Binary or large files that are not part of `diffCommit(stack, rev)` |
| b69ab31 | | | 78 | * will be moved to the last split commit. |
| b69ab31 | | | 79 | */ |
| b69ab31 | | | 80 | export function applyDiffSplit( |
| b69ab31 | | | 81 | stack: CommitStackState, |
| b69ab31 | | | 82 | rev: CommitRev, |
| b69ab31 | | | 83 | selections: ReadonlyArray<PartiallySelectedDiffCommit>, |
| b69ab31 | | | 84 | ): CommitStackState { |
| b69ab31 | | | 85 | const originalDiff = diffCommit(stack, rev); |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | // Drop the last diff since its content is forced to match `rev`. |
| b69ab31 | | | 88 | const len = selections.length - 1; |
| b69ab31 | | | 89 | if (len < 0) { |
| b69ab31 | | | 90 | return stack; |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | // Calculate the file contents. |
| b69ab31 | | | 94 | const affectedFiles = new Map(originalDiff.files.map(f => [f.bPath, f])); |
| b69ab31 | | | 95 | const diffFiles: Array<Map<RepoPath, [Set<number>, Set<number>]>> = selections |
| b69ab31 | | | 96 | .slice(0, len) |
| b69ab31 | | | 97 | .map(d => new Map(d.files.map(f => [f.bPath, [new Set(f.aLines), new Set(f.bLines)]]))); |
| b69ab31 | | | 98 | const allRevs = ImSet(Range(0, len)); |
| b69ab31 | | | 99 | const noneRevs = ImSet<number>(); |
| b69ab31 | | | 100 | const fileStacks: Map<RepoPath, FileStackState> = new Map( |
| b69ab31 | | | 101 | [...affectedFiles.entries()].map(([path, file]) => { |
| b69ab31 | | | 102 | const lines = file.lines.map(({a, b, content}) => { |
| b69ab31 | | | 103 | let revs = allRevs; |
| b69ab31 | | | 104 | if (a == null && b != null) { |
| b69ab31 | | | 105 | // Figure out which rev adds (selects) the line. |
| b69ab31 | | | 106 | const rev = diffFiles.findIndex(map => map.get(path)?.[1]?.has(b)); |
| b69ab31 | | | 107 | revs = rev == -1 ? noneRevs : ImSet(Range(rev, len)); |
| b69ab31 | | | 108 | } else if (b == null && a != null) { |
| b69ab31 | | | 109 | // Figure out which rev removes (selects) the line. |
| b69ab31 | | | 110 | const rev = diffFiles.findIndex(map => map.get(path)?.[0]?.has(a)); |
| b69ab31 | | | 111 | revs = rev == -1 ? allRevs : ImSet(Range(0, rev)); |
| b69ab31 | | | 112 | } |
| b69ab31 | | | 113 | return new FlattenLine({revs, data: content}); |
| b69ab31 | | | 114 | }); |
| b69ab31 | | | 115 | const fileStack = new FileStackState([]); |
| b69ab31 | | | 116 | return [path, fileStack.fromFlattenLines(List(lines), len)]; |
| b69ab31 | | | 117 | }), |
| b69ab31 | | | 118 | ); |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | // Create new commits and populate their content. |
| b69ab31 | | | 121 | const copyFromMap = new Map( |
| b69ab31 | | | 122 | [...affectedFiles.values()].map(file => [ |
| b69ab31 | | | 123 | file.bPath, |
| b69ab31 | | | 124 | file.aPath === file.bPath ? undefined : file.aPath, |
| b69ab31 | | | 125 | ]), |
| b69ab31 | | | 126 | ); |
| b69ab31 | | | 127 | let newStack = stack; |
| b69ab31 | | | 128 | selections.slice(0, len).forEach((selection, i) => { |
| b69ab31 | | | 129 | const currentRev = next(rev, i); |
| b69ab31 | | | 130 | newStack = newStack.insertEmpty(currentRev, selection.message, currentRev); |
| b69ab31 | | | 131 | selection.files.forEach(file => { |
| b69ab31 | | | 132 | const content = fileStacks.get(file.bPath)?.getRev(i as FileRev); |
| b69ab31 | | | 133 | if (content != null) { |
| b69ab31 | | | 134 | // copyFrom is set when the file is first modified. |
| b69ab31 | | | 135 | const copyFrom: string | undefined = |
| b69ab31 | | | 136 | file.bFlag === ABSENT_FLAG ? undefined : copyFromMap.get(file.bPath); |
| b69ab31 | | | 137 | newStack = newStack.setFile(currentRev, file.bPath, _f => |
| b69ab31 | | | 138 | FileState({data: content, copyFrom, flags: file.bFlag ?? ''}), |
| b69ab31 | | | 139 | ); |
| b69ab31 | | | 140 | copyFromMap.delete(file.bPath); |
| b69ab31 | | | 141 | } |
| b69ab31 | | | 142 | }); |
| b69ab31 | | | 143 | }); |
| b69ab31 | | | 144 | |
| b69ab31 | | | 145 | // Update commit message of the last commit. |
| b69ab31 | | | 146 | newStack = newStack.editCommitMessage(next(rev, len), selections[len].message); |
| b69ab31 | | | 147 | |
| b69ab31 | | | 148 | return newStack; |
| b69ab31 | | | 149 | } |
| b69ab31 | | | 150 | |
| b69ab31 | | | 151 | /** Produce a readable diff for debugging or testing purpose. */ |
| b69ab31 | | | 152 | export function displayDiff(diff: DiffCommit): string { |
| b69ab31 | | | 153 | const output = [diff.message.trimEnd(), '\n']; |
| b69ab31 | | | 154 | diff.files.forEach(file => { |
| b69ab31 | | | 155 | output.push(`diff a/${file.aPath} b/${file.bPath}\n`); |
| b69ab31 | | | 156 | if (file.aFlag !== file.bFlag) { |
| b69ab31 | | | 157 | if (file.bFlag === ABSENT_FLAG) { |
| b69ab31 | | | 158 | output.push(`deleted file mode ${flagToMode(file.aFlag)}\n`); |
| b69ab31 | | | 159 | } else if (file.aFlag === ABSENT_FLAG) { |
| b69ab31 | | | 160 | output.push(`new file mode ${flagToMode(file.bFlag)}\n`); |
| b69ab31 | | | 161 | } else { |
| b69ab31 | | | 162 | output.push(`old mode ${flagToMode(file.aFlag)}\n`); |
| b69ab31 | | | 163 | output.push(`new mode ${flagToMode(file.bFlag)}\n`); |
| b69ab31 | | | 164 | } |
| b69ab31 | | | 165 | } |
| b69ab31 | | | 166 | if (file.aPath !== file.bPath) { |
| b69ab31 | | | 167 | output.push(`copy from ${file.aPath}\n`); |
| b69ab31 | | | 168 | output.push(`copy to ${file.bPath}\n`); |
| b69ab31 | | | 169 | } |
| b69ab31 | | | 170 | file.lines.forEach(line => { |
| b69ab31 | | | 171 | const sign = line.a == null ? '+' : line.b == null ? '-' : ' '; |
| b69ab31 | | | 172 | output.push(`${sign}${line.content}`); |
| b69ab31 | | | 173 | if (!line.content.includes('\n')) { |
| b69ab31 | | | 174 | output.push('\n\\ No newline at end of file'); |
| b69ab31 | | | 175 | } |
| b69ab31 | | | 176 | }); |
| b69ab31 | | | 177 | }); |
| b69ab31 | | | 178 | return output.join(''); |
| b69ab31 | | | 179 | } |
| b69ab31 | | | 180 | |
| b69ab31 | | | 181 | function flagToMode(flag: FileFlag): string { |
| b69ab31 | | | 182 | switch (flag) { |
| b69ab31 | | | 183 | case '': |
| b69ab31 | | | 184 | return '100644'; |
| b69ab31 | | | 185 | case 'x': |
| b69ab31 | | | 186 | return '100755'; |
| b69ab31 | | | 187 | case 'l': |
| b69ab31 | | | 188 | return '120000'; |
| b69ab31 | | | 189 | case 'm': |
| b69ab31 | | | 190 | return '160000'; |
| b69ab31 | | | 191 | default: |
| b69ab31 | | | 192 | return '100644'; |
| b69ab31 | | | 193 | } |
| b69ab31 | | | 194 | } |
| b69ab31 | | | 195 | |
| b69ab31 | | | 196 | /** Produce `DiffFile` based on contents of both sides. */ |
| b69ab31 | | | 197 | export function diffFile({ |
| b69ab31 | | | 198 | aContent, |
| b69ab31 | | | 199 | bContent, |
| b69ab31 | | | 200 | aPath, |
| b69ab31 | | | 201 | bPath, |
| b69ab31 | | | 202 | aFlag, |
| b69ab31 | | | 203 | bFlag, |
| b69ab31 | | | 204 | }: DiffFileProps): DiffFile { |
| b69ab31 | | | 205 | const aLines = splitLines(aContent); |
| b69ab31 | | | 206 | const bLines = splitLines(bContent); |
| b69ab31 | | | 207 | const lines: DiffLine[] = []; |
| b69ab31 | | | 208 | diffBlocks(aLines, bLines).forEach(([sign, [a1, a2, b1, b2]]) => { |
| b69ab31 | | | 209 | if (sign === '=') { |
| b69ab31 | | | 210 | for (let ai = a1; ai < a2; ++ai) { |
| b69ab31 | | | 211 | lines.push({a: ai, b: ai + b1 - a1, content: aLines[ai]}); |
| b69ab31 | | | 212 | } |
| b69ab31 | | | 213 | } else { |
| b69ab31 | | | 214 | for (let ai = a1; ai < a2; ++ai) { |
| b69ab31 | | | 215 | lines.push({a: ai, b: null, content: aLines[ai]}); |
| b69ab31 | | | 216 | } |
| b69ab31 | | | 217 | for (let bi = b1; bi < b2; ++bi) { |
| b69ab31 | | | 218 | lines.push({a: null, b: bi, content: bLines[bi]}); |
| b69ab31 | | | 219 | } |
| b69ab31 | | | 220 | } |
| b69ab31 | | | 221 | }); |
| b69ab31 | | | 222 | return { |
| b69ab31 | | | 223 | aPath, |
| b69ab31 | | | 224 | bPath, |
| b69ab31 | | | 225 | aFlag, |
| b69ab31 | | | 226 | bFlag, |
| b69ab31 | | | 227 | lines, |
| b69ab31 | | | 228 | }; |
| b69ab31 | | | 229 | } |
| b69ab31 | | | 230 | /** |
| b69ab31 | | | 231 | * Calculate the diff between two commits using `sl debugexport stack`. |
| b69ab31 | | | 232 | * This is similar to `diffCommit` but works with commit hashes instead of CommitStackState. |
| b69ab31 | | | 233 | * |
| b69ab31 | | | 234 | * @param runSlCommand - Function to run sl commands, typically from SaplingRepository.runSlCommand |
| b69ab31 | | | 235 | * @param commitHash - The commit hash to diff |
| b69ab31 | | | 236 | * @param parentHash - The parent commit hash to diff against |
| b69ab31 | | | 237 | * @returns DiffCommit containing the message and file diffs |
| b69ab31 | | | 238 | */ |
| b69ab31 | | | 239 | export async function diffCurrentCommit( |
| b69ab31 | | | 240 | repo: Repository, |
| b69ab31 | | | 241 | ctx: RepositoryContext, |
| b69ab31 | | | 242 | ): Promise<DiffCommit> { |
| b69ab31 | | | 243 | // Export both commits |
| b69ab31 | | | 244 | const results = await repo.runCommand( |
| b69ab31 | | | 245 | ['debugexportstack', '-r', '.|.^'], |
| b69ab31 | | | 246 | 'ExportStackCommand', |
| b69ab31 | | | 247 | ctx, |
| b69ab31 | | | 248 | ); |
| b69ab31 | | | 249 | |
| b69ab31 | | | 250 | if (results.exitCode !== 0) { |
| b69ab31 | | | 251 | throw new Error(`Failed to export commit . ${results.stderr}`); |
| b69ab31 | | | 252 | } |
| b69ab31 | | | 253 | |
| b69ab31 | | | 254 | // Parse the exported stacks |
| b69ab31 | | | 255 | const stack: Array<{ |
| b69ab31 | | | 256 | node: string; |
| b69ab31 | | | 257 | text: string; |
| b69ab31 | | | 258 | requested: boolean; |
| b69ab31 | | | 259 | files?: {[path: string]: {data?: string; flags?: FileFlag; copyFrom?: RepoPath} | null}; |
| b69ab31 | | | 260 | relevantFiles?: {[path: string]: {data?: string; flags?: FileFlag; copyFrom?: RepoPath} | null}; |
| b69ab31 | | | 261 | }> = JSON.parse(results.stdout); |
| b69ab31 | | | 262 | const requestedCommits = stack.filter(commit => commit.requested); |
| b69ab31 | | | 263 | |
| b69ab31 | | | 264 | if (requestedCommits.length !== 2) { |
| b69ab31 | | | 265 | throw new Error(`Expected 2 commits from debugexportstack, got ${requestedCommits.length}`); |
| b69ab31 | | | 266 | } |
| b69ab31 | | | 267 | |
| b69ab31 | | | 268 | // The second requested commit is the current one (.), the first is parent (.^) |
| b69ab31 | | | 269 | // because debugexportstack sorts topologically (ancestors first, descendants last) |
| b69ab31 | | | 270 | const parentCommit = requestedCommits[0]; |
| b69ab31 | | | 271 | const currentCommit = requestedCommits[1]; |
| b69ab31 | | | 272 | |
| b69ab31 | | | 273 | // Get all file paths from the commit |
| b69ab31 | | | 274 | const commitFiles = currentCommit.files ?? {}; |
| b69ab31 | | | 275 | const parentFiles = parentCommit.files ?? {}; |
| b69ab31 | | | 276 | const parentRelevantFiles = parentCommit.relevantFiles ?? {}; |
| b69ab31 | | | 277 | |
| b69ab31 | | | 278 | // Collect all paths that changed |
| b69ab31 | | | 279 | const allPaths = new Set([...Object.keys(commitFiles)]); |
| b69ab31 | | | 280 | |
| b69ab31 | | | 281 | const files = []; |
| b69ab31 | | | 282 | for (const bPath of allPaths) { |
| b69ab31 | | | 283 | const bFile = commitFiles[bPath]; |
| b69ab31 | | | 284 | const aPath = bFile?.copyFrom ?? bPath; |
| b69ab31 | | | 285 | // Get parent file from either files or relevantFiles |
| b69ab31 | | | 286 | const aFile = |
| b69ab31 | | | 287 | aPath === bPath |
| b69ab31 | | | 288 | ? (parentFiles[bPath] ?? parentRelevantFiles[bPath]) |
| b69ab31 | | | 289 | : (parentFiles[aPath] ?? parentRelevantFiles[aPath]); |
| b69ab31 | | | 290 | |
| b69ab31 | | | 291 | const aContent = aFile?.data ?? ''; |
| b69ab31 | | | 292 | const bContent = bFile?.data ?? ''; |
| b69ab31 | | | 293 | |
| b69ab31 | | | 294 | // Skip if both are null (shouldn't happen, but be safe) |
| b69ab31 | | | 295 | if (aFile === null && bFile === null) { |
| b69ab31 | | | 296 | continue; |
| b69ab31 | | | 297 | } |
| b69ab31 | | | 298 | |
| b69ab31 | | | 299 | const aFlag = aFile?.flags ?? ''; |
| b69ab31 | | | 300 | const bFlag = bFile?.flags ?? ''; |
| b69ab31 | | | 301 | |
| b69ab31 | | | 302 | // Skip if content and flags are unchanged |
| b69ab31 | | | 303 | if (aContent === bContent && aFlag === bFlag) { |
| b69ab31 | | | 304 | continue; |
| b69ab31 | | | 305 | } |
| b69ab31 | | | 306 | |
| b69ab31 | | | 307 | const diff = diffFile({aContent, bContent, aPath, bPath, aFlag, bFlag}); |
| b69ab31 | | | 308 | const reducedLines = reduceContextualLines(diff.lines, 10); |
| b69ab31 | | | 309 | files.push({...diff, lines: reducedLines}); |
| b69ab31 | | | 310 | } |
| b69ab31 | | | 311 | |
| b69ab31 | | | 312 | return { |
| b69ab31 | | | 313 | message: currentCommit.text, |
| b69ab31 | | | 314 | files, |
| b69ab31 | | | 315 | }; |
| b69ab31 | | | 316 | } |
| b69ab31 | | | 317 | |
| b69ab31 | | | 318 | export type PhabricatorAiDiffSplitCommitDiffFileLine = { |
| b69ab31 | | | 319 | a: number | null; |
| b69ab31 | | | 320 | b: number | null; |
| b69ab31 | | | 321 | content: string; |
| b69ab31 | | | 322 | }; |
| b69ab31 | | | 323 | |
| b69ab31 | | | 324 | /** |
| b69ab31 | | | 325 | * Reduces the number of lines in a diff by keeping only the lines that are within |
| b69ab31 | | | 326 | * a specified number of lines from a changed line. |
| b69ab31 | | | 327 | * |
| b69ab31 | | | 328 | * @param lines The lines to filter |
| b69ab31 | | | 329 | * @param maxContextLines The maximum number of lines to keep around each changed line |
| b69ab31 | | | 330 | * @returns A new array with only the lines that are within the specified number of lines from a changed line |
| b69ab31 | | | 331 | */ |
| b69ab31 | | | 332 | export function reduceContextualLines( |
| b69ab31 | | | 333 | lines: ReadonlyArray<PhabricatorAiDiffSplitCommitDiffFileLine>, |
| b69ab31 | | | 334 | maxContextLines: number = 3, |
| b69ab31 | | | 335 | ): Array<PhabricatorAiDiffSplitCommitDiffFileLine> { |
| b69ab31 | | | 336 | const distanceToLastClosestChangedLine: number[] = []; |
| b69ab31 | | | 337 | let lastClosestChangedLineIndex = -1; |
| b69ab31 | | | 338 | |
| b69ab31 | | | 339 | for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { |
| b69ab31 | | | 340 | const line = lines[lineIndex]; |
| b69ab31 | | | 341 | |
| b69ab31 | | | 342 | const a = line.a; |
| b69ab31 | | | 343 | const b = line.b; |
| b69ab31 | | | 344 | if ((a == null && b != null) || (a != null && b == null)) { |
| b69ab31 | | | 345 | // line was added or removed |
| b69ab31 | | | 346 | lastClosestChangedLineIndex = lineIndex; |
| b69ab31 | | | 347 | } |
| b69ab31 | | | 348 | |
| b69ab31 | | | 349 | if (lastClosestChangedLineIndex === -1) { |
| b69ab31 | | | 350 | distanceToLastClosestChangedLine.push(Number.MAX_SAFE_INTEGER); |
| b69ab31 | | | 351 | } else { |
| b69ab31 | | | 352 | distanceToLastClosestChangedLine.push(lineIndex - lastClosestChangedLineIndex); |
| b69ab31 | | | 353 | } |
| b69ab31 | | | 354 | } |
| b69ab31 | | | 355 | |
| b69ab31 | | | 356 | const distanceToNextClosestChangedLine: number[] = []; |
| b69ab31 | | | 357 | let nextClosestChangedLineIndex = -1; |
| b69ab31 | | | 358 | |
| b69ab31 | | | 359 | for (let lineIndex = lines.length - 1; lineIndex >= 0; lineIndex--) { |
| b69ab31 | | | 360 | const line = lines[lineIndex]; |
| b69ab31 | | | 361 | |
| b69ab31 | | | 362 | const a = line.a; |
| b69ab31 | | | 363 | const b = line.b; |
| b69ab31 | | | 364 | if ((a == null && b != null) || (a != null && b == null)) { |
| b69ab31 | | | 365 | // line was added or removed |
| b69ab31 | | | 366 | nextClosestChangedLineIndex = lineIndex; |
| b69ab31 | | | 367 | } |
| b69ab31 | | | 368 | |
| b69ab31 | | | 369 | if (nextClosestChangedLineIndex === -1) { |
| b69ab31 | | | 370 | distanceToNextClosestChangedLine.push(Number.MAX_SAFE_INTEGER); |
| b69ab31 | | | 371 | } else { |
| b69ab31 | | | 372 | distanceToNextClosestChangedLine.push(nextClosestChangedLineIndex - lineIndex); |
| b69ab31 | | | 373 | } |
| b69ab31 | | | 374 | } |
| b69ab31 | | | 375 | |
| b69ab31 | | | 376 | // Reverse the array since we built it backwards |
| b69ab31 | | | 377 | distanceToNextClosestChangedLine.reverse(); |
| b69ab31 | | | 378 | |
| b69ab31 | | | 379 | const newLines: Array<PhabricatorAiDiffSplitCommitDiffFileLine> = []; |
| b69ab31 | | | 380 | |
| b69ab31 | | | 381 | for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { |
| b69ab31 | | | 382 | if ( |
| b69ab31 | | | 383 | distanceToLastClosestChangedLine[lineIndex] <= maxContextLines || |
| b69ab31 | | | 384 | distanceToNextClosestChangedLine[lineIndex] <= maxContextLines |
| b69ab31 | | | 385 | ) { |
| b69ab31 | | | 386 | newLines.push(lines[lineIndex]); |
| b69ab31 | | | 387 | } |
| b69ab31 | | | 388 | } |
| b69ab31 | | | 389 | |
| b69ab31 | | | 390 | return newLines; |
| b69ab31 | | | 391 | } |