How to get the list of files that are selected in the commit dialog?

Answered

I have implemented an action and added it to the group:

<add-to-group group-id="Vcs.MessageActionGroup" anchor="first"/>

I would like to run this action only on the files, that are selected in the commit dialog. In the example below, I have selected GameServer.py and Mine.py. How can I get these two files (path / name / VirtualFile) in my action?

I have tried:

e.dataContext.getData(VcsDataKeys.SELECTED_CHANGES)

but it returns null.


2
4 comments

There is no direct data key.
It is further complicated by three (and a half) different UIs for commit - Modal, Git Staging and Non-Modal.

1) You can use 'com.intellij.openapi.vcs.VcsDataKeys#COMMIT_WORKFLOW_HANDLER' key, safe cast it to the 'AbstractCommitWorkflowHandler', read its 'ui: CommitWorkflowUi' field and call 'com.intellij.vcs.commit.CommitWorkflowUi#getIncludedChanges'.
This should work for all modes.

2) Get 'com.intellij.openapi.vcs.ui.Refreshable#PANEL_KEY' from context, safe cast it to 'com.intellij.openapi.vcs.CheckinProjectPanel', use 'CheckinProjectPanel#getSelectedChanges`.
This should work for both "non-modal" and "modal" commit workflows and old IDE builds, but not for "Git Staging Area".

3) You can use `com.intellij.openapi.vcs.changes.ui.ChangesListView#DATA_KEY` and  VcsTreeModelData.included(view).userObjects(Change.class) / VcsTreeModelData.included(view).userObjects(FilePath.class) (for unversioned files).
But this will only work for "Non-Modal Commit" (visible on the screenshot).

2

To follow up, there's a gray area (in a sense, that it's more of poorly-hidden internal data structures, than API) to access "partially included into commit" files (aka checkboxes in diff viewer gutter).

One can use `com.intellij.openapi.vcs.impl.PartialChangesUtil#getPartialTracker(Project, Change)` on the changes from above.
From there - use `tracker.getChangesToBeCommitted(...): String` to get content that will be committed (ex: using `honorExcludedFromCommit = true // aka do not ignore checkboxes` and changelist from '(change as? ChangeListChange)?.changeListId').
Or iterate 'tracker.getRanges()' checking for 'LocalRange' flags to find needed subset of changed lines.

Main caveat here is that one cannot be sure that data is up-to-date and in some 'stable state' (and is not being reloaded after previous commit, for example). See also 'com.intellij.openapi.vcs.impl.LineStatusTrackerManagerI'.

1

Hello, I have installed a commit analysis and generation plugin by OpenAI, and it has an issue with payload size in the JavaScript project. This issue occurs whenever `package-lock.json` is included in the commit due to its large amount of data. Since the project is open-source, I thought about investigating the problem and noticed that it sends the collected data through `VcsDataKeys.COMMIT_WORKFLOW_HANDLER`. In a basic search, I came across this topic, so it might be relevant here. The question is, how can I add an exception to the collected data, for example, to ignore `package-lock.json` or similar files in other projects?

0

Do you need help filtering `List<T>` by condition?

includedChanges.filter { change -> com.intellij.openapi.vcs.changes.ChangesUtil.getFilePath(change).getName() != "package-lock.json" }

 

0

Please sign in to leave a comment.