Skip to main content

Captain Hook Reject Push Plugin

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

These days we get more and more in situations where we have to push our code into Git repositories of our customers, either because the customer wants access to the source code or because we are working closely with their in-house team. Maintaining our own local copy of the source code and constantly keeping our copy in sync is a no brainer with Git.

Things get a little bit more complicated if our local copy needs some changes so that it runs locally as intended. With that I mean things like modifications in composer.json so that dependencies are pulled from our GitLab instance instead of the customers' instance or adjustments in the Docker setup. When pushing the code back to the repository of the customer those changes need to be removed. When doing things manually, things can fail. And they did. A couple of times. It is easy to forget to remove the commits in question. Pushing our changes to the customers' repository will lead to trouble in the customer's development team.

I was thinking back and forth how we could automate this or at least make sure that pushes won't go through when certain commit Ids can found in the Git history. Being a fan of Captain Hook, a git hook manager written in PHP, I thought the easiest solution is to write a plugin for Captain Hook. The plugin could check the history of the current branch for a given set of commit Ids and deny the push if one of those commit Ids can found in the history. This is what bitexpert/captainhook-rejectpush is all about.

To install the plugin run the following composer command:

composer.phar require --dev bitexpert/captainhook-rejectpush

Afterwards, add the following code to your captainhook.json configuration:

{
"pre-commit": {
"enabled": true,
"actions": [
{
"action": "\\bitExpert\\CaptainHook\\RejectPush\\RejectPushAction",
"options": {
"my-origin": [
"cc9d54f"
],
"other-remote": [
"41ce954"
]
}
}
]
}
}

In the options array list all origins and the commit Ids that are blacklisted for that origin and need to be removed before the push. The reject push plugin will now check for every push to the defined remotes if one of those commit Ids can be found in the git history. If so, the push will be denied.