| 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 {Hash} from '../types'; |
| b69ab31 | | | 9 | import type {CommitMessageFields} from './types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {atom} from 'jotai'; |
| b69ab31 | | | 12 | import {firstLine} from 'shared/utils'; |
| b69ab31 | | | 13 | import serverAPI from '../ClientToServerAPI'; |
| b69ab31 | | | 14 | import {successionTracker} from '../SuccessionTracker'; |
| b69ab31 | | | 15 | import {tracker} from '../analytics'; |
| b69ab31 | | | 16 | import {latestCommitMessageFields} from '../codeReview/CodeReviewInfo'; |
| b69ab31 | | | 17 | import {islDrawerState} from '../drawerState'; |
| b69ab31 | | | 18 | import {atomFamilyWeak, localStorageBackedAtomFamily, readAtom, writeAtom} from '../jotaiUtils'; |
| b69ab31 | | | 19 | import {AmendMessageOperation} from '../operations/AmendMessageOperation'; |
| b69ab31 | | | 20 | import {AmendOperation, PartialAmendOperation} from '../operations/AmendOperation'; |
| b69ab31 | | | 21 | import {CommitOperation, PartialCommitOperation} from '../operations/CommitOperation'; |
| b69ab31 | | | 22 | import {onOperationExited, queuedOperations, queuedOperationsErrorAtom} from '../operationsState'; |
| b69ab31 | | | 23 | import {dagWithPreviews} from '../previews'; |
| b69ab31 | | | 24 | import {selectedCommitInfos, selectedCommits} from '../selection'; |
| b69ab31 | | | 25 | import {latestHeadCommit} from '../serverAPIState'; |
| b69ab31 | | | 26 | import {registerCleanup, registerDisposable} from '../utils'; |
| b69ab31 | | | 27 | import { |
| b69ab31 | | | 28 | allFieldsBeingEdited, |
| b69ab31 | | | 29 | anyEditsMade, |
| b69ab31 | | | 30 | applyEditedFields, |
| b69ab31 | | | 31 | commitMessageFieldsSchema, |
| b69ab31 | | | 32 | mergeCommitMessageFields, |
| b69ab31 | | | 33 | mergeOnlyEmptyMessageFields, |
| b69ab31 | | | 34 | parseCommitMessageFields, |
| b69ab31 | | | 35 | } from './CommitMessageFields'; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | export type EditedMessage = Partial<CommitMessageFields>; |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | export type CommitInfoMode = 'commit' | 'amend'; |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | export const commitMessageTemplate = atom<EditedMessage | undefined>(undefined); |
| b69ab31 | | | 42 | registerDisposable( |
| b69ab31 | | | 43 | commitMessageTemplate, |
| b69ab31 | | | 44 | serverAPI.onMessageOfType('fetchedCommitMessageTemplate', event => { |
| b69ab31 | | | 45 | const title = firstLine(event.template); |
| b69ab31 | | | 46 | const description = event.template.slice(title.length + 1); |
| b69ab31 | | | 47 | const schema = readAtom(commitMessageFieldsSchema); |
| b69ab31 | | | 48 | const fields = parseCommitMessageFields(schema, title, description); |
| b69ab31 | | | 49 | writeAtom(commitMessageTemplate, fields); |
| b69ab31 | | | 50 | }), |
| b69ab31 | | | 51 | import.meta.hot, |
| b69ab31 | | | 52 | ); |
| b69ab31 | | | 53 | registerCleanup( |
| b69ab31 | | | 54 | commitMessageTemplate, |
| b69ab31 | | | 55 | serverAPI.onSetup(() => |
| b69ab31 | | | 56 | serverAPI.postMessage({ |
| b69ab31 | | | 57 | type: 'fetchCommitMessageTemplate', |
| b69ab31 | | | 58 | }), |
| b69ab31 | | | 59 | ), |
| b69ab31 | | | 60 | import.meta.hot, |
| b69ab31 | | | 61 | ); |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | /** Typed update messages when submitting a commit or set of commits. |
| b69ab31 | | | 64 | * Unlike editedCommitMessages, you can't provide an update message when committing the first time, |
| b69ab31 | | | 65 | * so we don't need to track this state for 'head'. |
| b69ab31 | | | 66 | */ |
| b69ab31 | | | 67 | export const diffUpdateMessagesState = atomFamilyWeak((_hash: Hash) => atom<string>('')); |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | export const getDefaultEditedCommitMessage = (): EditedMessage => ({}); |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | /** |
| b69ab31 | | | 72 | * Map of hash -> latest edited commit message, representing any changes made to the commit's message fields. |
| b69ab31 | | | 73 | * Only fields that are edited are entered here. Fields that are not edited are not in the object. |
| b69ab31 | | | 74 | * |
| b69ab31 | | | 75 | * `{}` corresponds to the original commit message. |
| b69ab31 | | | 76 | * `{Title: 'hello'}` means the title was changed to "hello", but all other fields are unchanged. |
| b69ab31 | | | 77 | * |
| b69ab31 | | | 78 | * When you begin editing a field, that field must be initialized in the EditedMessage with the latest value. |
| b69ab31 | | | 79 | * This also stores the state of new commit messages being written, keyed by "head" instead of a commit hash. |
| b69ab31 | | | 80 | * Note: this state should be cleared when amending / committing / meta-editing. |
| b69ab31 | | | 81 | */ |
| b69ab31 | | | 82 | export const editedCommitMessages = localStorageBackedAtomFamily<Hash | 'head', EditedMessage>( |
| b69ab31 | | | 83 | 'isl.edited-commit-messages:', |
| b69ab31 | | | 84 | () => getDefaultEditedCommitMessage(), |
| b69ab31 | | | 85 | ); |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | function updateEditedCommitMessagesFromSuccessions() { |
| b69ab31 | | | 88 | return successionTracker.onSuccessions(successions => { |
| b69ab31 | | | 89 | for (const [oldHash, newHash] of successions) { |
| b69ab31 | | | 90 | const existing = readAtom(editedCommitMessages(oldHash)); |
| b69ab31 | | | 91 | writeAtom( |
| b69ab31 | | | 92 | editedCommitMessages(newHash), |
| b69ab31 | | | 93 | Object.keys(existing).length === 0 |
| b69ab31 | | | 94 | ? // If the edited message is empty, write undefined to remove from persisted storage |
| b69ab31 | | | 95 | undefined |
| b69ab31 | | | 96 | : existing, |
| b69ab31 | | | 97 | ); |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | const existingUpdateMessage = readAtom(diffUpdateMessagesState(oldHash)); |
| b69ab31 | | | 100 | if (existingUpdateMessage && existingUpdateMessage !== '') { |
| b69ab31 | | | 101 | // TODO: this doesn't work if you have multiple commits selected... |
| b69ab31 | | | 102 | writeAtom(diffUpdateMessagesState(newHash), existingUpdateMessage); |
| b69ab31 | | | 103 | } |
| b69ab31 | | | 104 | } |
| b69ab31 | | | 105 | }); |
| b69ab31 | | | 106 | } |
| b69ab31 | | | 107 | let editedCommitMessageSuccessionDisposable = updateEditedCommitMessagesFromSuccessions(); |
| b69ab31 | | | 108 | export const __TEST__ = { |
| b69ab31 | | | 109 | renewEditedCommitMessageSuccessionSubscription() { |
| b69ab31 | | | 110 | editedCommitMessageSuccessionDisposable(); |
| b69ab31 | | | 111 | editedCommitMessageSuccessionDisposable = updateEditedCommitMessagesFromSuccessions(); |
| b69ab31 | | | 112 | }, |
| b69ab31 | | | 113 | }; |
| b69ab31 | | | 114 | registerCleanup(successionTracker, updateEditedCommitMessagesFromSuccessions, import.meta.hot); |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | // Handle updateDraftCommitMessage messages |
| b69ab31 | | | 117 | registerDisposable( |
| b69ab31 | | | 118 | serverAPI, |
| b69ab31 | | | 119 | serverAPI.onMessageOfType('updateDraftCommitMessage', event => { |
| b69ab31 | | | 120 | const title = event.title; |
| b69ab31 | | | 121 | const description = event.description; |
| b69ab31 | | | 122 | const mode = event.mode ?? 'commit'; // Default to 'commit' if not specified |
| b69ab31 | | | 123 | const hash = event.hash ?? readAtom(latestHeadCommit)?.hash ?? 'head'; |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | writeAtom(islDrawerState, val => ({...val, right: {...val.right, collapsed: false}})); |
| b69ab31 | | | 126 | const schema = readAtom(commitMessageFieldsSchema); |
| b69ab31 | | | 127 | const fields = parseCommitMessageFields(schema, title, description); |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | if (mode === 'commit') { |
| b69ab31 | | | 130 | writeAtom(editedCommitMessages('head'), fields); |
| b69ab31 | | | 131 | } else { |
| b69ab31 | | | 132 | const currentMessage = readAtom(editedCommitMessages(hash)); |
| b69ab31 | | | 133 | // For amend mode: non-empty fields replace existing values, empty fields preserve current values |
| b69ab31 | | | 134 | // By passing fields first, mergeOnlyEmptyMessageFields will prefer the new fields when non-empty |
| b69ab31 | | | 135 | writeAtom( |
| b69ab31 | | | 136 | editedCommitMessages(hash), |
| b69ab31 | | | 137 | mergeOnlyEmptyMessageFields(schema, fields, currentMessage as CommitMessageFields), |
| b69ab31 | | | 138 | ); |
| b69ab31 | | | 139 | } |
| b69ab31 | | | 140 | |
| b69ab31 | | | 141 | writeAtom(selectedCommits, new Set([hash])); |
| b69ab31 | | | 142 | writeAtom(rawCommitMode, mode); |
| b69ab31 | | | 143 | }), |
| b69ab31 | | | 144 | ); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | registerDisposable( |
| b69ab31 | | | 147 | serverAPI, |
| b69ab31 | | | 148 | onOperationExited((progress, exitedOperation) => { |
| b69ab31 | | | 149 | if (progress.exitCode === 0) { |
| b69ab31 | | | 150 | return; |
| b69ab31 | | | 151 | } |
| b69ab31 | | | 152 | |
| b69ab31 | | | 153 | const queuedError = readAtom(queuedOperationsErrorAtom); |
| b69ab31 | | | 154 | const queued = readAtom(queuedOperations); |
| b69ab31 | | | 155 | const affectedOperations = |
| b69ab31 | | | 156 | queuedError?.operationThatErrored?.id !== exitedOperation.id |
| b69ab31 | | | 157 | ? [exitedOperation, ...queued] |
| b69ab31 | | | 158 | : [exitedOperation, ...queuedError.operations]; |
| b69ab31 | | | 159 | |
| b69ab31 | | | 160 | for (const operation of affectedOperations) { |
| b69ab31 | | | 161 | const isCommit = |
| b69ab31 | | | 162 | operation instanceof CommitOperation || operation instanceof PartialCommitOperation; |
| b69ab31 | | | 163 | const isAmend = |
| b69ab31 | | | 164 | operation instanceof AmendOperation || operation instanceof PartialAmendOperation; |
| b69ab31 | | | 165 | const isMetaedit = operation instanceof AmendMessageOperation; |
| b69ab31 | | | 166 | if (!(isCommit || isAmend || isMetaedit)) { |
| b69ab31 | | | 167 | continue; |
| b69ab31 | | | 168 | } |
| b69ab31 | | | 169 | |
| b69ab31 | | | 170 | // Operation involving commit message failed, let's restore your edited commit message so you might save it or try again |
| b69ab31 | | | 171 | const message = operation.message; |
| b69ab31 | | | 172 | if (message == null) { |
| b69ab31 | | | 173 | continue; |
| b69ab31 | | | 174 | } |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | const headOrHash = isCommit |
| b69ab31 | | | 177 | ? 'head' |
| b69ab31 | | | 178 | : isMetaedit |
| b69ab31 | | | 179 | ? operation.getCommitHash() |
| b69ab31 | | | 180 | : readAtom(latestHeadCommit)?.hash; |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | if (!headOrHash) { |
| b69ab31 | | | 183 | continue; |
| b69ab31 | | | 184 | } |
| b69ab31 | | | 185 | |
| b69ab31 | | | 186 | const [title] = message.split(/\n+/, 1); |
| b69ab31 | | | 187 | const description = message.slice(title.length); |
| b69ab31 | | | 188 | |
| b69ab31 | | | 189 | tracker.track('RecoverCommitMessageFromOperationError'); |
| b69ab31 | | | 190 | |
| b69ab31 | | | 191 | const schema = readAtom(commitMessageFieldsSchema); |
| b69ab31 | | | 192 | const fields = parseCommitMessageFields(schema, title, description); |
| b69ab31 | | | 193 | const currentMessage = readAtom(editedCommitMessages(headOrHash)); |
| b69ab31 | | | 194 | writeAtom( |
| b69ab31 | | | 195 | editedCommitMessages(headOrHash), |
| b69ab31 | | | 196 | mergeCommitMessageFields(schema, currentMessage as CommitMessageFields, fields), |
| b69ab31 | | | 197 | ); |
| b69ab31 | | | 198 | writeAtom(commitMode, isCommit ? 'commit' : 'amend'); |
| b69ab31 | | | 199 | if (!isCommit) { |
| b69ab31 | | | 200 | writeAtom(selectedCommits, new Set([headOrHash])); |
| b69ab31 | | | 201 | } |
| b69ab31 | | | 202 | } |
| b69ab31 | | | 203 | }), |
| b69ab31 | | | 204 | import.meta.hot, |
| b69ab31 | | | 205 | ); |
| b69ab31 | | | 206 | |
| b69ab31 | | | 207 | export const latestCommitMessageFieldsWithEdits = atomFamilyWeak((hashOrHead: Hash | 'head') => { |
| b69ab31 | | | 208 | return atom(get => { |
| b69ab31 | | | 209 | const edited = get(editedCommitMessages(hashOrHead)); |
| b69ab31 | | | 210 | const latest = get(latestCommitMessageFields(hashOrHead)); |
| b69ab31 | | | 211 | return applyEditedFields(latest, edited); |
| b69ab31 | | | 212 | }); |
| b69ab31 | | | 213 | }); |
| b69ab31 | | | 214 | |
| b69ab31 | | | 215 | /** |
| b69ab31 | | | 216 | * Fields being edited is computed from editedCommitMessage, |
| b69ab31 | | | 217 | * and reset to only substantially changed fields when changing commits. |
| b69ab31 | | | 218 | * This state skips the substantial changes check, |
| b69ab31 | | | 219 | * which allows all fields to be edited for example when clicking "amend...", |
| b69ab31 | | | 220 | * but without actually changing the underlying edited messages. |
| b69ab31 | | | 221 | */ |
| b69ab31 | | | 222 | export const forceNextCommitToEditAllFields = atom<boolean>(false); |
| b69ab31 | | | 223 | |
| b69ab31 | | | 224 | export const unsavedFieldsBeingEdited = atomFamilyWeak((hashOrHead: Hash | 'head') => { |
| b69ab31 | | | 225 | return atom(get => { |
| b69ab31 | | | 226 | const edited = get(editedCommitMessages(hashOrHead)); |
| b69ab31 | | | 227 | const schema = get(commitMessageFieldsSchema); |
| b69ab31 | | | 228 | if (hashOrHead === 'head') { |
| b69ab31 | | | 229 | return allFieldsBeingEdited(schema); |
| b69ab31 | | | 230 | } |
| b69ab31 | | | 231 | return Object.fromEntries(schema.map(field => [field.key, field.key in edited])); |
| b69ab31 | | | 232 | }); |
| b69ab31 | | | 233 | }); |
| b69ab31 | | | 234 | |
| b69ab31 | | | 235 | export const hasUnsavedEditedCommitMessage = atomFamilyWeak((hashOrHead: Hash | 'head') => { |
| b69ab31 | | | 236 | return atom(get => { |
| b69ab31 | | | 237 | const beingEdited = get(unsavedFieldsBeingEdited(hashOrHead)); |
| b69ab31 | | | 238 | if (Object.values(beingEdited).some(Boolean)) { |
| b69ab31 | | | 239 | // Some fields are being edited, let's look more closely to see if anything is actually different. |
| b69ab31 | | | 240 | const edited = get(editedCommitMessages(hashOrHead)); |
| b69ab31 | | | 241 | const latest = get(latestCommitMessageFields(hashOrHead)); |
| b69ab31 | | | 242 | const schema = get(commitMessageFieldsSchema); |
| b69ab31 | | | 243 | return anyEditsMade(schema, latest, edited); |
| b69ab31 | | | 244 | } |
| b69ab31 | | | 245 | return false; |
| b69ab31 | | | 246 | }); |
| b69ab31 | | | 247 | }); |
| b69ab31 | | | 248 | |
| b69ab31 | | | 249 | /** |
| b69ab31 | | | 250 | * Toggle state between commit/amend modes. Note that this may be "commit" even if |
| b69ab31 | | | 251 | * the commit info is not looking at the head commit (this allows persistence as you select other commits and come back). |
| b69ab31 | | | 252 | * We should only behave in "commit" mode when in commit mode AND looking at the head commit. |
| b69ab31 | | | 253 | * Prefer using `commitMode` atom. |
| b69ab31 | | | 254 | */ |
| b69ab31 | | | 255 | const rawCommitMode = atom<CommitInfoMode>('amend'); |
| b69ab31 | | | 256 | |
| b69ab31 | | | 257 | /** |
| b69ab31 | | | 258 | * Whether the commit info view is in "commit" or "amend" mode. |
| b69ab31 | | | 259 | * It may only be in the "commit" mode when the commit being viewed is the head commit, |
| b69ab31 | | | 260 | * though it may be set to "commit" mode even when looking at a non-head commit, |
| b69ab31 | | | 261 | * and it'll be in commit when when you do look at the head commit. |
| b69ab31 | | | 262 | */ |
| b69ab31 | | | 263 | export const commitMode = atom( |
| b69ab31 | | | 264 | get => { |
| b69ab31 | | | 265 | const commitInfoCommit = get(commitInfoViewCurrentCommits); |
| b69ab31 | | | 266 | const rawMode = get(rawCommitMode); |
| b69ab31 | | | 267 | if (commitInfoCommit == null) { |
| b69ab31 | | | 268 | // loading state |
| b69ab31 | | | 269 | return 'amend'; |
| b69ab31 | | | 270 | } |
| b69ab31 | | | 271 | if (commitInfoCommit.length === 1 && commitInfoCommit[0].isDot) { |
| b69ab31 | | | 272 | // allow using "commit" mode only if looking at exactly the single head commit |
| b69ab31 | | | 273 | return rawMode; |
| b69ab31 | | | 274 | } |
| b69ab31 | | | 275 | // otherwise, it's a non-head commit or multi-selection, so only show "amend" mode |
| b69ab31 | | | 276 | return 'amend'; |
| b69ab31 | | | 277 | }, |
| b69ab31 | | | 278 | (_get, set, newMode: CommitInfoMode | ((m: CommitInfoMode) => CommitInfoMode)) => { |
| b69ab31 | | | 279 | set(rawCommitMode, newMode); |
| b69ab31 | | | 280 | }, |
| b69ab31 | | | 281 | ); |
| b69ab31 | | | 282 | |
| b69ab31 | | | 283 | export const commitInfoViewCurrentCommits = atom(get => { |
| b69ab31 | | | 284 | const selected = get(selectedCommitInfos); |
| b69ab31 | | | 285 | |
| b69ab31 | | | 286 | // show selected commit, if there's exactly 1 |
| b69ab31 | | | 287 | const selectedCommit = selected.length === 1 ? selected[0] : undefined; |
| b69ab31 | | | 288 | const commit = selectedCommit ?? get(dagWithPreviews).resolve('.'); |
| b69ab31 | | | 289 | |
| b69ab31 | | | 290 | if (commit == null) { |
| b69ab31 | | | 291 | return null; |
| b69ab31 | | | 292 | } else { |
| b69ab31 | | | 293 | return selected.length > 1 ? selected : [commit]; |
| b69ab31 | | | 294 | } |
| b69ab31 | | | 295 | }); |