Skip to content

Commit b0745bf

Browse files
committed
feat: git hooks
1 parent d55eaf2 commit b0745bf

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-6
lines changed

.gitconfig

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[core]
2+
hooksPath = .hooks

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
tags

.hooks/commit-msg

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env zsh
2+
set -euo pipefail
3+
4+
readonly commit_message=$1
5+
readonly title=`cat $commit_message | head -1`
6+
7+
# Allow for a range of commit title prefixes to be less constrained.
8+
if [[ ! -z `echo $title | grep -E '^(Merge |ver|fixup!|squash!|wip|WIP)'` ]]; then
9+
exit 0
10+
fi
11+
12+
function handle_invalid_message() {
13+
echo ""
14+
echo "Edit your previous commit message with 'git commit -eF `readlink -f $commit_message`'."
15+
sed -i '/^#/d' $commit_message # Strip comments from invalid message.
16+
exit 1
17+
}
18+
19+
readonly prefix_regex='^(feat|fix|ref|rename|clean|docs|format|perf|test|bench|security|xcode):'
20+
21+
if [[ -z `echo $title | grep -E $prefix_regex` ]]; then
22+
echo "error: Commit title must start with a valid prefix:"
23+
echo "'feat:', 'fix:', 'ref:', 'rename:', 'clean:', 'docs:', 'format:', 'perf:', 'test:', 'bench:', 'security:', 'xcode:', 'Merge', 'fixup!' 'squash!', 'wip' or 'WIP'"
24+
handle_invalid_message
25+
26+
elif [[ -z `echo $title | grep -E "$prefix_regex "` ]]; then
27+
echo "error: Commit title prefix must be followed by a space."
28+
handle_invalid_message
29+
30+
elif [[ ! -z `echo $title | grep -E '\.$'` ]]; then
31+
echo "error: Commit title must not end with a dot."
32+
handle_invalid_message
33+
fi

.hooks/pre-commit

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env zsh
2+
set -euo pipefail
3+
cd `git rev-parse --show-toplevel`
4+
5+
# Use 'new line' as the only separator in for-loops.
6+
IFS=$'\n'
7+
8+
# Exit code to signal if all checks passed.
9+
exit_code=0
10+
11+
# Sync ctags file on commit.
12+
ctags **/*.(c|cpp|h|hpp|mm|m|swift|rs)
13+
14+
# Install missing tools.
15+
[[ ! `command -v clang-format` ]] && brew install clang-format
16+
[[ ! `command -v gsed` ]] && brew install gnu-sed
17+
18+
# Check for new and renamed files if there is a space in the file name.
19+
function check_for_spaces_in_file_names() {
20+
for file in `git diff --name-only --diff-filter=AR --staged | grep '\s'`; do
21+
echo "error: '$file' contains a space in the file name."
22+
exit_code=1
23+
done
24+
}
25+
26+
# Check if the staged content contains trailing white space.
27+
function check_for_trailing_white_space() {
28+
for file in `git diff --name-only --diff-filter=AM --staged`; do
29+
if [[ ! -z `file $file | grep text` ]]; then
30+
if [[ $file == mono.xcodeproj/* ]]; then
31+
continue
32+
fi
33+
34+
gsed -i 's/[ \t]*$//' $file
35+
git add $file
36+
fi
37+
done
38+
}
39+
40+
# Check if the staged content contains tabs.
41+
function check_for_tabs() {
42+
for file in `git diff --name-only --diff-filter=AM --staged`; do
43+
if [[ $file == *.sh ]]; then
44+
gsed -i 's/\t/ /g' $file
45+
git add $file
46+
fi
47+
done
48+
}
49+
50+
# Replace #include with #import.
51+
function check_include_pragmas() {
52+
for file in `git diff --name-only --diff-filter=AM --staged`; do
53+
if [[ $file == Third/* ]]; then
54+
continue
55+
fi
56+
57+
if [[ $file == *.(c|cpp|mm|h|hpp) ]]; then
58+
gsed -i 's/#include/#import/' $file
59+
git add $file
60+
fi
61+
done
62+
}
63+
64+
# Check if the staged files which were added or modified are formatted completely.
65+
function check_for_formatting() {
66+
for file in `git diff --name-only --diff-filter=AM --staged`; do
67+
if [[ $file == *.(c|cpp|mm|m|h|hpp) ]]; then
68+
clang-format -i --style=file $file
69+
git add $file
70+
fi
71+
72+
if [[ $file == *.(rs) ]]; then
73+
rustfmt $file
74+
git add $file
75+
fi
76+
done
77+
}
78+
79+
check_for_formatting
80+
check_for_spaces_in_file_names
81+
check_for_trailing_white_space
82+
check_for_tabs
83+
check_include_pragmas
84+
85+
exit $exit_code

src/main.rs

-6
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ fn view_tree() -> impl view::AnyView {
114114
Rectangle::default()
115115
.size(100.0, 100.0),
116116

117-
Loop::new(10, |idx|
118-
Circle::default()
119-
.diameter(10.0 * (idx + 1) as f64 / 2.0)
120-
.padding_left((idx * 40 + 1) as f64)
121-
),
122-
123117
Circle::default()
124118
.diameter(50.0)
125119
.padding_top(25.0)

0 commit comments

Comments
 (0)