-
Notifications
You must be signed in to change notification settings - Fork 3
/
check-format.sh
executable file
·53 lines (44 loc) · 1.92 KB
/
check-format.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
#!/bin/sh
set -e
die() { echo "$@" >&2; exit 1; }
exitCode=0
# Make sure target branch exists
target="$1"; shift
if [ "$CI" ]; then
git rev-parse -q --no-revs --verify "origin/${target}" || \
git rev-parse -q --no-revs --verify "${target}" || \
git fetch origin --depth=1 "${target}"
git rev-parse -q --no-revs --verify "origin/${target}" || \
git rev-parse -q --no-revs --verify "${target}" || \
git fetch origin --depth=1 tag "${target}"
# Ensure that the target revision has some history
target_sha=$(git rev-parse -q --verify "origin/${target}" || git rev-parse -q --verify "${target}")
git fetch -q "--depth=${FETCH_DEPTH:-50}" origin "+${target_sha}"
else
target_sha=$(git rev-parse -q --verify "${target}") || die "fatal: couldn't find ref ${target}"
fi
if [ -z "$1" ] || [ -e "$1" ]; then
src=HEAD
else
src="$1"; shift
fi
# We expect the user or CI (either GitLab or entrypoint.sh) to have taken care of source history
src_sha=$(git rev-parse -q --verify "${src}") || die "fatal: couldn't find ref ${src}"
echo "Using $(clang-format --version)"
echo "Checking $(git rev-list --count "${src_sha}" "^${target_sha}") commits since revision ${target_sha}"
for commit in $(git rev-list --reverse "${src_sha}" "^${target_sha}"); do
printf "%s" "${commit}... "
cfOutput="$(git -c color.ui=always clang-format --diff "${commit}^" "${commit}" -- "$@" || true)";
if [ "$SHOW_SKIPPED" ] && [ "${cfOutput}" = "no modified files to format" ]; then
printf "%b\n" "\e[32mSKIPPED\e[0m"
elif [ -z "${cfOutput}" ] || [ "${cfOutput}" = "no modified files to format" ]; then
printf "%b\n" "\e[32mPASSED\e[0m"
else
printf "%b\n" "\e[31;1mFAILED\e[0m"
exitCode=1
commitMessage=$(git log -1 --oneline --no-decorate "${commit}");
echo "Commit ${commitMessage} introduces invalid format:";
echo "${cfOutput}"
fi
done
exit ${exitCode}