Git checkout event?

Answered

I wonder if there's a possibility to listen to git checkout event. I've seen API and code samples for pushes and commits, but got no luck looking for its checkout counterpart. Is there a way to handle the case as soon as checkout gets successfully performed?

0
7 comments

You can try `com.intellij.openapi.vcs.BranchChangeListener`. but it might not be fired for some checkouts (ex: performed from command line or for rolled back checkouts).

You can also listen for all `git4idea.repo.GitRepository#GIT_REPO_CHANGE` events and check if `git4idea.repo.GitRepository#getCurrentBranch` is not the same as it was last time.

It is hard to detect if there are an ongoing operation in repository (ex: executed from command line).
For IDE-side commands you can try taking `GitVcs.getInstance(project).getCommandLock().readLock()` (it it fails, there are an ongoing write operation).

0

Thanks, Aleksey! I was mostly aiming for specific commit checkouts and as I can see I don't have many options there. The solution that comes to mind is to manually look for HEAD every time before I invoke my functionality (to know where the HEAD really is). However, it takes some time before the checkout is finished and 'repository.getCurrentRevision()' keeps returning repetitive values. Can you think of options I have for the task?

0

You could, probably, register "post-checkout" hook in git and notify IDE from it (using launcher, custom url handler, rpc, etc).
But that requires modifying repository config, which might be an annoyance (ex: when IDE is not running).

How fast do you need to be notified and how slow is `getCurrentRevision` update for you?
This might, in theory, indicate an issue in IDE refresh process.
Are you able to reproduce it on a small project/repository? Which OS is being used?

0

Yes, the repo has about 20 commits and we speak 1-2 seconds of delay approx (hard to measure). I'm running it on Windows and it's fairly well-performing PC.

I can't say how fast I want it to be. I just hoped for the 'getCurrentRevision' to be able to grab that moved HEAD, but it didn't pan out. 

There might be an option which I haven't tried building yet which is having my buttons (which switch/check-out next/previous commits) inactive for a couple of seconds, but I hoped for a more standardized way of capturing this kind of events.

Another case when I would need what I'm asking for is when a user checks out a random commit (whereas my buttons do that in order). Then I'd have to react to that action to update the content of the plugin (tool window with text from a file) and adjust the buttons functionality to switch commits with respect to the manually changed HEAD position.

0

If you need to know up-to-date value of HEAD when button is clicked (to know which next commit to checkout), it's better to re-check its value manually via `GitRevisionNumber.resolve(project, root, "HEAD")`.
(But this call should not be used in UI thread (EDT) to enable/disable action, to avoid freezes)

How do you checkout revision in your actions? You can try calling `GitRepository.update` after the checkout to force refresh of repository state.
Otherwise it has to fallback to FS listener on files in `.git` directory, that might increase the delay.

0

Thank you! I'm trying GitRevisionNumber.resolve next.

For checkouts I used GitBrancher.checkout() if memory serves me well with the exact spelling. I guessed it would be the cause of some delay, but it was neat since it provided a lambda parameter to update some content accordingly.

0

IIUC, `GitRepository.getCurrentRevision` should be already updated, when accessed from `callInAwtLater` callback.

0

Please sign in to leave a comment.