xref: /aosp_15_r20/external/coreboot/util/lint/helper_functions.sh (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1#!/usr/bin/env sh
2#
3# SPDX-License-Identifier: GPL-2.0-only
4
5# This file is sourced by the linters so that each one doesn't have to
6# specify these routines individually
7
8LC_ALL=C export LC_ALL
9
10if [ -z "$GIT" ]; then
11	GIT="$(command -v git)"
12else
13	# If git is specified, Do a basic check that it runs and seems like
14	# it's actually git
15	if ! "${GIT}" --version | grep -q git; then
16		echo "Error: ${GIT} does not seem to be valid."
17		exit 1;
18	fi
19fi
20
21if [ "$(${GIT} rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
22	IN_GIT_TREE=1
23else
24	IN_GIT_TREE=0
25fi
26
27if [ "${IN_GIT_TREE}" -eq 1 ] && [ -z "${GIT}" ]; then
28	echo "This test needs git to run.  Please install it, then run this test again."
29	exit 1
30fi
31
32# Use git ls-files if the code is in a git repo, otherwise use find.
33if [ "${IN_GIT_TREE}" -eq 1 ]; then
34	FIND_FILES="${GIT} ls-files"
35else
36	FIND_FILES="find "
37	FINDOPTS="-type f"
38fi
39
40# Use git grep if the code is in a git repo, otherwise use grep.
41if [ "${IN_GIT_TREE}" -eq 1 ]; then
42	GREP_FILES="${GIT} grep"
43else
44	GREP_FILES="grep -r"
45fi
46