Skip to content

Visual Studio Code, rebuilt in Rust

Rust
9.0 KB
Raw Blame

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::Scm is the list of Providers in registration order, each with its Input, its Groups and their Resources. No GPUI.
  • app/src/scm_history.rs: the graph of scmHistory.ts. to_history_item_view_models is upstream's toISCMHistoryItemViewModelArray: history items in, swimlanes and colors out. No GPUI, upstream's tests beside it in app/src/scm_history/tests.rs.
  • app/src/workbench/scm_view.rs: ScmView (the model plus the view's state, the scm field 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

  1. The extension calls a MainThreadSCM method. The session forwards it to the main thread (extension-host.md), and app/src/main_thread_commands.rs::handle_host_request hands it to app/src/workbench/scm_view.rs::handle_scm_request.
  2. app/src/scm.rs::apply changes the model and returns a ScmChange: None (unknown provider or method), Registered or Changed.
  3. handle_scm_request does 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 calls cx.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_diff runs in the workbench's render. It asks the host only when QuickDiffs::generation moved 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 MainThreadSCM request bumps the generation, except $setInput*, $showValidation* and the two that never reach apply: 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 calls app/src/editor.rs::split_query first. files/src/lib.rs::Resource carries the query too, because git's file system provider fails without it.
  • ScmChange::None returns before the view refresh. A new method added to apply must return Changed, or the view keeps the old state until something else notifies.
  • A provider with hidden set is in Scm::providers and in the quick diff, and left out of the views' lists. Views read scm_repositories, not the model's list.
  • Provider::count is 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 in sync_scm_input_model. Code that writes provider.input.value directly 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 (matches scm_view, scm_actions, scm_graph_view).
  • port/tools/cargo-test quick_diff: providers, changes, the peek. port/tools/cargo-test -p diff fixture when the diff itself changed.
  • port/tools/ui-pair with port/fixtures/ui-pair/m14/quick-diff-peek.json for the look of the peek (testing.md).
  • port/tools/extension-tests git runs git's own tests against the port; the counts to match are in port/results/scm.md.