/ util / lint / helper_functions.sh
helper_functions.sh
 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  
 8  LC_ALL=C export LC_ALL
 9  
10  if [ -z "$GIT" ]; then
11  	GIT="$(command -v git)"
12  else
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
19  fi
20  
21  if [ "$(${GIT} rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
22  	IN_GIT_TREE=1
23  else
24  	IN_GIT_TREE=0
25  fi
26  
27  if [ "${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
30  fi
31  
32  # Use git ls-files if the code is in a git repo, otherwise use find.
33  if [ "${IN_GIT_TREE}" -eq 1 ]; then
34  	FIND_FILES="${GIT} ls-files"
35  else
36  	FIND_FILES="find "
37  	FINDOPTS="-type f"
38  fi
39  
40  # Use git grep if the code is in a git repo, otherwise use grep.
41  if [ "${IN_GIT_TREE}" -eq 1 ]; then
42  	GREP_FILES="${GIT} grep"
43  else
44  	GREP_FILES="grep -r"
45  fi