|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ******************************************************************************* |
| 4 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 5 | +# |
| 6 | +# See the NOTICE file(s) distributed with this work for additional |
| 7 | +# information regarding copyright ownership. |
| 8 | +# |
| 9 | +# This program and the accompanying materials are made available under the |
| 10 | +# terms of the Apache License Version 2.0 which is available at |
| 11 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# SPDX-License-Identifier: Apache-2.0 |
| 14 | +# ******************************************************************************* |
| 15 | + |
| 16 | +set -exuo pipefail |
| 17 | + |
| 18 | +# This script runs opengrep in such a way that it only works on the changeset that is to be checked |
| 19 | +# when running opengrep in the scope of a precommit hook. |
| 20 | +# The CI system runs the same script, but in that context no changeset exists, so all files are to |
| 21 | +# be checked. This also solves the problem that it is technically possible to work around the |
| 22 | +# precommit checks. |
| 23 | + |
| 24 | +changeset="$(git diff --staged --diff-filter=ACM --name-only)" |
| 25 | +length="${#changeset}" |
| 26 | +if [[ ${length} -gt 2048 ]]; then |
| 27 | + # The changeset is too long, it would result in errors from opengrep/underlying OS about filenames |
| 28 | + # being too long. Workaround: ignore the changeset and run opengrep on all files. |
| 29 | + changeset="" |
| 30 | +fi |
| 31 | +if [[ -z "${changeset}" ]]; then |
| 32 | + # Limit concurrency to 2 threads to reduce memory consumption |
| 33 | + OPENGREP_MAX_CONCURRENCY="--jobs=1" |
| 34 | + # No changeset, run opengrep on all files |
| 35 | + changeset="." |
| 36 | + opengrep scan "${OPENGREP_MAX_CONCURRENCY}" --error --disable-version-check --skip-unknown-extensions --emacs --sarif-output=build/opengrep.sarif -f ./opengrep/mandatory/ "${changeset}" |
| 37 | +else |
| 38 | + # When changing ${changeset} to "${changeset}" it will break the script, ${changeset} actually contains *multiple* filenames |
| 39 | + # shellcheck disable=SC2086 |
| 40 | + opengrep scan --error --disable-version-check --skip-unknown-extensions --emacs -f ./opengrep/mandatory/ ${changeset} |
| 41 | +fi |
0 commit comments