| 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 {RepositoryReference} from 'isl-server/src/RepositoryCache'; |
| b69ab31 | | | 9 | import type {ServerSideTracker} from 'isl-server/src/analytics/serverSideTracker'; |
| b69ab31 | | | 10 | import type {Logger} from 'isl-server/src/logger'; |
| b69ab31 | | | 11 | import type {RepoRelativePath} from 'isl/src/types'; |
| b69ab31 | | | 12 | import {GeneratedStatus, type ChangedFile} from 'isl/src/types'; |
| b69ab31 | | | 13 | import type {Comparison} from 'shared/Comparison'; |
| b69ab31 | | | 14 | import type {Writable} from 'shared/typeUtils'; |
| b69ab31 | | | 15 | import type { |
| b69ab31 | | | 16 | SaplingChangedFile, |
| b69ab31 | | | 17 | SaplingCommandOutput, |
| b69ab31 | | | 18 | SaplingCommitInfo, |
| b69ab31 | | | 19 | SaplingComparison, |
| b69ab31 | | | 20 | SaplingConflictContext, |
| b69ab31 | | | 21 | SaplingCurrentCommitDiff, |
| b69ab31 | | | 22 | SaplingRepository, |
| b69ab31 | | | 23 | } from './api/types'; |
| b69ab31 | | | 24 | import type {EnabledSCMApiFeature} from './types'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | import {generatedFilesDetector} from 'isl-server/src/GeneratedFiles'; |
| b69ab31 | | | 27 | import {Repository} from 'isl-server/src/Repository'; |
| b69ab31 | | | 28 | import {repositoryCache} from 'isl-server/src/RepositoryCache'; |
| b69ab31 | | | 29 | import type {TrackEventName} from 'isl-server/src/analytics/eventNames'; |
| b69ab31 | | | 30 | import {getMainFetchTemplate, parseCommitInfoOutput} from 'isl-server/src/templates'; |
| b69ab31 | | | 31 | import {ResolveOperation, ResolveTool} from 'isl/src/operations/ResolveOperation'; |
| b69ab31 | | | 32 | import {diffCurrentCommit} from 'isl/src/stackEdit/diffSplit'; |
| b69ab31 | | | 33 | import type {DiffCommit} from 'isl/src/stackEdit/diffSplitTypes'; |
| b69ab31 | | | 34 | import * as path from 'path'; |
| b69ab31 | | | 35 | import {beforeRevsetForComparison, ComparisonType} from 'shared/Comparison'; |
| b69ab31 | | | 36 | import {filterFilesFromPatch, parsePatch} from 'shared/patch/parse'; |
| b69ab31 | | | 37 | import {notEmpty} from 'shared/utils'; |
| b69ab31 | | | 38 | import * as vscode from 'vscode'; |
| b69ab31 | | | 39 | import {encodeSaplingDiffUri} from './DiffContentProvider'; |
| 5ffddc6 | | | 40 | import IgnoredFileDecorationProvider from './IgnoredFileDecorationProvider'; |
| b69ab31 | | | 41 | import SaplingFileDecorationProvider from './SaplingFileDecorationProvider'; |
| b69ab31 | | | 42 | import {executeVSCodeCommand} from './commands'; |
| b69ab31 | | | 43 | import {getCLICommand} from './config'; |
| b69ab31 | | | 44 | import {t} from './i18n'; |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | const mergeConflictStartRegex = new RegExp('<{7}|>{7}|[|]{7}'); |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | export class VSCodeReposList { |
| b69ab31 | | | 49 | private knownRepos = new Map</* attached folder root */ string, RepositoryReference>(); |
| b69ab31 | | | 50 | private vscodeRepos = new Map</* repo root path */ string, VSCodeRepo>(); |
| b69ab31 | | | 51 | private disposables: Array<vscode.Disposable> = []; |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | private reposByPath = new Map</* arbitrary subpath of repo */ string, VSCodeRepo>(); |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | constructor( |
| b69ab31 | | | 56 | private logger: Logger, |
| b69ab31 | | | 57 | private tracker: ServerSideTracker, |
| b69ab31 | | | 58 | private enabledFeatures: Set<EnabledSCMApiFeature>, |
| b69ab31 | | | 59 | ) { |
| b69ab31 | | | 60 | if (vscode.workspace.workspaceFolders) { |
| b69ab31 | | | 61 | this.updateRepos(vscode.workspace.workspaceFolders, []); |
| b69ab31 | | | 62 | } |
| b69ab31 | | | 63 | this.disposables.push( |
| b69ab31 | | | 64 | vscode.workspace.onDidChangeWorkspaceFolders(e => { |
| b69ab31 | | | 65 | this.updateRepos(e.added, e.removed); |
| b69ab31 | | | 66 | }), |
| b69ab31 | | | 67 | ); |
| b69ab31 | | | 68 | // TODO: consider also listening for vscode.workspace.onDidOpenTextDocument to support repos |
| b69ab31 | | | 69 | // for ad-hoc non-workspace-folder files |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | private updateRepos( |
| b69ab31 | | | 73 | added: ReadonlyArray<vscode.WorkspaceFolder>, |
| b69ab31 | | | 74 | removed: ReadonlyArray<vscode.WorkspaceFolder>, |
| b69ab31 | | | 75 | ) { |
| b69ab31 | | | 76 | for (const add of added) { |
| b69ab31 | | | 77 | const {fsPath} = add.uri; |
| b69ab31 | | | 78 | if (this.knownRepos.has(fsPath)) { |
| b69ab31 | | | 79 | throw new Error(`Attempted to add workspace folder path twice: ${fsPath}`); |
| b69ab31 | | | 80 | } |
| b69ab31 | | | 81 | const repoReference = repositoryCache.getOrCreate({ |
| b69ab31 | | | 82 | cwd: fsPath, |
| b69ab31 | | | 83 | cmd: getCLICommand(), |
| b69ab31 | | | 84 | logger: this.logger, |
| b69ab31 | | | 85 | tracker: this.tracker, |
| b69ab31 | | | 86 | }); |
| b69ab31 | | | 87 | this.knownRepos.set(fsPath, repoReference); |
| b69ab31 | | | 88 | repoReference.promise.then(repo => { |
| b69ab31 | | | 89 | if (repo instanceof Repository) { |
| b69ab31 | | | 90 | const root = repo?.info.repoRoot; |
| b69ab31 | | | 91 | const existing = this.vscodeRepos.get(root); |
| b69ab31 | | | 92 | if (existing) { |
| b69ab31 | | | 93 | return; |
| b69ab31 | | | 94 | } |
| b69ab31 | | | 95 | const vscodeRepo = new VSCodeRepo(repo, this.logger, this.enabledFeatures); |
| b69ab31 | | | 96 | this.vscodeRepos.set(root, vscodeRepo); |
| b69ab31 | | | 97 | repo.onDidDispose(() => { |
| b69ab31 | | | 98 | vscodeRepo.dispose(); |
| b69ab31 | | | 99 | this.vscodeRepos.delete(root); |
| b69ab31 | | | 100 | }); |
| b69ab31 | | | 101 | |
| b69ab31 | | | 102 | this.emitActiveRepos(); |
| b69ab31 | | | 103 | } |
| b69ab31 | | | 104 | }); |
| b69ab31 | | | 105 | } |
| b69ab31 | | | 106 | for (const remove of removed) { |
| b69ab31 | | | 107 | const {fsPath} = remove.uri; |
| b69ab31 | | | 108 | const repo = this.knownRepos.get(fsPath); |
| b69ab31 | | | 109 | repo?.unref(); |
| b69ab31 | | | 110 | this.knownRepos.delete(fsPath); |
| b69ab31 | | | 111 | } |
| b69ab31 | | | 112 | |
| b69ab31 | | | 113 | executeVSCodeCommand('setContext', 'sapling:hasRepo', this.knownRepos.size > 0); |
| b69ab31 | | | 114 | |
| b69ab31 | | | 115 | Promise.all(Array.from(this.knownRepos.values()).map(repo => repo.promise)).then(repos => { |
| b69ab31 | | | 116 | const hasRemoteLinkRepo = repos.some( |
| b69ab31 | | | 117 | repo => repo instanceof Repository && repo.codeReviewProvider?.getRemoteFileURL, |
| b69ab31 | | | 118 | ); |
| b69ab31 | | | 119 | executeVSCodeCommand('setContext', 'sapling:hasRemoteLinkRepo', hasRemoteLinkRepo); |
| b69ab31 | | | 120 | }); |
| b69ab31 | | | 121 | } |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | /** return the VSCodeRepo that contains the given path */ |
| b69ab31 | | | 124 | public repoForPath(path: string): VSCodeRepo | undefined { |
| b69ab31 | | | 125 | if (this.reposByPath.has(path)) { |
| b69ab31 | | | 126 | return this.reposByPath.get(path); |
| b69ab31 | | | 127 | } |
| b69ab31 | | | 128 | for (const value of this.vscodeRepos.values()) { |
| b69ab31 | | | 129 | if (path.startsWith(value.rootPath)) { |
| b69ab31 | | | 130 | return value; |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | return undefined; |
| b69ab31 | | | 134 | } |
| b69ab31 | | | 135 | |
| b69ab31 | | | 136 | public repoForPhabricatorCallsign(callsign: string): VSCodeRepo | undefined { |
| b69ab31 | | | 137 | for (const repo of this.vscodeRepos.values()) { |
| b69ab31 | | | 138 | const system = repo.repo.info.codeReviewSystem; |
| b69ab31 | | | 139 | if (system.type === 'phabricator' && system.callsign === callsign) { |
| b69ab31 | | | 140 | return repo; |
| b69ab31 | | | 141 | } |
| b69ab31 | | | 142 | } |
| b69ab31 | | | 143 | return undefined; |
| b69ab31 | | | 144 | } |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | private emitActiveRepos() { |
| b69ab31 | | | 147 | for (const cb of this.updateCallbacks) { |
| b69ab31 | | | 148 | cb(Array.from(this.vscodeRepos.values())); |
| b69ab31 | | | 149 | } |
| b69ab31 | | | 150 | } |
| b69ab31 | | | 151 | |
| b69ab31 | | | 152 | private updateCallbacks: Array<(repos: Array<VSCodeRepo>) => void> = []; |
| b69ab31 | | | 153 | /** Subscribe to the list of active repositories */ |
| b69ab31 | | | 154 | public observeActiveRepos(cb: (repos: Array<VSCodeRepo>) => void): vscode.Disposable { |
| b69ab31 | | | 155 | this.updateCallbacks.push(cb); |
| b69ab31 | | | 156 | return { |
| b69ab31 | | | 157 | dispose: () => { |
| b69ab31 | | | 158 | this.updateCallbacks = this.updateCallbacks.filter(c => c !== cb); |
| b69ab31 | | | 159 | }, |
| b69ab31 | | | 160 | }; |
| b69ab31 | | | 161 | } |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | public getCurrentActiveRepos(): Array<VSCodeRepo> { |
| b69ab31 | | | 164 | return Array.from(this.vscodeRepos.values()); |
| b69ab31 | | | 165 | } |
| b69ab31 | | | 166 | |
| b69ab31 | | | 167 | public dispose() { |
| b69ab31 | | | 168 | for (const disposable of this.disposables) { |
| b69ab31 | | | 169 | disposable.dispose(); |
| b69ab31 | | | 170 | } |
| b69ab31 | | | 171 | } |
| b69ab31 | | | 172 | } |
| b69ab31 | | | 173 | |
| b69ab31 | | | 174 | type SaplingResourceState = vscode.SourceControlResourceState & { |
| b69ab31 | | | 175 | status?: string; |
| b69ab31 | | | 176 | }; |
| b69ab31 | | | 177 | export type SaplingResourceGroup = vscode.SourceControlResourceGroup & { |
| b69ab31 | | | 178 | resourceStates: SaplingResourceState[]; |
| b69ab31 | | | 179 | }; |
| b69ab31 | | | 180 | /** |
| b69ab31 | | | 181 | * vscode-API-compatible repository. |
| b69ab31 | | | 182 | * This handles vscode-api integrations, but defers to Repository for any actual work. |
| b69ab31 | | | 183 | */ |
| b69ab31 | | | 184 | export class VSCodeRepo implements vscode.QuickDiffProvider, SaplingRepository { |
| b69ab31 | | | 185 | private disposables: Array<vscode.Disposable> = []; |
| b69ab31 | | | 186 | private sourceControl?: vscode.SourceControl; |
| b69ab31 | | | 187 | private resourceGroups?: Record< |
| b69ab31 | | | 188 | 'changes' | 'untracked' | 'unresolved' | 'resolved', |
| b69ab31 | | | 189 | SaplingResourceGroup |
| b69ab31 | | | 190 | >; |
| b69ab31 | | | 191 | public rootUri: vscode.Uri; |
| b69ab31 | | | 192 | public rootPath: string; |
| b69ab31 | | | 193 | |
| b69ab31 | | | 194 | constructor( |
| b69ab31 | | | 195 | public repo: Repository, |
| b69ab31 | | | 196 | private logger: Logger, |
| b69ab31 | | | 197 | private enabledFeatures: Set<EnabledSCMApiFeature>, |
| b69ab31 | | | 198 | ) { |
| b69ab31 | | | 199 | repo.onDidDispose(() => this.dispose()); |
| b69ab31 | | | 200 | this.rootUri = vscode.Uri.file(repo.info.repoRoot); |
| b69ab31 | | | 201 | this.rootPath = repo.info.repoRoot; |
| b69ab31 | | | 202 | |
| b69ab31 | | | 203 | this.autoResolveFilesOnSave(); |
| b69ab31 | | | 204 | |
| b69ab31 | | | 205 | if (!this.enabledFeatures.has('sidebar')) { |
| b69ab31 | | | 206 | // if sidebar is not enabled, VSCodeRepo is mostly useless, but still used for checking which paths can be used for ISL and blame. |
| b69ab31 | | | 207 | return; |
| b69ab31 | | | 208 | } |
| b69ab31 | | | 209 | |
| b69ab31 | | | 210 | this.sourceControl = vscode.scm.createSourceControl( |
| b69ab31 | | | 211 | 'sapling', |
| b69ab31 | | | 212 | t('Sapling'), |
| b69ab31 | | | 213 | vscode.Uri.file(repo.info.repoRoot), |
| b69ab31 | | | 214 | ); |
| b69ab31 | | | 215 | this.sourceControl.quickDiffProvider = this; |
| b69ab31 | | | 216 | this.sourceControl.inputBox.enabled = false; |
| b69ab31 | | | 217 | this.sourceControl.inputBox.visible = false; |
| b69ab31 | | | 218 | this.resourceGroups = { |
| b69ab31 | | | 219 | changes: this.sourceControl.createResourceGroup('changes', t('Uncommitted Changes')), |
| b69ab31 | | | 220 | untracked: this.sourceControl.createResourceGroup('untracked', t('Untracked Changes')), |
| b69ab31 | | | 221 | unresolved: this.sourceControl.createResourceGroup( |
| b69ab31 | | | 222 | 'unresolved', |
| b69ab31 | | | 223 | t('Unresolved Merge Conflicts'), |
| b69ab31 | | | 224 | ), |
| b69ab31 | | | 225 | resolved: this.sourceControl.createResourceGroup('resolved', t('Resolved Merge Conflicts')), |
| b69ab31 | | | 226 | }; |
| b69ab31 | | | 227 | for (const group of Object.values(this.resourceGroups)) { |
| b69ab31 | | | 228 | group.hideWhenEmpty = true; |
| b69ab31 | | | 229 | } |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | const fileDecorationProvider = new SaplingFileDecorationProvider(this, logger); |
| 5ffddc6 | | | 232 | const ignoredFileDecorationProvider = new IgnoredFileDecorationProvider(this, logger); |
| b69ab31 | | | 233 | this.disposables.push( |
| b69ab31 | | | 234 | repo.subscribeToUncommittedChanges(() => { |
| b69ab31 | | | 235 | this.updateResourceGroups(); |
| b69ab31 | | | 236 | }), |
| b69ab31 | | | 237 | repo.onChangeConflictState(() => { |
| b69ab31 | | | 238 | this.updateResourceGroups(); |
| b69ab31 | | | 239 | }), |
| b69ab31 | | | 240 | fileDecorationProvider, |
| 5ffddc6 | | | 241 | ignoredFileDecorationProvider, |
| b69ab31 | | | 242 | ); |
| b69ab31 | | | 243 | this.updateResourceGroups(); |
| b69ab31 | | | 244 | } |
| b69ab31 | | | 245 | |
| b69ab31 | | | 246 | /** If this uri is for file inside the repo or not */ |
| b69ab31 | | | 247 | public containsUri(uri: vscode.Uri): boolean { |
| b69ab31 | | | 248 | return ( |
| b69ab31 | | | 249 | uri.scheme === this.rootUri.scheme && |
| b69ab31 | | | 250 | uri.authority === this.rootUri.authority && |
| b69ab31 | | | 251 | uri.fsPath.startsWith(this.rootPath) |
| b69ab31 | | | 252 | ); |
| b69ab31 | | | 253 | } |
| b69ab31 | | | 254 | |
| b69ab31 | | | 255 | /** If this uri is for a file inside the repo, return the repo-relative path. Otherwise, return undefined. */ |
| b69ab31 | | | 256 | public repoRelativeFsPath(uri: vscode.Uri): string | undefined { |
| b69ab31 | | | 257 | return this.containsUri(uri) ? path.relative(this.rootPath, uri.fsPath) : undefined; |
| b69ab31 | | | 258 | } |
| b69ab31 | | | 259 | |
| b69ab31 | | | 260 | private autoResolveFilesOnSave(): vscode.Disposable { |
| b69ab31 | | | 261 | return vscode.workspace.onDidSaveTextDocument(document => { |
| b69ab31 | | | 262 | const repoRelativePath = this.repoRelativeFsPath(document.uri); |
| b69ab31 | | | 263 | const conflicts = this.repo.getMergeConflicts(); |
| b69ab31 | | | 264 | if (conflicts == null || repoRelativePath == null) { |
| b69ab31 | | | 265 | return; |
| b69ab31 | | | 266 | } |
| b69ab31 | | | 267 | const filesWithConflicts = conflicts.files?.map(file => file.path); |
| b69ab31 | | | 268 | if (filesWithConflicts?.includes(repoRelativePath) !== true) { |
| b69ab31 | | | 269 | return; |
| b69ab31 | | | 270 | } |
| b69ab31 | | | 271 | const autoResolveEnabled = vscode.workspace |
| b69ab31 | | | 272 | .getConfiguration('sapling') |
| b69ab31 | | | 273 | .get<boolean>('markConflictingFilesResolvedOnSave'); |
| b69ab31 | | | 274 | if (!autoResolveEnabled) { |
| b69ab31 | | | 275 | return; |
| b69ab31 | | | 276 | } |
| b69ab31 | | | 277 | const allConflictsThisFileResolved = !mergeConflictStartRegex.test(document.getText()); |
| b69ab31 | | | 278 | if (!allConflictsThisFileResolved) { |
| b69ab31 | | | 279 | return; |
| b69ab31 | | | 280 | } |
| b69ab31 | | | 281 | this.logger.info( |
| b69ab31 | | | 282 | 'auto marking file with no remaining conflicts as resolved:', |
| b69ab31 | | | 283 | repoRelativePath, |
| b69ab31 | | | 284 | ); |
| b69ab31 | | | 285 | |
| b69ab31 | | | 286 | this.repo.runOrQueueOperation( |
| b69ab31 | | | 287 | this.repo.initialConnectionContext, |
| b69ab31 | | | 288 | { |
| b69ab31 | | | 289 | ...new ResolveOperation(repoRelativePath, ResolveTool.mark).getRunnableOperation(), |
| b69ab31 | | | 290 | // Distinguish in analytics from manually resolving |
| b69ab31 | | | 291 | trackEventName: 'AutoMarkResolvedOperation', |
| b69ab31 | | | 292 | }, |
| b69ab31 | | | 293 | () => null, |
| b69ab31 | | | 294 | ); |
| b69ab31 | | | 295 | }); |
| b69ab31 | | | 296 | } |
| b69ab31 | | | 297 | |
| b69ab31 | | | 298 | private updateResourceGroups() { |
| b69ab31 | | | 299 | if (this.resourceGroups == null || this.sourceControl == null) { |
| b69ab31 | | | 300 | return; |
| b69ab31 | | | 301 | } |
| b69ab31 | | | 302 | const data = this.repo.getUncommittedChanges(); |
| b69ab31 | | | 303 | const conflicts = this.repo.getMergeConflicts()?.files; |
| b69ab31 | | | 304 | |
| b69ab31 | | | 305 | // only show merge conflicts if they are given |
| b69ab31 | | | 306 | const fileChanges = conflicts ?? data?.files?.value ?? []; |
| b69ab31 | | | 307 | |
| b69ab31 | | | 308 | const changes: Array<SaplingResourceState> = []; |
| b69ab31 | | | 309 | const untracked: Array<SaplingResourceState> = []; |
| b69ab31 | | | 310 | const unresolved: Array<SaplingResourceState> = []; |
| b69ab31 | | | 311 | const resolved: Array<SaplingResourceState> = []; |
| b69ab31 | | | 312 | |
| b69ab31 | | | 313 | for (const change of fileChanges) { |
| b69ab31 | | | 314 | const uri = vscode.Uri.joinPath(this.rootUri, change.path); |
| b69ab31 | | | 315 | const resource: SaplingResourceState = { |
| b69ab31 | | | 316 | command: { |
| b69ab31 | | | 317 | command: 'vscode.open', |
| b69ab31 | | | 318 | title: 'Open', |
| b69ab31 | | | 319 | arguments: [uri], |
| b69ab31 | | | 320 | }, |
| b69ab31 | | | 321 | resourceUri: uri, |
| b69ab31 | | | 322 | decorations: this.decorationForChange(change), |
| b69ab31 | | | 323 | status: change.status, |
| b69ab31 | | | 324 | }; |
| b69ab31 | | | 325 | switch (change.status) { |
| b69ab31 | | | 326 | case '?': |
| b69ab31 | | | 327 | case '!': |
| b69ab31 | | | 328 | untracked.push(resource); |
| b69ab31 | | | 329 | break; |
| b69ab31 | | | 330 | case 'U': |
| b69ab31 | | | 331 | unresolved.push(resource); |
| b69ab31 | | | 332 | break; |
| b69ab31 | | | 333 | case 'Resolved': |
| b69ab31 | | | 334 | resolved.push(resource); |
| b69ab31 | | | 335 | break; |
| b69ab31 | | | 336 | default: |
| b69ab31 | | | 337 | changes.push(resource); |
| b69ab31 | | | 338 | break; |
| b69ab31 | | | 339 | } |
| b69ab31 | | | 340 | } |
| b69ab31 | | | 341 | this.resourceGroups.changes.resourceStates = changes; |
| b69ab31 | | | 342 | this.resourceGroups.untracked.resourceStates = untracked; |
| b69ab31 | | | 343 | this.resourceGroups.unresolved.resourceStates = unresolved; |
| b69ab31 | | | 344 | this.resourceGroups.resolved.resourceStates = resolved; |
| b69ab31 | | | 345 | |
| b69ab31 | | | 346 | // don't include resolved files in count |
| b69ab31 | | | 347 | this.sourceControl.count = changes.length + untracked.length + unresolved.length; |
| b69ab31 | | | 348 | } |
| b69ab31 | | | 349 | |
| b69ab31 | | | 350 | public getResourceGroups() { |
| b69ab31 | | | 351 | return this.resourceGroups; |
| b69ab31 | | | 352 | } |
| b69ab31 | | | 353 | |
| b69ab31 | | | 354 | public dispose() { |
| b69ab31 | | | 355 | this.disposables.forEach(d => d?.dispose()); |
| b69ab31 | | | 356 | } |
| b69ab31 | | | 357 | |
| b69ab31 | | | 358 | private decorationForChange(change: ChangedFile): vscode.SourceControlResourceDecorations { |
| b69ab31 | | | 359 | const decoration: Writable<vscode.SourceControlResourceDecorations> = {}; |
| b69ab31 | | | 360 | switch (change.status) { |
| b69ab31 | | | 361 | case 'M': |
| b69ab31 | | | 362 | decoration.iconPath = new vscode.ThemeIcon('diff-modified', themeColors.modified); |
| b69ab31 | | | 363 | break; |
| b69ab31 | | | 364 | case 'A': |
| b69ab31 | | | 365 | decoration.iconPath = new vscode.ThemeIcon('diff-added', themeColors.added); |
| b69ab31 | | | 366 | break; |
| b69ab31 | | | 367 | case 'R': |
| b69ab31 | | | 368 | decoration.iconPath = new vscode.ThemeIcon('diff-removed', themeColors.deleted); |
| b69ab31 | | | 369 | break; |
| b69ab31 | | | 370 | case '?': |
| b69ab31 | | | 371 | decoration.faded = true; |
| b69ab31 | | | 372 | decoration.iconPath = new vscode.ThemeIcon('question', themeColors.untracked); |
| b69ab31 | | | 373 | break; |
| b69ab31 | | | 374 | case '!': |
| b69ab31 | | | 375 | decoration.faded = true; |
| b69ab31 | | | 376 | decoration.iconPath = new vscode.ThemeIcon('warning', themeColors.untracked); |
| b69ab31 | | | 377 | break; |
| b69ab31 | | | 378 | case 'U': |
| b69ab31 | | | 379 | decoration.iconPath = new vscode.ThemeIcon('diff-ignored', themeColors.conflicting); |
| b69ab31 | | | 380 | break; |
| b69ab31 | | | 381 | case 'Resolved': |
| b69ab31 | | | 382 | decoration.faded = true; |
| b69ab31 | | | 383 | decoration.iconPath = new vscode.ThemeIcon('pass', themeColors.added); |
| b69ab31 | | | 384 | break; |
| b69ab31 | | | 385 | default: |
| b69ab31 | | | 386 | break; |
| b69ab31 | | | 387 | } |
| b69ab31 | | | 388 | return decoration; |
| b69ab31 | | | 389 | } |
| b69ab31 | | | 390 | |
| b69ab31 | | | 391 | /** |
| b69ab31 | | | 392 | * Use ContentProvider + encodeSaplingDiffUri |
| b69ab31 | | | 393 | */ |
| b69ab31 | | | 394 | provideOriginalResource(uri: vscode.Uri): vscode.Uri | undefined { |
| b69ab31 | | | 395 | if (uri.scheme !== 'file') { |
| b69ab31 | | | 396 | return; |
| b69ab31 | | | 397 | } |
| b69ab31 | | | 398 | // TODO: make this configurable via vscode setting to allow |
| b69ab31 | | | 399 | // diff gutters to be either uncommitted changes / head changes / stack changes |
| b69ab31 | | | 400 | const comparison = {type: ComparisonType.UncommittedChanges} as Comparison; |
| b69ab31 | | | 401 | |
| b69ab31 | | | 402 | return encodeSaplingDiffUri(uri, beforeRevsetForComparison(comparison)); |
| b69ab31 | | | 403 | } |
| b69ab31 | | | 404 | |
| b69ab31 | | | 405 | //////////////////////////////////////////////////////////////////////////////////// |
| b69ab31 | | | 406 | |
| b69ab31 | | | 407 | get info() { |
| b69ab31 | | | 408 | return this.repo.info; |
| b69ab31 | | | 409 | } |
| b69ab31 | | | 410 | |
| b69ab31 | | | 411 | getDotCommit(): SaplingCommitInfo | undefined { |
| b69ab31 | | | 412 | return this.repo.getHeadCommit(); |
| b69ab31 | | | 413 | } |
| b69ab31 | | | 414 | onChangeDotCommit(callback: (commit: SaplingCommitInfo | undefined) => void): vscode.Disposable { |
| b69ab31 | | | 415 | return this.repo.subscribeToHeadCommit(callback); |
| b69ab31 | | | 416 | } |
| b69ab31 | | | 417 | getUncommittedChanges(): ReadonlyArray<SaplingChangedFile> { |
| b69ab31 | | | 418 | return this.repo.getUncommittedChanges()?.files?.value ?? []; |
| b69ab31 | | | 419 | } |
| b69ab31 | | | 420 | onChangeUncommittedChanges( |
| b69ab31 | | | 421 | callback: (changes: ReadonlyArray<SaplingChangedFile>) => void, |
| b69ab31 | | | 422 | ): vscode.Disposable { |
| b69ab31 | | | 423 | return this.repo.subscribeToUncommittedChanges(result => { |
| b69ab31 | | | 424 | callback(result.files?.value ?? []); |
| b69ab31 | | | 425 | }); |
| b69ab31 | | | 426 | } |
| b69ab31 | | | 427 | |
| b69ab31 | | | 428 | runSlCommand( |
| b69ab31 | | | 429 | args: Array<string>, |
| b69ab31 | | | 430 | eventName: TrackEventName | undefined = undefined, |
| b69ab31 | | | 431 | ): Promise<SaplingCommandOutput> { |
| b69ab31 | | | 432 | return this.repo.runCommand(args, eventName, this.repo.initialConnectionContext); |
| b69ab31 | | | 433 | } |
| b69ab31 | | | 434 | |
| b69ab31 | | | 435 | async getCurrentStack(): Promise<ReadonlyArray<SaplingCommitInfo>> { |
| b69ab31 | | | 436 | const revset = 'sort(draft() and ancestors(.), topo)'; |
| b69ab31 | | | 437 | const result = await this.runSlCommand( |
| b69ab31 | | | 438 | ['log', '--rev', revset, '--template', getMainFetchTemplate(this.info.codeReviewSystem)], |
| b69ab31 | | | 439 | 'GetCurrentStack', |
| b69ab31 | | | 440 | ); |
| b69ab31 | | | 441 | if (result.exitCode === 0) { |
| b69ab31 | | | 442 | return parseCommitInfoOutput(this.logger, result.stdout, this.repo.info.codeReviewSystem); |
| b69ab31 | | | 443 | } else { |
| b69ab31 | | | 444 | throw new Error(result.stderr); |
| b69ab31 | | | 445 | } |
| b69ab31 | | | 446 | } |
| b69ab31 | | | 447 | |
| b69ab31 | | | 448 | async getFullFocusedBranch(): Promise<ReadonlyArray<SaplingCommitInfo>> { |
| b69ab31 | | | 449 | const revset = 'sort(focusedbranch(.), topo)'; |
| b69ab31 | | | 450 | const result = await this.runSlCommand( |
| b69ab31 | | | 451 | ['log', '--rev', revset, '--template', getMainFetchTemplate(this.info.codeReviewSystem)], |
| b69ab31 | | | 452 | 'GetFullFocusedBranch', |
| b69ab31 | | | 453 | ); |
| b69ab31 | | | 454 | if (result.exitCode === 0) { |
| b69ab31 | | | 455 | return parseCommitInfoOutput(this.logger, result.stdout, this.repo.info.codeReviewSystem); |
| b69ab31 | | | 456 | } else { |
| b69ab31 | | | 457 | throw new Error(result.stderr); |
| b69ab31 | | | 458 | } |
| b69ab31 | | | 459 | } |
| b69ab31 | | | 460 | |
| b69ab31 | | | 461 | /** @deprecated - prefer `diff({type: 'Commit', hash: commit || '.'})` */ |
| b69ab31 | | | 462 | async getDiff(commit?: string): Promise<string> { |
| b69ab31 | | | 463 | const result = await this.runSlCommand(['diff', '-c', commit || '.']); |
| b69ab31 | | | 464 | |
| b69ab31 | | | 465 | if (result.exitCode === 0) { |
| b69ab31 | | | 466 | return result.stdout; |
| b69ab31 | | | 467 | } else { |
| b69ab31 | | | 468 | throw new Error(result.stderr); |
| b69ab31 | | | 469 | } |
| b69ab31 | | | 470 | } |
| b69ab31 | | | 471 | |
| b69ab31 | | | 472 | async diff( |
| b69ab31 | | | 473 | comparison: Comparison | SaplingComparison, |
| b69ab31 | | | 474 | options?: {excludeGenerated?: boolean}, |
| b69ab31 | | | 475 | ): Promise<string> { |
| b69ab31 | | | 476 | const output = await this.repo.runDiff( |
| b69ab31 | | | 477 | this.repo.initialConnectionContext, |
| b69ab31 | | | 478 | comparison as Comparison, |
| b69ab31 | | | 479 | undefined, |
| b69ab31 | | | 480 | ); |
| b69ab31 | | | 481 | |
| b69ab31 | | | 482 | if (options?.excludeGenerated === true) { |
| b69ab31 | | | 483 | const filenames = parsePatch(output) |
| b69ab31 | | | 484 | .map(diff => diff.newFileName ?? diff.oldFileName) |
| b69ab31 | | | 485 | .filter(notEmpty); |
| b69ab31 | | | 486 | const generatedFiles = await this.getGeneratedPaths(filenames); |
| b69ab31 | | | 487 | if (generatedFiles) { |
| b69ab31 | | | 488 | return filterFilesFromPatch(output, generatedFiles); |
| b69ab31 | | | 489 | } |
| b69ab31 | | | 490 | } |
| b69ab31 | | | 491 | return output; |
| b69ab31 | | | 492 | } |
| b69ab31 | | | 493 | |
| b69ab31 | | | 494 | async getGeneratedPaths(paths: Array<RepoRelativePath>): Promise<Array<RepoRelativePath>> { |
| b69ab31 | | | 495 | const generatedMap = await generatedFilesDetector.queryFilesGenerated( |
| b69ab31 | | | 496 | this.repo, |
| b69ab31 | | | 497 | this.repo.initialConnectionContext, |
| b69ab31 | | | 498 | this.repo.info.repoRoot, |
| b69ab31 | | | 499 | paths, |
| b69ab31 | | | 500 | ); |
| b69ab31 | | | 501 | return paths.filter( |
| b69ab31 | | | 502 | path => |
| b69ab31 | | | 503 | generatedMap[path] === GeneratedStatus.Generated || |
| b69ab31 | | | 504 | generatedMap[path] === GeneratedStatus.PartiallyGenerated, |
| b69ab31 | | | 505 | ); |
| b69ab31 | | | 506 | } |
| b69ab31 | | | 507 | |
| b69ab31 | | | 508 | async commit(title: string, commitMessage: string): Promise<void> { |
| b69ab31 | | | 509 | const message = `${title}\n\n${commitMessage}`; |
| b69ab31 | | | 510 | const result = await this.runSlCommand(['commit', '-m', message]); |
| b69ab31 | | | 511 | |
| b69ab31 | | | 512 | if (result.exitCode !== 0) { |
| b69ab31 | | | 513 | throw new Error(result.stderr); |
| b69ab31 | | | 514 | } |
| b69ab31 | | | 515 | } |
| b69ab31 | | | 516 | |
| b69ab31 | | | 517 | async getMergeConflictContext(): Promise<SaplingConflictContext[]> { |
| b69ab31 | | | 518 | const result = await this.runSlCommand(['debugconflictcontext']); |
| b69ab31 | | | 519 | if (result.exitCode !== 0) { |
| b69ab31 | | | 520 | throw new Error(result.stderr); |
| b69ab31 | | | 521 | } |
| b69ab31 | | | 522 | |
| b69ab31 | | | 523 | return JSON.parse(result.stdout) as SaplingConflictContext[]; |
| b69ab31 | | | 524 | } |
| b69ab31 | | | 525 | |
| b69ab31 | | | 526 | async getCurrentCommitDiff(): Promise<SaplingCurrentCommitDiff> { |
| b69ab31 | | | 527 | const diff = (await diffCurrentCommit( |
| b69ab31 | | | 528 | this.repo, |
| b69ab31 | | | 529 | this.repo.initialConnectionContext, |
| b69ab31 | | | 530 | )) as DiffCommit; |
| b69ab31 | | | 531 | |
| b69ab31 | | | 532 | return {message: diff.message, files: diff.files}; |
| b69ab31 | | | 533 | } |
| b69ab31 | | | 534 | } |
| b69ab31 | | | 535 | |
| b69ab31 | | | 536 | const themeColors = { |
| b69ab31 | | | 537 | deleted: new vscode.ThemeColor('gitDecoration.deletedResourceForeground'), |
| b69ab31 | | | 538 | modified: new vscode.ThemeColor('gitDecoration.modifiedResourceForeground'), |
| b69ab31 | | | 539 | added: new vscode.ThemeColor('gitDecoration.addedResourceForeground'), |
| b69ab31 | | | 540 | untracked: new vscode.ThemeColor('gitDecoration.untrackedResourceForeground'), |
| b69ab31 | | | 541 | conflicting: new vscode.ThemeColor('gitDecoration.conflictingResourceForeground'), |
| b69ab31 | | | 542 | }; |