Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.
Closed
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
96 changes: 53 additions & 43 deletions hooks/pre-commit.clang-format
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
#!/usr/bin/env python

import subprocess

def check_git_config(key, value):
try:
output = subprocess.check_output(["git", "config", "--global", "--get", key]).strip()
except subprocess.CalledProcessError as e:
print("""ERROR: You need to update your global git config:
git config --global %s %s""" % (key, value))
exit(1)
else:
if (output != value):
print("""ERROR: Expected `git config --global --get {key}` == {value}
Instead found {output}

You need to run:
git config --global {key} {value}""".format(key=key, value=value, output=output))
exit(1)

try:
output = subprocess.check_output(["git", "clang-format", "-v"], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
if "'clang-format' is not a git command" in e.output:
print("""ERROR: You need to install git-clang-format:
curl https://raw.githubusercontent.com/llvm-mirror/clang/master/tools/clang-format/git-clang-format > /usr/local/bin/git-clang-format""")
elif "cannot exec 'git-clang-format'" in e.output:
print("""ERROR: You need to make git-clang-format executable:
chmod u+x /usr/local/bin/git-clang-format""")
else:
print("unexpected error, please report: %s" % e.output)

exit(1)
else:
check_git_config("clangFormat.binary", "node_modules/.bin/clang-format")
check_git_config("clangFormat.style", "file")

if ("no modified files to format" in output or
"clang-format did not modify any files" in output):
exit(0)

print("ERROR: You need to run 'git clang-format -f' to re-format your staged edits before commit")
exit(1)
#!/usr/bin/env bash

read -d '' help <<- EOF
This repository requires you to install the git clang-format command.

One-time setup steps:
1) install the git-clang-format script in your \$PATH, for example:

curl https://raw.githubusercontent.com/llvm-mirror/clang/master/tools/clang-format/git-clang-format > /usr/local/bin/git-clang-format

2) make sure git-clang-format is executable:

chmod u+x /usr/local/bin/git-clang-format

In each repository where you use clang-format, add git config keys
so that git clang-format finds your version and config:

git config clangFormat.binary node_modules/.bin/clang-format
git config clangFormat.style file
EOF

check_clang_format() {
if hash git-clang-format 2>/dev/null; then
return
else
echo "SETUP ERROR: no git-clang-format executable found, or it is not executable"
echo "$help"
exit 1
fi
}

check_git_config() {
if [[ "$(git config --get clangFormat.binary)" != "node_modules/.bin/clang-format" ]]; then
echo "SETUP ERROR: git config clangFormat.binary is wrong."
echo "$help"
exit 1
elif [[ "$(git config --get clangFormat.style)" != "file" ]]; then
echo "SETUP ERROR: git config clangFormat.style is wrong."
echo "$help"
exit 1
fi
}

check_clang_format
check_git_config

readonly out=$(git clang-format -v --diff)

if [[ "$out" == *"no modified files to format"* ]]; then exit 0; fi
if [[ "$out" == *"clang-format did not modify any files"* ]]; then exit 0; fi

echo "ERROR: you need to run git clang-format on your commit"
exit 1
3 changes: 1 addition & 2 deletions src/hello-world/hello-world.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ import {HelloWorldComponent} from './hello-world.component';
declarations: [HelloWorldComponent],
exports: [HelloWorldComponent],
})
export class HelloWorldModule {
}
export class HelloWorldModule {}