-
Notifications
You must be signed in to change notification settings - Fork 0
/
differential-checkton.sh
executable file
·206 lines (170 loc) · 5.56 KB
/
differential-checkton.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
set -o errexit -o nounset -o pipefail
SCRIPTDIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
BASE=${CHECKTON_DIFF_BASE:-$(git rev-parse main)}
HEAD=${CHECKTON_DIFF_HEAD:-$(git rev-parse HEAD)}
MERGE_BASE=$(git merge-base "$BASE" "$HEAD")
DIFF_ARGS=(--diff-filter=d)
if [[ "${CHECKTON_FIND_RENAMES:-}" != "false" ]]; then
DIFF_ARGS+=("-M${CHECKTON_FIND_RENAMES:-}")
fi
if [[ "${CHECKTON_FIND_COPIES:-}" != "false" ]]; then
DIFF_ARGS+=("-C${CHECKTON_FIND_COPIES:-}")
fi
if [[ "${CHECKTON_FIND_COPIES_HARDER:-}" == "true" ]]; then
DIFF_ARGS+=(--find-copies-harder)
fi
DIFFERENTIAL=${CHECKTON_DIFFERENTIAL:-"true"}
WORKDIR=$(mktemp -d --tmpdir "checkton-workdir.XXXXXX")
cleanup() {
rm -rf "$WORKDIR" || true
git worktree list | awk '{ print $1 }' | while read -r worktree; do
if [[ "$worktree" == "$WORKDIR"/* ]]; then
git worktree remove "$worktree" 2>/dev/null || true
fi
done
}
trap cleanup EXIT
# {head_ref_filename => base_ref_filename or ""}
declare -A FILE_PAIRS=()
collect_file_pairs() {
local filename
while read -r filename; do
FILE_PAIRS[$filename]=""
done < <(_ls_files_to_check)
echo "Files to check:" >&2
if [[ ${#FILE_PAIRS[@]} -eq 0 ]]; then
echo " <none>" >&2
return
fi
if [[ "$DIFFERENTIAL" == "false" ]]; then
# print just the filenames at HEAD
printf " %s\n" "${!FILE_PAIRS[@]}" >&2
return
fi
local line status old_name current_name
while read -r line; do
read -r status old_name current_name <<< "$line"
if [[ -z "$current_name" ]]; then
current_name=$old_name
fi
if [[ "${FILE_PAIRS[$current_name]+is_set}" != is_set ]]; then
continue
fi
if _should_diff_file "$status"; then
FILE_PAIRS[$current_name]=$old_name
fi
# print the full status of each file
echo " $line" >&2
done < <(git diff --name-status "${DIFF_ARGS[@]}" "${MERGE_BASE}...${HEAD}")
}
_ls_files_to_check() {
if [[ "$DIFFERENTIAL" == "false" ]]; then
git ls-tree -r --name-only "$HEAD"
else
git diff --name-only "${DIFF_ARGS[@]}" "${MERGE_BASE}...${HEAD}"
fi | _apply_include_exlude_patterns | sort -u
}
_apply_include_exlude_patterns() {
local include_regex=${CHECKTON_INCLUDE_REGEX:-'\.ya?ml$'}
local exclude_regex=${CHECKTON_EXCLUDE_REGEX:-}
_nofail_grep -E "$include_regex" |
if [[ -n "$exclude_regex" ]]; then _nofail_grep -v -E "$exclude_regex"; else cat; fi
}
_nofail_grep() {
local rc=0
grep "$@" || rc=$?
[[ $rc -eq 1 ]] && rc=0
return $rc
}
_should_diff_file() {
local gitstatus=$1
case "$gitstatus" in
*A*) false ;; # the file didn't exist in the base ref
*R*) [[ "${CHECKTON_FIND_RENAMES:-}" != "false" ]] ;;
*C*) [[ "${CHECKTON_FIND_COPIES:-}" != "false" ]] ;;
*) true ;;
esac
}
# NOTE: may change the PWD, best called in a subshell
at_ref() {
local ref=$1
shift
if [[ "$ref" != "$(git rev-parse HEAD)" ]]; then
local worktree=$WORKDIR/repo-${ref}
# don't print unwanted fluff to stderr unless the command errors
_with_deferred_stderr git worktree add -d "$worktree" "$ref" >/dev/null
cd "$worktree"
_with_deferred_stderr git checkout "$ref" >/dev/null
fi
}
_with_deferred_stderr() {
local errfile
errfile=$(mktemp -p "$WORKDIR" err.XXXXXX)
local rc=0
"$@" 2> "$errfile" || rc=$?
if [[ $rc -ne 0 ]]; then
cat "$errfile" >&2
fi
return $rc
}
dupe_renamed_and_copied_files() {
local current_name old_name
for current_name in "${!FILE_PAIRS[@]}"; do
old_name=${FILE_PAIRS[$current_name]}
if [[ -n "$old_name" && "$old_name" != "$current_name" && ! -e "$current_name" ]]; then
mkdir -p "$(dirname "$current_name")"
cp "$old_name" "$current_name"
fi
done
}
checkton() {
"${SCRIPTDIR}/checkton.py" "$@"
}
csgrep_embed() {
csgrep --mode=json --embed 1
}
main() {
collect_file_pairs
local old_files
mapfile -t old_files < <(printf "%s\n" "${FILE_PAIRS[@]}" | sed '/^$/d')
local new_files=("${!FILE_PAIRS[@]}")
local old_results=$WORKDIR/old.err
local new_results=$WORKDIR/new.err
if [[ "${#new_files[@]}" -gt 0 ]]; then
(
at_ref "$HEAD"
checkton "${new_files[@]}" | csgrep_embed > "$new_results"
)
else
touch "$new_results"
fi
if [[ ${#old_files[@]} -gt 0 ]]; then
local all_files
mapfile -t all_files < <(printf "%s\n" "${old_files[@]}" "${new_files[@]}" | sort -u)
(
at_ref "$MERGE_BASE"
dupe_renamed_and_copied_files
checkton "${all_files[@]}" | csgrep_embed > "$old_results"
)
else
touch "$old_results"
fi
local shellcheck_version
shellcheck_version=$(shellcheck --version | awk '/version:/ { print $2 }')
csdiff "$old_results" "$new_results" |
csgrep \
--mode=sarif \
--set-scan-prop="tool:ShellCheck" \
--set-scan-prop="tool-version:${shellcheck_version}" \
--set-scan-prop="tool-url:https://www.shellcheck.net/wiki/" |
# inject ruleIndex properties so that sarif-fmt can show
# the shortDescription and fullDescription of each rule
jq '.runs[] |=
(
(.tool.driver.rules // [] | map(.id)) as $rule_ids |
.results[] |= (. as $result | .ruleIndex = ($rule_ids | index($result.ruleId)))
)
'
}
main