Skip to content

Commit 91ff0c6

Browse files
authored
Have check.sh fallback to checking everything (#4614)
Sometimes PRs have been cut from so long ago that there's no merge base with master in the shallow clone that Travis makes. In this case, fall back on just checking everything.
1 parent 7dcf7fc commit 91ff0c6

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

scripts/check.sh

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ cd "${top_dir}"
8484

8585
ALLOW_DIRTY=false
8686
COMMIT_METHOD="none"
87+
CHECK_DIFF=false
8788
START_REVISION="origin/master"
8889
TEST_ONLY=false
8990
VERBOSE=false
@@ -98,12 +99,14 @@ fi
9899
# is not available and we need to compute the START_REVISION from the common
99100
# ancestor of $TRAVIS_COMMIT and origin/master.
100101
if [[ -n "${TRAVIS_COMMIT_RANGE:-}" ]] ; then
102+
CHECK_DIFF=true
101103
START_REVISION="$TRAVIS_COMMIT_RANGE"
102104
elif [[ -n "${TRAVIS_COMMIT:-}" ]] ; then
103105
if ! git rev-parse origin/master >& /dev/null; then
104106
git remote set-branches --add origin master
105107
git fetch origin
106108
fi
109+
CHECK_DIFF=true
107110
START_REVISION=$(git merge-base origin/master "${TRAVIS_COMMIT}")
108111
fi
109112

@@ -146,6 +149,7 @@ while [[ $# -gt 0 ]]; do
146149
;;
147150

148151
*)
152+
CHECK_DIFF=true
149153
START_REVISION="$1"
150154
shift
151155
break
@@ -187,9 +191,24 @@ if [[ "${START_REVISION}" == *..* ]]; then
187191
git fetch origin
188192
fi
189193

190-
NEW_RANGE_START=$(git merge-base origin/master "${RANGE_END}")
191-
START_REVISION="${START_REVISION/$RANGE_START/$NEW_RANGE_START}"
192-
START_SHA="${START_REVISION}"
194+
# Try to come up with a more accurate representation of the merge, so that
195+
# checks will operate on just the differences the PR would merge into master.
196+
# The start of the revision range that Travis supplies can sometimes be a
197+
# seemingly random value.
198+
NEW_RANGE_START=$(git merge-base origin/master "${RANGE_END}" || echo "")
199+
if [[ -n "$NEW_RANGE_START" ]]; then
200+
START_REVISION="${NEW_RANGE_START}..${RANGE_END}"
201+
START_SHA="${START_REVISION}"
202+
else
203+
# In the shallow clone that Travis has created there's no merge base
204+
# between the PR and master. In this case just fall back on checking
205+
# everything.
206+
echo "Unable to detect base commit for change detection."
207+
echo "Failling back on just checking everything."
208+
CHECK_DIFF=false
209+
START_REVISION="origin/master"
210+
START_SHA="origin/master"
211+
fi
193212

194213
else
195214
START_SHA=$(git rev-parse "${START_REVISION}")
@@ -236,7 +255,9 @@ style_cmd=("${top_dir}/scripts/style.sh")
236255
if [[ "${TEST_ONLY}" == true ]]; then
237256
style_cmd+=(test-only)
238257
fi
239-
style_cmd+=("${START_SHA}")
258+
if [[ "$CHECK_DIFF" == true ]]; then
259+
style_cmd+=("${START_SHA}")
260+
fi
240261

241262
# Restyle and commit any changes
242263
"${style_cmd[@]}"
@@ -247,7 +268,9 @@ fi
247268
# If there are changes to the Firestore project, ensure they're ordered
248269
# correctly to minimize conflicts.
249270
if [ -z "${GITHUB_WORKFLOW-}" ]; then
250-
if ! git diff --quiet "${START_SHA}" -- Firestore; then
271+
if [[ "$CHECK_DIFF" == "false" ]] || \
272+
! git diff --quiet "${START_SHA}" -- Firestore; then
273+
251274
sync_project_cmd=("${top_dir}/scripts/sync_project.rb")
252275
if [[ "${TEST_ONLY}" == true ]]; then
253276
sync_project_cmd+=(--test-only)
@@ -268,4 +291,8 @@ fi
268291
"${top_dir}/scripts/check_cmake_files.py"
269292

270293
# Google C++ style
271-
"${top_dir}/scripts/check_lint.py" "${START_SHA}"
294+
lint_cmd=("${top_dir}/scripts/check_lint.py")
295+
if [[ "$CHECK_DIFF" == true ]]; then
296+
lint_cmd+=("${START_SHA}")
297+
fi
298+
"${lint_cmd[@]}"

0 commit comments

Comments
 (0)