aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Kirchoff <[email protected]>2022-04-05 13:56:02 -0700
committerGitHub <[email protected]>2022-04-05 13:56:02 -0700
commit3a33d040ddaaa9634ca4fab04e3182e7a8313547 (patch)
treec463890deaec7fb7c524f18de70e893797477fec
parentMinor formatting fix (diff)
downloadzen-3a33d040ddaaa9634ca4fab04e3182e7a8313547.tar.xz
zen-3a33d040ddaaa9634ca4fab04e3182e7a8313547.zip
Add pre-commit config (#69)
* Add .pre-commit-config.yaml * format all using pre-commit clang-format hook * Add pre-commit to inject unreal header comment * Remove prepare_commit.bat & update CODING.md * Remove check-added-large-files, add headers before clang-format
-rw-r--r--.pre-commit-config.yaml28
-rw-r--r--.unreal-header1
-rw-r--r--CODING.md6
-rw-r--r--prepare_commit.bat1
-rw-r--r--scripts/formatcode.py138
-rw-r--r--zen/zen.cpp2
-rw-r--r--zencore-test/zencore-test.cpp2
-rw-r--r--zenserver/compute/function.cpp4
-rw-r--r--zenserver/testing/launch.cpp8
-rw-r--r--zenutil/zenserverprocess.cpp4
10 files changed, 46 insertions, 148 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 000000000..98535dd94
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,28 @@
+# See https://pre-commit.com for more information
+# See https://pre-commit.com/hooks.html for more hooks
+repos:
+- repo: https://github.com/pre-commit/pre-commit-hooks
+ rev: v3.2.0
+ hooks:
+ #- id: end-of-file-fixer
+ #- id: trailing-whitespace
+ #- id: check-added-large-files
+ - id: mixed-line-ending
+ - id: check-yaml
+ - id: check-case-conflict
+- repo: https://github.com/Lucas-C/pre-commit-hooks
+ rev: v1.1.13
+ hooks:
+ - id: insert-license
+ exclude: sha1\.(cpp|h)$
+ files: \.(cpp|h)$
+ args:
+ - --license-filepath
+ - .unreal-header # defaults to: LICENSE.txt
+ - --comment-style
+ - // # defaults to: #
+- repo: https://github.com/pre-commit/mirrors-clang-format
+ rev: v13.0.1
+ hooks:
+ - id: clang-format
+exclude: ^thirdparty/
diff --git a/.unreal-header b/.unreal-header
new file mode 100644
index 000000000..473471916
--- /dev/null
+++ b/.unreal-header
@@ -0,0 +1 @@
+Copyright Epic Games, Inc. All Rights Reserved. \ No newline at end of file
diff --git a/CODING.md b/CODING.md
index d94d9d665..54774590f 100644
--- a/CODING.md
+++ b/CODING.md
@@ -12,4 +12,8 @@ Those who are familiar with the UE coding standards will note that we do not req
To ensure consistent formatting we rely on `clang-format` to automatically format source code. This leads to consistent formatting which should lead to less surprises and more straightforward merging.
-Formatting is triggered via `prepare_commit` which should be used ahead of commit. We do not currently reject commits which have not been formatted, but we probably should at some point in the future.
+Formatting is ensured by using [pre-commit](https://pre-commit.com/)
+- [Install pre-commit](https://pre-commit.com/#installation) so it is available in PATH
+- Run pre-commit manually on staged files `pre-commit run`
+- Run pre-commit manually on all files `pre-commit run --all-files`
+- Install git commit hooks `pre-commit install`, which will automatically run before every commit.
diff --git a/prepare_commit.bat b/prepare_commit.bat
deleted file mode 100644
index 8e621c96c..000000000
--- a/prepare_commit.bat
+++ /dev/null
@@ -1 +0,0 @@
-python %~dp0scripts\formatcode.py %*
diff --git a/scripts/formatcode.py b/scripts/formatcode.py
deleted file mode 100644
index 49a8753da..000000000
--- a/scripts/formatcode.py
+++ /dev/null
@@ -1,138 +0,0 @@
-import argparse
-import fileinput
-import os
-import pathlib
-import re
-import subprocess
-
-match_expressions = []
-valid_extensions = []
-root_dir = ''
-use_batching = True
-
-def is_header_missing(f):
- with open(f) as reader:
- lines = reader.read().lstrip().splitlines()
- if len(lines) > 0: return not lines[0].startswith("// ")
- return True
-
-def add_headers(files, header):
- for line in fileinput.input(files, inplace=True):
- if fileinput.isfirstline():
- [ print(h) for h in header.splitlines() ]
- print(line, end="")
-
-def scan_tree(root):
- files = []
- header_files = []
- with os.scandir(root) as dirs:
- for entry in dirs:
- if entry.is_dir():
- scan_tree(os.path.join(root, entry.name))
- continue
- full_path = os.path.join(root, entry.name)
- relative_root_path = os.path.relpath(full_path, start=root_dir)
- if is_matching_filename(relative_root_path):
- print("... formatting: {}".format(relative_root_path))
- files.append(full_path)
- if is_header_missing(full_path):
- header_files.append(full_path)
- args = ""
- if files:
- if use_batching:
- os.system("clang-format -i " + " ".join(files))
- else:
- for file in files:
- os.system("clang-format -i " + file)
- if header_files:
- add_headers(header_files, "// Copyright Epic Games, Inc. All Rights Reserved.\n\n")
-
-def scan_zen(root):
- with os.scandir(root) as dirs:
- for entry in dirs:
- if entry.is_dir() and entry.name.startswith("zen"):
- scan_tree(os.path.join(root, entry.name))
-
-def is_matching_filename(relative_root_path):
- global match_expressions
- global root_dir
- global valid_extensions
-
- if os.path.splitext(relative_root_path)[1].lower() not in valid_extensions:
- return False
- if not match_expressions:
- return True
- relative_root_path = relative_root_path.replace('\\', '/')
- for regex in match_expressions:
- if regex.fullmatch(relative_root_path):
- return True
- return False
-
-def parse_match_expressions(wildcards, matches):
- global match_expressions
- global valid_extensions
-
- valid_extensions = ['.cpp', '.h']
-
- for wildcard in wildcards:
- regex = wildcard.replace('*', '%FORMAT_STAR%').replace('\\', '/')
- regex = re.escape(regex)
- regex = '.*' + regex.replace('%FORMAT_STAR%', '.*') + '.*'
- try:
- match_expressions.append(re.compile(regex, re.IGNORECASE))
- except Exception as ex:
- print(f'Could not parse input filename expression \'{wildcard}\': {str(ex)}')
- quit()
- for regex in matches:
- try:
- match_expressions.append(re.compile(regex, re.IGNORECASE))
- except Exception as ex:
- print(f'Could not parse input --match expression \'{regex}\': {str(ex)}')
- quit()
-
-def validate_clang_format():
- vstring = subprocess.check_output("clang-format --version", shell=True).decode().rstrip()
-
- match = re.search(r'(\d+)\.(\d+)(\.(\d+))?$', vstring)
- if not match:
- raise ValueError("invalid version number '%s'" % vstring)
-
- (major, minor, patch) = match.group(1, 2, 4)
-
- if int(major) < 13:
- if int(minor) == 0:
- if int(patch) < 1:
- raise ValueError(f'invalid clang-format version -- we require at least v12.0.1')
-
-def _main():
- global root_dir, use_batching
-
- parser = argparse.ArgumentParser()
- parser.add_argument('filenames', nargs='*', help="Match text for filenames. If fullpath contains text it is a match, " +\
- "* is a wildcard. Directory separators are matched by either / or \\. Case insensitive.")
- parser.add_argument('--match', action='append', default=[], help="Match regular expression for filenames. " +\
- "Relative path from the root zen directory must be a complete match. Directory separators are matched only by /. Case insensitive.")
- parser.add_argument('--batch', dest='use_batching', action='store_true', help="Enable batching calls to clang-format.")
- parser.add_argument('--no-batch', dest='use_batching', action='store_false', help="Disable batching calls to clang-format.")
- parser.set_defaults(use_batching=True)
- options = parser.parse_args()
-
- parse_match_expressions(options.filenames, options.match)
- root_dir = pathlib.Path(__file__).parent.parent.resolve()
- use_batching = options.use_batching
-
- validate_clang_format()
-
- while True:
- if (os.path.isfile(".clang-format")):
- scan_zen(".")
- quit()
- else:
- cwd = os.getcwd()
- if os.path.dirname(cwd) == cwd:
- quit()
- os.chdir("..")
-
-
-if __name__ == '__main__':
- _main() \ No newline at end of file
diff --git a/zen/zen.cpp b/zen/zen.cpp
index 7f547a77a..dc9be5d15 100644
--- a/zen/zen.cpp
+++ b/zen/zen.cpp
@@ -1,3 +1,5 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
// Zen command line client utility
//
diff --git a/zencore-test/zencore-test.cpp b/zencore-test/zencore-test.cpp
index cd4ce3e0a..7d7365c54 100644
--- a/zencore-test/zencore-test.cpp
+++ b/zencore-test/zencore-test.cpp
@@ -1,3 +1,5 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
// zencore-test.cpp : Defines the entry point for the console application.
//
diff --git a/zenserver/compute/function.cpp b/zenserver/compute/function.cpp
index 996573573..7c5f33e78 100644
--- a/zenserver/compute/function.cpp
+++ b/zenserver/compute/function.cpp
@@ -485,8 +485,8 @@ HttpFunctionService::ExecActionUpstream(const WorkerDesc& Worker, CbObject& Obje
HttpResponseCode
HttpFunctionService::ExecActionUpstreamResult(const IoHash& WorkerId, CbObject& Object)
{
- const static IoHash Empty = CbObject().GetHash();
- auto Status = m_UpstreamApply->GetStatus(WorkerId, Empty);
+ const static IoHash Empty = CbObject().GetHash();
+ auto Status = m_UpstreamApply->GetStatus(WorkerId, Empty);
if (!Status.Success)
{
return HttpResponseCode::NotFound;
diff --git a/zenserver/testing/launch.cpp b/zenserver/testing/launch.cpp
index 0334429b0..1236e6adb 100644
--- a/zenserver/testing/launch.cpp
+++ b/zenserver/testing/launch.cpp
@@ -167,10 +167,10 @@ SandboxedJob::GrantNamedObjectAccess(PWSTR ObjectName, SE_OBJECT_TYPE ObjectType
.grfAccessMode = GRANT_ACCESS,
.grfInheritance = grfInhericance,
.Trustee = {.pMultipleTrustee = nullptr,
- .MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE,
- .TrusteeForm = TRUSTEE_IS_SID,
- .TrusteeType = TRUSTEE_IS_GROUP,
- .ptstrName = (PWSTR)m_AppContainerSid}};
+ .MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE,
+ .TrusteeForm = TRUSTEE_IS_SID,
+ .TrusteeType = TRUSTEE_IS_GROUP,
+ .ptstrName = (PWSTR)m_AppContainerSid}};
PACL OldAcl = nullptr;
diff --git a/zenutil/zenserverprocess.cpp b/zenutil/zenserverprocess.cpp
index f49d5f6d8..3a4957b76 100644
--- a/zenutil/zenserverprocess.cpp
+++ b/zenutil/zenserverprocess.cpp
@@ -550,8 +550,8 @@ ZenServerInstance::SpawnServer(int BasePort, std::string_view AdditionalServerAr
const std::filesystem::path BaseDir = m_Env.ProgramBaseDir();
const std::filesystem::path Executable = BaseDir / "zenserver" ZEN_EXE_SUFFIX_LITERAL;
CreateProcOptions CreateOptions = {
- .WorkingDirectory = &CurrentDirectory,
- .Flags = CreationFlags,
+ .WorkingDirectory = &CurrentDirectory,
+ .Flags = CreationFlags,
};
CreateProcResult ChildPid = CreateProc(Executable, CommandLine.ToView(), CreateOptions);
#if ZEN_PLATFORM_WINDOWS