-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathformat-python.sh
executable file
·58 lines (51 loc) · 2 KB
/
format-python.sh
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
BASE_REVISION=$1
WHOAMI=$(whoami)
OWNER=$(ls -ld . | awk '{print $3}')
GOSU_USER=""
if [ "$WHOAMI" != "$OWNER" ]; then
GOSU_USER=$OWNER
fi
if [ -z "$BASE_REVISION" ]; then
echo "You must provide the base branch, e.g.: format-python.sh origin/beta"
exit
elif [ "$BASE_REVISION" == "-l" ] || [ "$BASE_REVISION" == "--last" ]; then
if [ -n "$GOSU_USER" ]; then
BASE_REVISION=$(gosu "$GOSU_USER" git log --oneline| head -n 1 | awk '{ print $1}')
else
BASE_REVISION=$(git log --oneline| head -n 1 | awk '{ print $1}')
fi
fi
# First, do not touch formatting but fix:
# - single/double quotes (--select Q)
# - sort imports (--select I)
# - unused imports (--select F401)
# - deprecated `mock` (--select UP026)
# - extraneous-parentheses (--select UP034)
# - Unnecessary parentheses after class definition (--select UP039)
# - Indentation warning (--select W1)
# - No newline at end of file (--select W292)
if [ -n "$GOSU_USER" ]; then
PYTHON_CHANGES=$(gosu "$GOSU_USER" git diff --name-only "$BASE_REVISION" | grep '\.py')
else
PYTHON_CHANGES=$(git diff --name-only "$BASE_REVISION" | grep '\.py')
fi
if [ -n "$PYTHON_CHANGES" ]; then
echo "Using ruff..."
if [ -n "$GOSU_USER" ]; then
gosu "$GOSU_USER" git diff --name-only "$BASE_REVISION" | grep '\.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix
else
git diff --name-only "$BASE_REVISION" | grep '\.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix
fi
# Applying Black format with `darker` with options:
# --isort: Using isort
# --revision: Compare changes with revision $BASE_REVISION
echo "Using darker..."
if [ -n "$GOSU_USER" ]; then
gosu "$GOSU_USER" darker --isort --revision "$BASE_REVISION"
else
darker --isort --revision "$BASE_REVISION"
fi
else
echo "No Python changes detected!"
fi