| 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 {ExportCommit, ExportFile, ExportStack} from 'shared/types/stack'; |
| b69ab31 | | | 10 | import type {CommitRev, FileRev} from '../commitStackState'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {Map as ImMap, Set as ImSet, List} from 'immutable'; |
| b69ab31 | | | 13 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 14 | import {WDIR_NODE} from '../../dag/virtualCommit'; |
| b69ab31 | | | 15 | import { |
| b69ab31 | | | 16 | ABSENT_FILE, |
| b69ab31 | | | 17 | CommitIdx, |
| b69ab31 | | | 18 | CommitStackState, |
| b69ab31 | | | 19 | CommitState, |
| b69ab31 | | | 20 | FileIdx, |
| b69ab31 | | | 21 | FileState, |
| b69ab31 | | | 22 | } from '../commitStackState'; |
| b69ab31 | | | 23 | import {FileStackState} from '../fileStackState'; |
| b69ab31 | | | 24 | import {describeAbsorbIdChunkMap} from './absorb.test'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | export const exportCommitDefault: ExportCommit = { |
| b69ab31 | | | 27 | requested: true, |
| b69ab31 | | | 28 | immutable: false, |
| b69ab31 | | | 29 | author: 'test <test@example.com>', |
| b69ab31 | | | 30 | date: [0, 0], |
| b69ab31 | | | 31 | node: '', |
| b69ab31 | | | 32 | text: '', |
| b69ab31 | | | 33 | }; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | // In this test we tend to use uppercase for commits (ex. A, B, C), |
| b69ab31 | | | 36 | // and lowercase for files (ex. x, y, z). |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | /** |
| b69ab31 | | | 39 | * Created by `drawdag --no-files`: |
| b69ab31 | | | 40 | * |
| b69ab31 | | | 41 | * C # C/z.txt=(removed) |
| b69ab31 | | | 42 | * | |
| b69ab31 | | | 43 | * B # B/y.txt=33 (renamed from x.txt) |
| b69ab31 | | | 44 | * | |
| b69ab31 | | | 45 | * A # A/x.txt=33 |
| b69ab31 | | | 46 | * | # A/z.txt=22 |
| b69ab31 | | | 47 | * / |
| b69ab31 | | | 48 | * Z # Z/z.txt=11 |
| b69ab31 | | | 49 | * |
| b69ab31 | | | 50 | * and exported via `debugexportstack -r 'desc(A)::'`. |
| b69ab31 | | | 51 | */ |
| b69ab31 | | | 52 | const exportStack1: ExportStack = [ |
| b69ab31 | | | 53 | { |
| b69ab31 | | | 54 | ...exportCommitDefault, |
| b69ab31 | | | 55 | immutable: true, |
| b69ab31 | | | 56 | node: 'Z_NODE', |
| b69ab31 | | | 57 | relevantFiles: { |
| b69ab31 | | | 58 | 'x.txt': null, |
| b69ab31 | | | 59 | 'z.txt': {data: '11'}, |
| b69ab31 | | | 60 | }, |
| b69ab31 | | | 61 | requested: false, |
| b69ab31 | | | 62 | text: 'Z', |
| b69ab31 | | | 63 | }, |
| b69ab31 | | | 64 | { |
| b69ab31 | | | 65 | ...exportCommitDefault, |
| b69ab31 | | | 66 | files: { |
| b69ab31 | | | 67 | 'x.txt': {data: '33'}, |
| b69ab31 | | | 68 | 'z.txt': {data: '22'}, |
| b69ab31 | | | 69 | }, |
| b69ab31 | | | 70 | node: 'A_NODE', |
| b69ab31 | | | 71 | parents: ['Z_NODE'], |
| b69ab31 | | | 72 | relevantFiles: {'y.txt': null}, |
| b69ab31 | | | 73 | text: 'A', |
| b69ab31 | | | 74 | }, |
| b69ab31 | | | 75 | { |
| b69ab31 | | | 76 | ...exportCommitDefault, |
| b69ab31 | | | 77 | files: { |
| b69ab31 | | | 78 | 'x.txt': null, |
| b69ab31 | | | 79 | 'y.txt': {copyFrom: 'x.txt', data: '33'}, |
| b69ab31 | | | 80 | }, |
| b69ab31 | | | 81 | node: 'B_NODE', |
| b69ab31 | | | 82 | parents: ['A_NODE'], |
| b69ab31 | | | 83 | relevantFiles: {'z.txt': {data: '22'}}, |
| b69ab31 | | | 84 | text: 'B', |
| b69ab31 | | | 85 | }, |
| b69ab31 | | | 86 | { |
| b69ab31 | | | 87 | ...exportCommitDefault, |
| b69ab31 | | | 88 | date: [0.0, 0], |
| b69ab31 | | | 89 | files: {'z.txt': null}, |
| b69ab31 | | | 90 | node: 'C_NODE', |
| b69ab31 | | | 91 | parents: ['B_NODE'], |
| b69ab31 | | | 92 | text: 'C', |
| b69ab31 | | | 93 | }, |
| b69ab31 | | | 94 | ]; |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | /** Construct `CommitStackState` from a stack of files for testing purpose. */ |
| b69ab31 | | | 97 | export function linearStackWithFiles( |
| b69ab31 | | | 98 | stackFiles: Array<{[path: RepoPath]: ExportFile | null}>, |
| b69ab31 | | | 99 | ): CommitStackState { |
| b69ab31 | | | 100 | return new CommitStackState( |
| b69ab31 | | | 101 | stackFiles.map((files, i) => { |
| b69ab31 | | | 102 | const nextFiles = stackFiles.at(i + 1) ?? {}; |
| b69ab31 | | | 103 | return { |
| b69ab31 | | | 104 | ...exportCommitDefault, |
| b69ab31 | | | 105 | node: `NODE_${i}`, |
| b69ab31 | | | 106 | parents: i === 0 ? [] : [`NODE_${i - 1}`], |
| b69ab31 | | | 107 | text: `Commit ${i}`, |
| b69ab31 | | | 108 | files, |
| b69ab31 | | | 109 | relevantFiles: Object.fromEntries( |
| b69ab31 | | | 110 | Object.entries(nextFiles).filter(([path, _file]) => !Object.hasOwn(files, path)), |
| b69ab31 | | | 111 | ), |
| b69ab31 | | | 112 | } as ExportCommit; |
| b69ab31 | | | 113 | }), |
| b69ab31 | | | 114 | ); |
| b69ab31 | | | 115 | } |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | describe('CommitStackState', () => { |
| b69ab31 | | | 118 | it('accepts an empty stack', () => { |
| b69ab31 | | | 119 | const stack = new CommitStackState([]); |
| b69ab31 | | | 120 | expect(stack.revs()).toStrictEqual([]); |
| b69ab31 | | | 121 | }); |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | it('accepts a stack without a public commit', () => { |
| b69ab31 | | | 124 | const stack = new CommitStackState([ |
| b69ab31 | | | 125 | { |
| b69ab31 | | | 126 | ...exportCommitDefault, |
| b69ab31 | | | 127 | files: {'a.txt': {data: 'a'}}, |
| b69ab31 | | | 128 | node: 'x', |
| b69ab31 | | | 129 | parents: [], |
| b69ab31 | | | 130 | text: 'A', |
| b69ab31 | | | 131 | }, |
| b69ab31 | | | 132 | ]); |
| b69ab31 | | | 133 | expect(stack.revs()).toStrictEqual([0]); |
| b69ab31 | | | 134 | }); |
| b69ab31 | | | 135 | |
| b69ab31 | | | 136 | it('rejects a stack with multiple roots', () => { |
| b69ab31 | | | 137 | const stack = [ |
| b69ab31 | | | 138 | {...exportCommitDefault, node: 'Z1'}, |
| b69ab31 | | | 139 | {...exportCommitDefault, node: 'Z2'}, |
| b69ab31 | | | 140 | ]; |
| b69ab31 | | | 141 | expect(() => new CommitStackState(stack)).toThrowError( |
| b69ab31 | | | 142 | 'Multiple roots ["Z1","Z2"] is not supported', |
| b69ab31 | | | 143 | ); |
| b69ab31 | | | 144 | }); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | it('rejects a stack with merges', () => { |
| b69ab31 | | | 147 | const stack = [ |
| b69ab31 | | | 148 | {...exportCommitDefault, node: 'A', parents: []}, |
| b69ab31 | | | 149 | {...exportCommitDefault, node: 'B', parents: ['A']}, |
| b69ab31 | | | 150 | {...exportCommitDefault, node: 'C', parents: ['A', 'B']}, |
| b69ab31 | | | 151 | ]; |
| b69ab31 | | | 152 | expect(() => new CommitStackState(stack)).toThrowError('Merge commit C is not supported'); |
| b69ab31 | | | 153 | }); |
| b69ab31 | | | 154 | |
| b69ab31 | | | 155 | it('rejects circular stack', () => { |
| b69ab31 | | | 156 | const stack = [ |
| b69ab31 | | | 157 | {...exportCommitDefault, node: 'A', parents: ['B']}, |
| b69ab31 | | | 158 | {...exportCommitDefault, node: 'B', parents: ['A']}, |
| b69ab31 | | | 159 | ]; |
| b69ab31 | | | 160 | expect(() => new CommitStackState(stack)).toThrowError(); |
| b69ab31 | | | 161 | }); |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | it('provides file paths', () => { |
| b69ab31 | | | 164 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 165 | expect(stack.getAllPaths()).toStrictEqual(['x.txt', 'y.txt', 'z.txt']); |
| b69ab31 | | | 166 | }); |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | it('logs commit history', () => { |
| b69ab31 | | | 169 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 170 | expect(stack.revs()).toStrictEqual([0, 1, 2, 3]); |
| b69ab31 | | | 171 | expect([...stack.log(1 as CommitRev)]).toStrictEqual([1, 0]); |
| b69ab31 | | | 172 | expect([...stack.log(3 as CommitRev)]).toStrictEqual([3, 2, 1, 0]); |
| b69ab31 | | | 173 | }); |
| b69ab31 | | | 174 | |
| b69ab31 | | | 175 | it('finds child commits via childRevs', () => { |
| b69ab31 | | | 176 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 177 | expect(stack.childRevs(0 as CommitRev)).toMatchInlineSnapshot(` |
| b69ab31 | | | 178 | [ |
| b69ab31 | | | 179 | 1, |
| b69ab31 | | | 180 | ] |
| b69ab31 | | | 181 | `); |
| b69ab31 | | | 182 | expect(stack.childRevs(1 as CommitRev)).toMatchInlineSnapshot(` |
| b69ab31 | | | 183 | [ |
| b69ab31 | | | 184 | 2, |
| b69ab31 | | | 185 | ] |
| b69ab31 | | | 186 | `); |
| b69ab31 | | | 187 | expect(stack.childRevs(2 as CommitRev)).toMatchInlineSnapshot(` |
| b69ab31 | | | 188 | [ |
| b69ab31 | | | 189 | 3, |
| b69ab31 | | | 190 | ] |
| b69ab31 | | | 191 | `); |
| b69ab31 | | | 192 | expect(stack.childRevs(3 as CommitRev)).toMatchInlineSnapshot(`[]`); |
| b69ab31 | | | 193 | }); |
| b69ab31 | | | 194 | |
| b69ab31 | | | 195 | describe('log file history', () => { |
| b69ab31 | | | 196 | // [rev, path] => [rev, path, file] |
| b69ab31 | | | 197 | const extend = (stack: CommitStackState, revPathPairs: Array<[number, string]>) => { |
| b69ab31 | | | 198 | return revPathPairs.map(([rev, path]) => { |
| b69ab31 | | | 199 | const file = |
| b69ab31 | | | 200 | rev >= 0 ? stack.get(rev as CommitRev)?.files.get(path) : stack.bottomFiles.get(path); |
| b69ab31 | | | 201 | expect(file).toBe(stack.getFile(rev as CommitRev, path)); |
| b69ab31 | | | 202 | return [rev, path, file]; |
| b69ab31 | | | 203 | }); |
| b69ab31 | | | 204 | }; |
| b69ab31 | | | 205 | |
| b69ab31 | | | 206 | it('logs file history', () => { |
| b69ab31 | | | 207 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 208 | expect([...stack.logFile(3 as CommitRev, 'x.txt')]).toStrictEqual( |
| b69ab31 | | | 209 | extend(stack, [ |
| b69ab31 | | | 210 | [2, 'x.txt'], |
| b69ab31 | | | 211 | [1, 'x.txt'], |
| b69ab31 | | | 212 | ]), |
| b69ab31 | | | 213 | ); |
| b69ab31 | | | 214 | expect([...stack.logFile(3 as CommitRev, 'y.txt')]).toStrictEqual( |
| b69ab31 | | | 215 | extend(stack, [[2, 'y.txt']]), |
| b69ab31 | | | 216 | ); |
| b69ab31 | | | 217 | expect([...stack.logFile(3 as CommitRev, 'z.txt')]).toStrictEqual( |
| b69ab31 | | | 218 | extend(stack, [ |
| b69ab31 | | | 219 | [3, 'z.txt'], |
| b69ab31 | | | 220 | [1, 'z.txt'], |
| b69ab31 | | | 221 | ]), |
| b69ab31 | | | 222 | ); |
| b69ab31 | | | 223 | // Changes in not requested commits (rev 0) are ignored. |
| b69ab31 | | | 224 | expect([...stack.logFile(3 as CommitRev, 'k.txt')]).toStrictEqual([]); |
| b69ab31 | | | 225 | }); |
| b69ab31 | | | 226 | |
| b69ab31 | | | 227 | it('logs file history following renames', () => { |
| b69ab31 | | | 228 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 229 | expect([...stack.logFile(3 as CommitRev, 'y.txt', true)]).toStrictEqual( |
| b69ab31 | | | 230 | extend(stack, [ |
| b69ab31 | | | 231 | [2, 'y.txt'], |
| b69ab31 | | | 232 | [1, 'x.txt'], |
| b69ab31 | | | 233 | ]), |
| b69ab31 | | | 234 | ); |
| b69ab31 | | | 235 | }); |
| b69ab31 | | | 236 | |
| b69ab31 | | | 237 | it('logs file history including the bottom', () => { |
| b69ab31 | | | 238 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 239 | ['x.txt', 'z.txt'].forEach(path => { |
| b69ab31 | | | 240 | expect([...stack.logFile(1 as CommitRev, path, true, true)]).toStrictEqual( |
| b69ab31 | | | 241 | extend(stack, [ |
| b69ab31 | | | 242 | [1, path], |
| b69ab31 | | | 243 | // rev 0 does not change x.txt or z.txt |
| b69ab31 | | | 244 | [-1, path], |
| b69ab31 | | | 245 | ]), |
| b69ab31 | | | 246 | ); |
| b69ab31 | | | 247 | }); |
| b69ab31 | | | 248 | }); |
| b69ab31 | | | 249 | |
| b69ab31 | | | 250 | it('parentFile follows rename to bottomFile', () => { |
| b69ab31 | | | 251 | const stack = new CommitStackState([ |
| b69ab31 | | | 252 | { |
| b69ab31 | | | 253 | ...exportCommitDefault, |
| b69ab31 | | | 254 | relevantFiles: { |
| b69ab31 | | | 255 | 'x.txt': {data: '11'}, |
| b69ab31 | | | 256 | 'z.txt': {data: '22'}, |
| b69ab31 | | | 257 | }, |
| b69ab31 | | | 258 | files: { |
| b69ab31 | | | 259 | 'z.txt': {data: '33', copyFrom: 'x.txt'}, |
| b69ab31 | | | 260 | }, |
| b69ab31 | | | 261 | text: 'Commit Foo', |
| b69ab31 | | | 262 | }, |
| b69ab31 | | | 263 | ]); |
| b69ab31 | | | 264 | const file = stack.getFile(0 as CommitRev, 'z.txt'); |
| b69ab31 | | | 265 | expect(stack.getUtf8Data(file)).toBe('33'); |
| b69ab31 | | | 266 | const [, , parentFileWithRename] = stack.parentFile(0 as CommitRev, 'z.txt', true); |
| b69ab31 | | | 267 | expect(stack.getUtf8Data(parentFileWithRename)).toBe('11'); |
| b69ab31 | | | 268 | const [, , parentFile] = stack.parentFile(0 as CommitRev, 'z.txt', false); |
| b69ab31 | | | 269 | expect(stack.getUtf8Data(parentFile)).toBe('22'); |
| b69ab31 | | | 270 | }); |
| b69ab31 | | | 271 | }); |
| b69ab31 | | | 272 | |
| b69ab31 | | | 273 | it('provides file contents at given revs', () => { |
| b69ab31 | | | 274 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 275 | expect(stack.getFile(0 as CommitRev, 'x.txt')).toBe(ABSENT_FILE); |
| b69ab31 | | | 276 | expect(stack.getFile(0 as CommitRev, 'y.txt')).toBe(ABSENT_FILE); |
| b69ab31 | | | 277 | expect(stack.getFile(0 as CommitRev, 'z.txt')).toMatchObject({data: '11'}); |
| b69ab31 | | | 278 | expect(stack.getFile(1 as CommitRev, 'x.txt')).toMatchObject({data: '33'}); |
| b69ab31 | | | 279 | expect(stack.getFile(1 as CommitRev, 'y.txt')).toBe(ABSENT_FILE); |
| b69ab31 | | | 280 | expect(stack.getFile(1 as CommitRev, 'z.txt')).toMatchObject({data: '22'}); |
| b69ab31 | | | 281 | expect(stack.getFile(2 as CommitRev, 'x.txt')).toBe(ABSENT_FILE); |
| b69ab31 | | | 282 | expect(stack.getFile(2 as CommitRev, 'y.txt')).toMatchObject({data: '33'}); |
| b69ab31 | | | 283 | expect(stack.getFile(2 as CommitRev, 'z.txt')).toMatchObject({data: '22'}); |
| b69ab31 | | | 284 | expect(stack.getFile(3 as CommitRev, 'x.txt')).toBe(ABSENT_FILE); |
| b69ab31 | | | 285 | expect(stack.getFile(3 as CommitRev, 'y.txt')).toMatchObject({data: '33'}); |
| b69ab31 | | | 286 | expect(stack.getFile(3 as CommitRev, 'z.txt')).toBe(ABSENT_FILE); |
| b69ab31 | | | 287 | }); |
| b69ab31 | | | 288 | |
| b69ab31 | | | 289 | describe('builds FileStack', () => { |
| b69ab31 | | | 290 | it('for double renames', () => { |
| b69ab31 | | | 291 | // x.txt renamed to both y.txt and z.txt. |
| b69ab31 | | | 292 | const stack = new CommitStackState([ |
| b69ab31 | | | 293 | {...exportCommitDefault, node: 'A', files: {'x.txt': {data: 'xx'}}}, |
| b69ab31 | | | 294 | { |
| b69ab31 | | | 295 | ...exportCommitDefault, |
| b69ab31 | | | 296 | node: 'B', |
| b69ab31 | | | 297 | parents: ['A'], |
| b69ab31 | | | 298 | files: { |
| b69ab31 | | | 299 | 'x.txt': null, |
| b69ab31 | | | 300 | 'y.txt': {data: 'yy', copyFrom: 'x.txt'}, |
| b69ab31 | | | 301 | 'z.txt': {data: 'zz', copyFrom: 'x.txt'}, |
| b69ab31 | | | 302 | }, |
| b69ab31 | | | 303 | }, |
| b69ab31 | | | 304 | ]); |
| b69ab31 | | | 305 | expect(stack.describeFileStacks()).toStrictEqual([ |
| b69ab31 | | | 306 | // y.txt inherits x.txt's history. |
| b69ab31 | | | 307 | '0:./x.txt 1:A/x.txt(xx) 2:B/y.txt(yy)', |
| b69ab31 | | | 308 | // z.txt does not inherit x.txt's history (but still has a parent for diff rendering purpose). |
| b69ab31 | | | 309 | '0:A/x.txt(xx) 1:B/z.txt(zz)', |
| b69ab31 | | | 310 | ]); |
| b69ab31 | | | 311 | }); |
| b69ab31 | | | 312 | |
| b69ab31 | | | 313 | it('for double copies', () => { |
| b69ab31 | | | 314 | // x.txt copied to both y.txt and z.txt. |
| b69ab31 | | | 315 | const stack = new CommitStackState([ |
| b69ab31 | | | 316 | {...exportCommitDefault, node: 'A', files: {'x.txt': {data: 'xx'}}}, |
| b69ab31 | | | 317 | { |
| b69ab31 | | | 318 | ...exportCommitDefault, |
| b69ab31 | | | 319 | node: 'B', |
| b69ab31 | | | 320 | parents: ['A'], |
| b69ab31 | | | 321 | files: { |
| b69ab31 | | | 322 | 'y.txt': {data: 'yy', copyFrom: 'x.txt'}, |
| b69ab31 | | | 323 | 'z.txt': {data: 'zz', copyFrom: 'y.txt'}, |
| b69ab31 | | | 324 | }, |
| b69ab31 | | | 325 | }, |
| b69ab31 | | | 326 | ]); |
| b69ab31 | | | 327 | expect(stack.describeFileStacks()).toStrictEqual([ |
| b69ab31 | | | 328 | // y.txt connects to x.txt's history. |
| b69ab31 | | | 329 | '0:./x.txt 1:A/x.txt(xx) 2:B/y.txt(yy)', |
| b69ab31 | | | 330 | // z.txt does not connect to x.txt's history (but still have one parent for diff). |
| b69ab31 | | | 331 | '0:./y.txt 1:B/z.txt(zz)', |
| b69ab31 | | | 332 | ]); |
| b69ab31 | | | 333 | }); |
| b69ab31 | | | 334 | |
| b69ab31 | | | 335 | it('for changes and copies', () => { |
| b69ab31 | | | 336 | // x.txt is changed, and copied to both y.txt and z.txt. |
| b69ab31 | | | 337 | const stack = new CommitStackState([ |
| b69ab31 | | | 338 | {...exportCommitDefault, node: 'A', files: {'x.txt': {data: 'xx'}}}, |
| b69ab31 | | | 339 | { |
| b69ab31 | | | 340 | ...exportCommitDefault, |
| b69ab31 | | | 341 | node: 'B', |
| b69ab31 | | | 342 | parents: ['A'], |
| b69ab31 | | | 343 | files: { |
| b69ab31 | | | 344 | 'x.txt': {data: 'yy'}, |
| b69ab31 | | | 345 | 'y.txt': {data: 'xx', copyFrom: 'x.txt'}, |
| b69ab31 | | | 346 | 'z.txt': {data: 'xx', copyFrom: 'x.txt'}, |
| b69ab31 | | | 347 | }, |
| b69ab31 | | | 348 | }, |
| b69ab31 | | | 349 | ]); |
| b69ab31 | | | 350 | expect(stack.describeFileStacks()).toStrictEqual([ |
| b69ab31 | | | 351 | // x.txt has its own history. |
| b69ab31 | | | 352 | '0:./x.txt 1:A/x.txt(xx) 2:B/x.txt(yy)', |
| b69ab31 | | | 353 | // y.txt and z.txt do not share x.txt's history (but still have one parent for diff). |
| b69ab31 | | | 354 | '0:A/x.txt(xx) 1:B/y.txt(xx)', |
| b69ab31 | | | 355 | '0:A/x.txt(xx) 1:B/z.txt(xx)', |
| b69ab31 | | | 356 | ]); |
| b69ab31 | | | 357 | }); |
| b69ab31 | | | 358 | |
| b69ab31 | | | 359 | it('for the the example stack', () => { |
| b69ab31 | | | 360 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 361 | expect(stack.describeFileStacks()).toStrictEqual([ |
| b69ab31 | | | 362 | // x.txt: added by A, modified and renamed by B. |
| b69ab31 | | | 363 | '0:./x.txt 1:A/x.txt(33) 2:B/y.txt(33)', |
| b69ab31 | | | 364 | // z.txt: modified by A, deleted by C. |
| b69ab31 | | | 365 | '0:./z.txt(11) 1:A/z.txt(22) 2:C/z.txt', |
| b69ab31 | | | 366 | ]); |
| b69ab31 | | | 367 | }); |
| b69ab31 | | | 368 | |
| b69ab31 | | | 369 | it('with rename tracking disabled', () => { |
| b69ab31 | | | 370 | const stack = new CommitStackState(exportStack1).buildFileStacks({followRenames: false}); |
| b69ab31 | | | 371 | // no x.txt -> y.txt rename |
| b69ab31 | | | 372 | expect(stack.describeFileStacks()).toStrictEqual([ |
| b69ab31 | | | 373 | '0:./x.txt 1:A/x.txt(33) 2:B/x.txt', |
| b69ab31 | | | 374 | '0:./z.txt(11) 1:A/z.txt(22) 2:C/z.txt', |
| b69ab31 | | | 375 | '0:./y.txt 1:B/y.txt(33)', |
| b69ab31 | | | 376 | ]); |
| b69ab31 | | | 377 | }); |
| b69ab31 | | | 378 | }); |
| b69ab31 | | | 379 | |
| b69ab31 | | | 380 | describe('calculates dependencies', () => { |
| b69ab31 | | | 381 | const e = exportCommitDefault; |
| b69ab31 | | | 382 | |
| b69ab31 | | | 383 | it('for content changes', () => { |
| b69ab31 | | | 384 | const stack = new CommitStackState([ |
| b69ab31 | | | 385 | {...e, node: 'Z', requested: false, relevantFiles: {'x.txt': null}}, |
| b69ab31 | | | 386 | {...e, node: 'A', parents: ['Z'], files: {'x.txt': {data: 'b\n'}}}, |
| b69ab31 | | | 387 | {...e, node: 'B', parents: ['A'], files: {'x.txt': {data: 'a\nb\n'}}}, |
| b69ab31 | | | 388 | {...e, node: 'C', parents: ['B'], files: {'x.txt': {data: 'a\nB\n'}}}, |
| b69ab31 | | | 389 | ]); |
| b69ab31 | | | 390 | expect(stack.calculateDepMap()).toStrictEqual( |
| b69ab31 | | | 391 | new Map([ |
| b69ab31 | | | 392 | [0, new Set()], |
| b69ab31 | | | 393 | [1, new Set()], |
| b69ab31 | | | 394 | [2, new Set()], // insertion at other insertion boundary is dependency-free |
| b69ab31 | | | 395 | [3, new Set([1])], |
| b69ab31 | | | 396 | ]), |
| b69ab31 | | | 397 | ); |
| b69ab31 | | | 398 | }); |
| b69ab31 | | | 399 | |
| b69ab31 | | | 400 | it('for file addition and deletion', () => { |
| b69ab31 | | | 401 | const stack = new CommitStackState([ |
| b69ab31 | | | 402 | {...e, node: 'Z', requested: false, relevantFiles: {'x.txt': {data: 'a'}}}, |
| b69ab31 | | | 403 | {...e, node: 'A', parents: ['Z'], files: {'x.txt': null}}, |
| b69ab31 | | | 404 | {...e, node: 'B', parents: ['A'], files: {'x.txt': {data: 'a'}}}, |
| b69ab31 | | | 405 | {...e, node: 'C', parents: ['B'], files: {'x.txt': null}}, |
| b69ab31 | | | 406 | ]); |
| b69ab31 | | | 407 | expect(stack.calculateDepMap()).toStrictEqual( |
| b69ab31 | | | 408 | new Map([ |
| b69ab31 | | | 409 | [0, new Set()], |
| b69ab31 | | | 410 | [1, new Set()], |
| b69ab31 | | | 411 | [2, new Set([1])], // commit B adds x.txt, depends on commit A's deletion. |
| b69ab31 | | | 412 | [3, new Set([2])], // commit C deletes x.txt, depends on commit B's addition. |
| b69ab31 | | | 413 | ]), |
| b69ab31 | | | 414 | ); |
| b69ab31 | | | 415 | }); |
| b69ab31 | | | 416 | |
| b69ab31 | | | 417 | it('for copies', () => { |
| b69ab31 | | | 418 | const stack = new CommitStackState([ |
| b69ab31 | | | 419 | {...e, node: 'A', files: {'x.txt': {data: 'a'}}}, |
| b69ab31 | | | 420 | {...e, node: 'B', parents: ['A'], files: {'y.txt': {data: 'a', copyFrom: 'x.txt'}}}, |
| b69ab31 | | | 421 | {...e, node: 'C', parents: ['B'], files: {'z.txt': {data: 'a', copyFrom: 'x.txt'}}}, |
| b69ab31 | | | 422 | { |
| b69ab31 | | | 423 | ...e, |
| b69ab31 | | | 424 | node: 'D', |
| b69ab31 | | | 425 | parents: ['C'], |
| b69ab31 | | | 426 | files: {'p.txt': {data: 'a', copyFrom: 'x.txt'}, 'q.txt': {data: 'a', copyFrom: 'z.txt'}}, |
| b69ab31 | | | 427 | }, |
| b69ab31 | | | 428 | ]); |
| b69ab31 | | | 429 | expect(stack.calculateDepMap()).toStrictEqual( |
| b69ab31 | | | 430 | new Map([ |
| b69ab31 | | | 431 | [0, new Set()], |
| b69ab31 | | | 432 | [1, new Set([0])], // commit B copies commit A's x.txt to y.txt. |
| b69ab31 | | | 433 | [2, new Set([0])], // commit C copies commit A's x.txt to z.txt. |
| b69ab31 | | | 434 | [3, new Set([0, 2])], // commit D copies commit A's x.txt to p.txt, and commit C's z.txt to q.txt. |
| b69ab31 | | | 435 | ]), |
| b69ab31 | | | 436 | ); |
| b69ab31 | | | 437 | }); |
| b69ab31 | | | 438 | }); |
| b69ab31 | | | 439 | |
| b69ab31 | | | 440 | describe('folding commits', () => { |
| b69ab31 | | | 441 | const e = exportCommitDefault; |
| b69ab31 | | | 442 | |
| b69ab31 | | | 443 | it('cannot be used for immutable commits', () => { |
| b69ab31 | | | 444 | const stack = new CommitStackState([ |
| b69ab31 | | | 445 | {...e, node: 'A', immutable: true}, |
| b69ab31 | | | 446 | {...e, node: 'B', parents: ['A'], immutable: false}, |
| b69ab31 | | | 447 | {...e, node: 'C', parents: ['B'], immutable: false}, |
| b69ab31 | | | 448 | ]); |
| b69ab31 | | | 449 | expect(stack.canFoldDown(1 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 450 | expect(stack.canFoldDown(2 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 451 | }); |
| b69ab31 | | | 452 | |
| b69ab31 | | | 453 | it('cannot be used for out-of-range commits', () => { |
| b69ab31 | | | 454 | const stack = new CommitStackState([ |
| b69ab31 | | | 455 | {...e, node: 'A'}, |
| b69ab31 | | | 456 | {...e, node: 'B', parents: ['A']}, |
| b69ab31 | | | 457 | ]); |
| b69ab31 | | | 458 | expect(stack.canFoldDown(0 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 459 | expect(stack.canFoldDown(1 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 460 | expect(stack.canFoldDown(2 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 461 | }); |
| b69ab31 | | | 462 | |
| b69ab31 | | | 463 | it('cannot be used for forks', () => { |
| b69ab31 | | | 464 | const stack = new CommitStackState([ |
| b69ab31 | | | 465 | {...e, node: 'A'}, |
| b69ab31 | | | 466 | {...e, node: 'B', parents: ['A']}, |
| b69ab31 | | | 467 | {...e, node: 'C', parents: ['A']}, |
| b69ab31 | | | 468 | ]); |
| b69ab31 | | | 469 | expect(stack.canFoldDown(1 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 470 | expect(stack.canFoldDown(2 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 471 | }); |
| b69ab31 | | | 472 | |
| b69ab31 | | | 473 | it('works for simple edits', () => { |
| b69ab31 | | | 474 | let stack = new CommitStackState([ |
| b69ab31 | | | 475 | { |
| b69ab31 | | | 476 | ...e, |
| b69ab31 | | | 477 | node: 'A', |
| b69ab31 | | | 478 | text: 'Commit A', |
| b69ab31 | | | 479 | parents: [], |
| b69ab31 | | | 480 | files: {'x.txt': {data: 'xx'}, 'y.txt': {data: 'yy'}}, |
| b69ab31 | | | 481 | }, |
| b69ab31 | | | 482 | {...e, node: 'B', text: 'Commit B', parents: ['A'], files: {'x.txt': {data: 'yy'}}}, |
| b69ab31 | | | 483 | {...e, node: 'C', text: 'Commit C', parents: ['B'], files: {'x.txt': {data: 'zz'}}}, |
| b69ab31 | | | 484 | ]); |
| b69ab31 | | | 485 | expect(stack.canFoldDown(1 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 486 | stack = stack.foldDown(1 as CommitRev); |
| b69ab31 | | | 487 | expect(stack.stack.size).toBe(2); |
| b69ab31 | | | 488 | expect(stack.stack.get(0)?.toJS()).toMatchObject({ |
| b69ab31 | | | 489 | key: 'A', |
| b69ab31 | | | 490 | files: { |
| b69ab31 | | | 491 | 'x.txt': {data: 'yy'}, |
| b69ab31 | | | 492 | 'y.txt': {data: 'yy'}, |
| b69ab31 | | | 493 | }, |
| b69ab31 | | | 494 | originalNodes: new Set(['A', 'B']), |
| b69ab31 | | | 495 | text: 'Commit A, Commit B', |
| b69ab31 | | | 496 | parents: [], |
| b69ab31 | | | 497 | }); |
| b69ab31 | | | 498 | expect(stack.stack.get(1)?.toJS()).toMatchObject({ |
| b69ab31 | | | 499 | key: 'C', |
| b69ab31 | | | 500 | text: 'Commit C', |
| b69ab31 | | | 501 | parents: [0], // Commit C's parent is updated to Commit A. |
| b69ab31 | | | 502 | }); |
| b69ab31 | | | 503 | }); |
| b69ab31 | | | 504 | |
| b69ab31 | | | 505 | it('removes copyFrom appropriately', () => { |
| b69ab31 | | | 506 | let stack = new CommitStackState([ |
| b69ab31 | | | 507 | {...e, node: 'A', parents: [], files: {'x.txt': {data: 'xx'}}}, |
| b69ab31 | | | 508 | {...e, node: 'B', parents: ['A'], files: {'y.txt': {data: 'yy', copyFrom: 'x.txt'}}}, |
| b69ab31 | | | 509 | ]); |
| b69ab31 | | | 510 | expect(stack.canFoldDown(1 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 511 | stack = stack.foldDown(1 as CommitRev); |
| b69ab31 | | | 512 | expect(stack.stack.get(0)?.toJS()).toMatchObject({ |
| b69ab31 | | | 513 | files: { |
| b69ab31 | | | 514 | 'x.txt': {data: 'xx'}, |
| b69ab31 | | | 515 | 'y.txt': {data: 'yy'}, // no longer has "copyFrom", since 'x.txt' does not exist in commit A. |
| b69ab31 | | | 516 | }, |
| b69ab31 | | | 517 | }); |
| b69ab31 | | | 518 | }); |
| b69ab31 | | | 519 | |
| b69ab31 | | | 520 | it('keeps copyFrom appropriately', () => { |
| b69ab31 | | | 521 | let stack = new CommitStackState([ |
| b69ab31 | | | 522 | {...e, node: 'A', parents: [], files: {xt: {data: 'xx'}, yt: {data: 'yy'}}}, |
| b69ab31 | | | 523 | {...e, node: 'B', parents: ['A'], files: {y1t: {data: 'yy', copyFrom: 'yt'}}}, |
| b69ab31 | | | 524 | { |
| b69ab31 | | | 525 | ...e, |
| b69ab31 | | | 526 | node: 'C', |
| b69ab31 | | | 527 | parents: ['B'], |
| b69ab31 | | | 528 | files: {x1t: {data: 'x1', copyFrom: 'xt'}, y1t: {data: 'y1'}}, |
| b69ab31 | | | 529 | }, |
| b69ab31 | | | 530 | ]); |
| b69ab31 | | | 531 | // Fold B+C. |
| b69ab31 | | | 532 | expect(stack.canFoldDown(2 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 533 | stack = stack.foldDown(2 as CommitRev); |
| b69ab31 | | | 534 | expect(stack.stack.get(1)?.toJS()).toMatchObject({ |
| b69ab31 | | | 535 | files: { |
| b69ab31 | | | 536 | y1t: {data: 'y1', copyFrom: 'yt'}, // reuse copyFrom: 'yt' from commit B. |
| b69ab31 | | | 537 | x1t: {data: 'x1', copyFrom: 'xt'}, // reuse copyFrom: 'xt' from commit C. |
| b69ab31 | | | 538 | }, |
| b69ab31 | | | 539 | }); |
| b69ab31 | | | 540 | }); |
| b69ab31 | | | 541 | |
| b69ab31 | | | 542 | it('chains renames', () => { |
| b69ab31 | | | 543 | let stack = new CommitStackState([ |
| b69ab31 | | | 544 | {...e, node: 'A', parents: [], files: {xt: {data: 'xx'}}}, |
| b69ab31 | | | 545 | {...e, node: 'B', parents: ['A'], files: {yt: {data: 'yy', copyFrom: 'xt'}, xt: null}}, |
| b69ab31 | | | 546 | {...e, node: 'C', parents: ['B'], files: {zt: {data: 'zz', copyFrom: 'yt'}, yt: null}}, |
| b69ab31 | | | 547 | ]); |
| b69ab31 | | | 548 | // Fold B+C. |
| b69ab31 | | | 549 | expect(stack.canFoldDown(2 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 550 | stack = stack.foldDown(2 as CommitRev); |
| b69ab31 | | | 551 | expect(stack.stack.get(1)?.toJS()).toMatchObject({ |
| b69ab31 | | | 552 | files: { |
| b69ab31 | | | 553 | xt: ABSENT_FILE.toJS(), |
| b69ab31 | | | 554 | // 'yt' is no longer considered changed. |
| b69ab31 | | | 555 | zt: {data: 'zz', copyFrom: 'xt'}, // 'xt'->'yt'->'zt' is folded to 'xt'->'zt'. |
| b69ab31 | | | 556 | }, |
| b69ab31 | | | 557 | }); |
| b69ab31 | | | 558 | }); |
| b69ab31 | | | 559 | |
| b69ab31 | | | 560 | it('removes cancel-out changes', () => { |
| b69ab31 | | | 561 | let stack = new CommitStackState([ |
| b69ab31 | | | 562 | {...e, node: 'A', parents: [], files: {xt: {data: 'xx'}}}, |
| b69ab31 | | | 563 | {...e, node: 'B', parents: ['A'], files: {xt: {data: 'yy'}, zt: {data: 'zz'}}}, |
| b69ab31 | | | 564 | {...e, node: 'C', parents: ['B'], files: {xt: {data: 'xx'}}}, |
| b69ab31 | | | 565 | ]); |
| b69ab31 | | | 566 | // Fold B+C. |
| b69ab31 | | | 567 | expect(stack.canFoldDown(2 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 568 | stack = stack.foldDown(2 as CommitRev); |
| b69ab31 | | | 569 | expect(stack.stack.get(1)?.toJS()).toMatchObject({ |
| b69ab31 | | | 570 | files: {zt: {data: 'zz'}}, // changes to 'yt' is removed. |
| b69ab31 | | | 571 | }); |
| b69ab31 | | | 572 | }); |
| b69ab31 | | | 573 | }); |
| b69ab31 | | | 574 | |
| b69ab31 | | | 575 | describe('dropping commits', () => { |
| b69ab31 | | | 576 | const e = exportCommitDefault; |
| b69ab31 | | | 577 | |
| b69ab31 | | | 578 | it('cannot be used for immutable commits', () => { |
| b69ab31 | | | 579 | const stack = new CommitStackState([ |
| b69ab31 | | | 580 | {...e, node: 'A', immutable: true}, |
| b69ab31 | | | 581 | {...e, node: 'B', parents: ['A'], immutable: true}, |
| b69ab31 | | | 582 | {...e, node: 'C', parents: ['B'], immutable: false}, |
| b69ab31 | | | 583 | ]); |
| b69ab31 | | | 584 | expect(stack.canDrop(0 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 585 | expect(stack.canDrop(1 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 586 | expect(stack.canDrop(2 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 587 | }); |
| b69ab31 | | | 588 | |
| b69ab31 | | | 589 | it('detects content dependencies', () => { |
| b69ab31 | | | 590 | const stack = new CommitStackState([ |
| b69ab31 | | | 591 | {...e, node: 'A', files: {xx: {data: '0\n2\n'}}}, |
| b69ab31 | | | 592 | {...e, node: 'B', parents: ['A'], files: {xx: {data: '0\n1\n2\n'}}}, |
| b69ab31 | | | 593 | {...e, node: 'C', parents: ['B'], files: {xx: {data: '0\n1\n2\n3\n'}}}, |
| b69ab31 | | | 594 | {...e, node: 'D', parents: ['C'], files: {xx: {data: '0\n1\n2\n4\n'}}}, |
| b69ab31 | | | 595 | ]); |
| b69ab31 | | | 596 | expect(stack.canDrop(0 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 597 | expect(stack.canDrop(1 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 598 | expect(stack.canDrop(2 as CommitRev)).toBeFalsy(); // D depends on C |
| b69ab31 | | | 599 | expect(stack.canDrop(3 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 600 | }); |
| b69ab31 | | | 601 | |
| b69ab31 | | | 602 | it('detects commit graph dependencies', () => { |
| b69ab31 | | | 603 | const stack = new CommitStackState([ |
| b69ab31 | | | 604 | {...e, node: 'A', files: {xx: {data: '1'}}}, |
| b69ab31 | | | 605 | {...e, node: 'B', parents: ['A'], files: {xx: {data: '2'}}}, |
| b69ab31 | | | 606 | {...e, node: 'C', parents: ['A'], files: {xx: {data: '3'}}}, |
| b69ab31 | | | 607 | {...e, node: 'D', parents: ['C'], files: {xx: {data: '4'}}}, |
| b69ab31 | | | 608 | ]); |
| b69ab31 | | | 609 | expect(stack.canDrop(0 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 610 | expect(stack.canDrop(1 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 611 | expect(stack.canDrop(2 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 612 | expect(stack.canDrop(3 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 613 | }); |
| b69ab31 | | | 614 | |
| b69ab31 | | | 615 | it('for a change in the middle of a stack', () => { |
| b69ab31 | | | 616 | let stack = new CommitStackState([ |
| b69ab31 | | | 617 | {...e, node: 'A', files: {xx: {data: 'p\ny\n'}}}, |
| b69ab31 | | | 618 | {...e, node: 'B', parents: ['A'], files: {xx: {data: 'p\nx\ny\n'}}}, |
| b69ab31 | | | 619 | {...e, node: 'C', parents: ['B'], files: {xx: {data: 'p\nx\ny\nz\n'}}}, |
| b69ab31 | | | 620 | ]); |
| b69ab31 | | | 621 | expect(stack.canDrop(0 as CommitRev)).toBeFalsy(); |
| b69ab31 | | | 622 | expect(stack.canDrop(1 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 623 | expect(stack.canDrop(2 as CommitRev)).toBeTruthy(); |
| b69ab31 | | | 624 | stack = stack.drop(1 as CommitRev); |
| b69ab31 | | | 625 | expect(stack.stack.size).toBe(2); |
| b69ab31 | | | 626 | expect(stack.stack.get(1)?.toJS()).toMatchObject({ |
| b69ab31 | | | 627 | originalNodes: ['C'], |
| b69ab31 | | | 628 | files: {xx: {data: 'p\ny\nz\n'}}, |
| b69ab31 | | | 629 | }); |
| b69ab31 | | | 630 | expect(stack.stack.toArray().map(c => c.key)).toMatchObject(['A', 'C']); |
| b69ab31 | | | 631 | }); |
| b69ab31 | | | 632 | }); |
| b69ab31 | | | 633 | |
| b69ab31 | | | 634 | describe('reordering commits', () => { |
| b69ab31 | | | 635 | const e = exportCommitDefault; |
| b69ab31 | | | 636 | |
| b69ab31 | | | 637 | it('cannot be used for immutable commits', () => { |
| b69ab31 | | | 638 | const stack = new CommitStackState([ |
| b69ab31 | | | 639 | {...e, node: 'A', immutable: true}, |
| b69ab31 | | | 640 | {...e, node: 'B', parents: ['A'], immutable: true}, |
| b69ab31 | | | 641 | {...e, node: 'C', parents: ['B'], immutable: false}, |
| b69ab31 | | | 642 | ]); |
| b69ab31 | | | 643 | expect(stack.canReorder([0, 2, 1] as CommitRev[])).toBeFalsy(); |
| b69ab31 | | | 644 | expect(stack.canReorder([1, 0, 2] as CommitRev[])).toBeFalsy(); |
| b69ab31 | | | 645 | expect(stack.canReorder([0, 1, 2] as CommitRev[])).toBeTruthy(); |
| b69ab31 | | | 646 | }); |
| b69ab31 | | | 647 | |
| b69ab31 | | | 648 | it('respects content dependencies', () => { |
| b69ab31 | | | 649 | const stack = new CommitStackState([ |
| b69ab31 | | | 650 | {...e, node: 'A', files: {xx: {data: '0\n2\n'}}}, |
| b69ab31 | | | 651 | {...e, node: 'B', parents: ['A'], files: {xx: {data: '0\n1\n2\n'}}}, |
| b69ab31 | | | 652 | {...e, node: 'C', parents: ['B'], files: {xx: {data: '0\n1\n2\n3\n'}}}, |
| b69ab31 | | | 653 | {...e, node: 'D', parents: ['C'], files: {xx: {data: '0\n1\n2\n4\n'}}}, |
| b69ab31 | | | 654 | ]); |
| b69ab31 | | | 655 | expect(stack.canReorder([0, 2, 3, 1] as CommitRev[])).toBeTruthy(); |
| b69ab31 | | | 656 | expect(stack.canReorder([0, 2, 1, 3] as CommitRev[])).toBeTruthy(); |
| b69ab31 | | | 657 | expect(stack.canReorder([0, 3, 2, 1] as CommitRev[])).toBeFalsy(); |
| b69ab31 | | | 658 | expect(stack.canReorder([0, 3, 1, 2] as CommitRev[])).toBeFalsy(); |
| b69ab31 | | | 659 | }); |
| b69ab31 | | | 660 | |
| b69ab31 | | | 661 | it('refuses to reorder non-linear stack', () => { |
| b69ab31 | | | 662 | const stack = new CommitStackState([ |
| b69ab31 | | | 663 | {...e, node: 'A', files: {xx: {data: '1'}}}, |
| b69ab31 | | | 664 | {...e, node: 'B', parents: ['A'], files: {xx: {data: '2'}}}, |
| b69ab31 | | | 665 | {...e, node: 'C', parents: ['A'], files: {xx: {data: '3'}}}, |
| b69ab31 | | | 666 | {...e, node: 'D', parents: ['C'], files: {xx: {data: '4'}}}, |
| b69ab31 | | | 667 | ]); |
| b69ab31 | | | 668 | expect(stack.canReorder([0, 2, 3, 1] as CommitRev[])).toBeFalsy(); |
| b69ab31 | | | 669 | expect(stack.canReorder([0, 2, 1, 3] as CommitRev[])).toBeFalsy(); |
| b69ab31 | | | 670 | expect(stack.canReorder([0, 1, 2, 3] as CommitRev[])).toBeFalsy(); |
| b69ab31 | | | 671 | }); |
| b69ab31 | | | 672 | |
| b69ab31 | | | 673 | it('can reorder a long stack', () => { |
| b69ab31 | | | 674 | const exportStack: ExportStack = [...Array(20).keys()].map(i => { |
| b69ab31 | | | 675 | return {...e, node: `A${i}`, parents: i === 0 ? [] : [`A${i - 1}`], files: {}}; |
| b69ab31 | | | 676 | }); |
| b69ab31 | | | 677 | const stack = new CommitStackState(exportStack); |
| b69ab31 | | | 678 | expect(stack.canReorder(stack.revs().reverse())).toBeTruthy(); |
| b69ab31 | | | 679 | }); |
| b69ab31 | | | 680 | |
| b69ab31 | | | 681 | it('reorders adjacent changes', () => { |
| b69ab31 | | | 682 | // Note: usually rev 0 is a public parent commit, rev 0 is not usually reordered. |
| b69ab31 | | | 683 | // But this test reorders rev 0 and triggers some interesting code paths. |
| b69ab31 | | | 684 | let stack = new CommitStackState([ |
| b69ab31 | | | 685 | {...e, node: 'A', files: {xx: {data: '1\n'}}}, |
| b69ab31 | | | 686 | {...e, node: 'B', parents: ['A'], files: {xx: {data: '1\n2\n'}}}, |
| b69ab31 | | | 687 | ]); |
| b69ab31 | | | 688 | expect(stack.canReorder([1, 0] as CommitRev[])).toBeTruthy(); |
| b69ab31 | | | 689 | stack = stack.reorder([1, 0] as CommitRev[]); |
| b69ab31 | | | 690 | expect(stack.stack.toArray().map(c => c.files.get('xx')?.data)).toMatchObject([ |
| b69ab31 | | | 691 | '2\n', |
| b69ab31 | | | 692 | '1\n2\n', |
| b69ab31 | | | 693 | ]); |
| b69ab31 | | | 694 | expect(stack.stack.toArray().map(c => c.key)).toMatchObject(['B', 'A']); |
| b69ab31 | | | 695 | // Reorder back. |
| b69ab31 | | | 696 | expect(stack.canReorder([1, 0] as CommitRev[])).toBeTruthy(); |
| b69ab31 | | | 697 | stack = stack.reorder([1, 0] as CommitRev[]); |
| b69ab31 | | | 698 | expect(stack.stack.toArray().map(c => c.files.get('xx')?.data)).toMatchObject([ |
| b69ab31 | | | 699 | '1\n', |
| b69ab31 | | | 700 | '1\n2\n', |
| b69ab31 | | | 701 | ]); |
| b69ab31 | | | 702 | expect(stack.stack.toArray().map(c => c.key)).toMatchObject(['A', 'B']); |
| b69ab31 | | | 703 | }); |
| b69ab31 | | | 704 | |
| b69ab31 | | | 705 | it('reorders content changes', () => { |
| b69ab31 | | | 706 | let stack = new CommitStackState([ |
| b69ab31 | | | 707 | {...e, node: 'A', files: {xx: {data: '1\n1\n'}}}, |
| b69ab31 | | | 708 | {...e, node: 'B', parents: ['A'], files: {xx: {data: '0\n1\n1\n'}}}, |
| b69ab31 | | | 709 | {...e, node: 'C', parents: ['B'], files: {yy: {data: '0'}}}, // Does not change 'xx'. |
| b69ab31 | | | 710 | {...e, node: 'D', parents: ['C'], files: {xx: {data: '0\n1\n1\n2\n'}}}, |
| b69ab31 | | | 711 | {...e, node: 'E', parents: ['D'], files: {xx: {data: '0\n1\n3\n1\n2\n'}}}, |
| b69ab31 | | | 712 | ]); |
| b69ab31 | | | 713 | |
| b69ab31 | | | 714 | // A-B-C-D-E => A-C-E-B-D. |
| b69ab31 | | | 715 | let order = [0, 2, 4, 1, 3] as CommitRev[]; |
| b69ab31 | | | 716 | expect(stack.canReorder(order)).toBeTruthy(); |
| b69ab31 | | | 717 | stack = stack.reorder(order); |
| b69ab31 | | | 718 | const getNode = (r: CommitRev) => stack.stack.get(r)?.originalNodes?.first(); |
| b69ab31 | | | 719 | const getParents = (r: CommitRev) => stack.stack.get(r)?.parents?.toJS(); |
| b69ab31 | | | 720 | expect(stack.revs().map(getNode)).toMatchObject(['A', 'C', 'E', 'B', 'D']); |
| b69ab31 | | | 721 | expect(stack.revs().map(getParents)).toMatchObject([[], [0], [1], [2], [3]]); |
| b69ab31 | | | 722 | expect(stack.revs().map(r => stack.getFile(r, 'xx').data)).toMatchObject([ |
| b69ab31 | | | 723 | '1\n1\n', |
| b69ab31 | | | 724 | '1\n1\n', // Not changed by 'C'. |
| b69ab31 | | | 725 | '1\n3\n1\n', |
| b69ab31 | | | 726 | '0\n1\n3\n1\n', |
| b69ab31 | | | 727 | '0\n1\n3\n1\n2\n', |
| b69ab31 | | | 728 | ]); |
| b69ab31 | | | 729 | expect(stack.revs().map(r => stack.getFile(r, 'yy').data)).toMatchObject([ |
| b69ab31 | | | 730 | '', |
| b69ab31 | | | 731 | '0', |
| b69ab31 | | | 732 | '0', |
| b69ab31 | | | 733 | '0', |
| b69ab31 | | | 734 | '0', |
| b69ab31 | | | 735 | ]); |
| b69ab31 | | | 736 | |
| b69ab31 | | | 737 | // Reorder back. A-C-E-B-D => A-B-C-D-E. |
| b69ab31 | | | 738 | order = [0, 3, 1, 4, 2] as CommitRev[]; |
| b69ab31 | | | 739 | expect(stack.canReorder(order)).toBeTruthy(); |
| b69ab31 | | | 740 | stack = stack.reorder(order); |
| b69ab31 | | | 741 | expect(stack.revs().map(getNode)).toMatchObject(['A', 'B', 'C', 'D', 'E']); |
| b69ab31 | | | 742 | expect(stack.revs().map(getParents)).toMatchObject([[], [0], [1], [2], [3]]); |
| b69ab31 | | | 743 | expect(stack.revs().map(r => stack.getFile(r, 'xx').data)).toMatchObject([ |
| b69ab31 | | | 744 | '1\n1\n', |
| b69ab31 | | | 745 | '0\n1\n1\n', |
| b69ab31 | | | 746 | '0\n1\n1\n', |
| b69ab31 | | | 747 | '0\n1\n1\n2\n', |
| b69ab31 | | | 748 | '0\n1\n3\n1\n2\n', |
| b69ab31 | | | 749 | ]); |
| b69ab31 | | | 750 | }); |
| b69ab31 | | | 751 | }); |
| b69ab31 | | | 752 | |
| b69ab31 | | | 753 | describe('calculating ImportStack', () => { |
| b69ab31 | | | 754 | it('skips all if nothing changed', () => { |
| b69ab31 | | | 755 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 756 | expect(stack.calculateImportStack()).toMatchObject([]); |
| b69ab31 | | | 757 | }); |
| b69ab31 | | | 758 | |
| b69ab31 | | | 759 | it('skips unchanged commits', () => { |
| b69ab31 | | | 760 | // Edits B/y.txt, affects descendants C. |
| b69ab31 | | | 761 | const stack = new CommitStackState(exportStack1).updateEachFile((_rev, file, path) => |
| b69ab31 | | | 762 | path === 'y.txt' ? file.set('data', '333') : file, |
| b69ab31 | | | 763 | ); |
| b69ab31 | | | 764 | expect(stack.calculateImportStack()).toMatchObject([ |
| b69ab31 | | | 765 | [ |
| b69ab31 | | | 766 | 'commit', |
| b69ab31 | | | 767 | { |
| b69ab31 | | | 768 | mark: ':r2', |
| b69ab31 | | | 769 | date: [0, 0], |
| b69ab31 | | | 770 | text: 'B', |
| b69ab31 | | | 771 | parents: ['A_NODE'], |
| b69ab31 | | | 772 | predecessors: ['B_NODE'], |
| b69ab31 | | | 773 | files: { |
| b69ab31 | | | 774 | 'x.txt': null, |
| b69ab31 | | | 775 | 'y.txt': {data: '333', copyFrom: 'x.txt', flags: ''}, |
| b69ab31 | | | 776 | }, |
| b69ab31 | | | 777 | }, |
| b69ab31 | | | 778 | ], |
| b69ab31 | | | 779 | [ |
| b69ab31 | | | 780 | 'commit', |
| b69ab31 | | | 781 | { |
| b69ab31 | | | 782 | mark: ':r3', |
| b69ab31 | | | 783 | date: [0, 0], |
| b69ab31 | | | 784 | text: 'C', |
| b69ab31 | | | 785 | parents: [':r2'], |
| b69ab31 | | | 786 | predecessors: ['C_NODE'], |
| b69ab31 | | | 787 | files: {'z.txt': null}, |
| b69ab31 | | | 788 | }, |
| b69ab31 | | | 789 | ], |
| b69ab31 | | | 790 | ]); |
| b69ab31 | | | 791 | }); |
| b69ab31 | | | 792 | |
| b69ab31 | | | 793 | it('follows reorder', () => { |
| b69ab31 | | | 794 | // Reorder B and C in the example stack. |
| b69ab31 | | | 795 | const stack = new CommitStackState(exportStack1).reorder([0, 1, 3, 2] as CommitRev[]); |
| b69ab31 | | | 796 | expect(stack.calculateImportStack({goto: 'B_NODE', preserveDirtyFiles: true})).toMatchObject([ |
| b69ab31 | | | 797 | ['commit', {text: 'C'}], |
| b69ab31 | | | 798 | ['commit', {mark: ':r3', text: 'B'}], |
| b69ab31 | | | 799 | ['reset', {mark: ':r3'}], |
| b69ab31 | | | 800 | ]); |
| b69ab31 | | | 801 | }); |
| b69ab31 | | | 802 | |
| b69ab31 | | | 803 | it('stays at the stack top on reorder', () => { |
| b69ab31 | | | 804 | // Reorder B and C in the example stack. |
| b69ab31 | | | 805 | const stack = new CommitStackState(exportStack1).reorder([0, 1, 3, 2] as CommitRev[]); |
| b69ab31 | | | 806 | expect(stack.calculateImportStack({goto: 'C_NODE'})).toMatchObject([ |
| b69ab31 | | | 807 | ['commit', {text: 'C'}], |
| b69ab31 | | | 808 | ['commit', {mark: ':r3', text: 'B'}], |
| b69ab31 | | | 809 | ['goto', {mark: ':r3'}], |
| b69ab31 | | | 810 | ]); |
| b69ab31 | | | 811 | }); |
| b69ab31 | | | 812 | |
| b69ab31 | | | 813 | it('hides dropped commits', () => { |
| b69ab31 | | | 814 | let stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 815 | const revs = stack.revs(); |
| b69ab31 | | | 816 | // Drop the last 2 commits: B and C. |
| b69ab31 | | | 817 | stack = stack.drop(revs[revs.length - 1]).drop(revs[revs.length - 2]); |
| b69ab31 | | | 818 | expect(stack.calculateImportStack()).toMatchObject([ |
| b69ab31 | | | 819 | [ |
| b69ab31 | | | 820 | 'hide', |
| b69ab31 | | | 821 | { |
| b69ab31 | | | 822 | nodes: ['B_NODE', 'C_NODE'], |
| b69ab31 | | | 823 | }, |
| b69ab31 | | | 824 | ], |
| b69ab31 | | | 825 | ]); |
| b69ab31 | | | 826 | }); |
| b69ab31 | | | 827 | |
| b69ab31 | | | 828 | it('produces goto or reset command', () => { |
| b69ab31 | | | 829 | const stack = new CommitStackState(exportStack1).updateEachFile((_rev, file, path) => |
| b69ab31 | | | 830 | path === 'y.txt' ? file.set('data', '333') : file, |
| b69ab31 | | | 831 | ); |
| b69ab31 | | | 832 | expect(stack.calculateImportStack({goto: 3 as CommitRev})).toMatchObject([ |
| b69ab31 | | | 833 | ['commit', {}], |
| b69ab31 | | | 834 | ['commit', {}], |
| b69ab31 | | | 835 | ['goto', {mark: ':r3'}], |
| b69ab31 | | | 836 | ]); |
| b69ab31 | | | 837 | expect( |
| b69ab31 | | | 838 | stack.calculateImportStack({goto: 3 as CommitRev, preserveDirtyFiles: true}), |
| b69ab31 | | | 839 | ).toMatchObject([ |
| b69ab31 | | | 840 | ['commit', {}], |
| b69ab31 | | | 841 | ['commit', {}], |
| b69ab31 | | | 842 | ['reset', {mark: ':r3'}], |
| b69ab31 | | | 843 | ]); |
| b69ab31 | | | 844 | }); |
| b69ab31 | | | 845 | |
| b69ab31 | | | 846 | it('optionally rewrites commit date', () => { |
| b69ab31 | | | 847 | // Swap the last 2 commits. |
| b69ab31 | | | 848 | const stack = new CommitStackState(exportStack1).reorder([0, 1, 3, 2] as CommitRev[]); |
| b69ab31 | | | 849 | expect(stack.calculateImportStack({rewriteDate: 40})).toMatchObject([ |
| b69ab31 | | | 850 | ['commit', {date: [40, 0], text: 'C'}], |
| b69ab31 | | | 851 | ['commit', {date: [40, 0], text: 'B'}], |
| b69ab31 | | | 852 | ]); |
| b69ab31 | | | 853 | }); |
| b69ab31 | | | 854 | |
| b69ab31 | | | 855 | it('setFile drops invalid "copyFrom"s', () => { |
| b69ab31 | | | 856 | // Commit A (x.txt) -> Commit B (y.txt, renamed from x.txt). |
| b69ab31 | | | 857 | const stack = new CommitStackState([ |
| b69ab31 | | | 858 | { |
| b69ab31 | | | 859 | ...exportCommitDefault, |
| b69ab31 | | | 860 | files: {'x.txt': {data: '33'}}, |
| b69ab31 | | | 861 | node: 'A_NODE', |
| b69ab31 | | | 862 | parents: [], |
| b69ab31 | | | 863 | relevantFiles: {'y.txt': null}, |
| b69ab31 | | | 864 | text: 'A', |
| b69ab31 | | | 865 | }, |
| b69ab31 | | | 866 | { |
| b69ab31 | | | 867 | ...exportCommitDefault, |
| b69ab31 | | | 868 | files: {'x.txt': null, 'y.txt': {data: '33', copyFrom: 'x.txt'}}, |
| b69ab31 | | | 869 | node: 'B_NODE', |
| b69ab31 | | | 870 | parents: ['A_NODE'], |
| b69ab31 | | | 871 | text: 'B', |
| b69ab31 | | | 872 | }, |
| b69ab31 | | | 873 | ]); |
| b69ab31 | | | 874 | |
| b69ab31 | | | 875 | // Invalid copyFrom is dropped. |
| b69ab31 | | | 876 | expect( |
| b69ab31 | | | 877 | stack |
| b69ab31 | | | 878 | .setFile(0 as CommitRev, 'x.txt', f => f.set('copyFrom', 'z.txt')) |
| b69ab31 | | | 879 | .getFile(0 as CommitRev, 'x.txt').copyFrom, |
| b69ab31 | | | 880 | ).toBeUndefined(); |
| b69ab31 | | | 881 | |
| b69ab31 | | | 882 | // Creating "y.txt" in the parent commit (0) makes the child commit (1) drop copyFrom of "y.txt". |
| b69ab31 | | | 883 | expect( |
| b69ab31 | | | 884 | stack |
| b69ab31 | | | 885 | .setFile(0 as CommitRev, 'y.txt', f => f.merge({data: '33', flags: ''})) |
| b69ab31 | | | 886 | .getFile(1 as CommitRev, 'y.txt').copyFrom, |
| b69ab31 | | | 887 | ).toBeUndefined(); |
| b69ab31 | | | 888 | |
| b69ab31 | | | 889 | // Dropping "x.txt" in the parent commit (0) makes the child commit (1) not copying from "x.txt". |
| b69ab31 | | | 890 | // The content of "y.txt" is not changed. |
| b69ab31 | | | 891 | const fileY = stack |
| b69ab31 | | | 892 | .setFile(0 as CommitRev, 'x.txt', _f => ABSENT_FILE) |
| b69ab31 | | | 893 | .getFile(1 as CommitRev, 'y.txt'); |
| b69ab31 | | | 894 | expect(fileY.copyFrom).toBeUndefined(); |
| b69ab31 | | | 895 | expect(fileY.data).toBe('33'); |
| b69ab31 | | | 896 | }); |
| b69ab31 | | | 897 | |
| b69ab31 | | | 898 | it('optionally skips wdir()', () => { |
| b69ab31 | | | 899 | const stack = new CommitStackState([ |
| b69ab31 | | | 900 | { |
| b69ab31 | | | 901 | ...exportCommitDefault, |
| b69ab31 | | | 902 | files: { |
| b69ab31 | | | 903 | 'x.txt': {data: '11'}, |
| b69ab31 | | | 904 | }, |
| b69ab31 | | | 905 | node: WDIR_NODE, |
| b69ab31 | | | 906 | parents: [], |
| b69ab31 | | | 907 | text: 'Temp commit', |
| b69ab31 | | | 908 | }, |
| b69ab31 | | | 909 | ]).setFile(0 as CommitRev, 'x.txt', f => f.set('data', '22')); |
| b69ab31 | | | 910 | expect(stack.calculateImportStack()).toMatchInlineSnapshot(` |
| b69ab31 | | | 911 | [ |
| b69ab31 | | | 912 | [ |
| b69ab31 | | | 913 | "commit", |
| b69ab31 | | | 914 | { |
| b69ab31 | | | 915 | "author": "test <test@example.com>", |
| b69ab31 | | | 916 | "date": [ |
| b69ab31 | | | 917 | 0, |
| b69ab31 | | | 918 | 0, |
| b69ab31 | | | 919 | ], |
| b69ab31 | | | 920 | "files": { |
| b69ab31 | | | 921 | "x.txt": { |
| b69ab31 | | | 922 | "data": "22", |
| b69ab31 | | | 923 | "flags": "", |
| b69ab31 | | | 924 | }, |
| b69ab31 | | | 925 | }, |
| b69ab31 | | | 926 | "mark": ":r0", |
| b69ab31 | | | 927 | "parents": [], |
| b69ab31 | | | 928 | "predecessors": [], |
| b69ab31 | | | 929 | "text": "Temp commit", |
| b69ab31 | | | 930 | }, |
| b69ab31 | | | 931 | ], |
| b69ab31 | | | 932 | ] |
| b69ab31 | | | 933 | `); |
| b69ab31 | | | 934 | expect(stack.calculateImportStack({skipWdir: true})).toMatchInlineSnapshot(`[]`); |
| b69ab31 | | | 935 | }); |
| b69ab31 | | | 936 | }); |
| b69ab31 | | | 937 | |
| b69ab31 | | | 938 | describe('denseSubStack', () => { |
| b69ab31 | | | 939 | it('provides bottomFiles', () => { |
| b69ab31 | | | 940 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 941 | let subStack = stack.denseSubStack(List([3 as CommitRev])); // C |
| b69ab31 | | | 942 | // The bottom files contains z (deleted) and its content is before deletion. |
| b69ab31 | | | 943 | expect([...subStack.bottomFiles.keys()].sort()).toEqual(['z.txt']); |
| b69ab31 | | | 944 | expect(subStack.bottomFiles.get('z.txt')?.data).toBe('22'); |
| b69ab31 | | | 945 | |
| b69ab31 | | | 946 | subStack = stack.denseSubStack(List([2, 3] as CommitRev[])); // B, C |
| b69ab31 | | | 947 | // The bottom files contains x (deleted), y (modified) and z (deleted). |
| b69ab31 | | | 948 | expect([...subStack.bottomFiles.keys()].sort()).toEqual(['x.txt', 'y.txt', 'z.txt']); |
| b69ab31 | | | 949 | }); |
| b69ab31 | | | 950 | |
| b69ab31 | | | 951 | it('marks all files at every commit as changed', () => { |
| b69ab31 | | | 952 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 953 | const subStack = stack.denseSubStack(List([2, 3] as CommitRev[])); // B, C |
| b69ab31 | | | 954 | // All commits (B, C) should have 3 files (x.txt, y.txt, z.txt) marked as "changed". |
| b69ab31 | | | 955 | expect(subStack.stack.map(c => c.files.size).toJS()).toEqual([3, 3]); |
| b69ab31 | | | 956 | // All file stacks (x.txt, y.txt, z.txt) should have 3 revs (bottomFile, B, C). |
| b69ab31 | | | 957 | expect(subStack.fileStacks.map(f => f.revLength).toJS()).toEqual([3, 3, 3]); |
| b69ab31 | | | 958 | }); |
| b69ab31 | | | 959 | }); |
| b69ab31 | | | 960 | |
| b69ab31 | | | 961 | describe('insertEmpty', () => { |
| b69ab31 | | | 962 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 963 | const getRevs = (stack: CommitStackState) => |
| b69ab31 | | | 964 | stack.stack.map(c => [c.rev, c.parents.toArray()]).toArray(); |
| b69ab31 | | | 965 | |
| b69ab31 | | | 966 | it('updates revs of commits', () => { |
| b69ab31 | | | 967 | expect(getRevs(stack)).toEqual([ |
| b69ab31 | | | 968 | [0, []], |
| b69ab31 | | | 969 | [1, [0]], |
| b69ab31 | | | 970 | [2, [1]], |
| b69ab31 | | | 971 | [3, [2]], |
| b69ab31 | | | 972 | ]); |
| b69ab31 | | | 973 | expect(getRevs(stack.insertEmpty(2 as CommitRev, 'foo'))).toEqual([ |
| b69ab31 | | | 974 | [0, []], |
| b69ab31 | | | 975 | [1, [0]], |
| b69ab31 | | | 976 | [2, [1]], |
| b69ab31 | | | 977 | [3, [2]], |
| b69ab31 | | | 978 | [4, [3]], |
| b69ab31 | | | 979 | ]); |
| b69ab31 | | | 980 | }); |
| b69ab31 | | | 981 | |
| b69ab31 | | | 982 | it('inserts at stack top', () => { |
| b69ab31 | | | 983 | expect(getRevs(stack.insertEmpty(4 as CommitRev, 'foo'))).toEqual([ |
| b69ab31 | | | 984 | [0, []], |
| b69ab31 | | | 985 | [1, [0]], |
| b69ab31 | | | 986 | [2, [1]], |
| b69ab31 | | | 987 | [3, [2]], |
| b69ab31 | | | 988 | [4, [3]], |
| b69ab31 | | | 989 | ]); |
| b69ab31 | | | 990 | }); |
| b69ab31 | | | 991 | |
| b69ab31 | | | 992 | it('uses the provided commit message', () => { |
| b69ab31 | | | 993 | const msg = 'provided message\nfoobar'; |
| b69ab31 | | | 994 | ([0, 2, 4] as CommitRev[]).forEach(i => { |
| b69ab31 | | | 995 | expect(stack.insertEmpty(i, msg).stack.get(i)?.text).toBe(msg); |
| b69ab31 | | | 996 | }); |
| b69ab31 | | | 997 | }); |
| b69ab31 | | | 998 | |
| b69ab31 | | | 999 | it('provides unique keys for inserted commits', () => { |
| b69ab31 | | | 1000 | const newStack = stack |
| b69ab31 | | | 1001 | .insertEmpty(1 as CommitRev, '') |
| b69ab31 | | | 1002 | .insertEmpty(1 as CommitRev, '') |
| b69ab31 | | | 1003 | .insertEmpty(1 as CommitRev, ''); |
| b69ab31 | | | 1004 | const keys = newStack.stack.map(c => c.key); |
| b69ab31 | | | 1005 | expect(keys.size).toBe(ImSet(keys).size); |
| b69ab31 | | | 1006 | }); |
| b69ab31 | | | 1007 | |
| b69ab31 | | | 1008 | // The "originalNodes" are useful for split to set predecessors correctly. |
| b69ab31 | | | 1009 | it('preserves the originalNodes with splitFromRev', () => { |
| b69ab31 | | | 1010 | ([1, 4] as CommitRev[]).forEach(i => { |
| b69ab31 | | | 1011 | const newStack = stack.insertEmpty(i, '', 2 as CommitRev); |
| b69ab31 | | | 1012 | expect(newStack.get(i)?.originalNodes).toBe(stack.get(2 as CommitRev)?.originalNodes); |
| b69ab31 | | | 1013 | expect(newStack.get(i)?.originalNodes?.isEmpty()).toBeFalsy(); |
| b69ab31 | | | 1014 | const anotherStack = stack.insertEmpty(i, ''); |
| b69ab31 | | | 1015 | expect(anotherStack.get(i)?.originalNodes?.isEmpty()).toBeTruthy(); |
| b69ab31 | | | 1016 | }); |
| b69ab31 | | | 1017 | }); |
| b69ab31 | | | 1018 | }); |
| b69ab31 | | | 1019 | |
| b69ab31 | | | 1020 | describe('applySubStack', () => { |
| b69ab31 | | | 1021 | const stack = new CommitStackState(exportStack1); |
| b69ab31 | | | 1022 | const subStack = stack.denseSubStack(List([2, 3] as CommitRev[])); |
| b69ab31 | | | 1023 | const emptyStack = subStack.set('stack', List()); |
| b69ab31 | | | 1024 | |
| b69ab31 | | | 1025 | const getChangedFiles = (state: CommitStackState, rev: number): Array<string> => { |
| b69ab31 | | | 1026 | return [...nullthrows(state.stack.get(rev as CommitRev)).files.keys()].sort(); |
| b69ab31 | | | 1027 | }; |
| b69ab31 | | | 1028 | |
| b69ab31 | | | 1029 | it('optimizes file changes by removing unmodified changes', () => { |
| b69ab31 | | | 1030 | const newStack = stack.applySubStack(2 as CommitRev, 4 as CommitRev, subStack); |
| b69ab31 | | | 1031 | expect(newStack.stack.size).toBe(stack.stack.size); |
| b69ab31 | | | 1032 | // The original `stack` does not have unmodified changes. |
| b69ab31 | | | 1033 | // To verify that `newStack` does not have unmodified changes, check it |
| b69ab31 | | | 1034 | // against the original `stack`. |
| b69ab31 | | | 1035 | stack.revs().forEach(i => { |
| b69ab31 | | | 1036 | expect(getChangedFiles(newStack, i)).toEqual(getChangedFiles(stack, i)); |
| b69ab31 | | | 1037 | }); |
| b69ab31 | | | 1038 | }); |
| b69ab31 | | | 1039 | |
| b69ab31 | | | 1040 | it('drops empty commits at the end of subStack', () => { |
| b69ab31 | | | 1041 | // Change the 2nd commit in subStack to empty. |
| b69ab31 | | | 1042 | const newSubStack = subStack.set( |
| b69ab31 | | | 1043 | 'stack', |
| b69ab31 | | | 1044 | subStack.stack.setIn([1, 'files'], nullthrows(subStack.stack.get(0)).files), |
| b69ab31 | | | 1045 | ); |
| b69ab31 | | | 1046 | // `applySubStack` should drop the 2nd commit in `newSubStack`. |
| b69ab31 | | | 1047 | const newStack = stack.applySubStack(2 as CommitRev, 4 as CommitRev, newSubStack); |
| b69ab31 | | | 1048 | newStack.assertRevOrder(); |
| b69ab31 | | | 1049 | expect(newStack.stack.size).toBe(stack.stack.size - 1); |
| b69ab31 | | | 1050 | }); |
| b69ab31 | | | 1051 | |
| b69ab31 | | | 1052 | it('rewrites revs for the remaining of the stack', () => { |
| b69ab31 | | | 1053 | const newStack = stack.applySubStack(1 as CommitRev, 2 as CommitRev, emptyStack); |
| b69ab31 | | | 1054 | newStack.assertRevOrder(); |
| b69ab31 | | | 1055 | [1, 2].forEach(i => { |
| b69ab31 | | | 1056 | expect(newStack.stack.get(i)?.toJS()).toMatchObject({rev: i, parents: [i - 1]}); |
| b69ab31 | | | 1057 | }); |
| b69ab31 | | | 1058 | }); |
| b69ab31 | | | 1059 | |
| b69ab31 | | | 1060 | it('rewrites revs for the inserted stack', () => { |
| b69ab31 | | | 1061 | const newStack = stack.applySubStack(2 as CommitRev, 3 as CommitRev, subStack); |
| b69ab31 | | | 1062 | newStack.assertRevOrder(); |
| b69ab31 | | | 1063 | [2, 3, 4].forEach(i => { |
| b69ab31 | | | 1064 | expect(newStack.stack.get(i)?.toJS()).toMatchObject({rev: i, parents: [i - 1]}); |
| b69ab31 | | | 1065 | }); |
| b69ab31 | | | 1066 | }); |
| b69ab31 | | | 1067 | |
| b69ab31 | | | 1068 | it('preserves file contents of the old stack', () => { |
| b69ab31 | | | 1069 | // Add a file 'x.txt' deleted by the original stack. |
| b69ab31 | | | 1070 | const newSubStack = subStack.set( |
| b69ab31 | | | 1071 | 'stack', |
| b69ab31 | | | 1072 | List([ |
| b69ab31 | | | 1073 | CommitState({ |
| b69ab31 | | | 1074 | key: 'foo', |
| b69ab31 | | | 1075 | files: ImMap([['x.txt', stack.getFile(1 as CommitRev, 'x.txt')]]), |
| b69ab31 | | | 1076 | }), |
| b69ab31 | | | 1077 | ]), |
| b69ab31 | | | 1078 | ); |
| b69ab31 | | | 1079 | const newStack = stack.applySubStack(1 as CommitRev, 3 as CommitRev, newSubStack); |
| b69ab31 | | | 1080 | |
| b69ab31 | | | 1081 | // 'y.txt' was added by the old stack, not the new stack. So it is re-added |
| b69ab31 | | | 1082 | // to preserve its old content. |
| b69ab31 | | | 1083 | // 'x.txt' was added by the new stack, deleted by the old stack. So it is |
| b69ab31 | | | 1084 | // re-deleted. |
| b69ab31 | | | 1085 | expect(getChangedFiles(newStack, 2)).toEqual(['x.txt', 'y.txt', 'z.txt']); |
| b69ab31 | | | 1086 | expect(newStack.getFile(2 as CommitRev, 'y.txt').data).toBe('33'); |
| b69ab31 | | | 1087 | expect(newStack.getFile(2 as CommitRev, 'x.txt')).toBe(ABSENT_FILE); |
| b69ab31 | | | 1088 | }); |
| b69ab31 | | | 1089 | |
| b69ab31 | | | 1090 | it('update keys to avoid conflict', () => { |
| b69ab31 | | | 1091 | const oldKey = nullthrows(stack.stack.get(1)).key; |
| b69ab31 | | | 1092 | const newSubStack = subStack.set('stack', subStack.stack.setIn([0, 'key'], oldKey)); |
| b69ab31 | | | 1093 | const newStack = stack.applySubStack(2 as CommitRev, 3 as CommitRev, newSubStack); |
| b69ab31 | | | 1094 | |
| b69ab31 | | | 1095 | // Keys are still unique. |
| b69ab31 | | | 1096 | const keys = newStack.stack.map(c => c.key); |
| b69ab31 | | | 1097 | const keysSet = ImSet(keys); |
| b69ab31 | | | 1098 | expect(keys.size).toBe(keysSet.size); |
| b69ab31 | | | 1099 | }); |
| b69ab31 | | | 1100 | |
| b69ab31 | | | 1101 | it('drops ABSENT flag if content is not empty', () => { |
| b69ab31 | | | 1102 | // x.txt was deleted by subStack rev 0 (B). We are moving it to be deleted by rev 1 (C). |
| b69ab31 | | | 1103 | expect(subStack.getFile(0 as CommitRev, 'x.txt').flags).toBe(ABSENT_FILE.flags); |
| b69ab31 | | | 1104 | // To break the deletion into done by 2 commits, we edit the file stack of 'x.txt'. |
| b69ab31 | | | 1105 | const fileIdx = nullthrows( |
| b69ab31 | | | 1106 | subStack.commitToFile.get(CommitIdx({rev: 0 as CommitRev, path: 'x.txt'})), |
| b69ab31 | | | 1107 | ).fileIdx; |
| b69ab31 | | | 1108 | const fileStack = nullthrows(subStack.fileStacks.get(fileIdx)); |
| b69ab31 | | | 1109 | // The file stack has 3 revs: (base, before deletion), (deleted at rev 0), (deleted at rev 1). |
| b69ab31 | | | 1110 | expect(fileStack.convertToPlainText().toArray()).toEqual(['33', '', '']); |
| b69ab31 | | | 1111 | const newFileStack = new FileStackState(['33', '3', '']); |
| b69ab31 | | | 1112 | const newSubStack = subStack.setFileStack(fileIdx, newFileStack); |
| b69ab31 | | | 1113 | expect(newSubStack.getUtf8Data(newSubStack.getFile(0 as CommitRev, 'x.txt'))).toBe('3'); |
| b69ab31 | | | 1114 | // Apply the file stack back to the main stack. |
| b69ab31 | | | 1115 | const newStack = stack.applySubStack(2 as CommitRev, 4 as CommitRev, newSubStack); |
| b69ab31 | | | 1116 | expect(newStack.stack.size).toBe(4); |
| b69ab31 | | | 1117 | // Check that x.txt in rev 2 (B) is '3', not absent. |
| b69ab31 | | | 1118 | const file = newStack.getFile(2 as CommitRev, 'x.txt'); |
| b69ab31 | | | 1119 | expect(file.data).toBe('3'); |
| b69ab31 | | | 1120 | expect(file.flags ?? '').not.toContain(ABSENT_FILE.flags); |
| b69ab31 | | | 1121 | |
| b69ab31 | | | 1122 | // Compare the old and new file stacks. |
| b69ab31 | | | 1123 | // - x.txt deletion is now by commit 'C', not 'B'. |
| b69ab31 | | | 1124 | // - x.txt -> y.txt rename is preserved. |
| b69ab31 | | | 1125 | expect(stack.describeFileStacks(true)).toEqual([ |
| b69ab31 | | | 1126 | '0:./x.txt 1:A/x.txt(33) 2:B/y.txt(33)', |
| b69ab31 | | | 1127 | '0:./z.txt(11) 1:A/z.txt(22) 2:C/z.txt', |
| b69ab31 | | | 1128 | ]); |
| b69ab31 | | | 1129 | expect(newStack.describeFileStacks(true)).toEqual([ |
| b69ab31 | | | 1130 | '0:./x.txt 1:A/x.txt(33) 2:B/x.txt(3) 3:C/x.txt', |
| b69ab31 | | | 1131 | '0:./z.txt(11) 1:A/z.txt(22) 2:C/z.txt', |
| b69ab31 | | | 1132 | '0:A/x.txt(33) 1:B/y.txt(33)', |
| b69ab31 | | | 1133 | ]); |
| b69ab31 | | | 1134 | }); |
| b69ab31 | | | 1135 | |
| b69ab31 | | | 1136 | it('does not add ABSENT flag if content becomes empty', () => { |
| b69ab31 | | | 1137 | // This was a herustics when `flags` are not handled properly. Now it is no longer needed. |
| b69ab31 | | | 1138 | // y.txt was added by subStack rev 0 (B). We are moving it to be added by rev 1 (C). |
| b69ab31 | | | 1139 | const fileIdx = nullthrows( |
| b69ab31 | | | 1140 | subStack.commitToFile.get(CommitIdx({rev: 0 as CommitRev, path: 'y.txt'})), |
| b69ab31 | | | 1141 | ).fileIdx; |
| b69ab31 | | | 1142 | const fileStack = nullthrows(subStack.fileStacks.get(fileIdx)); |
| b69ab31 | | | 1143 | // The file stack has 3 revs: (base, before add), (add by rev 0), (unchanged by rev 1). |
| b69ab31 | | | 1144 | expect(fileStack.convertToPlainText().toArray()).toEqual(['', '33', '33']); |
| b69ab31 | | | 1145 | const newFileStack = new FileStackState(['', '', '33']); |
| b69ab31 | | | 1146 | const newSubStack = subStack.setFileStack(fileIdx, newFileStack); |
| b69ab31 | | | 1147 | // Apply the file stack back to the main stack. |
| b69ab31 | | | 1148 | const newStack = stack.applySubStack(2 as CommitRev, 4 as CommitRev, newSubStack); |
| b69ab31 | | | 1149 | // Check that y.txt in rev 2 (B) is absent, not just empty. |
| b69ab31 | | | 1150 | const file = newStack.getFile(2 as CommitRev, 'y.txt'); |
| b69ab31 | | | 1151 | expect(file.data).toBe(''); |
| b69ab31 | | | 1152 | expect(file.flags).toBe(''); |
| b69ab31 | | | 1153 | }); |
| b69ab31 | | | 1154 | }); |
| b69ab31 | | | 1155 | |
| b69ab31 | | | 1156 | describe('absorb', () => { |
| b69ab31 | | | 1157 | const absorbStack1: ExportStack = [ |
| b69ab31 | | | 1158 | { |
| b69ab31 | | | 1159 | ...exportCommitDefault, |
| b69ab31 | | | 1160 | immutable: true, |
| b69ab31 | | | 1161 | node: 'Z_NODE', |
| b69ab31 | | | 1162 | relevantFiles: { |
| b69ab31 | | | 1163 | 'seq.txt': {data: '0\n'}, |
| b69ab31 | | | 1164 | 'rename_from.txt': null, |
| b69ab31 | | | 1165 | }, |
| b69ab31 | | | 1166 | requested: false, |
| b69ab31 | | | 1167 | text: 'PublicCommit', |
| b69ab31 | | | 1168 | }, |
| b69ab31 | | | 1169 | { |
| b69ab31 | | | 1170 | ...exportCommitDefault, |
| b69ab31 | | | 1171 | files: { |
| b69ab31 | | | 1172 | 'seq.txt': {data: '0\n1\n'}, |
| b69ab31 | | | 1173 | 'rename_from.txt': {data: '1\n'}, |
| b69ab31 | | | 1174 | }, |
| b69ab31 | | | 1175 | node: 'A_NODE', |
| b69ab31 | | | 1176 | parents: ['Z_NODE'], |
| b69ab31 | | | 1177 | relevantFiles: {'rename_to.txt': null}, |
| b69ab31 | | | 1178 | text: 'CommitA', |
| b69ab31 | | | 1179 | }, |
| b69ab31 | | | 1180 | { |
| b69ab31 | | | 1181 | ...exportCommitDefault, |
| b69ab31 | | | 1182 | files: { |
| b69ab31 | | | 1183 | 'seq.txt': {data: '0\n1\n2\n'}, |
| b69ab31 | | | 1184 | 'rename_to.txt': {copyFrom: 'rename_from.txt', data: '1\n'}, |
| b69ab31 | | | 1185 | 'rename_from.txt': null, |
| b69ab31 | | | 1186 | }, |
| b69ab31 | | | 1187 | node: 'B_NODE', |
| b69ab31 | | | 1188 | parents: ['A_NODE'], |
| b69ab31 | | | 1189 | text: 'CommitB', |
| b69ab31 | | | 1190 | }, |
| b69ab31 | | | 1191 | { |
| b69ab31 | | | 1192 | ...exportCommitDefault, |
| b69ab31 | | | 1193 | // Working copy changes. 012 => xyz. |
| b69ab31 | | | 1194 | files: { |
| b69ab31 | | | 1195 | 'seq.txt': {data: 'x\ny\nz\n'}, |
| b69ab31 | | | 1196 | 'rename_to.txt': {data: 'p\n'}, |
| b69ab31 | | | 1197 | }, |
| b69ab31 | | | 1198 | node: 'WDIR', |
| b69ab31 | | | 1199 | parents: ['B_NODE'], |
| b69ab31 | | | 1200 | text: 'Wdir', |
| b69ab31 | | | 1201 | }, |
| b69ab31 | | | 1202 | ]; |
| b69ab31 | | | 1203 | |
| b69ab31 | | | 1204 | it('can prepare for absorb', () => { |
| b69ab31 | | | 1205 | const stack = new CommitStackState(absorbStack1); |
| b69ab31 | | | 1206 | expect(stack.describeFileStacks()).toMatchInlineSnapshot(` |
| b69ab31 | | | 1207 | [ |
| b69ab31 | | | 1208 | "0:./rename_from.txt 1:CommitA/rename_from.txt(1↵) 2:CommitB/rename_to.txt(1↵) 3:Wdir/rename_to.txt(p↵)", |
| b69ab31 | | | 1209 | "0:./seq.txt(0↵) 1:CommitA/seq.txt(0↵1↵) 2:CommitB/seq.txt(0↵1↵2↵) 3:Wdir/seq.txt(x↵y↵z↵)", |
| b69ab31 | | | 1210 | ] |
| b69ab31 | | | 1211 | `); |
| b69ab31 | | | 1212 | const stackWithAbsorb = stack.analyseAbsorb(); |
| b69ab31 | | | 1213 | expect(stackWithAbsorb.hasPendingAbsorb()).toBeTruthy(); |
| b69ab31 | | | 1214 | // The "1 => p" change in "rename_to.txt" is absorbed following file renames into rename_from.txt. |
| b69ab31 | | | 1215 | // The "1 => y", "2 => z" changes in "seq.txt" are absorbed to CommitA and CommitB. |
| b69ab31 | | | 1216 | // The "0 => x" change in "seq.txt" is left in the working copy as "0" is an immutable line (public commit). |
| b69ab31 | | | 1217 | expect(stackWithAbsorb.describeFileStacks()).toMatchInlineSnapshot(` |
| b69ab31 | | | 1218 | [ |
| b69ab31 | | | 1219 | "0:./rename_from.txt 1:CommitA/rename_from.txt(1↵;absorbed:p↵)", |
| b69ab31 | | | 1220 | "0:./seq.txt(0↵) 1:CommitA/seq.txt(0↵1↵;absorbed:0↵y↵) 2:CommitB/seq.txt(0↵y↵2↵;absorbed:0↵y↵z↵) 3:Wdir/seq.txt(0↵y↵z↵;absorbed:x↵y↵z↵)", |
| b69ab31 | | | 1221 | ] |
| b69ab31 | | | 1222 | `); |
| b69ab31 | | | 1223 | expect(describeAbsorbExtra(stackWithAbsorb)).toMatchInlineSnapshot(` |
| b69ab31 | | | 1224 | { |
| b69ab31 | | | 1225 | "0": [ |
| b69ab31 | | | 1226 | "0: -1↵ +p↵ Selected=1 Introduced=1", |
| b69ab31 | | | 1227 | ], |
| b69ab31 | | | 1228 | "1": [ |
| b69ab31 | | | 1229 | "0: -0↵ +x↵ Introduced=0", |
| b69ab31 | | | 1230 | "1: -1↵ +y↵ Selected=1 Introduced=1", |
| b69ab31 | | | 1231 | "2: -2↵ +z↵ Selected=2 Introduced=2", |
| b69ab31 | | | 1232 | ], |
| b69ab31 | | | 1233 | } |
| b69ab31 | | | 1234 | `); |
| b69ab31 | | | 1235 | }); |
| b69ab31 | | | 1236 | |
| b69ab31 | | | 1237 | const absorbStack2: ExportStack = [ |
| b69ab31 | | | 1238 | { |
| b69ab31 | | | 1239 | ...exportCommitDefault, |
| b69ab31 | | | 1240 | immutable: true, |
| b69ab31 | | | 1241 | node: 'Z_NODE', |
| b69ab31 | | | 1242 | relevantFiles: { |
| b69ab31 | | | 1243 | 'a.txt': null, |
| b69ab31 | | | 1244 | }, |
| b69ab31 | | | 1245 | requested: false, |
| b69ab31 | | | 1246 | text: 'PublicCommit', |
| b69ab31 | | | 1247 | }, |
| b69ab31 | | | 1248 | { |
| b69ab31 | | | 1249 | ...exportCommitDefault, |
| b69ab31 | | | 1250 | files: { |
| b69ab31 | | | 1251 | 'a.txt': {data: 'a1\na2\na3\n'}, |
| b69ab31 | | | 1252 | }, |
| b69ab31 | | | 1253 | node: 'A_NODE', |
| b69ab31 | | | 1254 | parents: ['Z_NODE'], |
| b69ab31 | | | 1255 | relevantFiles: {'b.txt': null}, |
| b69ab31 | | | 1256 | text: 'CommitA', |
| b69ab31 | | | 1257 | }, |
| b69ab31 | | | 1258 | { |
| b69ab31 | | | 1259 | ...exportCommitDefault, |
| b69ab31 | | | 1260 | files: { |
| b69ab31 | | | 1261 | 'b.txt': {data: 'b1\nb2\nb3\n'}, |
| b69ab31 | | | 1262 | }, |
| b69ab31 | | | 1263 | relevantFiles: { |
| b69ab31 | | | 1264 | 'a.txt': {data: 'a1\na2\na3\n'}, |
| b69ab31 | | | 1265 | 'c.txt': {data: 'c1\nc2\nc3\n'}, |
| b69ab31 | | | 1266 | }, |
| b69ab31 | | | 1267 | node: 'B_NODE', |
| b69ab31 | | | 1268 | parents: ['A_NODE'], |
| b69ab31 | | | 1269 | text: 'CommitB', |
| b69ab31 | | | 1270 | }, |
| b69ab31 | | | 1271 | { |
| b69ab31 | | | 1272 | ...exportCommitDefault, |
| b69ab31 | | | 1273 | files: { |
| b69ab31 | | | 1274 | 'a.txt': {data: 'a1\na2\na3\nx1\n'}, |
| b69ab31 | | | 1275 | 'b.txt': {data: 'b1\nb2\nb3\ny1\n'}, |
| b69ab31 | | | 1276 | 'c.txt': {data: 'c1\nc2\nc3\nz1\n'}, |
| b69ab31 | | | 1277 | }, |
| b69ab31 | | | 1278 | node: 'C_NODE', |
| b69ab31 | | | 1279 | parents: ['B_NODE'], |
| b69ab31 | | | 1280 | text: 'CommitC', |
| b69ab31 | | | 1281 | }, |
| b69ab31 | | | 1282 | { |
| b69ab31 | | | 1283 | ...exportCommitDefault, |
| b69ab31 | | | 1284 | files: { |
| b69ab31 | | | 1285 | 'a.txt': {data: 'A1\na2\na3\nX1\n'}, |
| b69ab31 | | | 1286 | 'b.txt': {data: 'B1\nb2\nb3\nY1\n'}, |
| b69ab31 | | | 1287 | 'c.txt': {data: 'C1\nC2\nc3\nz1\n'}, |
| b69ab31 | | | 1288 | }, |
| b69ab31 | | | 1289 | node: 'WDIR', |
| b69ab31 | | | 1290 | parents: ['C_NODE'], |
| b69ab31 | | | 1291 | text: 'Wdir', |
| b69ab31 | | | 1292 | }, |
| b69ab31 | | | 1293 | ]; |
| b69ab31 | | | 1294 | |
| b69ab31 | | | 1295 | it('provides absorb candidate revs', () => { |
| b69ab31 | | | 1296 | const stack = new CommitStackState(absorbStack2).analyseAbsorb(); |
| b69ab31 | | | 1297 | expect(describeAbsorbExtra(stack)).toMatchInlineSnapshot(` |
| b69ab31 | | | 1298 | { |
| b69ab31 | | | 1299 | "0": [ |
| b69ab31 | | | 1300 | "0: -a1↵ +A1↵ Selected=1 Introduced=1", |
| b69ab31 | | | 1301 | "1: -x1↵ +X1↵ Selected=2 Introduced=2", |
| b69ab31 | | | 1302 | ], |
| b69ab31 | | | 1303 | "1": [ |
| b69ab31 | | | 1304 | "0: -b1↵ +B1↵ Selected=1 Introduced=1", |
| b69ab31 | | | 1305 | "1: -y1↵ +Y1↵ Selected=2 Introduced=2", |
| b69ab31 | | | 1306 | ], |
| b69ab31 | | | 1307 | "2": [ |
| b69ab31 | | | 1308 | "0: -c1↵ c2↵ +C1↵ C2↵ Introduced=0", |
| b69ab31 | | | 1309 | ], |
| b69ab31 | | | 1310 | } |
| b69ab31 | | | 1311 | `); |
| b69ab31 | | | 1312 | expect(describeAbsorbEditCommits(stack)).toEqual([ |
| b69ab31 | | | 1313 | { |
| b69ab31 | | | 1314 | // The "a1 -> A1" change is applied to "CommitA" which introduced "a". |
| b69ab31 | | | 1315 | // It can be applied to "CommitC" which changes "a.txt" too. |
| b69ab31 | | | 1316 | // It cannot be applied to "CommitB" which didn't change "a.txt" (and |
| b69ab31 | | | 1317 | // therefore not tracked by linelog). |
| b69ab31 | | | 1318 | id: 'a.txt/0', |
| b69ab31 | | | 1319 | diff: ['a1↵', 'A1↵'], |
| b69ab31 | | | 1320 | selected: 'CommitA', |
| b69ab31 | | | 1321 | candidates: ['CommitA', 'CommitC', 'Wdir'], |
| b69ab31 | | | 1322 | }, |
| b69ab31 | | | 1323 | { |
| b69ab31 | | | 1324 | // The "x1 -> X1" change is applied to "CommitC" which introduced "c". |
| b69ab31 | | | 1325 | id: 'a.txt/1', |
| b69ab31 | | | 1326 | diff: ['x1↵', 'X1↵'], |
| b69ab31 | | | 1327 | selected: 'CommitC', |
| b69ab31 | | | 1328 | candidates: ['CommitC', 'Wdir'], |
| b69ab31 | | | 1329 | }, |
| b69ab31 | | | 1330 | { |
| b69ab31 | | | 1331 | // The "b1 -> B1" change belongs to CommitB. |
| b69ab31 | | | 1332 | id: 'b.txt/0', |
| b69ab31 | | | 1333 | diff: ['b1↵', 'B1↵'], |
| b69ab31 | | | 1334 | selected: 'CommitB', |
| b69ab31 | | | 1335 | candidates: ['CommitB', 'CommitC', 'Wdir'], |
| b69ab31 | | | 1336 | }, |
| b69ab31 | | | 1337 | { |
| b69ab31 | | | 1338 | // The "y1 -> Y1" change belongs to CommitB. |
| b69ab31 | | | 1339 | id: 'b.txt/1', |
| b69ab31 | | | 1340 | diff: ['y1↵', 'Y1↵'], |
| b69ab31 | | | 1341 | selected: 'CommitC', |
| b69ab31 | | | 1342 | candidates: ['CommitC', 'Wdir'], |
| b69ab31 | | | 1343 | }, |
| b69ab31 | | | 1344 | { |
| b69ab31 | | | 1345 | // The "c1c2 -> C1C2" change is not automatically absorbed, since |
| b69ab31 | | | 1346 | // "ccc" is public/immutable. |
| b69ab31 | | | 1347 | id: 'c.txt/0', |
| b69ab31 | | | 1348 | diff: ['c1↵c2↵', 'C1↵C2↵'], |
| b69ab31 | | | 1349 | selected: undefined, |
| b69ab31 | | | 1350 | // CommitC is a candidate because it modifies c.txt. |
| b69ab31 | | | 1351 | candidates: ['CommitC', 'Wdir'], |
| b69ab31 | | | 1352 | }, |
| b69ab31 | | | 1353 | ]); |
| b69ab31 | | | 1354 | }); |
| b69ab31 | | | 1355 | |
| b69ab31 | | | 1356 | it('updates absorb destination commit', () => { |
| b69ab31 | | | 1357 | const stack = new CommitStackState(absorbStack2).analyseAbsorb(); |
| b69ab31 | | | 1358 | // Current state. Note the "-a1 +A1" has "Selected=1" where 1 is the "file stack rev". |
| b69ab31 | | | 1359 | expect(stack.absorbExtra.get(0)?.get(0)?.selectedRev).toBe(1); |
| b69ab31 | | | 1360 | // Move the "a1 -> A1" change from CommitA to CommitC. |
| b69ab31 | | | 1361 | // See the above test's "describeAbsorbExtra" to confirm that "a1 -> A1" |
| b69ab31 | | | 1362 | // has fileIdx=0 and absorbEditId=0. |
| b69ab31 | | | 1363 | // CommitC has rev=3. |
| b69ab31 | | | 1364 | const newStack = stack.setAbsorbEditDestination(0, 0, 3 as CommitRev); |
| b69ab31 | | | 1365 | // "-a1 +A1" now has "Selected=2": |
| b69ab31 | | | 1366 | expect(newStack.absorbExtra.get(0)?.get(0)?.selectedRev).toBe(2); |
| b69ab31 | | | 1367 | expect(describeAbsorbExtra(newStack)).toMatchInlineSnapshot(` |
| b69ab31 | | | 1368 | { |
| b69ab31 | | | 1369 | "0": [ |
| b69ab31 | | | 1370 | "0: -a1↵ +A1↵ Selected=2 Introduced=1", |
| b69ab31 | | | 1371 | "1: -x1↵ +X1↵ Selected=2 Introduced=2", |
| b69ab31 | | | 1372 | ], |
| b69ab31 | | | 1373 | "1": [ |
| b69ab31 | | | 1374 | "0: -b1↵ +B1↵ Selected=1 Introduced=1", |
| b69ab31 | | | 1375 | "1: -y1↵ +Y1↵ Selected=2 Introduced=2", |
| b69ab31 | | | 1376 | ], |
| b69ab31 | | | 1377 | "2": [ |
| b69ab31 | | | 1378 | "0: -c1↵ c2↵ +C1↵ C2↵ Introduced=0", |
| b69ab31 | | | 1379 | ], |
| b69ab31 | | | 1380 | } |
| b69ab31 | | | 1381 | `); |
| b69ab31 | | | 1382 | // The A1 is now absorbed at CommitC. |
| b69ab31 | | | 1383 | expect(newStack.describeFileStacks()).toMatchInlineSnapshot(` |
| b69ab31 | | | 1384 | [ |
| b69ab31 | | | 1385 | "0:./a.txt 1:CommitA/a.txt(a1↵a2↵a3↵) 2:CommitC/a.txt(a1↵a2↵a3↵x1↵;absorbed:A1↵a2↵a3↵X1↵)", |
| b69ab31 | | | 1386 | "0:./b.txt 1:CommitB/b.txt(b1↵b2↵b3↵;absorbed:B1↵b2↵b3↵) 2:CommitC/b.txt(B1↵b2↵b3↵y1↵;absorbed:B1↵b2↵b3↵Y1↵)", |
| b69ab31 | | | 1387 | "0:./c.txt(c1↵c2↵c3↵) 1:CommitC/c.txt(c1↵c2↵c3↵z1↵) 2:Wdir/c.txt(c1↵c2↵c3↵z1↵;absorbed:C1↵C2↵c3↵z1↵)", |
| b69ab31 | | | 1388 | ] |
| b69ab31 | | | 1389 | `); |
| b69ab31 | | | 1390 | // It can be moved back. |
| b69ab31 | | | 1391 | const newStack2 = newStack.setAbsorbEditDestination(0, 0, 1 as CommitRev); |
| b69ab31 | | | 1392 | expect(newStack2.absorbExtra.get(0)?.get(0)?.selectedRev).toBe(1); |
| b69ab31 | | | 1393 | // It can be moved to wdir(), the top rev. |
| b69ab31 | | | 1394 | const topRev = nullthrows(newStack2.revs().at(-1)); |
| b69ab31 | | | 1395 | const newStack3 = newStack2.setAbsorbEditDestination(0, 0, topRev); |
| b69ab31 | | | 1396 | expect(newStack3.getAbsorbCommitRevs(0, 0).selectedRev).toBe(topRev); |
| b69ab31 | | | 1397 | }); |
| b69ab31 | | | 1398 | |
| b69ab31 | | | 1399 | it('updates getUtf8 with pending absorb edits', () => { |
| b69ab31 | | | 1400 | const stack1 = new CommitStackState(absorbStack2).useFileStack(); |
| b69ab31 | | | 1401 | const get = ( |
| b69ab31 | | | 1402 | stack: CommitStackState, |
| b69ab31 | | | 1403 | fileIdx: number, |
| b69ab31 | | | 1404 | fileRev: number, |
| b69ab31 | | | 1405 | considerAbsorb?: boolean, |
| b69ab31 | | | 1406 | ) => |
| b69ab31 | | | 1407 | replaceNewLines( |
| b69ab31 | | | 1408 | stack.getUtf8Data( |
| b69ab31 | | | 1409 | FileState({data: FileIdx({fileIdx, fileRev: fileRev as FileRev})}), |
| b69ab31 | | | 1410 | considerAbsorb, |
| b69ab31 | | | 1411 | ), |
| b69ab31 | | | 1412 | ); |
| b69ab31 | | | 1413 | expect(get(stack1, 0, 1)).toMatchInlineSnapshot(`"a1↵a2↵a3↵"`); |
| b69ab31 | | | 1414 | // getUtf8Data considers the pending absorb (a1 -> A1). |
| b69ab31 | | | 1415 | const stack2 = stack1.analyseAbsorb(); |
| b69ab31 | | | 1416 | expect(get(stack2, 0, 1)).toMatchInlineSnapshot(`"A1↵a2↵a3↵"`); |
| b69ab31 | | | 1417 | // Can still ask for the content without absorb explicitly. |
| b69ab31 | | | 1418 | expect(get(stack2, 0, 1, false)).toMatchInlineSnapshot(`"a1↵a2↵a3↵"`); |
| b69ab31 | | | 1419 | }); |
| b69ab31 | | | 1420 | |
| b69ab31 | | | 1421 | it('can apply absorb edits', () => { |
| b69ab31 | | | 1422 | const beforeStack = new CommitStackState(absorbStack2).useFileStack().analyseAbsorb(); |
| b69ab31 | | | 1423 | expect(beforeStack.useFileStack().describeFileStacks()).toMatchInlineSnapshot(` |
| b69ab31 | | | 1424 | [ |
| b69ab31 | | | 1425 | "0:./a.txt 1:CommitA/a.txt(a1↵a2↵a3↵;absorbed:A1↵a2↵a3↵) 2:CommitC/a.txt(A1↵a2↵a3↵x1↵;absorbed:A1↵a2↵a3↵X1↵)", |
| b69ab31 | | | 1426 | "0:./b.txt 1:CommitB/b.txt(b1↵b2↵b3↵;absorbed:B1↵b2↵b3↵) 2:CommitC/b.txt(B1↵b2↵b3↵y1↵;absorbed:B1↵b2↵b3↵Y1↵)", |
| b69ab31 | | | 1427 | "0:./c.txt(c1↵c2↵c3↵) 1:CommitC/c.txt(c1↵c2↵c3↵z1↵) 2:Wdir/c.txt(c1↵c2↵c3↵z1↵;absorbed:C1↵C2↵c3↵z1↵)", |
| b69ab31 | | | 1428 | ] |
| b69ab31 | | | 1429 | `); |
| b69ab31 | | | 1430 | // After `applyAbsorbEdits`, "absorbed:" contents become real contents. |
| b69ab31 | | | 1431 | const afterStack = beforeStack.applyAbsorbEdits(); |
| b69ab31 | | | 1432 | expect(afterStack.hasPendingAbsorb()).toBeFalsy(); |
| b69ab31 | | | 1433 | expect(describeAbsorbExtra(afterStack)).toMatchInlineSnapshot(`{}`); |
| b69ab31 | | | 1434 | expect(afterStack.useFileStack().describeFileStacks()).toMatchInlineSnapshot(` |
| b69ab31 | | | 1435 | [ |
| b69ab31 | | | 1436 | "0:./a.txt 1:CommitA/a.txt(A1↵a2↵a3↵) 2:CommitC/a.txt(A1↵a2↵a3↵X1↵) 3:Wdir/a.txt(A1↵a2↵a3↵X1↵)", |
| b69ab31 | | | 1437 | "0:./b.txt 1:CommitB/b.txt(B1↵b2↵b3↵) 2:CommitC/b.txt(B1↵b2↵b3↵Y1↵) 3:Wdir/b.txt(B1↵b2↵b3↵Y1↵)", |
| b69ab31 | | | 1438 | "0:./c.txt(c1↵c2↵c3↵) 1:CommitC/c.txt(c1↵c2↵c3↵z1↵) 2:Wdir/c.txt(C1↵C2↵c3↵z1↵)", |
| b69ab31 | | | 1439 | ] |
| b69ab31 | | | 1440 | `); |
| b69ab31 | | | 1441 | }); |
| b69ab31 | | | 1442 | |
| b69ab31 | | | 1443 | function describeAbsorbExtra(stack: CommitStackState) { |
| b69ab31 | | | 1444 | return stack.absorbExtra.map(describeAbsorbIdChunkMap).toJS(); |
| b69ab31 | | | 1445 | } |
| b69ab31 | | | 1446 | |
| b69ab31 | | | 1447 | function replaceNewLines(text: string): string { |
| b69ab31 | | | 1448 | return text.replaceAll('\n', '↵'); |
| b69ab31 | | | 1449 | } |
| b69ab31 | | | 1450 | |
| b69ab31 | | | 1451 | function describeAbsorbEditCommits(stack: CommitStackState) { |
| b69ab31 | | | 1452 | const describeCommit = (rev: CommitRev) => nullthrows(stack.get(rev)).text; |
| b69ab31 | | | 1453 | const result: object[] = []; |
| b69ab31 | | | 1454 | stack.absorbExtra.forEach((absorbEdits, fileIdx) => { |
| b69ab31 | | | 1455 | absorbEdits.forEach((absorbEdit, absorbEditId) => { |
| b69ab31 | | | 1456 | const {candidateRevs, selectedRev} = stack.getAbsorbCommitRevs(fileIdx, absorbEditId); |
| b69ab31 | | | 1457 | result.push({ |
| b69ab31 | | | 1458 | id: `${stack.getFileStackPath(fileIdx, absorbEdit.introductionRev)}/${absorbEditId}`, |
| b69ab31 | | | 1459 | diff: [ |
| b69ab31 | | | 1460 | replaceNewLines(absorbEdit.oldLines.join('')), |
| b69ab31 | | | 1461 | replaceNewLines(absorbEdit.newLines.join('')), |
| b69ab31 | | | 1462 | ], |
| b69ab31 | | | 1463 | candidates: candidateRevs.map(describeCommit), |
| b69ab31 | | | 1464 | selected: selectedRev && describeCommit(selectedRev), |
| b69ab31 | | | 1465 | }); |
| b69ab31 | | | 1466 | }); |
| b69ab31 | | | 1467 | }); |
| b69ab31 | | | 1468 | return result; |
| b69ab31 | | | 1469 | } |
| b69ab31 | | | 1470 | }); |
| b69ab31 | | | 1471 | }); |