Disallow to merge WIP commits

This document describes how to setup merge hook script for validating that target branch for merge doesn’t contain WIP commits. WIP is a well-known abbreviation for “Work In Progress”, it’s often used by developers to push something that is not yet ready.

You can extend the script to implement any policy which is required by your workflow.

Setting up Hook #

Put the following Python script under the
<bitbucket-home-dir>/external-hooks/disallow-wip path on your Bitbucket Server installation.

If you use Bitbucket Data Center, put script under the
<bitbucket-home-dir>/shared/external-hooks/disallow-wip path.

Make sure it has the executable flag set (e.g. run chmod +x <path-to-file>).

#!/bin/env python

import sys
import subprocess
import os

target = os.getenv('BB_FROM_HASH')
source = os.getenv('BB_TO_HASH')
branch = os.getenv('BB_FROM_REF')


if source == z40:
    # if it is a new branch then check all branch related commits
    rev_list   = os.popen("git rev-list --simplify-by-decoration -2 "+target).read()
    boundaries = rev_list.strip().split()
    commits    = os.popen("git rev-list "+boundaries[0]+".."+target).read()
     # if it is existing branch then check only new commits
    commits  = os.popen("git rev-list "+source+".."+target).read()

for commit in commits.strip().split('\n'):
    commit_message = os.popen("git show -s --format=%B "+commit).read()
    if "WIP" in commit_message:
        print("Changeset contains WIP commits")

Read more about used environment variables: List of Environment Variables

Configuration #

Navigate to your repository page → Repository settings → Merge Checks, and enable the External merge Check Hook with the following parameters:

  • Type: Pre Receive
  • Executable: disallow-wip
  • Safe mode:
  • Positional arguments: leave it empty

After these actions are done, you can test that hooks works by pushing some commits into your repo with a string WIP in commit message and create PR.

Got Some Questions? #

We will be glad to help to adapt configuration for your workflow.

Join our Slack or file an issue on GitHub: