| 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 {ImportStack} from 'shared/types/stack'; |
| b69ab31 | | | 9 | import type {PartialSelection} from '../partialSelection'; |
| b69ab31 | | | 10 | import type { |
| b69ab31 | | | 11 | ApplyUncommittedChangesPreviewsFuncType, |
| b69ab31 | | | 12 | Dag, |
| b69ab31 | | | 13 | UncommittedChangesPreviewContext, |
| b69ab31 | | | 14 | } from '../previews'; |
| b69ab31 | | | 15 | import type { |
| b69ab31 | | | 16 | ChangedFile, |
| b69ab31 | | | 17 | CommandArg, |
| b69ab31 | | | 18 | CommitInfo, |
| b69ab31 | | | 19 | Hash, |
| b69ab31 | | | 20 | RepoRelativePath, |
| b69ab31 | | | 21 | UncommittedChanges, |
| b69ab31 | | | 22 | } from '../types'; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | import {DagCommitInfo} from '../dag/dagCommitInfo'; |
| b69ab31 | | | 25 | import {t} from '../i18n'; |
| b69ab31 | | | 26 | import {readAtom} from '../jotaiUtils'; |
| b69ab31 | | | 27 | import {uncommittedChangesWithPreviews} from '../previews'; |
| b69ab31 | | | 28 | import {authorString} from '../serverAPIState'; |
| b69ab31 | | | 29 | import {CommitBaseOperation} from './CommitBaseOperation'; |
| b69ab31 | | | 30 | import {Operation} from './Operation'; |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | export class CommitOperation extends CommitBaseOperation { |
| b69ab31 | | | 33 | private beforeCommitDate: Date; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | /** |
| b69ab31 | | | 36 | * @param message the commit message. The first line is used as the title. |
| b69ab31 | | | 37 | * @param originalHeadHash the hash of the current head commit, needed to track when optimistic state is resolved. |
| b69ab31 | | | 38 | * @param filesPathsToCommit if provided, only these file paths will be included in the commit operation. If undefined, ALL uncommitted changes are included. Paths should be relative to repo root. |
| b69ab31 | | | 39 | */ |
| b69ab31 | | | 40 | constructor( |
| b69ab31 | | | 41 | public message: string, |
| b69ab31 | | | 42 | private originalHeadHash: Hash, |
| b69ab31 | | | 43 | protected filesPathsToCommit?: Array<RepoRelativePath>, |
| b69ab31 | | | 44 | ) { |
| b69ab31 | | | 45 | super(message, filesPathsToCommit); |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | // New commit should have a greater date. |
| b69ab31 | | | 48 | this.beforeCommitDate = new Date(); |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | // When rendering optimistic state, we need to know the set of files that will be part of this commit. |
| b69ab31 | | | 51 | // This is not necessarily the same as filePathsToCommit, since it may be undefined to represent "all files". |
| b69ab31 | | | 52 | // This is done once at Operation creation time, not on each call to optimisticDag, since we |
| b69ab31 | | | 53 | // only care about the list of changed files when the CommitOperation was enqueued. |
| b69ab31 | | | 54 | this.optimisticChangedFiles = readAtom(uncommittedChangesWithPreviews).filter(changedFile => { |
| b69ab31 | | | 55 | return filesPathsToCommit == null |
| b69ab31 | | | 56 | ? true |
| b69ab31 | | | 57 | : filesPathsToCommit.some(f => f === changedFile.path); |
| b69ab31 | | | 58 | }); |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | private optimisticChangedFiles: Array<ChangedFile>; |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 64 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 65 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| b69ab31 | | | 66 | const filesToCommit = new Set(this.filesPathsToCommit); |
| b69ab31 | | | 67 | // optimistic state is over when there's no uncommitted changes that we wanted to commit left |
| b69ab31 | | | 68 | if ( |
| b69ab31 | | | 69 | context.uncommittedChanges.length === 0 || |
| b69ab31 | | | 70 | (filesToCommit.size > 0 && |
| b69ab31 | | | 71 | context.uncommittedChanges.every(change => !filesToCommit.has(change.path))) |
| b69ab31 | | | 72 | ) { |
| b69ab31 | | | 73 | return undefined; |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 77 | if (this.filesPathsToCommit != null) { |
| b69ab31 | | | 78 | return changes.filter(change => !filesToCommit.has(change.path)); |
| b69ab31 | | | 79 | } else { |
| b69ab31 | | | 80 | return []; |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | }; |
| b69ab31 | | | 83 | return func; |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | optimisticDag(dag: Dag): Dag { |
| b69ab31 | | | 87 | const base = this.originalHeadHash; |
| b69ab31 | | | 88 | const baseInfo = dag.get(base); |
| b69ab31 | | | 89 | if (!baseInfo) { |
| b69ab31 | | | 90 | return dag; |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | const [title] = this.message.split(/\n+/, 1); |
| b69ab31 | | | 94 | const children = dag.children(base); |
| b69ab31 | | | 95 | const hasWantedChild = children.toHashes().some(h => { |
| b69ab31 | | | 96 | const info = dag.get(h); |
| b69ab31 | | | 97 | return info?.title === title && info?.date > this.beforeCommitDate; |
| b69ab31 | | | 98 | }); |
| b69ab31 | | | 99 | if (hasWantedChild) { |
| b69ab31 | | | 100 | // A new commit was made on `base` with the the expected title. |
| b69ab31 | | | 101 | // Consider the commit operation as completed. |
| b69ab31 | | | 102 | return dag; |
| b69ab31 | | | 103 | } |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | const now = new Date(Date.now()); |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | // The fake optimistic commit can be resolved into a real commit by taking the |
| b69ab31 | | | 108 | // first child of the given parent that's created after the commit operation was created. |
| b69ab31 | | | 109 | const optimisticRevset = `first(sort((children(${base})-${base}) & date(">${now.toUTCString()}"),date))`; |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | // NOTE: We might want to check the "active bookmark" state |
| b69ab31 | | | 112 | // and update bookmarks accordingly. |
| b69ab31 | | | 113 | const hash = `OPTIMISTIC_COMMIT_${base}`; |
| b69ab31 | | | 114 | const description = this.message.slice(title.length); |
| b69ab31 | | | 115 | const author = readAtom(authorString); |
| b69ab31 | | | 116 | const info = DagCommitInfo.fromCommitInfo({ |
| b69ab31 | | | 117 | author: author ?? baseInfo?.author ?? '', |
| b69ab31 | | | 118 | description, |
| b69ab31 | | | 119 | title, |
| b69ab31 | | | 120 | bookmarks: [], |
| b69ab31 | | | 121 | remoteBookmarks: [], |
| b69ab31 | | | 122 | isDot: true, |
| b69ab31 | | | 123 | parents: [base], |
| b69ab31 | | | 124 | hash, |
| b69ab31 | | | 125 | optimisticRevset, |
| b69ab31 | | | 126 | phase: 'draft', |
| b69ab31 | | | 127 | filePathsSample: this.optimisticChangedFiles.map(f => f.path), |
| b69ab31 | | | 128 | totalFileCount: this.optimisticChangedFiles.length, |
| b69ab31 | | | 129 | date: now, |
| b69ab31 | | | 130 | }); |
| b69ab31 | | | 131 | |
| b69ab31 | | | 132 | return dag.replaceWith([base, hash], (h, _c) => { |
| b69ab31 | | | 133 | if (h === base) { |
| b69ab31 | | | 134 | return baseInfo?.set('isDot', false); |
| b69ab31 | | | 135 | } else { |
| b69ab31 | | | 136 | return info; |
| b69ab31 | | | 137 | } |
| b69ab31 | | | 138 | }); |
| b69ab31 | | | 139 | } |
| b69ab31 | | | 140 | } |
| b69ab31 | | | 141 | |
| b69ab31 | | | 142 | export class PartialCommitOperation extends Operation { |
| b69ab31 | | | 143 | /** |
| b69ab31 | | | 144 | * See also `CommitOperation`. This operation takes a `PartialSelection` and |
| b69ab31 | | | 145 | * uses `debugimportstack` under the hood, to achieve `commit -i` effect. |
| b69ab31 | | | 146 | */ |
| b69ab31 | | | 147 | constructor( |
| b69ab31 | | | 148 | public message: string, |
| b69ab31 | | | 149 | private originalHeadHash: Hash, |
| b69ab31 | | | 150 | private selection: PartialSelection, |
| b69ab31 | | | 151 | // We need "selected" or "all" files since `selection` only tracks deselected files. |
| b69ab31 | | | 152 | private allFiles: Array<RepoRelativePath>, |
| b69ab31 | | | 153 | ) { |
| b69ab31 | | | 154 | super('PartialCommitOperation'); |
| b69ab31 | | | 155 | } |
| b69ab31 | | | 156 | |
| b69ab31 | | | 157 | getArgs(): CommandArg[] { |
| b69ab31 | | | 158 | return ['debugimportstack']; |
| b69ab31 | | | 159 | } |
| b69ab31 | | | 160 | |
| b69ab31 | | | 161 | getStdin(): string | undefined { |
| b69ab31 | | | 162 | const files = this.selection.calculateImportStackFiles(this.allFiles); |
| b69ab31 | | | 163 | const importStack: ImportStack = [ |
| b69ab31 | | | 164 | [ |
| b69ab31 | | | 165 | 'commit', |
| b69ab31 | | | 166 | { |
| b69ab31 | | | 167 | mark: ':1', |
| b69ab31 | | | 168 | text: this.message, |
| b69ab31 | | | 169 | parents: [this.originalHeadHash], |
| b69ab31 | | | 170 | files, |
| b69ab31 | | | 171 | }, |
| b69ab31 | | | 172 | ], |
| b69ab31 | | | 173 | ['reset', {mark: ':1'}], |
| b69ab31 | | | 174 | ]; |
| b69ab31 | | | 175 | return JSON.stringify(importStack); |
| b69ab31 | | | 176 | } |
| b69ab31 | | | 177 | |
| b69ab31 | | | 178 | getDescriptionForDisplay() { |
| b69ab31 | | | 179 | return { |
| b69ab31 | | | 180 | description: t('Committing selected changes'), |
| b69ab31 | | | 181 | tooltip: t( |
| b69ab31 | | | 182 | 'This operation does not have a traditional command line equivalent. \n' + |
| b69ab31 | | | 183 | 'You can use `commit -i` on the command line to select changes to commit.', |
| b69ab31 | | | 184 | ), |
| b69ab31 | | | 185 | }; |
| b69ab31 | | | 186 | } |
| b69ab31 | | | 187 | } |
| b69ab31 | | | 188 | |
| b69ab31 | | | 189 | /** Choose `PartialCommitOperation` or `CommitOperation` based on input. */ |
| b69ab31 | | | 190 | export function getCommitOperation( |
| b69ab31 | | | 191 | message: string, |
| b69ab31 | | | 192 | originalHead: CommitInfo | undefined, |
| b69ab31 | | | 193 | selection: PartialSelection, |
| b69ab31 | | | 194 | allFiles: Array<RepoRelativePath>, |
| b69ab31 | | | 195 | ): CommitOperation | PartialCommitOperation { |
| b69ab31 | | | 196 | const originalHeadHash = originalHead?.hash ?? '.'; |
| b69ab31 | | | 197 | if (selection.hasChunkSelection()) { |
| b69ab31 | | | 198 | return new PartialCommitOperation(message, originalHeadHash, selection, allFiles); |
| b69ab31 | | | 199 | } else if (selection.isEverythingSelected(() => allFiles)) { |
| b69ab31 | | | 200 | return new CommitOperation(message, originalHeadHash); |
| b69ab31 | | | 201 | } else { |
| b69ab31 | | | 202 | const selectedFiles = allFiles.filter(path => selection.isFullyOrPartiallySelected(path)); |
| b69ab31 | | | 203 | return new CommitOperation(message, originalHeadHash, selectedFiles); |
| b69ab31 | | | 204 | } |
| b69ab31 | | | 205 | } |