Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/git/src/operation.ts
5221 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
/* eslint-disable local/code-no-dangerous-type-assertions */
6
7
import { LogOutputChannel } from 'vscode';
8
9
export const enum OperationKind {
10
Add = 'Add',
11
Apply = 'Apply',
12
Blame = 'Blame',
13
Branch = 'Branch',
14
CheckIgnore = 'CheckIgnore',
15
Checkout = 'Checkout',
16
CheckoutTracking = 'CheckoutTracking',
17
CherryPick = 'CherryPick',
18
Clean = 'Clean',
19
Commit = 'Commit',
20
Config = 'Config',
21
DeleteBranch = 'DeleteBranch',
22
DeleteRef = 'DeleteRef',
23
DeleteRemoteRef = 'DeleteRemoteRef',
24
DeleteTag = 'DeleteTag',
25
DeleteWorktree = 'DeleteWorktree',
26
Diff = 'Diff',
27
Fetch = 'Fetch',
28
FindTrackingBranches = 'GetTracking',
29
GetBranch = 'GetBranch',
30
GetBranches = 'GetBranches',
31
GetCommitTemplate = 'GetCommitTemplate',
32
GetObjectDetails = 'GetObjectDetails',
33
GetObjectFiles = 'GetObjectFiles',
34
GetRefs = 'GetRefs',
35
GetRemoteRefs = 'GetRemoteRefs',
36
HashObject = 'HashObject',
37
Ignore = 'Ignore',
38
Log = 'Log',
39
LogFile = 'LogFile',
40
Merge = 'Merge',
41
MergeAbort = 'MergeAbort',
42
MergeBase = 'MergeBase',
43
Move = 'Move',
44
PostCommitCommand = 'PostCommitCommand',
45
Pull = 'Pull',
46
Push = 'Push',
47
Remote = 'Remote',
48
RenameBranch = 'RenameBranch',
49
Remove = 'Remove',
50
Reset = 'Reset',
51
Rebase = 'Rebase',
52
RebaseAbort = 'RebaseAbort',
53
RebaseContinue = 'RebaseContinue',
54
Refresh = 'Refresh',
55
RevertFiles = 'RevertFiles',
56
RevList = 'RevList',
57
RevParse = 'RevParse',
58
SetBranchUpstream = 'SetBranchUpstream',
59
Show = 'Show',
60
Stage = 'Stage',
61
Status = 'Status',
62
Stash = 'Stash',
63
SubmoduleUpdate = 'SubmoduleUpdate',
64
Sync = 'Sync',
65
Tag = 'Tag',
66
Worktree = 'Worktree'
67
}
68
69
export type Operation = AddOperation | ApplyOperation | BlameOperation | BranchOperation | CheckIgnoreOperation | CherryPickOperation |
70
CheckoutOperation | CheckoutTrackingOperation | CleanOperation | CommitOperation | ConfigOperation | DeleteBranchOperation |
71
DeleteRefOperation | DeleteRemoteRefOperation | DeleteTagOperation | DiffOperation | FetchOperation | FindTrackingBranchesOperation |
72
GetBranchOperation | GetBranchesOperation | GetCommitTemplateOperation | GetObjectDetailsOperation | GetObjectFilesOperation | GetRefsOperation |
73
GetRemoteRefsOperation | HashObjectOperation | IgnoreOperation | LogOperation | LogFileOperation | MergeOperation | MergeAbortOperation |
74
MergeBaseOperation | MoveOperation | PostCommitCommandOperation | PullOperation | PushOperation | RemoteOperation | RenameBranchOperation |
75
RemoveOperation | ResetOperation | RebaseOperation | RebaseAbortOperation | RebaseContinueOperation | RefreshOperation | RevertFilesOperation |
76
RevListOperation | RevParseOperation | SetBranchUpstreamOperation | ShowOperation | StageOperation | StatusOperation | StashOperation |
77
SubmoduleUpdateOperation | SyncOperation | TagOperation | WorktreeOperation;
78
79
type BaseOperation = { kind: OperationKind; blocking: boolean; readOnly: boolean; remote: boolean; retry: boolean; showProgress: boolean };
80
export type AddOperation = BaseOperation & { kind: OperationKind.Add };
81
export type ApplyOperation = BaseOperation & { kind: OperationKind.Apply };
82
export type BlameOperation = BaseOperation & { kind: OperationKind.Blame };
83
export type BranchOperation = BaseOperation & { kind: OperationKind.Branch };
84
export type CheckIgnoreOperation = BaseOperation & { kind: OperationKind.CheckIgnore };
85
export type CherryPickOperation = BaseOperation & { kind: OperationKind.CherryPick };
86
export type CheckoutOperation = BaseOperation & { kind: OperationKind.Checkout; refLabel: string };
87
export type CheckoutTrackingOperation = BaseOperation & { kind: OperationKind.CheckoutTracking; refLabel: string };
88
export type CleanOperation = BaseOperation & { kind: OperationKind.Clean };
89
export type CommitOperation = BaseOperation & { kind: OperationKind.Commit };
90
export type ConfigOperation = BaseOperation & { kind: OperationKind.Config };
91
export type DeleteBranchOperation = BaseOperation & { kind: OperationKind.DeleteBranch };
92
export type DeleteRefOperation = BaseOperation & { kind: OperationKind.DeleteRef };
93
export type DeleteRemoteRefOperation = BaseOperation & { kind: OperationKind.DeleteRemoteRef };
94
export type DeleteTagOperation = BaseOperation & { kind: OperationKind.DeleteTag };
95
export type DiffOperation = BaseOperation & { kind: OperationKind.Diff };
96
export type FetchOperation = BaseOperation & { kind: OperationKind.Fetch };
97
export type FindTrackingBranchesOperation = BaseOperation & { kind: OperationKind.FindTrackingBranches };
98
export type GetBranchOperation = BaseOperation & { kind: OperationKind.GetBranch };
99
export type GetBranchesOperation = BaseOperation & { kind: OperationKind.GetBranches };
100
export type GetCommitTemplateOperation = BaseOperation & { kind: OperationKind.GetCommitTemplate };
101
export type GetObjectDetailsOperation = BaseOperation & { kind: OperationKind.GetObjectDetails };
102
export type GetObjectFilesOperation = BaseOperation & { kind: OperationKind.GetObjectFiles };
103
export type GetRefsOperation = BaseOperation & { kind: OperationKind.GetRefs };
104
export type GetRemoteRefsOperation = BaseOperation & { kind: OperationKind.GetRemoteRefs };
105
export type HashObjectOperation = BaseOperation & { kind: OperationKind.HashObject };
106
export type IgnoreOperation = BaseOperation & { kind: OperationKind.Ignore };
107
export type LogOperation = BaseOperation & { kind: OperationKind.Log };
108
export type LogFileOperation = BaseOperation & { kind: OperationKind.LogFile };
109
export type MergeOperation = BaseOperation & { kind: OperationKind.Merge };
110
export type MergeAbortOperation = BaseOperation & { kind: OperationKind.MergeAbort };
111
export type MergeBaseOperation = BaseOperation & { kind: OperationKind.MergeBase };
112
export type MoveOperation = BaseOperation & { kind: OperationKind.Move };
113
export type PostCommitCommandOperation = BaseOperation & { kind: OperationKind.PostCommitCommand };
114
export type PullOperation = BaseOperation & { kind: OperationKind.Pull };
115
export type PushOperation = BaseOperation & { kind: OperationKind.Push };
116
export type RemoteOperation = BaseOperation & { kind: OperationKind.Remote };
117
export type RenameBranchOperation = BaseOperation & { kind: OperationKind.RenameBranch };
118
export type RemoveOperation = BaseOperation & { kind: OperationKind.Remove };
119
export type ResetOperation = BaseOperation & { kind: OperationKind.Reset };
120
export type RebaseOperation = BaseOperation & { kind: OperationKind.Rebase };
121
export type RebaseAbortOperation = BaseOperation & { kind: OperationKind.RebaseAbort };
122
export type RebaseContinueOperation = BaseOperation & { kind: OperationKind.RebaseContinue };
123
export type RefreshOperation = BaseOperation & { kind: OperationKind.Refresh };
124
export type RevertFilesOperation = BaseOperation & { kind: OperationKind.RevertFiles };
125
export type RevListOperation = BaseOperation & { kind: OperationKind.RevList };
126
export type RevParseOperation = BaseOperation & { kind: OperationKind.RevParse };
127
export type SetBranchUpstreamOperation = BaseOperation & { kind: OperationKind.SetBranchUpstream };
128
export type ShowOperation = BaseOperation & { kind: OperationKind.Show };
129
export type StageOperation = BaseOperation & { kind: OperationKind.Stage };
130
export type StatusOperation = BaseOperation & { kind: OperationKind.Status };
131
export type StashOperation = BaseOperation & { kind: OperationKind.Stash };
132
export type SubmoduleUpdateOperation = BaseOperation & { kind: OperationKind.SubmoduleUpdate };
133
export type SyncOperation = BaseOperation & { kind: OperationKind.Sync };
134
export type TagOperation = BaseOperation & { kind: OperationKind.Tag };
135
export type WorktreeOperation = BaseOperation & { kind: OperationKind.Worktree };
136
137
export const Operation = {
138
Add: (showProgress: boolean): AddOperation => ({ kind: OperationKind.Add, blocking: false, readOnly: false, remote: false, retry: false, showProgress }),
139
Apply: { kind: OperationKind.Apply, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as ApplyOperation,
140
Blame: (showProgress: boolean) => ({ kind: OperationKind.Blame, blocking: false, readOnly: true, remote: false, retry: false, showProgress } as BlameOperation),
141
Branch: { kind: OperationKind.Branch, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as BranchOperation,
142
CheckIgnore: { kind: OperationKind.CheckIgnore, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as CheckIgnoreOperation,
143
CherryPick: { kind: OperationKind.CherryPick, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as CherryPickOperation,
144
Checkout: (refLabel: string) => ({ kind: OperationKind.Checkout, blocking: true, readOnly: false, remote: false, retry: false, showProgress: true, refLabel } as CheckoutOperation),
145
CheckoutTracking: (refLabel: string) => ({ kind: OperationKind.CheckoutTracking, blocking: true, readOnly: false, remote: false, retry: false, showProgress: true, refLabel } as CheckoutTrackingOperation),
146
Clean: (showProgress: boolean) => ({ kind: OperationKind.Clean, blocking: false, readOnly: false, remote: false, retry: false, showProgress } as CleanOperation),
147
Commit: { kind: OperationKind.Commit, blocking: true, readOnly: false, remote: false, retry: false, showProgress: true } as CommitOperation,
148
Config: (readOnly: boolean) => ({ kind: OperationKind.Config, blocking: false, readOnly, remote: false, retry: false, showProgress: false } as ConfigOperation),
149
DeleteBranch: { kind: OperationKind.DeleteBranch, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as DeleteBranchOperation,
150
DeleteRef: { kind: OperationKind.DeleteRef, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as DeleteRefOperation,
151
DeleteRemoteRef: { kind: OperationKind.DeleteRemoteRef, blocking: false, readOnly: false, remote: true, retry: false, showProgress: true } as DeleteRemoteRefOperation,
152
DeleteTag: { kind: OperationKind.DeleteTag, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as DeleteTagOperation,
153
Diff: { kind: OperationKind.Diff, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as DiffOperation,
154
Fetch: (showProgress: boolean) => ({ kind: OperationKind.Fetch, blocking: false, readOnly: false, remote: true, retry: true, showProgress } as FetchOperation),
155
FindTrackingBranches: { kind: OperationKind.FindTrackingBranches, blocking: false, readOnly: true, remote: false, retry: false, showProgress: true } as FindTrackingBranchesOperation,
156
GetBranch: { kind: OperationKind.GetBranch, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as GetBranchOperation,
157
GetBranches: { kind: OperationKind.GetBranches, blocking: false, readOnly: true, remote: false, retry: false, showProgress: true } as GetBranchesOperation,
158
GetCommitTemplate: { kind: OperationKind.GetCommitTemplate, blocking: false, readOnly: true, remote: false, retry: false, showProgress: true } as GetCommitTemplateOperation,
159
GetObjectDetails: { kind: OperationKind.GetObjectDetails, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as GetObjectDetailsOperation,
160
GetObjectFiles: { kind: OperationKind.GetObjectFiles, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as GetObjectFilesOperation,
161
GetRefs: { kind: OperationKind.GetRefs, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as GetRefsOperation,
162
GetRemoteRefs: { kind: OperationKind.GetRemoteRefs, blocking: false, readOnly: true, remote: true, retry: false, showProgress: false } as GetRemoteRefsOperation,
163
HashObject: { kind: OperationKind.HashObject, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as HashObjectOperation,
164
Ignore: { kind: OperationKind.Ignore, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as IgnoreOperation,
165
Log: (showProgress: boolean) => ({ kind: OperationKind.Log, blocking: false, readOnly: true, remote: false, retry: false, showProgress }) as LogOperation,
166
LogFile: { kind: OperationKind.LogFile, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as LogFileOperation,
167
Merge: { kind: OperationKind.Merge, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as MergeOperation,
168
MergeAbort: { kind: OperationKind.MergeAbort, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as MergeAbortOperation,
169
MergeBase: { kind: OperationKind.MergeBase, blocking: false, readOnly: true, remote: false, retry: false, showProgress: true } as MergeBaseOperation,
170
Move: { kind: OperationKind.Move, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as MoveOperation,
171
PostCommitCommand: { kind: OperationKind.PostCommitCommand, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as PostCommitCommandOperation,
172
Pull: { kind: OperationKind.Pull, blocking: true, readOnly: false, remote: true, retry: true, showProgress: true } as PullOperation,
173
Push: { kind: OperationKind.Push, blocking: true, readOnly: false, remote: true, retry: false, showProgress: true } as PushOperation,
174
Remote: { kind: OperationKind.Remote, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as RemoteOperation,
175
RenameBranch: { kind: OperationKind.RenameBranch, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as RenameBranchOperation,
176
Remove: { kind: OperationKind.Remove, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as RemoveOperation,
177
Reset: { kind: OperationKind.Reset, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as ResetOperation,
178
Rebase: { kind: OperationKind.Rebase, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as RebaseOperation,
179
RebaseAbort: { kind: OperationKind.RebaseAbort, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as RebaseAbortOperation,
180
RebaseContinue: { kind: OperationKind.RebaseContinue, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as RebaseContinueOperation,
181
Refresh: { kind: OperationKind.Refresh, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as RefreshOperation,
182
RevertFiles: (showProgress: boolean) => ({ kind: OperationKind.RevertFiles, blocking: false, readOnly: false, remote: false, retry: false, showProgress } as RevertFilesOperation),
183
RevList: { kind: OperationKind.RevList, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as RevListOperation,
184
RevParse: { kind: OperationKind.RevParse, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as RevParseOperation,
185
SetBranchUpstream: { kind: OperationKind.SetBranchUpstream, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as SetBranchUpstreamOperation,
186
Show: { kind: OperationKind.Show, blocking: false, readOnly: true, remote: false, retry: false, showProgress: false } as ShowOperation,
187
Stage: { kind: OperationKind.Stage, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as StageOperation,
188
Status: { kind: OperationKind.Status, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as StatusOperation,
189
Stash: (readOnly: boolean) => ({ kind: OperationKind.Stash, blocking: false, readOnly, remote: false, retry: false, showProgress: true } as StashOperation),
190
SubmoduleUpdate: { kind: OperationKind.SubmoduleUpdate, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as SubmoduleUpdateOperation,
191
Sync: { kind: OperationKind.Sync, blocking: true, readOnly: false, remote: true, retry: true, showProgress: true } as SyncOperation,
192
Tag: { kind: OperationKind.Tag, blocking: false, readOnly: false, remote: false, retry: false, showProgress: true } as TagOperation,
193
Worktree: (readOnly: boolean) => ({ kind: OperationKind.Worktree, blocking: false, readOnly, remote: false, retry: false, showProgress: true } as WorktreeOperation)
194
};
195
196
export interface OperationResult {
197
operation: Operation;
198
error: unknown;
199
}
200
201
interface IOperationManager {
202
getOperations(operationKind: OperationKind): Operation[];
203
isIdle(): boolean;
204
isRunning(operationKind: OperationKind): boolean;
205
shouldDisableCommands(): boolean;
206
shouldShowProgress(): boolean;
207
}
208
209
export class OperationManager implements IOperationManager {
210
211
private operations = new Map<OperationKind, Set<Operation>>();
212
213
constructor(private readonly logger: LogOutputChannel) { }
214
215
start(operation: Operation): void {
216
if (this.operations.has(operation.kind)) {
217
this.operations.get(operation.kind)!.add(operation);
218
} else {
219
this.operations.set(operation.kind, new Set([operation]));
220
}
221
222
this.logger.trace(`[OperationManager][start] ${operation.kind} (blocking: ${operation.blocking}, readOnly: ${operation.readOnly}; retry: ${operation.retry}; showProgress: ${operation.showProgress})`);
223
}
224
225
end(operation: Operation): void {
226
const operationSet = this.operations.get(operation.kind);
227
if (operationSet) {
228
operationSet.delete(operation);
229
if (operationSet.size === 0) {
230
this.operations.delete(operation.kind);
231
}
232
}
233
234
this.logger.trace(`[OperationManager][end] ${operation.kind} (blocking: ${operation.blocking}, readOnly: ${operation.readOnly}; retry: ${operation.retry}; showProgress: ${operation.showProgress})`);
235
}
236
237
getOperations(operationKind: OperationKind): Operation[] {
238
const operationSet = this.operations.get(operationKind);
239
return operationSet ? Array.from(operationSet) : [];
240
}
241
242
isIdle(): boolean {
243
const operationSets = this.operations.values();
244
245
for (const operationSet of operationSets) {
246
for (const operation of operationSet) {
247
if (!operation.readOnly) {
248
return false;
249
}
250
}
251
}
252
253
return true;
254
}
255
256
isRunning(operationKind: OperationKind): boolean {
257
return this.operations.has(operationKind);
258
}
259
260
shouldDisableCommands(): boolean {
261
const operationSets = this.operations.values();
262
263
for (const operationSet of operationSets) {
264
for (const operation of operationSet) {
265
if (operation.blocking) {
266
return true;
267
}
268
}
269
}
270
271
return false;
272
}
273
274
shouldShowProgress(): boolean {
275
const operationSets = this.operations.values();
276
277
for (const operationSet of operationSets) {
278
for (const operation of operationSet) {
279
if (operation.showProgress) {
280
return true;
281
}
282
}
283
}
284
285
return false;
286
}
287
}
288
289