Search code examples
gitvscode-extensions

VSCode extension : programatically opening git diff for specific file (not currently open)


I'm working on a VSCode extension and I need to open the git diff of a file for which I have the full path and/or resource URI.

I have the following vscode.Uri:

{
    "$mid": 1,
    "path": "/d:\\git-skip-demo\\nuxt.config.ts",
    "scheme": "file"
}

(The Uri is valid because I can open the file by calling vscode.open() on it and it works)

I also have the full path:

d:\git-skip-demo\nuxt.config.ts

But none of them work to open the git diff (changes) for the file when I call:

vscode.commands.executeCommand('git.openChange', ___URI_OR_FILENAME_HERE___);

Nothing happens, vscode ignores the call and nothing shows in the dev tools console.

Windows paths and linux path have both been tested, none work.

Any help is greatly appreciated !


Solution

  • This seems to work:

    await vscode.commands.executeCommand('git.openChange', { uri: vscode.Uri.file("C:\\Users\\Mark\\OneDrive\\BuildSACC\\workFlow.txt") });
    

    That opens a git diff editor. Note that nothing is returned from that call.

    So you need to use an argument object like { uri: yourURI }.