3.5 KB106 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import type {TrackEventName} from 'isl-server/src/analytics/eventNames';
9import type {
10 ApplyMergeConflictsPreviewsFuncType,
11 ApplyUncommittedChangesPreviewsFuncType,
12 Dag,
13 MergeConflictsPreviewContext,
14 UncommittedChangesPreviewContext,
15} from '../previews';
16import type {CommandArg, RunnableOperation} from '../types';
17
18import {randomId} from 'shared/utils';
19import {CommandRunner} from '../types';
20
21// NOTE: This file is sensitive to what is imported, as it is used by the vscode extension as well as the client.
22// It should not import the platform, even transitively, or vscode will use the wrong platform.
23// TODO: Can we adjust how the extension uses this to prevent import issues?
24
25/**
26 * Operations represent commands that mutate the repository, such as rebasing, committing, etc.
27 * Operations are intended to be relatively long-lived processes which show progress, are cancellable, and must be run one-at-a-time.
28 * This is as opposed to other commands like status, log, cat, which may be run in parallel and do not (necessarily) show stdout progress.
29 * You can get arguments, get the preview applier function, get the optimistic state applier function, get documentation, etc.
30 */
31export abstract class Operation {
32 static operationName: string;
33 public id: string = randomId();
34
35 constructor(public trackEventName: TrackEventName) {}
36
37 abstract getArgs(): Array<CommandArg>;
38
39 /** Optional stdin data piped to the process. */
40 getStdin(): string | undefined {
41 return undefined;
42 }
43
44 /** Description of the operation. This can replace the default display. */
45 getDescriptionForDisplay(): OperationDescription | undefined {
46 return undefined;
47 }
48
49 /**
50 * When the operation starts running, prefill inline progress messages to show up next to one or more commits.
51 * Note: most operations/runners never report additional inline progress, meaning this is typically shown for the duration of the operation.
52 */
53 getInitialInlineProgress?(): Array<[hash: string, message: string]>;
54
55 public runner: CommandRunner = CommandRunner.Sapling;
56
57 makeOptimisticUncommittedChangesApplier?(
58 context: UncommittedChangesPreviewContext,
59 ): ApplyUncommittedChangesPreviewsFuncType | undefined;
60
61 makeOptimisticMergeConflictsApplier?(
62 context: MergeConflictsPreviewContext,
63 ): ApplyMergeConflictsPreviewsFuncType | undefined;
64
65 /** Effects to `dag` before confirming the operation. */
66 previewDag(dag: Dag): Dag {
67 return dag;
68 }
69
70 /**
71 * Effects to `dag` after confirming the operation.
72 * The operation is running or queued.
73 */
74 optimisticDag(dag: Dag): Dag {
75 return dag;
76 }
77
78 getRunnableOperation(): RunnableOperation {
79 return {
80 args: this.getArgs(),
81 id: this.id,
82 stdin: this.getStdin(),
83 runner: this.runner,
84 trackEventName: this.trackEventName,
85 };
86 }
87}
88
89/** Access static opName field of an operation */
90export function getOpName(op: Operation): string {
91 return (op.constructor as unknown as {opName: string}).opName;
92}
93
94/** Describe how to display a operation. */
95export type OperationDescription = {
96 /** If set, this replaces the default command arguments. */
97 description?: string;
98
99 /**
100 * If set, this replaces the default command in the output tooltip.
101 * It also indicates that the output lines might contain a JSON string
102 * that is not suitable for human reading.
103 */
104 tooltip?: string;
105};
106