To add a "when" clause to your shortcut bindings for the "git.sync" command in Visual Studio Code, you can specify a condition that should be met for the shortcut to be active. In your case, you want the shortcut to be active only if there are uncommitted changes in your Git repository.
You can use the following "when" clause to achieve this:
"when": "gitUncommittedChanges"
This clause checks if there are uncommitted changes in your Git repository before allowing the shortcut to trigger the "git.sync" command.
So, your updated shortcut binding would look like this:
Neither do I see anything undocumented by searching "when":.*scm with regex mode on in the defaultKeybindings.json pseudo-file.
Neither do I see anything undocumented by searching github.dev/microsoft/vscode. Searching (createKey|createScoped|createOverlayy|setContext|removeContext|getContext|createChildContext).*?(git|scm) with regex mode on,
There’s a git.state context key, but the only values listed for its type definition APIState in extensions/git/src/api/git.d.ts are 'initialized' and 'uninitialized'.
There’s also the gitRebaseInProgress and gitMergeInProgress context keys, but I don’t think those are sufficient to get what you want.
There’s also scmActiveResourceHasChanges and scmActiveResourceRepository, but those are only for the active resource (singular) and not the whole repository.
So I’d suggest that you raise a feature-request issue ticket asking for it. If you do, please comment here with a link to your issue ticket, or edit the link into this answer post.
2
Answers
To add a "when" clause to your shortcut bindings for the "git.sync" command in Visual Studio Code, you can specify a condition that should be met for the shortcut to be active. In your case, you want the shortcut to be active only if there are uncommitted changes in your Git repository.
You can use the following "when" clause to achieve this:
This clause checks if there are uncommitted changes in your Git repository before allowing the shortcut to trigger the "git.sync" command.
So, your updated shortcut binding would look like this:
This ensures that the shortcut will only work when there are uncommitted changes in your Git repository.
As far as I know, at the time of this writing (VS Code 1.85), there is no such context key you can use to do this. To substantiate:
You can see docs for available context keys in https://code.visualstudio.com/api/references/when-clause-contexts. I don’t see anything that sounds useful there when searching "scm" or "git".
Neither do I see anything undocumented by searching
"when":.*scm
with regex mode on in the defaultKeybindings.json pseudo-file.Neither do I see anything undocumented by searching github.dev/microsoft/vscode. Searching
(createKey|createScoped|createOverlayy|setContext|removeContext|getContext|createChildContext).*?(git|scm)
with regex mode on,git.state
context key, but the only values listed for its type definitionAPIState
in extensions/git/src/api/git.d.ts are'initialized'
and'uninitialized'
.gitRebaseInProgress
andgitMergeInProgress
context keys, but I don’t think those are sufficient to get what you want.scmActiveResourceHasChanges
andscmActiveResourceRepository
, but those are only for the active resource (singular) and not the whole repository.And looking at the existing issue tickets, I don’t see one already asking for this: https://github.com/microsoft/vscode/issues?q=is%3Aopen+is%3Aissue+label%3Acontext-keys.
So I’d suggest that you raise a feature-request issue ticket asking for it. If you do, please comment here with a link to your issue ticket, or edit the link into this answer post.