Last active
August 29, 2015 14:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <[email protected]> | |
# @author Bram Borggreve <[email protected]> (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