🐙 Guarding when pushing on develop

Filed under:

At work, we mostly use PRs to merge features into our development branch.

For quick fixes or things that do not need to pass everybody's eyes it is ok to push on the develop branch, but while doing this works fine most of the time, stuff like snapshot tests can often be forgotten to run before the push.

We do not want to force running tests on prepush. This is tedious when your project has a lot of tests and after all, you probably have CI which takes care of this task.

Below are some snippets you can use to configure tests only to run on develop when pushing directly, via the pre-push hook.

Note that I am using husky and Bash but you can of course use whatever you want since it falls back on git rev-parse mosltly.

.huskyrc

{
  "hooks": {
    "pre-commit": "lint-staged",
    "pre-push": "./Taskfile pre_push_guard"
  }
}

Taskfile

#!/usr/bin/env bash
set -euo pipefail

function pre_push_guard() {
  local currBranch
  currBranch=$(git rev-parse --abbrev-ref HEAD)

  if [ "$currBranch" == "develop" ]; then
    npm test
  fi
}

function help {
  echo "$0 <task> <args>"
  echo "Available tasks:"
  compgen -A function | cat -n
  echo ""
}

"${@:-help}"