I'm trying to create a version control extension in Visual Studio Code. I tried to look at the Git implementation in Visual Studio Code. The confusing part is file diff. In the Git extension source code, for seeing the file changes, vscode.diff
is used. To get the URI of the original file, The new URI is generated by changing the scheme
of the modified file's URI. How is this working?
For example, in https://github.com/Microsoft/vscode/blob/master/extensions/git/src/commands.ts, getRightResource
method, toGitUri
is called with the URI of the file. toGitUri
implemention is as follows,
export function toGitUri(uri: Uri, ref: string, replaceFileExtension = false): Uri {
return uri.with({
scheme: 'git',
path: replaceFileExtension ? `${uri.path}.git` : uri.path,
query: JSON.stringify({
path: uri.fsPath,
ref
})
});
}
Here, toGitUri
is just changing the scheme of the file from file
to git
with the query. This URI is then provided to vscode.diff along with the original URI of the file to show the Git diff. How is the toGitUri
working here?
I think the diff does not happen here. I also think you have correctly understood what this function does: it takes a file URI for file on disk and finds the corresponding URI for the Git repository. And then it has the two resources to compare.
Those two resources are then passed to the built-in diffing functionality.
Let's track the code in 1.12.1 (follow the links one by one):
vscode.diff
is registered here
It delegates to _workbench.diff
That one is registered here and delegates to editor's built-in diff ...
... like this: editorService.openEditor({ leftResource, rightResource, ...)
...
... where leftResource
is file on disk and rightResource
is URI for file in the Git repository.