Overview


Git Hooks are scripts that run automatically when a specific event occurs in a Git repository. For instance, when a commit is pushed or a pull request is merged.

Git Hooks have several benefits:

  • customizing git’s behavior,
  • triggering actions at key points in the development life-cycle,
  • extending the default actions, that are performed when a repository changes,
  • integrating with other systems.

While it is possible to write a Post- or Pre- Receive Hooks in any language for different git hosting platforms, Atlassian Bitbucket proposes to the individuals to build a full-blown Java add-on as an executable script.

There we suggest using External Hooks Add-on for Atlassian Bitbucket to simplify the development process and exclude adverse effects of working with code and repositories to the companies and developers.

The External Hooks add-on is able to eliminate code-related errors during development life-cycle. And as the main function lets developers write a bash script with git commands, linters or required patterns for analyzing the code. For writing the hooks, developers can use any language such as Bash, Python or Perl.

There are three types of hooks that the add-on supports:

Pre Receive Hook: runs when a developer is pushing commit in the repository, merging pull requests or applying received commits to the codebase. For example, a developer can create a pre-receive hook to check if all commits contain valid Jira ticket numbers. In that case, a developer needs to specify a valid ticket number for the Atlassian Jira installation.

Post Receive Hook: runs after the entire process is completed. It can be used to update the external services or send notifications to a chat or integration server.

Merge Check Hook: runs when a developer opens a pull request in the repository.

Therefore, the add-on can be utilized in many cases:

  • before pushing to a repository (in sync mode),
  • after pushing to a repository (in sync or async mode),
  • on opening a page for a specific pull request (one time per set of commits),
  • on pressing the Merge button.

Features:

  • use any binary script, executable script or shell script as a hook,
  • use any arbitrary git command to execute the script, by specifying the command in the configuration of a repository directory,
  • create a mirror hook to any other git repository which means, publish code to open-source but keep its development inside your company’s repository.

Next Step #

Visit the Getting Started page to obtain the installation instructions for the External Hooks add-on.