Skip to content

Commit

Permalink
build: Install git hooks to check lint before pushing changes (#3802)
Browse files Browse the repository at this point in the history
* Install .git hook to check lint before pushing changes
* Exclude build related C sources from clangfmt
* Fix formatting in Settings.scala
* Test linting of only modifying files on push
* Try to make linting of 2 C files cross-version compliant
  • Loading branch information
WojciechMazur committed Mar 4, 2024
1 parent 7ecd3a2 commit deb41ec
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
3 changes: 2 additions & 1 deletion javalib/src/main/resources/scala-native/ifaddrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ struct scalanative_ifaddrs {
/* Point-to-point destination address */
} ifa_ifu;
#endif
void *ifa_data; /* Address-specific data */
/* Address-specific data */
void *ifa_data;
};

_Static_assert(sizeof(struct scalanative_ifaddrs) <= sizeof(struct ifaddrs),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ size_t getMemorySize() {
#elif defined(HW_PHYSMEM64)
mib[1] = HW_PHYSMEM64; /* NetBSD, OpenBSD. --------- */
#endif
int64_t size = 0; /* 64-bit */
/* 64-bit */
int64_t size = 0;
size_t len = sizeof(size);
if (sysctl(mib, 2, &size, &len, NULL, 0) == 0)
return (size_t)size;
Expand All @@ -82,7 +83,8 @@ size_t getMemorySize() {
int mib[2];
mib[0] = CTL_HW;
#if defined(HW_REALMEM)
mib[1] = HW_REALMEM; /* FreeBSD. ----------------- */
/* FreeBSD. ----------------- */
mib[1] = HW_REALMEM;
#elif defined(HW_PYSMEM)
mib[1] = HW_PHYSMEM; /* Others. ------------------ */
#endif
Expand Down
18 changes: 17 additions & 1 deletion project/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ object Settings {
"This build requires JDK 8 or later. Aborting."
)
v
},
Global / onLoad ~= { prev =>
if (!scala.util.Properties.isWin) {
import java.nio.file._
val prePush = Paths.get(".git", "hooks", "pre-push")
Files.createDirectories(prePush.getParent)
Files.write(
prePush,
"""#!/bin/sh
|set -eux
|CHECK_MODIFIED_ONLY=1 ./scripts/check-lint.sh
|""".stripMargin.getBytes()
)
prePush.toFile.setExecutable(true)
}
prev
}
)

Expand Down Expand Up @@ -580,7 +596,7 @@ object Settings {
libraryDependencies ++= Deps.compilerPluginDependencies(scalaVersion.value),
publishSettings(None),
mavenPublishSettings,
exportJars := true,
exportJars := true
)

lazy val sbtPluginSettings = Def.settings(
Expand Down
12 changes: 11 additions & 1 deletion scripts/clangfmt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ set -euo pipefail
# The minimum version of clang-format with the new options
CLANG_FORMAT_VERSION=10

CHECK_MODIFIED_ONLY=${CHECK_MODIFIED_ONLY:-false}

die() {
while [ "$#" -gt 0 ]; do
echo >&2 "$1"; shift
Expand Down Expand Up @@ -84,8 +86,16 @@ fi

if [ "$#" -gt 0 ]; then
"$clang_format" --style=file "$opts" "$@"
elif [[ "$CHECK_MODIFIED_ONLY" == "1" ]]; then
git diff --name-only main | \
grep -E '.*\.(c|h|cpp|hpp)$' | \
xargs "$clang_format" --style=file "$opts" $err || \
die "C/C++ code formatting changes detected" \
"Run '$0' to reformat."
else
find . -name "*.[ch]" -or -name "*.cpp" -or -name "*.hpp" | \
find . -name "*.[ch]" -or -name "*.[ch]pp" | \
grep -E -v '.*/target/scala.*' | \
grep -E -v ".*/(.venv|.scala-build)/" | \
xargs "$clang_format" --style=file "$opts" $err || \
die "C/C++ code formatting changes detected" \
"Run '$0' to reformat."
Expand Down
9 changes: 8 additions & 1 deletion scripts/scalafmt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ HERE="`dirname $0`"
VERSION=$(sed -nre "s#[[:space:]]*version[^0-9]+([0-9.]+)#\1#p" $HERE/../.scalafmt.conf)
COURSIER="$HERE/.coursier"
SCALAFMT="$HERE/.scalafmt-$VERSION"
CHECK_MODIFIED_ONLY=${CHECK_MODIFIED_ONLY:-false}

if [ ! -f $COURSIER ]; then
curl -L -o $COURSIER https://git.io/coursier-cli
Expand All @@ -17,4 +18,10 @@ if [ ! -f $SCALAFMT ]; then
chmod +x $SCALAFMT
fi

$SCALAFMT "$@"
if [[ "$CHECK_MODIFIED_ONLY" == "1" ]]; then
git diff --name-only main | \
grep -E '.*\.scala$' |
xargs "$SCALAFMT"
else
$SCALAFMT "$@"
fi

0 comments on commit deb41ec

Please sign in to comment.