forked from quantumlib/Cirq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-incremental
executable file
·137 lines (120 loc) · 4.57 KB
/
format-incremental
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
################################################################################
# Formats python files that have been modified.
#
# Usage:
# check/format-incremental [BASE_REVISION] [--apply] [--all]
#
# By default, the script runs flynt to convert old string literal formatting to f-strings
# and analyzes python files that have changed relative to the
# base revision and determines whether they need to be formatted. If any changes
# are needed, it prints the diff and exits with code 1, otherwise it exits with
# code 0.
#
# With '--apply', reformats the files instead of printing the diff and exits
# with code 0.
#
# With '--all', analyzes all python files, instead of only changed files.
#
# You can specify a base git revision to compare against (i.e. to use when
# determining whether or not a file is considered to have "changed"). For
# example, you can compare against 'origin/master' or 'HEAD~1'.
#
# If you don't specify a base revision, the following defaults will be tried, in
# order, until one exists:
#
# 1. upstream/master
# 2. origin/master
# 3. master
#
# If none exists, the script fails.
################################################################################
# Get the working directory to the repo root.
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$(git rev-parse --show-toplevel)"
# Parse arguments.
only_print=1
only_changed=1
rev=""
for arg in $@; do
if [[ "${arg}" == "--apply" ]]; then
only_print=0
elif [[ "${arg}" == "--all" ]]; then
only_changed=0
elif [ -z "${rev}" ]; then
if [ "$(git cat-file -t ${arg} 2> /dev/null)" != "commit" ]; then
echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2
exit 1
fi
rev="${arg}"
else
echo -e "\033[31mToo many arguments. Expected [revision] [--apply] [--all].\033[0m" >&2
exit 1
fi
done
if (( only_changed == 1 )); then
# Figure out which branch to compare against.
if [ -z "${rev}" ]; then
if [ "$(git cat-file -t upstream/master 2> /dev/null)" == "commit" ]; then
rev=upstream/master
elif [ "$(git cat-file -t origin/master 2> /dev/null)" == "commit" ]; then
rev=origin/master
elif [ "$(git cat-file -t master 2> /dev/null)" == "commit" ]; then
rev=master
else
echo -e "\033[31mNo default revision found to compare against. Argument #1 must be what to diff against (e.g. 'origin/master' or 'HEAD~1').\033[0m" >&2
exit 1
fi
fi
base="$(git merge-base ${rev} HEAD)"
if [ "$(git rev-parse ${rev})" == "${base}" ]; then
echo -e "Comparing against revision '${rev}'." >&2
else
echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2
rev="${base}"
fi
# Get the modified and added python files. Lines in git diff --name-status look like this:
# M cirq/__init__.py
# A cirq/added.py
modified_files=$(git diff --name-status ${rev} -- | grep '\.py$' | grep -v '_pb2\.py$' | grep '^[MA].*$' | awk '{print $2}')
# Get the moved python files. Lines in git diff --name-status look like this:
# R100 cirq/_doc.py cirq/_doc2.py
moved_files=$(git diff --name-status ${rev} -- | grep '\.py$' | grep -v '_pb2\.py$' | grep '^R.*$' | awk '{print $3}')
format_files="$modified_files $moved_files"
else
echo -e "Formatting all python files." >&2
format_files=$(find . -name "*.py" | grep -v "_pb2\.py$")
fi
any_files=0
for format_file in ${format_files}; do
any_files=1
done
if (( any_files == 0 )); then
echo -e "\033[32mNo files to format\033[0m."
exit 0
fi
flynt_args=("--quiet" "-f")
if (( only_print == 1 )); then
flynt_args+=("--dry-run")
fi
flynt ${format_files} "${flynt_args[@]}"
FLYNTSTATUS=$?
echo "Running the black formatter..."
args=("--color")
if (( only_print == 1 )); then
args+=("--check" "--diff")
fi
# Warn users about bug in black: https://github.com/psf/black/issues/1629
# Once that is fixed upstream, we can just do:
# black "${args[@]}" ${format_files}
# exit $?
LOGS="$(black "${args[@]}" ${format_files} 2>&1)"
BLACKSTATUS=$?
echo "${LOGS}"
if [[ "$BLACKSTATUS" == "123" && "$LOGS" =~ ^.*"INTERNAL ERROR: Black produced different code on the second pass of the formatter.".*$ ]]; then
echo -e "\033[31mWarning, it seems we ran into https://github.com/psf/black/issues/1629. Typically, this can be fixed by adding a trailing comma. If you get stuck, please file a cirq issue on github.\033[0m"
fi
if [[ "$FLYNTSTATUS" != "0" || "$BLACKSTATUS" != "0" ]]; then
exit 1
fi
exit 0