forked from eisop-plume-lib/plume-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci-lint-diff
executable file
·174 lines (156 loc) · 5.5 KB
/
ci-lint-diff
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
#!/bin/sh
# Given a file of warnings, reports only those that are in the diff for the
# current pull request. Works for Azure Pipelines, CircleCI, GitHub Actions,
# and Travis CI. Exit status is non-zero if any warnings are output.
# (On CircleCI, it depends on environment variable CIRCLE_COMPARE_URL;
# see documentation of `ci-info` script for details.)
# Example use:
#
# if [ -d /tmp/$USER/plume-scripts ] ; then
# git -C /tmp/$USER/plume-scripts pull -q > /dev/null 2>&1
# else
# mkdir -p /tmp/$USER && git -C /tmp/$USER clone --depth 1 -q https://github.com/plume-lib/plume-scripts.git
# fi
# (command-that-issues-warnings > /tmp/warnings.txt 2>&1) || true
# /tmp/$USER/plume-scripts/ci-lint-diff /tmp/warnings.txt
#
# If you get a warning that /tmp/diff is empty, here are two possible reasons:
# * Your branch is identical to the base/upstream branch. In this case, the pull
# request is pointless.
# * Your clone is shallow and does not contain all the commits. You can fix that
# by pulling upstream.
#
# Requires the `jq` program to be installed, when used in Azure Pipelines jobs.
DEBUG=""
if [ "$#" -eq 0 ] ; then
echo "Usage: $0 [--debug] WARNINGS-FILE" >&2
exit 2
fi
if [ "$1" = "--debug" ] ; then
DEBUG="--debug"
shift
fi
if [ "$#" -ne 1 ] ; then
echo "Usage: $0 [--debug] WARNINGS-FILE" >&2
exit 2
fi
if ! [ -f "$1" ] ; then
echo "File $1 does not exist" >&2
exit 2
fi
WARNINGSFILE=$1
set -e
if [ "$DEBUG" = "--debug" ] ; then
env | sort
git log --graph | head --lines=10000
fi
SCRIPTDIR="$( cd "$(dirname "$0")" ; pwd -P )"
eval "$("${SCRIPTDIR}"/ci-info)"
# Ensure the commits mentioned in $CI_COMMIT_RANGE are available.
if ! git cat-file -e "${CI_COMMIT_RANGE_START}^{commit}" ; then
CI_LINT_DIFF_ERROR="Commit CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START not found; clone more deeply."
fi
if ! git cat-file -e "${CI_COMMIT_RANGE_END}^{commit}" ; then
CI_LINT_DIFF_ERROR="Commit CI_COMMIT_RANGE_END=$CI_COMMIT_RANGE_END not found; clone more deeply."
fi
set +e
rm -rf /tmp/diff.txt
echo "git diff $CI_COMMIT_RANGE > /tmp/diff.txt"
git --no-pager diff --exit-code "$CI_COMMIT_RANGE" > /tmp/diff.txt 2>&1
git_status=$?
if [ $git_status -gt 1 ] ; then
CI_LINT_DIFF_ERROR="git diff exited with status $git_status"
cat /tmp/diff.txt
git --no-pager diff --exit-code "$CI_COMMIT_RANGE"
git --no-pager branch -a
git --no-pager diff "$CI_COMMIT_RANGE"
elif [ ! -r /tmp/diff.txt ] ; then
CI_LINT_DIFF_ERROR="/tmp/diff.txt does not exist"
fi
if [ ! -s /tmp/diff.txt ] ; then
# Other causes of an empty diff:
# * the commit range is a commit and its revert
# * HEAD is a merge commit and the merge took all its changes from its second argument
CI_LINT_DIFF_ERROR="ERROR: Empty diff for $CI_COMMIT_RANGE. Try pulling base branch (often master) into compare branch (often your feature branch)."
fi
if [ -n "$CI_LINT_DIFF_ERROR" ] ; then
echo
echo "*****"
echo "$CI_LINT_DIFF_ERROR"
echo "*****"
echo
echo "This command will print diagnostic information, then exit."
echo
# This is not very effective because environment variables were set by
# the previous invocation of ci-info.
echo "${SCRIPTDIR}"/ci-info --debug
"${SCRIPTDIR}"/ci-info --debug
echo end of "${SCRIPTDIR}"/ci-info --debug
echo
echo git rev-parse \$CI_COMMIT_RANGE_START
echo git rev-parse "$CI_COMMIT_RANGE_START"
git rev-parse "$CI_COMMIT_RANGE_START"
echo git rev-parse \$CI_COMMIT_RANGE_END
echo git rev-parse "$CI_COMMIT_RANGE_END"
git rev-parse "$CI_COMMIT_RANGE_END"
echo git rev-parse remotes/origin/\$CI_COMMIT_RANGE_START
echo git rev-parse "remotes/origin/$CI_COMMIT_RANGE_START"
git rev-parse "remotes/origin/$CI_COMMIT_RANGE_START" || true
echo git rev-parse remotes/origin/\$CI_COMMIT_RANGE_END
echo git rev-parse "remotes/origin/$CI_COMMIT_RANGE_END"
git rev-parse "remotes/origin/$CI_COMMIT_RANGE_END" || true
echo
echo git fetch
git fetch
echo
echo git rev-parse "$CI_COMMIT_RANGE_START"
git rev-parse "$CI_COMMIT_RANGE_START"
echo git rev-parse "$CI_COMMIT_RANGE_END"
git rev-parse "$CI_COMMIT_RANGE_END"
echo git rev-parse "remotes/origin/$CI_COMMIT_RANGE_START"
git rev-parse "remotes/origin/$CI_COMMIT_RANGE_START" || true
echo git rev-parse "remotes/origin/$CI_COMMIT_RANGE_END"
git rev-parse "remotes/origin/$CI_COMMIT_RANGE_END" || true
echo
echo "Branches:"
git --no-pager branch -a
echo
echo "git remote show origin"
git remote show origin
echo
echo "Re-execution of diff command:"
echo git --no-pager diff --exit-code "$CI_COMMIT_RANGE"
git --no-pager diff --exit-code "$CI_COMMIT_RANGE"
echo
echo "Second re-execution of diff command:"
echo "git --no-pager diff --exit-code $CI_COMMIT_RANGE > /tmp/tmp.tmp && cat /tmp/tmp.tmp"
git --no-pager diff --exit-code "$CI_COMMIT_RANGE" > /tmp/tmp.tmp && cat /tmp/tmp.tmp
echo
echo "Endpoints of commit range ($CI_COMMIT_RANGE_START and $CI_COMMIT_RANGE_END):"
git --no-pager show "$CI_COMMIT_RANGE_START"
git --no-pager show "$CI_COMMIT_RANGE_END"
echo
echo "Current directory:"
pwd
echo
echo "Contents of /tmp:"
ls -l /tmp/
echo
echo "Environment:"
env | sort
echo
echo "*****"
echo "$CI_LINT_DIFF_ERROR"
echo "*****"
exit 2
fi
if [ "$DEBUG" = "--debug" ] ; then
echo "warnings file $WARNINGSFILE:"
cat "$WARNINGSFILE"
echo "end of warnings file $WARNINGSFILE."
echo "/tmp/diff.txt file:"
cat /tmp/diff.txt
echo "end of /tmp/diff.txt file."
fi
set -e
"${SCRIPTDIR}"/lint-diff.py $DEBUG --guess-strip /tmp/diff.txt "$WARNINGSFILE"