| 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 {Dag} from '../previews'; |
| b69ab31 | | | 9 | import type {CommitInfo} from '../types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {firstLine} from 'shared/utils'; |
| b69ab31 | | | 12 | import {CommitPreview} from '../previews'; |
| b69ab31 | | | 13 | import {exactRevset} from '../types'; |
| b69ab31 | | | 14 | import {Operation} from './Operation'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | /** |
| b69ab31 | | | 17 | * Returns [bottom, top] of an array. |
| b69ab31 | | | 18 | */ |
| b69ab31 | | | 19 | function ends<T>(range: Array<T>): [T, T] { |
| b69ab31 | | | 20 | return [range[0], range[range.length - 1]]; |
| b69ab31 | | | 21 | } |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | export class FoldOperation extends Operation { |
| b69ab31 | | | 24 | constructor( |
| b69ab31 | | | 25 | private foldRange: Array<CommitInfo>, |
| b69ab31 | | | 26 | newMessage: string, |
| b69ab31 | | | 27 | ) { |
| b69ab31 | | | 28 | super('FoldOperation'); |
| b69ab31 | | | 29 | this.newTitle = firstLine(newMessage); |
| b69ab31 | | | 30 | this.newDescription = newMessage.substring(firstLine(newMessage).length + 1); |
| b69ab31 | | | 31 | } |
| b69ab31 | | | 32 | private newTitle: string; |
| b69ab31 | | | 33 | private newDescription: string; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | static opName = 'Fold'; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | getArgs() { |
| b69ab31 | | | 38 | const [bottom, top] = ends(this.foldRange); |
| b69ab31 | | | 39 | return [ |
| b69ab31 | | | 40 | 'fold', |
| b69ab31 | | | 41 | '--exact', |
| b69ab31 | | | 42 | exactRevset(`${bottom.hash}::${top.hash}`), |
| b69ab31 | | | 43 | '--message', |
| b69ab31 | | | 44 | `${this.newTitle}\n${this.newDescription}`, |
| b69ab31 | | | 45 | ]; |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | public getFoldRange(): Array<CommitInfo> { |
| b69ab31 | | | 49 | return this.foldRange; |
| b69ab31 | | | 50 | } |
| b69ab31 | | | 51 | public getFoldedMessage(): [string, string] { |
| b69ab31 | | | 52 | return [this.newTitle, this.newDescription]; |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | previewDag(dag: Dag): Dag { |
| b69ab31 | | | 56 | return this.calculateDagPreview(dag, true); |
| b69ab31 | | | 57 | } |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | optimisticDag(dag: Dag): Dag { |
| b69ab31 | | | 60 | return this.calculateDagPreview(dag, false); |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | private calculateDagPreview(dag: Dag, isPreview: boolean): Dag { |
| b69ab31 | | | 64 | const hashes = this.foldRange.map(info => info.hash); |
| b69ab31 | | | 65 | const top = hashes.at(-1); |
| b69ab31 | | | 66 | const parents = dag.get(hashes.at(0))?.parents; |
| b69ab31 | | | 67 | if (top == null || parents == null) { |
| b69ab31 | | | 68 | return dag; |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | const hash = getFoldRangeCommitHash(this.foldRange, isPreview); |
| b69ab31 | | | 71 | const bookmarks = hashes.flatMap(h => dag.get(h)?.bookmarks ?? []).sort(); |
| b69ab31 | | | 72 | return dag |
| b69ab31 | | | 73 | .replaceWith(hashes, (h, c) => { |
| b69ab31 | | | 74 | if (h !== top && c == null) { |
| b69ab31 | | | 75 | return undefined; |
| b69ab31 | | | 76 | } |
| b69ab31 | | | 77 | return c?.merge({ |
| b69ab31 | | | 78 | date: new Date(), |
| b69ab31 | | | 79 | hash, |
| b69ab31 | | | 80 | bookmarks, |
| b69ab31 | | | 81 | title: this.newTitle, |
| b69ab31 | | | 82 | description: this.newDescription, |
| b69ab31 | | | 83 | previewType: isPreview ? CommitPreview.FOLD_PREVIEW : CommitPreview.FOLD, |
| b69ab31 | | | 84 | parents, |
| b69ab31 | | | 85 | }); |
| b69ab31 | | | 86 | }) |
| b69ab31 | | | 87 | .replaceWith(dag.children(top), (_h, c) => { |
| b69ab31 | | | 88 | return c?.set( |
| b69ab31 | | | 89 | 'parents', |
| b69ab31 | | | 90 | c.parents.map(p => (p === top ? hash : p)), |
| b69ab31 | | | 91 | ); |
| b69ab31 | | | 92 | }); |
| b69ab31 | | | 93 | } |
| b69ab31 | | | 94 | } |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | export const FOLD_COMMIT_PREVIEW_HASH_PREFIX = 'OPTIMISTIC_FOLDED_PREVIEW_'; |
| b69ab31 | | | 97 | export const FOLD_COMMIT_OPTIMISTIC_HASH_PREFIX = 'OPTIMISTIC_FOLDED_'; |
| b69ab31 | | | 98 | export function getFoldRangeCommitHash(range: Array<CommitInfo>, isPreview: boolean): string { |
| b69ab31 | | | 99 | const [bottom, top] = ends(range); |
| b69ab31 | | | 100 | return ( |
| b69ab31 | | | 101 | (isPreview ? FOLD_COMMIT_PREVIEW_HASH_PREFIX : FOLD_COMMIT_OPTIMISTIC_HASH_PREFIX) + |
| b69ab31 | | | 102 | `${bottom.hash}:${top.hash}` |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | } |