how git diff works in vscode git extension?
Im trying to create a version control extension in vscode. I tried to look at the git implementation in vscode. 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 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 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?
thanks and regards,
Sathish V
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 git repo. And then it has the 2 resources to compare.
Those 2 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, ...)
...
leftResource
is file on disk and rightResource
is URI for file in git repo.