Captain Hook Reject Push Plugin

Captain Hook Reject Push Plugin

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.


Eintrag von Stephan Hochdörfer am 03.04.2019

Tags: PHP, CaptainHook, git, CLI

Diese Webseite verwendet Cookies, um die Bedienfreundlichkeit zu erhöhen. Mit der Nutzung unserer Webseite wird das Einverständnis erklärt, dass wir Cookies verwenden. Weitere Informationen.