forked from OpenTTD/OpenTTD-git-hooks
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Checks for commit message and diffs
- Loading branch information
Showing
10 changed files
with
276 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test/main | ||
test/main.git | ||
test/goodguy | ||
test/badguy |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
@cd test; ./test.sh |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import re, sys | ||
|
||
KEYWORDS = "(Add|Feature|Change|Remove|Codechange|Cleanup|Fix|Revert|Doc|Update|Prepare)" | ||
ISSUE = "#\d+" | ||
COMMIT = "[0-9a-f]{4,}" | ||
|
||
MSG_PAT1 = re.compile(KEYWORDS + "$") | ||
MSG_PAT2 = re.compile(KEYWORDS + " " + ISSUE + "$") | ||
MSG_PAT3 = re.compile(KEYWORDS + " " + COMMIT + "$") | ||
MSG_PAT4 = re.compile(COMMIT + "$") | ||
|
||
ERROR = """ | ||
*** First line of message must match: '<keyword>( #<issue>| <commit>(, (<keyword> #<issue>|<commit>))*)?: ([<section])? <Details>' | ||
Valid <keyword>: """+KEYWORDS+""" | ||
Examples: | ||
'Fix: [YAPF] Infinite loop in pathfinder.' | ||
'Fix #5926: [YAPF] Infinite loop in pathfinder.' | ||
'Fix 80dffae130: Warning about unsigned unary minus. | ||
'Fix #6673, 99bb3a95b4: Store the map variety setting in the samegame.' | ||
'Revert d9065fbfbe, Fix #5922: ClientSizeChanged is only called via WndProcGdi which already has the mutex.' | ||
'Fix #1264, Fix #2037, Fix #2038, Fix #2110: Rewrite the autoreplace kernel.' | ||
""" | ||
|
||
first_line = True | ||
for l in open(sys.argv[1]): | ||
l = l.rstrip("\n") | ||
|
||
# Check trailing whitespace | ||
if l != l.rstrip(): | ||
sys.stderr.write("*** Message contains trailing whitespace: '{}'\n".format(l)) | ||
sys.exit(1) | ||
|
||
# Check ASCII, and no control chars | ||
if any(ord(c) < 32 or ord(c) > 127 for c in l): | ||
sys.stderr.write("*** Message contains non-ASCII characters or tabs: '{}'\n".format(l)) | ||
sys.exit(1) | ||
|
||
# Check first line | ||
if first_line: | ||
first_line = False | ||
|
||
parts = l.split(": ", 1) | ||
if len(parts) != 2: | ||
sys.stderr.write(ERROR) | ||
sys.exit(1) | ||
|
||
prefixes = parts[0].split(", ") | ||
first_prefix = True | ||
for p in prefixes: | ||
if (len(prefixes) == 1 and MSG_PAT1.match(p)) or MSG_PAT2.match(p) or MSG_PAT3.match(p) or (not first_prefix and MSG_PAT4.match(p)): | ||
first_prefix = False | ||
else: | ||
sys.stderr.write(ERROR) | ||
sys.exit(1) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** Foo | ||
* bar | ||
*/ | ||
int foo() | ||
{ | ||
return bar; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** Foo | ||
* bar | ||
*/ | ||
int foo() | ||
{ | ||
return bar; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** Foo | ||
* bar | ||
*/ | ||
int foo() | ||
{ | ||
return bar; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** Foo | ||
* bar | ||
*/ | ||
int foo() | ||
{ | ||
int bar = 1; | ||
return bar; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** Foo | ||
* bar | ||
*/ | ||
int foo() | ||
{ | ||
return bar; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
#!/bin/sh | ||
|
||
git_good() | ||
{ | ||
git "$@" >/dev/null 2>&1 | ||
if [ $? -ne 0 ] | ||
then | ||
echo "Command '$@' failed" | ||
exit 1 | ||
fi | ||
} | ||
|
||
git_bad() | ||
{ | ||
git "$@" >/dev/null 2>&1 | ||
if [ $? -eq 0 ] | ||
then | ||
echo "Command '$@' should have failed" | ||
exit 1 | ||
fi | ||
} | ||
|
||
test_commit_good() | ||
{ | ||
git_good commit -m "$1" | ||
} | ||
|
||
test_commit_bad() | ||
{ | ||
git_bad commit -m "$1" | ||
} | ||
|
||
# reset all | ||
rm -rf main main.git goodguy badguy | ||
|
||
# setup main | ||
git_good init main | ||
cd main | ||
git_good config --local core.autocrlf input | ||
git_good config --local core.whitespace trailing-space,space-before-tab,indent-with-non-tab | ||
echo "init" > readme | ||
git_good add readme | ||
test_commit_good "Add: Init" | ||
|
||
cd .. | ||
mv main/.git main.git | ||
rm -rf main | ||
git_good --git-dir=main.git config core.bare true | ||
|
||
cd main.git/hooks | ||
ln -s -t . ../../../hooks/* | ||
cd ../.. | ||
|
||
# setup goodguy | ||
echo "goodguy" | ||
git_good clone main.git goodguy | ||
cd goodguy | ||
git_good config --local core.autocrlf input | ||
git_good config --local core.whitespace trailing-space,space-before-tab,indent-with-non-tab | ||
cd .git/hooks | ||
ln -s -t . ../../../../hooks/* | ||
cd ../../../ | ||
|
||
# test cases | ||
cp cases/* goodguy | ||
cd goodguy | ||
|
||
git_good add case1.cpp | ||
test_commit_bad "Add No ref" | ||
test_commit_bad "-Add: No ref" | ||
test_commit_bad " Add: No ref" | ||
test_commit_bad "Add : No ref" | ||
test_commit_bad "Add:No ref" | ||
test_commit_good "Add: No ref" | ||
|
||
cp case1.cpp case11.cpp | ||
git_good add case11.cpp | ||
test_commit_bad "Add [FS#123]: Issue ref" | ||
test_commit_bad "Add [#123]: Issue ref" | ||
test_commit_bad "Add, #123: Issue ref" | ||
test_commit_bad "Add #123: Issue ref" | ||
test_commit_bad "Add #123 : Issue ref" | ||
test_commit_bad "Add # 123: Issue ref" | ||
test_commit_bad "Add #123:Issue ref" | ||
test_commit_good "Add #123: Issue ref" | ||
|
||
cp case1.cpp case12.cpp | ||
git_good add case12.cpp | ||
test_commit_bad "Fix, abcdef: Commit ref" | ||
test_commit_bad "Fix abcdef: Commit ref" | ||
test_commit_bad "Fix abcdef : Commit ref" | ||
test_commit_bad "Fix abcdef:Commit ref" | ||
test_commit_good "Fix abcdef: Commit ref" | ||
|
||
cp case1.cpp case13.cpp | ||
git_good add case13.cpp | ||
test_commit_bad "Fix #123 #456: Two issue ref" | ||
test_commit_bad "Fix #123, #456: Two issue ref" | ||
test_commit_bad "Fix #123,Fix #456: Two issue ref" | ||
test_commit_good "Fix #123, Fix #456: Two issue ref" | ||
|
||
cp case1.cpp case14.cpp | ||
git_good add case14.cpp | ||
test_commit_bad "abcdef, Fix #456: Commit and issue ref" | ||
test_commit_bad "Fix abcdef #456: Commit and issue ref" | ||
test_commit_bad "Fix abcdef, #456: Commit and issue ref" | ||
test_commit_good "Fix abcdef, Fix #456: Commit and issue ref" | ||
|
||
cp case1.cpp case15.cpp | ||
git_good add case15.cpp | ||
test_commit_bad "#123, Fix abcdef: Issue and commit ref" | ||
test_commit_bad "Fix #123 abcdef: Issue and commit ref" | ||
test_commit_bad "Fix #123,abcdef: Issue and commit ref" | ||
test_commit_good "Fix #123, abcdef: Issue and commit ref" | ||
|
||
cp case1.cpp case16.cpp | ||
git_good add case16.cpp | ||
test_commit_good "Fix #123, Fix abcdef: Issue and commit ref" | ||
|
||
cp case1.cpp case17.cpp | ||
git_good add case17.cpp | ||
test_commit_bad "fedcba, Fix abcdef: Two commit ref" | ||
test_commit_bad "Fix fedcba abcdef: Two commit ref" | ||
test_commit_good "Fix fedcba, abcdef: Two commit ref" | ||
|
||
cp case1.cpp case18.cpp | ||
git_good add case18.cpp | ||
test_commit_good "Fix fedcba, Fix abcdef: Two commit ref" | ||
|
||
git_good add case2.cpp | ||
test_commit_bad "Add: Whitespace" | ||
git_good reset case2.cpp | ||
|
||
#git_good add case3.cpp | ||
#test_commit_bad "Add: Tabs" | ||
#git_good reset case3.cpp | ||
|
||
git_good add case4.cpp | ||
test_commit_bad "Add: Mixed indent" | ||
git_good reset case4.cpp | ||
|
||
#git_good add case5.cpp | ||
#test_commit_bad "Add: Space indent" | ||
#git_good reset case5.cpp | ||
|
||
git_good push | ||
|
||
# setup badguy | ||
cd .. | ||
echo "badguy" | ||
git_good clone main.git badguy | ||
cd badguy | ||
|
||
cp case1.cpp case19.cpp | ||
git_good add case19.cpp | ||
test_commit_good "fixit" | ||
git_bad push | ||
|
||
cp ../cases/case2.cpp . | ||
git_good add case2.cpp | ||
git_good commit --amend -m "Fix: Message" | ||
git_bad push | ||
|
||
cd .. |