Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Git diff work in the vscode Git extension?

I'm 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?

like image 875
Sathish V Avatar asked May 08 '17 12:05

Sathish V


1 Answers

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, ...) ...

  • ... where leftResource is file on disk and rightResource is URI for file in git repo.
like image 170
Hugues M. Avatar answered Nov 20 '22 06:11

Hugues M.