Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-push Git hook to determine if branch name is valid

Tags:

git

bash

I want to write a Bash script to test a regex against a branch name with a Git pre-push hook. I have read the pre-push documentation but I am having trouble getting the hook into my application. Anyone have any suggestions.

local_branch = $(git rev-parse --abbrev-ref HEAD)
valid_chars = $(^[a-z0-9-]+$)

if [[ "$local_branch" =~ valid_chars]]; then
  echo 'Failed to push.  Branch is using incorrect characters.  Valid        Characters are lower case (a-z), numbers (0-9) and dashes(-).  Please rename branch to continue'
  exit 1
fi

exit 0
like image 301
panda-king Avatar asked Apr 01 '15 20:04

panda-king


People also ask

What is git pre-commit hook?

The pre-commit script is executed every time you run git commit before Git asks the developer for a commit message or generates a commit object. You can use this hook to inspect the snapshot that is about to be committed.

How do you push a pre-commit hook?

If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook. Note that running a hook for the first time may be slow.

Where are git pre-commit hooks?

All git hooks are stored in the . git/hooks/ directory under your project root. A pre-commit hook is just an executable file stored in this folder with the magic name pre-commit .


1 Answers

Running the script you have above results in various errors. I am also not sure why you are executing ^[a-z0-9-]+$ and storing the result in valid_chars. Nonetheless:

  • You probably want to exit with an error if the branch name does not match the regex
  • You're missing a $ prefix for valid_chars in your test
  • if [[ "$local_branch" =~ valid_chars]]; then should have a space inside the ]]

As always, ensure that the script is under .git/hooks/pre-push, named correctly, and is marked as executable.

The following works for me (I have left the sample hook comment in because I'm lazy):

#!/bin/bash

# An example hook script to verify what is about to be pushed.  Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed.  If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
#   <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).

local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_chars="^[a-z0-9-]+$"
message='...'

if [[ ! $local_branch =~ $valid_chars ]]
then
    printf '%s\n' "$message"
    exit 1
fi

exit 0
like image 185
Whymarrh Avatar answered Sep 24 '22 14:09

Whymarrh