Get all selected changes from non-modal commit interface.

Answered

I want to get access to all changes selected (included) in non-modal commit interface. See the attached image as a reference. I have tried it numerous times, browsing the source code, but did not succeed. Ultimatively, it would be great if I could get a revision content of file (like in Change.beforeRevision.content) and a new content of changed file, only with selected (included) changes.

For example, in the attached image, the Planet.java file has been changed in two places, one of them is included, the other is not. I want to get two contents of this file, one without any changes, and the second one with included changes.

This question was already somewhat answered on support, and I was redirected here.

Right now I am trying to implement this code:

com.intellij.openapi.vcs.checkin.TodoCheckinHandlerWorker#execute

But I was told that "... there are a few things that should be taken into account in order to make it work."

6 comments
Comment actions Permalink

>there are a few things that should be taken into account in order to make it work
True.
There are no explicit API to get this information so far. But one can combine multiple calls to get it.

>TodoCheckinHandlerWorker#execute
Yes, this can be used as an example.

There's no context specified, so I assume you're accessing this data from `CheckinHandler` using `CheckinProjectPanel#getSelectedChanges`.

Generally, when thinking about "what will be committed", things may work in three modes:
1) Just a plain old commit ("commit as it is on disk").
2) Commit via Git staging area.
3) Commit using changelists or checkboxes (as shown on the screenshot)

For (1) and (2) it's enough to use `change.getAfterRevision()?.getContent()`.

For (3) `Change` will be an instance of `ChangeListChange` that references committed changelist.
It can be used in `PartialChangesUtil.getPartialTracker(myProject, change)` to get an instance of `PartialLocalLineStatusTracker`.
If it is not null and `hasPartialChangesToCommit() == true`, file will be committed partially.
In this case, `tracker.handlePartialCommit(Side.LEFT, Collections.singletonList(((ChangeListChange)change).getChangeListId()), true).getContent()` can be used to get content that will be committed.
Otherwise, `tracker.getPartialCommitContent` can be used to get "HEAD content", "Local content" and list of changed lines between them, that are going to be committed (this can be used to construct "To be committed content").

0
Comment actions Permalink

Following reasonable question, yes it makes sense to fix platform for `change.getAfterRevision()?.getContent()` to work as expected n all 3 cases.
There must have been a reason, why it was had not been done in the first place. But I'm not sure, which one.

0
Comment actions Permalink

Aleksey Pivovarov thank you very much for an exhaustive answer. I tried it out and it actually works!
I have been trying to do it for quite some time, this part of API is really complex to get around.

Now I can get the HEAD changes and locally accepted (selected by user in non-modal dialog) changes. But I did not find this method:

tracker.getPartialCommitContent

Maybe I am using an old version of git4Idea plugin. But it is enough for now.

Thank you again!

0
Comment actions Permalink

Aleksey Pivovarov

pluginVersion = 0.1.0
pluginSinceBuild = 202
pluginUntilBuild = 211.*
pluginVerifierIdeVersions = 2020.2.4, 2020.3.4, 2021.1.1

platformType = IC
platformVersion = 2020.2.4
platformDownloadSources = true
platformPlugins = com.intellij.java, git4idea, com.intellij.platform.images

I will try to update right now.

0
Comment actions Permalink

Aleksey Pivovarov, updating to newer version of platform helped. Now I can use the 

tracker.getPartialCommitContent()

Thank you!

0

Please sign in to leave a comment.