Update extension UI after a Git commit in VS Code

Viewed 136

I have a VS Code extension called Git Mob VS Code.

I'd like to be able to call a CLI command after a commit and update my extension UI. The CLI command is easy and can be done using a post-commit hook. However, the issue is with updating the UI, as it has no way of knowing if a commit has happened.

I'm looking for ideas on how I can achieve this.

Here is the related issue: Add option to clear co-authors after commit

Thanks.

1 Answers

One approach I have found which is similar to how the vs code git extension does it. Is to watch the .git/COMMIT_EDITMSG file.

When the file is updated we know a commit in progress and you can run a VS code UI update you need.

However, it's not perfect because if the commit fails (e.g. commit-msg hook exits non-zero) the file watch will still see a change and run your commands. Depending on the operations you want to do this might or might not be a big issue.

NodeJS example:

const fs = require("fs");

fs.watch('project-folder/.git/COMMIT_EDITMSG', function (evt, filename) {
  if (filename) {
 // changes trigger multiple times which you might want to handle
    if (debounceFsWatch()) return;
 // run your commands here
    console.log('event, filename', evt + ' ' + filename);
  }
});

Implementation in Git Mob VS Code

Related