Handle Git commit made not from IntelliJ UI
Answered
Hello!
Is there a way to obtain some kind of event when a commit to VCS (Actually, I am only interested in Git support for this) was made not from IDE, but from, for example, a linux terminal? If project, in which commit was made, was opened in IDE at this moment.
Thanks!
Please sign in to leave a comment.
It is not possible to get such notification directly. (As git won't notify IDE about new commits, unless you register your own post-commit hook).
There are might be somewhat hacky way to get smth similar:
You can subscribe for events an event when "something" changes in git repository using `git4idea.repo.GitRepository#GIT_REPO_CHANGE` listener.
If you store old HEAD reference from `GitRepository#getCurrentRevision` and compare it with up-to-date value, you can detect when HEAD was changed.
Here you can try to parse git reflog output (or read reflog file directly) and check if reference was changed because of a new commit or for some other reason (and whether there were multiple commits during single `GIT_REPO_CHANGE` event - reflog file has timestamps).
Reflog can be replaced with a simple check of "committer-date" for latest commits.
But this check can be fooled both by passing `GIT_COMMITTER_DATE` ENV and fast commit-push-pull even sequence.
Thank you, Aleksey!
So, I guess similar hack is used for updating VCS log displayed in UI of Intellij? Because it updates right after commit, even when commits made from outside of the IDE. Where it could be found in sources of intellij-community? Because I think I might need an example :)
VCS log is using `git4idea.repo.GitRepository#GIT_REPO_CHANGE` indeed.
But it doesn't need such hacks, as the event of the commit is not interesting. Only the final state matters, which is a simple `git log` request.
What is your use case for new commits in the plugin?
IJ sources are available on GitHub:
https://github.com/JetBrains/intellij-community