Add items to current GIT commit in CheckinHandler

Answered

Hello,

I'm working on a plugin for Intellij which creates baseline projects for GO and takes care of some specific project configurations in our company.
One of the things I'm trying to do is to attach to a GIT commit and add some files to the current commit.
These files are produced by "govendor" which copies all dependencies to the project folder. 

Right now I execute git commands from within my CheckinHandler code, but I don't think this is a nice way to go. 
Is it somehow possible to add items to the current commit context?

Thanks for you help.

Regards,
Karsten

0
6 comments

So what actions do you actually perform in the CheckinHandler?

0
Avatar
Permanently deleted user

Right now I use the ProcessBuilder to execute the following:

  1. "govendor add +external", which copies all dependencies to a folder called "vendor" in the project root 
  2. "git add vendor", to add all files in the vendor folder to git
  3. "git commit -m "bla"", to commit the vendored files to git

And I am wondering if I could do step 2+3 somehow in code without calling the external git command. 

0

You can use the same framework which IDEA uses to call Git commands.

There are different levels of API available: 

* GitCheckinEnvironment to add and commit to Git;

* Git interface to call various Git commands;

* GitHandler to call Git commands with more control and tuning.

(check usages of these classes for examples)

Note that all of them still call `git` executable under the hood.

0
Avatar
Permanently deleted user

That is good to know. 

But that would produce a new commit, right?
So it wouldn't add the vendoring files to the current commit that is executed when I click on the "commit" button in the "Commit changes" dialog?

0

That's true. It will produce a new commit. Moreover, this commit will be made before the actual user commit (which is probably not what you want especially if user commit fails for some reason).

AFAIK there is no way to add files to the current commit, when commit procedure has already started (i.e. commit dialog was opened).

Since your operation requires only calling a couple of external commands, why don't you use a native Git's post-commit hook, generate the files you need from that hook and amend them to the last commit?

0
Avatar
Permanently deleted user

That is a very good point. I will do it with a post-commit hook. 

Thank you for your help.

0

Please sign in to leave a comment.