Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions hooks/check-diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
PAT_TAB = re.compile("#?\t*[^\t]*$")

def checkascii(l):
return any((ord(c) < 32 or ord(c) > 127) and c != '\t' for c in l)
return any((ord(c) < 32 or ord(c) > 127) and c != '\t' for c in l)

status = 0
filename = None
Expand All @@ -14,47 +14,47 @@ def checkascii(l):
lastline = None

for l in open(sys.argv[1], encoding="utf-8"):
l = l.rstrip("\n")
l = l.rstrip("\n")

if l.startswith("+++"):
line = 1
lastline = None
filename = l[4:].strip()
if checkascii(filename):
sys.stderr.write("*** Filename is non-ASCII: '{}'\n".format(filename))
status = 1
is_source = (filename.find("3rdparty") < 0) and filename.endswith((".cpp", ".c", ".hpp", ".h", ".mm"))
elif l.startswith("@@"):
line = int(l.split()[2].split(",")[0])
lastline = None
elif l.startswith("+"):
l = l[1:]
lastline = "+"
if is_source and checkascii(l):
sys.stderr.write("*** {}:{}: Non-ASCII found: '{}'\n".format(filename, line, l))
status = 1
if l != l.rstrip():
sys.stderr.write("*** {}:{}: Trailing whitespace: '{}'\n".format(filename, line, l))
status = 1
if is_source and not PAT_TAB.match(l):
sys.stderr.write("*** {}:{}: Invalid tab usage: '{}'\n".format(filename, line, l))
status = 1
if is_source and (l.find("\t#") >= 0):
sys.stderr.write("*** {}:{}: Preprocessor hash is put into the first column, before the tab indentation: '{}'\n".format(filename, line, l))
status = 1
if is_source and l.startswith(" "):
sys.stderr.write("*** {}:{}: Use tabs for indentation: '{}'\n".format(filename, line, l))
status = 1
line += 1
elif l.startswith(" "):
line += 1
lastline = " "
elif l.startswith("-"):
# do not increment line
lastline = "-"
elif l == "\\ No newline at end of file" and lastline != "-":
sys.stderr.write("*** {}: No newline at end of file\n".format(filename))
if lastline == " ": sys.stderr.write("Please fix the existing newline problem in the file.\n")
status = 1
if l.startswith("+++"):
line = 1
lastline = None
filename = l[4:].strip()
if checkascii(filename):
sys.stderr.write("*** Filename is non-ASCII: '{}'\n".format(filename))
status = 1
is_source = (filename.find("3rdparty") < 0) and filename.endswith((".cpp", ".c", ".hpp", ".h", ".mm"))
elif l.startswith("@@"):
line = int(l.split()[2].split(",")[0])
lastline = None
elif l.startswith("+"):
l = l[1:]
lastline = "+"
if is_source and checkascii(l):
sys.stderr.write("*** {}:{}: Non-ASCII found: '{}'\n".format(filename, line, l))
status = 1
if l != l.rstrip():
sys.stderr.write("*** {}:{}: Trailing whitespace: '{}'\n".format(filename, line, l))
status = 1
if is_source and not PAT_TAB.match(l):
sys.stderr.write("*** {}:{}: Invalid tab usage: '{}'\n".format(filename, line, l))
status = 1
if is_source and (l.find("\t#") >= 0):
sys.stderr.write("*** {}:{}: Preprocessor hash is put into the first column, before the tab indentation: '{}'\n".format(filename, line, l))
status = 1
if is_source and l.startswith(" "):
sys.stderr.write("*** {}:{}: Use tabs for indentation: '{}'\n".format(filename, line, l))
status = 1
line += 1
elif l.startswith(" "):
line += 1
lastline = " "
elif l.startswith("-"):
# do not increment line
lastline = "-"
elif l == "\\ No newline at end of file" and lastline != "-":
sys.stderr.write("*** {}: No newline at end of file\n".format(filename))
if lastline == " ": sys.stderr.write("Please fix the existing newline problem in the file.\n")
status = 1

sys.exit(status)
64 changes: 32 additions & 32 deletions hooks/check-message.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,41 @@

first_line = True
for l in open(sys.argv[1], encoding="utf-8"):
l = l.rstrip("\n")
l = l.rstrip("\n")

# Skip comments on client side:
# There are various ways how parts of the commit message can be ignored.
# One method is by using some comment character (# by default).
# Another is by using scissor lines.
# We cannot tell which method is active, so we make a convenient assumption.
# On server side we always check the whole message, since there are no longer any comments.
if is_client and l.startswith("#"):
continue
# Skip comments on client side:
# There are various ways how parts of the commit message can be ignored.
# One method is by using some comment character (# by default).
# Another is by using scissor lines.
# We cannot tell which method is active, so we make a convenient assumption.
# On server side we always check the whole message, since there are no longer any comments.
if is_client and l.startswith("#"):
continue

# Check trailing whitespace
if l != l.rstrip():
sys.stderr.write("*** Message contains trailing whitespace: {!a}\n".format(l))
sys.exit(1)
# Check trailing whitespace
if l != l.rstrip():
sys.stderr.write("*** Message contains trailing whitespace: {!a}\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: {!a}\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: {!a}\n".format(l))
sys.exit(1)

# Check first line
if first_line:
first_line = False
# Check first line
if first_line:
first_line = False

parts = l.split(": ", 1)
if len(parts) != 2:
sys.stderr.write(ERROR)
sys.exit(1)
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)
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)
5 changes: 5 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rm -rf main main.git goodguy badguy
# setup main
git_good init main
cd main
git_good config --local user.email [email protected]
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
Expand All @@ -59,6 +60,7 @@ cd ../..
echo "goodguy"
git_good clone main.git goodguy
cd goodguy
git_good config --local user.email [email protected]
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
Expand Down Expand Up @@ -170,6 +172,7 @@ cd ..
echo "badguy"
git_good clone main.git badguy
cd badguy
git_good config --local user.email [email protected]

cp case1.cpp case19.cpp
git_good add case19.cpp
Expand All @@ -182,3 +185,5 @@ git_good commit --amend -m "Fix: Message"
git_bad push

cd ..

echo "All tests passed successfully"