Skip to content

Commit c5dcbb0

Browse files
committed
Add script to change git settings and use hooks
- pre-commit hook to check for white-space errors - commit-msg hook to ensure commit messages are properly formatted - setup line endings and white space - pre-push hook to ensure work in progress (AKA WIP) commits are not pushed to remote repos
1 parent c3ee1cb commit c5dcbb0

File tree

11 files changed

+681
-0
lines changed

11 files changed

+681
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*.F90 text
1010
*.md text
1111
*.txt text
12+
*.sh text
13+
install_prerequisites/build text
14+
*.cu text
15+
*.x64 text
1216

1317
# Denote all files that are truly binary and should not be modified.
1418
*.mod binary
@@ -22,3 +26,4 @@
2226
.travis.yml export-ignore
2327
.gitattributes export-ignore
2428
.gitignore export-ignore
29+
developer-scripts export-ignore
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, rename this file to "applypatch-msg".
11+
12+
. git-sh-setup
13+
test -x "$GIT_DIR/hooks/commit-msg" &&
14+
exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
15+
:
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message.
4+
# Called by "git commit" with one argument, the name of the file
5+
# that has the commit message. The hook should exit with non-zero
6+
# status after issuing an appropriate message if it wants to stop the
7+
# commit. The hook is allowed to edit the commit message file.
8+
#
9+
# To enable this hook, rename this file to "commit-msg".
10+
11+
# Uncomment the below to add a Signed-off-by line to the message.
12+
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13+
# hook is more suited to it.
14+
#
15+
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16+
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17+
18+
# error message function for writing good commit messages
19+
error_message () {
20+
cat <<\EOF >&2
21+
Error: bad commit message format. A copy of your commit message is in:
22+
`.git/COMMIT_EDITMSG`. Please fix it or write a new one according to the
23+
guidlines below.
24+
25+
The first line of a commit message should start with a capitalized imperative
26+
verb used in a phrase to summarize the change. This first line must be less
27+
than 50 characters, followed by an additional blank line. Further detials are
28+
added after these first two lines, and may be up to 72 characters in length.
29+
30+
This is so that `--oneline` formatted `git log`s will contain human readable,
31+
meaningful information, and the summaries on Github.com will also contain
32+
this concise summary. The 72 line limit is to ensure propper formatting on
33+
all terminals.
34+
35+
Here is an example of a good commit message:
36+
37+
```
38+
Redirect user to the requested page after login
39+
40+
https://github.com/sourceryinstitute/opencoarrays/Issues/29
41+
42+
Users were being redirected to the home page after login, which is less
43+
useful than redirecting to the page they had originally requested before
44+
being redirected to the login form.
45+
46+
* Store requested path in a session variable
47+
* Redirect to the stored location after successfully logging in the user
48+
* Fixes #1
49+
```
50+
51+
As opposed to doing something bad like this:
52+
53+
`git commit -m "Fix login bug"`
54+
55+
For more information on writting good commit messages,
56+
and keeping a clean history, please see:
57+
58+
1. https://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message
59+
2. http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
60+
3. https://www.reviewboard.org/docs/codebase/dev/git/clean-commits/
61+
62+
And for funny examples of what not to do, see: http://whatthecommit.com
63+
EOF
64+
65+
}
66+
67+
let status=0
68+
69+
# This example catches duplicate Signed-off-by lines.
70+
71+
test "" = "$(grep '^Signed-off-by: ' "$1" |
72+
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
73+
echo >&2 "Duplicate Signed-off-by lines."
74+
let status=1
75+
}
76+
77+
# Check that the first line of the commit starts with a
78+
# capitalized letter.
79+
if ! (head -n 1 $1 | grep '^[A-Z]' &>/dev/null ); then
80+
echo >&2 "First word of commit message must be a capitalized imperative verb.\n"
81+
let status=1
82+
fi
83+
84+
# Check each line for propper length
85+
ln=0
86+
cat $1 | \
87+
while read line; do
88+
let ln+=1
89+
nchars=$(wc -c <<<$line)
90+
if [[ "$ln" -eq "1" ]]; then
91+
if [[ "$nchars" -gt "51" ]]; then
92+
echo >&2 "First line of commit message too long ($nchars > 50 chars)\n"
93+
let status=1
94+
fi
95+
elif [[ "$ln" -eq "2" ]]; then
96+
if [[ "$nchars" -gt "1" ]] && ! grep '^#' <<<"$line" >/dev/null; then
97+
echo >&2 "Second line of commit message not blank\n"
98+
let status=1
99+
fi
100+
else
101+
if [[ "$nchars" -gt "72" ]]; then
102+
echo >&2 "Line $ln of commit message too long ($nchars > 72 chars)\n"
103+
let status=1
104+
fi
105+
fi
106+
done
107+
108+
if [[ $status != 0 ]]; then
109+
error_message
110+
exit 1
111+
fi
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to prepare a packed repository for use over
4+
# dumb transports.
5+
#
6+
# To enable this hook, rename this file to "post-update".
7+
8+
exec git update-server-info
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed
4+
# by applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit.
8+
#
9+
# To enable this hook, rename this file to "pre-applypatch".
10+
11+
. git-sh-setup
12+
test -x "$GIT_DIR/hooks/pre-commit" &&
13+
exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"}
14+
:
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed.
4+
# Called by "git commit" with no arguments. The hook should
5+
# exit with non-zero status after issuing an appropriate message if
6+
# it wants to stop the commit.
7+
#
8+
# To enable this hook, rename this file to "pre-commit".
9+
10+
function ends_with_newline() {
11+
test ! -s "$1" || test $(tail -r -c1 "$1" | xxd -ps) = "0a"
12+
}
13+
14+
function ends_with_single_new_line () {
15+
tail -2 "$1" | ( read x && read y && ! grep '^[[:blank:]]\+$' <<<$x >/dev/null)
16+
}
17+
18+
if git rev-parse --verify HEAD >/dev/null 2>&1
19+
then
20+
against=HEAD
21+
else
22+
# Initial commit: diff against an empty tree object
23+
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
24+
fi
25+
26+
# if we are in the middle of merging skip hooks
27+
MERGING=$(git rev-parse MERGE_HEAD 2>&1)
28+
if [[ $? == 0 ]]; then
29+
exit 0
30+
fi
31+
32+
# if we are in the middle of merging skip hooks
33+
PICKING=$(git rev-parse CHERRY_PICK_HEAD 2>&1)
34+
if [[ $? == 0 ]]; then
35+
exit 0
36+
fi
37+
38+
# If you want to allow non-ASCII filenames set this variable to true.
39+
allownonascii=$(git config --bool hooks.allownonascii)
40+
41+
# Redirect output to stderr.
42+
exec 1>&2
43+
44+
let status=0
45+
# Cross platform projects tend to avoid non-ASCII filenames; prevent
46+
# them from being added to the repository. We exploit the fact that the
47+
# printable range starts at the space character and ends with tilde.
48+
if [ "$allownonascii" != "true" ] &&
49+
# Note that the use of brackets around a tr range is ok here, (it's
50+
# even required, for portability to Solaris 10's /usr/bin/tr), since
51+
# the square bracket bytes happen to fall in the designated range.
52+
test $(git diff --cached --name-only --diff-filter=A -z $against |
53+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
54+
then
55+
cat &>2 <<\EOF
56+
Error: Attempt to add a non-ASCII file name.
57+
58+
This can cause problems if you want to work with people on other platforms.
59+
60+
To be portable it is advisable to rename the file.
61+
62+
If you know what you are doing you can disable this check using:
63+
64+
git config hooks.allownonascii true
65+
EOF
66+
let status=1
67+
fi
68+
69+
# If there is a file that does not end with a newline, print its name and fail.
70+
for f in $(git diff-index --name-status --cached $against | grep -v ^D | cut -c3-); do
71+
if [[ "$f" =~ ([.](h|m|c|rb|sh|py|txt|md|f90|F90|cmake|x64|csh)|makefile|Makefile)$ ]] ||
72+
head -n 1 | grep '^#!/'; then
73+
if ! ends_with_newline "$f"; then
74+
echo &>2 "No newline at end of file: $f\n"
75+
let status=1
76+
fi
77+
fi
78+
done
79+
80+
# If there are whitespace errors, print the offending file names and fail.
81+
git diff-index --check --cached $against -- || let status=1
82+
83+
if [[ $status != 0 ]]; then
84+
exit 1
85+
fi

developer-scripts/git-hooks/pre-push

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
# An example hook script to verify what is about to be pushed. Called by "git
4+
# push" after it has checked the remote status, but before anything has been
5+
# pushed. If this script exits with a non-zero status nothing will be pushed.
6+
#
7+
# This hook is called with the following parameters:
8+
#
9+
# $1 -- Name of the remote to which the push is being done
10+
# $2 -- URL to which the push is being done
11+
#
12+
# If pushing without using a named remote those arguments will be equal.
13+
#
14+
# Information about the commits which are being pushed is supplied as lines to
15+
# the standard input in the form:
16+
#
17+
# <local ref> <local sha1> <remote ref> <remote sha1>
18+
#
19+
# This sample shows how to prevent push of commits where the log message starts
20+
# with "WIP" (work in progress).
21+
22+
remote="$1"
23+
url="$2"
24+
25+
z40=0000000000000000000000000000000000000000
26+
27+
while read local_ref local_sha remote_ref remote_sha
28+
do
29+
if [ "$local_sha" = $z40 ]
30+
then
31+
# Handle delete
32+
:
33+
else
34+
if [ "$remote_sha" = $z40 ]
35+
then
36+
# New branch, examine all commits
37+
range="$local_sha"
38+
else
39+
# Update to existing branch, examine new commits
40+
range="$remote_sha..$local_sha"
41+
fi
42+
43+
# Check for WIP commit
44+
commit=`git rev-list -n 1 --grep '^WIP' "$range"`
45+
if [ -n "$commit" ]
46+
then
47+
echo >&2 "Found WIP commit in $local_ref, not pushing"
48+
exit 1
49+
fi
50+
fi
51+
done
52+
exit 0

0 commit comments

Comments
 (0)