Skip to content

Commit 47288df

Browse files
feat: git hooks implementation
1 parent c5f82ff commit 47288df

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

.githooks/init_hooks.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/sh
2+
# Creates symlinks of hooks in .githooks directory in .git/hooks
3+
# If user already has hooks in .git/hooks then hooks from .githooks
4+
# will be added as separate files with different names and a command
5+
# to run these hooks will be added to the respective existing hooks
6+
# on the last line of the file.
7+
8+
current_dir=${PWD##*/}
9+
10+
if [ "$current_dir" != ".githooks" ]; then
11+
cd .githooks
12+
fi
13+
14+
shopt -s nullglob
15+
raw_githooks=(*)
16+
exclude=(init_hooks.sh)
17+
githooks=( "${raw_githooks[@]/$exclude}" )
18+
19+
for hook in ${githooks[*]}
20+
do
21+
git_hook_path="../.git/hooks/$hook"
22+
23+
web3swift_hook_comment="# web3swift git hook to perform actions like SwiftLint and codespell"
24+
web3swift_hook_path="source $(pwd)/${hook}"
25+
26+
is_hook_linked=false
27+
28+
if test -f $git_hook_path; then
29+
last_line=$( tac ${git_hook_path} | grep -m 1 -E '[^[:space:]]' )
30+
if [ "$last_line" = "$web3swift_hook_path" ]; then
31+
is_hook_linked=true
32+
fi
33+
else
34+
touch $git_hook_path
35+
chmod +x $git_hook_path
36+
echo "#!/bin/sh\n" >> $git_hook_path
37+
fi
38+
39+
if [ "$is_hook_linked" = false ] ; then
40+
echo "$web3swift_hook_path" >> $git_hook_path
41+
echo "${hook} is linked successfully."
42+
else
43+
echo "${hook} is already linked."
44+
fi
45+
done

.githooks/pre-commit

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
3+
if ! command -v pip3 &> /dev/null
4+
then
5+
>&2 echo "\033[31mpip3 could not be found. Please, install pip3 following the guide on https://pip.pypa.io/en/stable/installation/\033[0m"
6+
exit 1
7+
fi
8+
9+
codespell_out=$(pip3 show codespell | grep "Location")
10+
codespell_path=(${codespell_out//"Location: "/ })
11+
12+
if [ -z "$codespell_path" ]
13+
then
14+
echo "codespell not found. Installing codespell..."
15+
pip3 install --upgrade pip
16+
pip3 install codespell
17+
18+
codespell_out=$(pip3 show codespell | grep "Location")
19+
codespell_path=(${codespell_out//"Location: "/ })
20+
codespell_path="${codespell_path}/bin/codespell"
21+
else
22+
codespell_path="${codespell_path}/bin/codespell"
23+
fi
24+
25+
$codespell_path --count --ignore-words-list=ans,deriver,inout,packag --skip="*.js,*WordLists.swift" Sources/ Tests/ Package.swift CHANGELOG.md CONTRIBUTION.md README.md Web3Core.podspec Web3Swift.podspec
26+
27+
if [ $? -eq 65 ]; then
28+
echo "codespell returned exit code 65. Please, fix the errors."
29+
exit 1
30+
fi
31+
32+
if [ "$(uname -s)" = "Darwin" ]; then
33+
export PATH="$PATH:/opt/homebrew/bin"
34+
35+
if ! command -v swiftlint &> /dev/null
36+
then
37+
echo "swiftlint wasn't found. Installing it from Homebrew."
38+
brew install swiftlint
39+
fi
40+
41+
swiftlint Sources
42+
swiftlint Tests
43+
fi

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ $ ganache
188188
This will create a local blockchain and also some test accounts that are used throughout our tests.
189189
Make sure that `ganache` is running on its default port `8546`. To change the port in test cases locate `LocalTestCase.swift` and modify the static `url` variable.
190190

191+
### Before you commit
192+
193+
Please, run `.githooks/init_hooks.sh` to initialize git hooks that will do `codespell`, `swiftlint` and other checks. _Issues may arise with hooks on platforms other than Mac OS._
194+
191195
## Contribute
192196
Want to improve? It's awesome:
193197
Then good news for you: **We are ready to pay for your contribution via [@gitcoin bot](https://gitcoin.co/grants/358/web3swift)!**

0 commit comments

Comments
 (0)