Visual Studio Code, rebuilt in Rust
Source control
Source control is the model of the repositories that extensions register, the three views that draw it (Changes, Graph, Repositories), and the quick diff in the editor gutter. This file goes one level under ARCHITECTURE.md. Upstream is src/vs/workbench/contrib/scm with src/vs/workbench/api/browser/mainThreadSCM.ts and mainThreadQuickDiff.ts. The diff editor, the multi diff editor and the merge editor have their notes in port/notes.md, "Milestone 14: source control and diff".
What is where
app/src/scm.rs: the model.app/src/scm.rs::Scmis the list ofProviders in registration order, each with itsInput, itsGroups and theirResources. No GPUI.app/src/scm_history.rs: the graph of scmHistory.ts.to_history_item_view_modelsis upstream'stoISCMHistoryItemViewModelArray: history items in, swimlanes and colors out. No GPUI, upstream's tests beside it inapp/src/scm_history/tests.rs.app/src/workbench/scm_view.rs:ScmView(the model plus the view's state, thescmfield of the workbench),handle_scm_request, the Changes view, the status bar entries and the activity badge.app/src/workbench/scm_tree.rs: list and tree mode of the Changes view.app/src/workbench/scm_actions.rs: the input history and the title menus.app/src/workbench/scm_repositories_view.rs: the Repositories view and the artifacts.app/src/workbench/scm_graph_view.rs: the Graph view and its requests.app/src/workbench/editor_view/quick_diff.rs: the quick diff model per file and its gutter, ruler and minimap marks.app/src/workbench/editor_view/quick_diff_widget.rs: the peek under a change.
Who owns what
The extension (git) runs in the extension host and owns the truth: which repositories exist, which files changed, the history. The app holds a mirror and never computes a repository's state itself. There is no git code in the app: upstream's MainThreadGitExtension, platform/git and contrib/git are removed, because they serve chat and agent sessions.
The mirror keeps the host's DTOs as serde_json::Value wherever the app only hands them back: a resource's uri and command, a provider's root_uri, action_button and status_bar_commands, a history item ref. Only what a view reads is a typed field.
The app owns the view state, which the host learns of in three calls: the selected repository ($setSelectedSourceControl), the input box text ($onInputBoxValueChange) and a clicked resource ($executeResourceCommand).
From a provider to the view
- The extension calls a
MainThreadSCMmethod. The session forwards it to the main thread (extension-host.md), andapp/src/main_thread_commands.rs::handle_host_requesthands it toapp/src/workbench/scm_view.rs::handle_scm_request. app/src/scm.rs::applychanges the model and returns aScmChange:None(unknown provider or method),RegisteredorChanged.handle_scm_requestdoes what upstream's listeners do for that method: it bumps the quick diff generation, opens or drops the input box model, moves the shown and focused repository, refreshes the status bar entries, and callscx.notify().
Two methods never reach apply, because they carry no state, only "ask again": $onDidChangeHistoryProviderHistoryItemRefs refreshes the Graph (a silent change of a scrolled Graph only marks it outdated) and $onDidChangeArtifacts reloads the artifacts.
The Graph, the artifacts and the quick diff originals are pulled, not pushed: the app asks ExtHostSCM ($provideHistoryItems, $provideHistoryItemChanges, $provideArtifacts, $provideOriginalResource) and draws the answer. The Graph reloads when app/src/workbench/scm_graph_view.rs::update_scm_graph sees another active repository or other current refs, and only while its pane is expanded.
Invariant: $spliceResourceStates carries several splices per group, whose indices all refer to the list before the call. app/src/scm.rs::splice_resource_states applies them last first. Applied in order, the second splice lands on the wrong rows.
Invariant: a late answer checks generation of app/src/workbench/scm_graph_view.rs::ScmGraph before it writes. reset bumps it, so a page asked for the repository of before is dropped.
Three repositories
scmViewService.ts has three notions, and the port keeps them apart in app/src/workbench/scm_view.rs::ScmView:
visible: the repositories the Changes view shows, picked in the Repositories view.focused: the repository whose input the input box edits. The host hears of it as the selected source control.active: the repository of the active editor or the focused one, whichever changed last (active_scm_provider). The Graph and the status bar entries follow it.
Which repositories show after a restart is scm:view:visibleRepositories in workspace storage. store_scm_view_state writes it only after finished_loading turned true, 5 s after the last repository came or went, as upstream's eventuallyFinishLoading. Stored earlier, a window that closes while git still registers repositories forgets the ones not yet there.
The editor and the diff
The input box is a text model. $registerSourceControl carries inputBoxDocumentUri, and scm_registered opens a text model there with language scminput, so the host sees the commit message as a document. The model has no tab and no host reference: app/src/workbench/host_requests.rs::release_host_models keeps the ones scm_input_models lists. Without that it dropped them on its first run and the next commit panicked. A change of the input goes through edit_scm_input, which updates the provider, the host and the model together.
A clicked resource. A resource whose command is _workbench.open or _workbench.diff runs it in the app. Any other command goes back to the host with $executeResourceCommand. A history item's change opens _workbench.diff with both sides. The multi diff editor of a resource group (_workbench.openScmMultiDiffEditor) takes the group's new files on every $spliceResourceStates through app/src/workbench/editor_view/multi_diff_editor_view.rs::scm_group_resources_changed.
The quick diff. For each visible file, app/src/workbench/editor_view/quick_diff.rs::update_quick_diff asks every provider whose root holds the file for an original URI, loads the original through the host's resolveTextContent as a text model, and diffs the two models with the diff crate (diff/src/computer.rs). The source controls' primary and secondary quick diffs and the providers of MainThreadQuickDiff go through the same path.
update_quick_diffruns in the workbench'srender. It asks the host only whenQuickDiffs::generationmoved or an original's model is gone, and diffs only when a model version moved. A new early exit must keep both checks cheap (performance.md).- Every
MainThreadSCMrequest bumps the generation, except$setInput*,$showValidation*and the two that never reachapply: typing a commit message must not ask for every original again. - The diff runs on the main thread, capped at 1000 ms (
MAX_COMPUTATION_TIME_MS), without moves and without subword extension.
Traps
- git names the ref in the query of a
git:URI. A text model keys on scheme and path, so a non-file URI's query is kept after a?inside the model's path, which keeps two refs of one file apart. Code that needs the file's path or name callsapp/src/editor.rs::split_queryfirst.files/src/lib.rs::Resourcecarries the query too, because git's file system provider fails without it. ScmChange::Nonereturns before the view refresh. A new method added toapplymust returnChanged, or the view keeps the old state until something else notifies.- A provider with
hiddenset is inScm::providersand in the quick diff, and left out of the views' lists. Views readscm_repositories, not the model's list. Provider::countis the provider's own count when it sent one, else the sum of the resources. A view that counts the resources itself disagrees with git, which leaves untracked files out of its count by setting.- The input box value has three writers: the user (
edit_scm_input), the host ($setInputBoxValue) and the stored input history at registration (app/src/workbench/scm_actions.rs::restore_scm_input). All end insync_scm_input_model. Code that writesprovider.input.valuedirectly leaves the text model and the host stale.
Check a change
port/tools/cargo-test groups_and_splices: the model.port/tools/cargo-test scm_history: upstream's graph tests.port/tools/cargo-test scm: the views, the input, the Graph requests (matchesscm_view,scm_actions,scm_graph_view).port/tools/cargo-test quick_diff: providers, changes, the peek.port/tools/cargo-test -p diff fixturewhen the diff itself changed.port/tools/ui-pairwithport/fixtures/ui-pair/m14/quick-diff-peek.jsonfor the look of the peek (testing.md).port/tools/extension-tests gitruns git's own tests against the port; the counts to match are inport/results/scm.md.