forked from firecracker-microvm/firecracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre-commit
executable file
·39 lines (34 loc) · 1.33 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/sh
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# We avoid checks which require building firecracker due to the issues this introduces when
# attempting to interact with the repository from an enviroment in which you cannot build
# firecracker.
# This will only fail when a staged file does not contain an appropriate license
# when formatting is incorrect this will automaticalt rectify it.
# Exit immediately when encountering a non-zero command
set -e
# Audit code base
cargo audit
# For every staged file
for i in $(git diff --name-only --cached --diff-filter=d); do
echo $i
# Get the extension
filename=$(basename -- "$i")
extension="${filename##*.}"
if [ "$extension" = "rs" ]; then
# Read rustfmt config, replace '\n' with ','
rustfmt_config="$(sed -z "s/\n/,/g;s/,$/\n/" ./tests/fmt.toml)"
# We first do a check run, this will fail when it finds a non-matching license.
rustfmt $i --check --config $rustfmt_config
# Run `cargo fmt` for this file
rustfmt $i --config $rustfmt_config
fi
if [ "$extension" = "py" ]; then
# Apply formatters for this file
black $i
isort $i
fi
# Add changes to this file (as a result of formatting) to the commit.
git add $i
done