You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ma...@apache.org on 2017/09/21 13:41:20 UTC

[incubator-openwhisk] branch master updated: Add git pre-commit hook scripts for Scala formatting. (#2782)

This is an automated email from the ASF dual-hosted git repository.

markusthoemmes pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 48a0d57  Add git pre-commit hook scripts for Scala formatting. (#2782)
48a0d57 is described below

commit 48a0d570a6a5c364d351bdb040bdb7ec4c027f96
Author: Sven Lange-Last <sv...@de.ibm.com>
AuthorDate: Thu Sep 21 15:41:17 2017 +0200

    Add git pre-commit hook scripts for Scala formatting. (#2782)
    
    Provided scripts can be used to apply project standard Scala formatting automatically
    when committing Scala source files. Developers only need to add the invocation
    of any of the scripts to their git `pre-commit` hook file.
    
    Standard Scala formatting was added with pull request #2650. At the moment,
    https://github.com/apache/incubator-openwhisk/wiki/Contributing:-Git-guidelines#scala
    describes how the `pre-commit` hook can be extended to apply Scala formatting.
    The script provided at this location has some flaws that are addressed by this PR:
    
    * The existing script starts formatting if Scala files are removed.
    * The existing script does not work if no `build.gradle` file can be found
      in the current working directory.
    * The existing script does only consider Scala files in the current
      working directory or below the current working directory.
---
 tools/git/README.md                      | 18 ++++++++++++
 tools/git/pre-commit-scalafmt-gradlew.sh | 36 +++++++++++++++++++++++
 tools/git/pre-commit-scalafmt-native.sh  | 49 ++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+)

diff --git a/tools/git/README.md b/tools/git/README.md
new file mode 100644
index 0000000..43324df
--- /dev/null
+++ b/tools/git/README.md
@@ -0,0 +1,18 @@
+# Purpose
+
+This directory contains shell scripts to be used in conjunction with `git` operations.
+
+## Pre-commit hooks
+
+This directory contains following `pre-commit` hooks. Read `man githooks` for details
+about `pre-commit` hooks and how to install / use them.
+
+### Scala source formatting
+
+Any of the following `pre-commit` hooks can be used to format all staged Scala source files (`*.scala`)
+according to project standards. Said files are changed in-place and re-staged so that committed
+files are properly formatted.
+
+* `pre-commit-scalafmt-gradlew.sh`: Use Gradle wrapper for formatting.
+* `pre-commit-scalafmt-native.sh`: Use `scalafmt` command for formatting. Less overhead and thus,
+  faster than Gradle wrapper approach. You have to install `scalafmt` command - see http://scalameta.org/scalafmt/.
diff --git a/tools/git/pre-commit-scalafmt-gradlew.sh b/tools/git/pre-commit-scalafmt-gradlew.sh
new file mode 100755
index 0000000..f455d82
--- /dev/null
+++ b/tools/git/pre-commit-scalafmt-gradlew.sh
@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+#
+# Purpose: Run this script as a git pre-commit hook to apply project-specific
+#          Scala formatting rules to all staged Scala source files (*.scala).
+#          Uses Gradle wrapper to perform Scala formatting with `scalafmt`.
+#          The script will re-stage the formatted Scala source files.
+
+# Uncomment the following line to obtain Shell script execution tracing.
+# set -x
+
+# -u: fail if variable is undefined
+# -f: disable globbing = file name expansion with regular expressions
+# -e: fail on non-zero exit code
+set -u -f -e
+
+# Determine OpenWhisk base directory
+ROOT_DIR="$(git rev-parse --show-toplevel)"
+
+# Run `scalafmt` iff there are staged .scala source files
+set +e
+STAGED_SCALA_FILES=$(git diff --cached --name-only --no-color --diff-filter=d --exit-code -- "${ROOT_DIR}/*.scala")
+STAGED_SCALA_FILES_DETECTED=$?
+set -e
+
+if [ "${STAGED_SCALA_FILES_DETECTED}" -eq 1 ]; then
+    # Re-format scala code iff a scala file is staged
+    "${ROOT_DIR}/gradlew" --project-dir "${ROOT_DIR}" scalafmtAll
+
+    # Re-add all staged .scala files
+    for SCALA_FILE in ${STAGED_SCALA_FILES}
+    do
+      git add -- "${ROOT_DIR}/${SCALA_FILE}"
+    done
+fi
+
+exit 0
\ No newline at end of file
diff --git a/tools/git/pre-commit-scalafmt-native.sh b/tools/git/pre-commit-scalafmt-native.sh
new file mode 100755
index 0000000..0bf028c
--- /dev/null
+++ b/tools/git/pre-commit-scalafmt-native.sh
@@ -0,0 +1,49 @@
+#!/usr/bin/env bash
+#
+# Purpose: Run this script as a git pre-commit hook to apply project-specific
+#          Scala formatting rules to all staged Scala source files (*.scala).
+#          Uses native command to perform Scala formatting with `scalafmt`.
+#          The script will re-stage the formatted Scala source files.
+#
+# Prerequisites: `scalafmt` command needs to be installed on the system.
+#                See http://scalameta.org/scalafmt/
+
+# Uncomment the following line to obtain Shell script execution tracing.
+# set -x
+
+# -u: fail if variable is undefined
+# -f: disable globbing = file name expansion with regular expressions
+# -e: fail on non-zero exit code
+set -u -f -e
+
+# Determine if `scalafmt` command is available and exit if not.
+set +e
+hash scalafmt
+SCALAFMT_CHECK=$?
+set -e
+
+if [ "${SCALAFMT_CHECK}" -ne 0 ]; then
+  echo "Required command 'scalafmt' not found. Please install."
+  echo "See http://scalameta.org/scalafmt/"
+  exit 1
+fi
+
+# Determine OpenWhisk base directory
+ROOT_DIR="$(git rev-parse --show-toplevel)"
+
+# Run `scalafmt` iff there are staged .scala source files
+set +e
+STAGED_SCALA_FILES=$(git diff --cached --name-only --no-color --diff-filter=d --exit-code -- "${ROOT_DIR}/*.scala")
+STAGED_SCALA_FILES_DETECTED=$?
+set -e
+
+if [ "${STAGED_SCALA_FILES_DETECTED}" -eq 1 ]; then
+    # Re-format and re-add all staged .scala files
+    for SCALA_FILE in ${STAGED_SCALA_FILES}
+    do
+      scalafmt --config "${ROOT_DIR}/.scalafmt.conf" "${ROOT_DIR}/${SCALA_FILE}"
+      git add -- "${ROOT_DIR}/${SCALA_FILE}"
+    done
+fi
+
+exit 0
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].