Commit b58ed9e9 authored by Alexis Girault's avatar Alexis Girault
Browse files

ENH: Add SetupForDevelopment and first git hooks

- SetupForDevelopment is a bash script that will setup the git
environment for the user, by running setup scripts in SetupScripts
directory.
- SetupGitUser will assure that a conform name and email adress
are configured.
- SetupGitHooks will copy git hooks in the .git/hooks directory
- SuggestGitTips will suggest useful tips to configure git
- prepare-commit-msg is a git hook which will display the standard
prefixes that can be use for the commit summary
- commit-msg will ensure the commit message follows those standards
as well as some other basic rules

In the future we will incorporate KWStyle to check the code style
in the git pre-commit hook.
parent 65be3263
#!/usr/bin/env bash
#-----------------------------------------------------------------------------
# Define egrep-q
#-----------------------------------------------------------------------------
egrep-q() {
egrep "$@" >/dev/null 2>/dev/null
}
#-----------------------------------------------------------------------------
# Prepare a copy of the message:
#-----------------------------------------------------------------------------
# - strip comment lines
# - stop at "diff --git" (git commit -v)
commit_msg="$GIT_DIR/COMMIT_MSG"
sed -n -e '/^#/d' -e '/^diff --git/q' -e 'p;d' "$1" > "$commit_msg"
#-----------------------------------------------------------------------------
# Define Error and Exit
#-----------------------------------------------------------------------------
advice='
To continue editing, run the command
git commit -e -F '"$commit_msg"'
(assuming your working directory is at the top).'
printErrorAndExit() {
echo 'commit-msg hook failure' 1>&2
echo '-----------------------' 1>&2
echo '' 1>&2
echo "$@" 1>&2
test -n "$advice" && echo "$advice" 1>&2
exit 1
}
#-----------------------------------------------------------------------------
# Merge message layout
#-----------------------------------------------------------------------------
msg_is_merge() {
echo "$line" | grep "^Merge " >/dev/null 2>&1
}
#-----------------------------------------------------------------------------
# Revert message layout
#-----------------------------------------------------------------------------
msg_is_revert() {
echo "$line" | grep "^Revert " >/dev/null 2>&1
}
#-----------------------------------------------------------------------------
# First line message layout
#-----------------------------------------------------------------------------
msg_first() {
len=$(echo -n "$line" | wc -c)
# if empty line
if test $len -eq 0; then
return
# if short line
elif test $len -lt 8; then
printErrorAndExit 'The first line must be at least 8 characters:
--------
'"$line"'
--------'
# if long line
elif test $len -gt 50 && ! msg_is_merge && ! msg_is_revert; then
printErrorAndExit 'The first line may be at most 50 characters:
--------------------------------------------------
'"$line"'
--------------------------------------------------'
# if trailing spaces
elif echo "$line" | grep "^[ ]\|[ ]$" >/dev/null 2>&1; then
printErrorAndExit 'The first line may not have leading or trailing space:
['"$line"']'
fi
# good line
firstLetters=$(echo "$line" | cut -c1-8)
if [ "${firstLetters:0:5}" == "BUG: " -o \
"${firstLetters:0:6}" == "COMP: " -o \
"${firstLetters:0:5}" == "DOC: " -o \
"${firstLetters:0:5}" == "ENH: " -o \
"${firstLetters:0:6}" == "PERF: " -o \
"${firstLetters:0:7}" == "STYLE: " -o \
"${firstLetters:0:5}" == "WIP: " ]; then
state=second
# wrong first letters
else
printErrorAndExit 'Git Commits to iMSTK require a standard prefix followed by a space.
Valid commit types are as followed:
BUG: - fix for runtime crash or incorrect result
COMP: - compiler error or warning fix
DOC: - documentation change
ENH: - new functionality
PERF: - performance improvement
STYLE: - no logic impact (indentation, comments)
WIP: - Work In Progress not ready for merge
'
fi
}
#-----------------------------------------------------------------------------
# Second line message layout
#-----------------------------------------------------------------------------
msg_second() {
if test "x$line" != "x"; then
printErrorAndExit 'The second line must be empty:
'"$line"
else
state=rest
fi
}
#-----------------------------------------------------------------------------
# Check the commit message layout with a simple state machine
#-----------------------------------------------------------------------------
state=first
cat "$commit_msg" |
while IFS='' read line; do
msg_$state || break
done &&
rm -f "$commit_msg" || exit 1
advice='' # No more temporary message file.
#!/usr/bin/env bash
#-----------------------------------------------------------------------------
# Define egrep-q
#-----------------------------------------------------------------------------
egrep-q() {
egrep "$@" >/dev/null 2>/dev/null
}
#-----------------------------------------------------------------------------
# First argument is file containing commit message
#-----------------------------------------------------------------------------
commit_msg="$1"
#-----------------------------------------------------------------------------
# Check for our extra instructions
#-----------------------------------------------------------------------------
egrep-q "^# Start iMSTK commit messages" -- "$commit_msg" && return 0
#-----------------------------------------------------------------------------
# Insert our extra instructions
#-----------------------------------------------------------------------------
commit_msg_tmp="$commit_msg.$$"
instructions='#\
# Start iMSTK commit messages with a standard prefix (and a space):\
# BUG: - fix for runtime crash or incorrect result\
# COMP: - compiler error or warning fix\
# DOC: - documentation change\
# ENH: - new functionality\
# PERF: - performance improvement\
# STYLE: - no logic impact (indentation, comments)\
# WIP: - Work In Progress not ready for merge\
#
#' &&
sed '/^# On branch.*$/ a\
'"$instructions"'
/^# Not currently on any branch.*$/ a\
'"$instructions"'
' "$commit_msg" > "$commit_msg_tmp" &&
mv "$commit_msg_tmp" "$commit_msg"
#!/usr/bin/env bash
#-----------------------------------------------------------------------------
# Define Error and Exit
#-----------------------------------------------------------------------------
printErrorAndExit() {
echo 'Failure during git development setup' 1>&2
echo '------------------------------------' 1>&2
echo '' 1>&2
echo "$@" 1>&2
exit 1
}
#-----------------------------------------------------------------------------
# Go to the repository root
#-----------------------------------------------------------------------------
cd "$(echo "$0" | sed 's/[^/]*$//')"/..
if test -d .git/.git; then
printErrorAndExit "The directory '.git/.git' exists, indicating a
configuration error. Please 'rm -rf' this directory."
fi
#-----------------------------------------------------------------------------
# Make 'git pull' on master always use rebase
#-----------------------------------------------------------------------------
git config branch.master.rebase true
#-----------------------------------------------------------------------------
# Make automatic and manual rebase operations to display a summary and stat
# display of changes merged in the fast-forward operation.
#-----------------------------------------------------------------------------
git config rebase.stat true
#-----------------------------------------------------------------------------
# Run Setup Scripts
#-----------------------------------------------------------------------------
cd Utilities/SetupScripts
echo "Checking basic user information..."
./SetupGitUser.sh || exit 1
echo
echo "Setting up git hooks..."
./SetupGitHooks.sh || exit 1
echo
echo "Suggesting git tips..."
./SuggestGitTips.sh || exit 1
echo
#-----------------------------------------------------------------------------
# Record the version of this setup so the developer can be notified that
# this script and/or hooks have been modified.
#-----------------------------------------------------------------------------
SetupForDevelopment_VERSION=1
git config hooks.SetupForDevelopment ${SetupForDevelopment_VERSION}
#!/usr/bin/env bash
#-----------------------------------------------------------------------------
# Define Error and Exit
#-----------------------------------------------------------------------------
printErrorAndExit() {
echo 'Failure during git hook setup' 1>&2
echo '------------------------------------' 1>&2
echo '' 1>&2
echo "$@" 1>&2
exit 1
}
AssertFileExists() {
FILE=$1
if [ ! -f $FILE ]; then
echo "Error: the file '$FILE' is missing."
printErrorAndExit
fi
}
#-----------------------------------------------------------------------------
# Go to GitHooks directory
#-----------------------------------------------------------------------------
cd "${BASH_SOURCE%/*}/../GitHooks"
#-----------------------------------------------------------------------------
# Populate ".git/hooks"
#-----------------------------------------------------------------------------
echo "Populating git hooks..."
git_dir=$(git rev-parse --git-dir)
cp * $git_dir/hooks
#-----------------------------------------------------------------------------
# Go to the project root directory
#-----------------------------------------------------------------------------
#cd "../.."
#-----------------------------------------------------------------------------
# Set up KWStyle hook
#-----------------------------------------------------------------------------
#echo "Setting up the KWStyle hook..."
#KWStyleConf="Utilities/KWStyle/iMSTK.kws.xml"
#KWStyleOverwriteConf="Utilities/KWStyle/iMSTKKWSOverwrite.txt"
#AssertFileExists $KWStyleConf
#AssertFileExists $KWStyleOverwriteConf
#git config hooks.KWStyle.conf $KWStyleConf
#git config hooks.KWStyle.overwriteRulesConf $KWStyleOverwriteConf
#git config hooks.KWStyle true
echo "Done."
#!/usr/bin/env bash
#-----------------------------------------------------------------------------
# Define Error and Exit
#-----------------------------------------------------------------------------
printErrorAndExit() {
echo 'Failure during git development setup' 1>&2
echo '------------------------------------' 1>&2
echo '' 1>&2
echo "$@" 1>&2
exit 1
}
#-----------------------------------------------------------------------------
# Define Setup User
#-----------------------------------------------------------------------------
setup_user() {
read -ep "Please enter your full name, such as 'John Doe': " name
echo "Setting name to '$name'"
git config user.name "$name"
read -ep "Please enter your email address, such as 'john@email.com': " email
echo "Setting email address to '$email'"
git config user.email "$email"
}
#-----------------------------------------------------------------------------
# Setup user if empty credentials
#-----------------------------------------------------------------------------
gitName=$(git config user.name)
gitEmail=$(git config user.email)
if [ "$gitName" == "" ] || [ "$gitEmail" == "" ]; then
setup_user
fi
#-----------------------------------------------------------------------------
# Loop until the user is happy with the authorship information
#-----------------------------------------------------------------------------
for (( ; ; ))
do
# Display the final user information
gitName=$(git config user.name)
gitEmail=$(git config user.email)
echo "Your commits will have the following author:
$gitName <$gitEmail>
"
# Ask for user confirmation
read -ep "Is the author name and email address above correct? [Y/n] " correct
if [ "$correct" == "n" ] || [ "$correct" == "N" ]; then
setup_user
else
break
fi
done
echo "Done."
#!/usr/bin/env bash
#-----------------------------------------------------------------------------
# Define egrep-q
#-----------------------------------------------------------------------------
egrep-q() {
egrep "$@" >/dev/null 2>/dev/null
}
#-----------------------------------------------------------------------------
# Git color output
#-----------------------------------------------------------------------------
if test "$(git config color.ui)" != "auto"; then
echo '
You may want to enable color output from Git commands with
git config --global color.ui auto
'
fi
#-----------------------------------------------------------------------------
# Git bash-completion
#-----------------------------------------------------------------------------
if ! bash -i -c 'echo $PS1' | egrep-q '__git_ps1'; then
echo '
A dynamic, informative Git shell prompt can be obtained by sourcing the git
bash-completion script in your ~/.bashrc. Set the PS1 environmental variable as
suggested in the comments at the top of the bash-completion script. You may
need to install the bash-completion package from your distribution to obtain the
script.
'
fi
#-----------------------------------------------------------------------------
# Git Merge tool configuration
#-----------------------------------------------------------------------------
if ! git config merge.tool >/dev/null; then
echo '
A merge tool can be configured with
git config merge.tool <toolname>
For more information, see
git help mergetool
'
fi
echo "Done."
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment