Skip to content

Instantly share code, notes, and snippets.

@beeman
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beeman/494de3a1ec14f9e6bd31 to your computer and use it in GitHub Desktop.
Save beeman/494de3a1ec14f9e6bd31 to your computer and use it in GitHub Desktop.
Stop accidental commits to master | Add Pivotal Ticket ID based on branch name
#!/bin/bash
# Stops accidental commits to master and develop. https://gist.github.com/beeman/494de3a1ec14f9e6bd31
# Copied from https://gist.github.com/stefansundin/9059706
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/beeman/494de3a1ec14f9e6bd31/raw/pre-commit
# chmod +x .git/hooks/pre-commit
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then
echo "You are on branch $BRANCH. Are you sure you want to commit to this branch?"
echo "If so, commit with -n to bypass this pre-commit hook."
exit 1
fi
exit 0
#!/usr/bin/php
<?php
# Git Prepare Commit Message Hook Script
#
# Location: <repository>/.git/hooks/prepare-commit-msg
#
# This script will automatically add the correct
# Pivotal Ticket ID to the beginning of each commit message
# When the branch ID ends with the Pivotal Message ID.
# It can be overridden if specified in the message.
#
# Example:
#
# branch-name-1234567 => '[#1234567] commit message'
#
# @author Tamas Kalman <ktamas77@gmail.com>
# @author Bram Borggreve <borggreve@gmail.com> (changed format)
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/prepare-commit-msg https://gist.githubusercontent.com/beeman/494de3a1ec14f9e6bd31/raw/prepare-commit-msg
# chmod +x .git/hooks/prepare-commit-msg
$messageFile = $argv[1];
$message = file_get_contents($messageFile);
$messagePivotalPattern = '/^\[#([0-9]{1,16})\]/';
preg_match($messagePivotalPattern, $message, $matches);
$messagePivotalId = (isset($matches[1])) ? $matches[1] : null;
if ($messagePivotalId === null) {
$currentBranchName = exec('git rev-parse --abbrev-ref HEAD');
$branchPivotalPattern = '/([0-9]{1,16})$/';
preg_match($branchPivotalPattern, $currentBranchName, $matches);
$branchPivotalId = (isset($matches[1])) ? $matches[1] : null;
if ($branchPivotalId !== null) {
$message = sprintf('[#%s] %s', $branchPivotalId, $message);
file_put_contents($messageFile, $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment