From 4fd4a9c5a44a4a14ccffc0c421b8b157cc857190 Mon Sep 17 00:00:00 2001 From: kos Date: Tue, 18 Nov 2025 19:54:02 +0100 Subject: [PATCH] Codechange: fix indentation --- hooks/check-diff.py | 84 +++++++++++++++++++++--------------------- hooks/check-message.py | 64 ++++++++++++++++---------------- test/test.sh | 5 +++ 3 files changed, 79 insertions(+), 74 deletions(-) diff --git a/hooks/check-diff.py b/hooks/check-diff.py index 36f5b69..10fef1b 100755 --- a/hooks/check-diff.py +++ b/hooks/check-diff.py @@ -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 @@ -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) diff --git a/hooks/check-message.py b/hooks/check-message.py index 5edeb8e..43cfa2b 100755 --- a/hooks/check-message.py +++ b/hooks/check-message.py @@ -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) diff --git a/test/test.sh b/test/test.sh index ede5ea4..6c15186 100755 --- a/test/test.sh +++ b/test/test.sh @@ -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 tester@acme.local 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 @@ -59,6 +60,7 @@ cd ../.. echo "goodguy" git_good clone main.git goodguy cd goodguy +git_good config --local user.email tester@acme.local 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 @@ -170,6 +172,7 @@ cd .. echo "badguy" git_good clone main.git badguy cd badguy +git_good config --local user.email tester@acme.local cp case1.cpp case19.cpp git_good add case19.cpp @@ -182,3 +185,5 @@ git_good commit --amend -m "Fix: Message" git_bad push cd .. + +echo "All tests passed successfully"